1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-16 22:50:10 +03:00

Completing VMM driver for VMWare (added DEPLOY, SHUTDOWN, CANCEL, SAVE).

git-svn-id: http://svn.opennebula.org/one/trunk@494 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Constantino Vázquez Blanco 2009-04-28 17:25:33 +00:00
parent f386cadf21
commit c73fec87d6
5 changed files with 361 additions and 70 deletions

View File

@ -36,7 +36,7 @@ public class DeployVM
{
// Helpers from VI samples
private static ServiceContent content;
private static AppUtil cb = null;
static AppUtil cb = null;
private static VimPortType service;
private String datacenterName ="";
@ -53,7 +53,7 @@ public class DeployVM
com.vmware.vim.ManagedObjectReference hostMor;
public boolean registerVirtualMachine() throws Exception
{
{
boolean registered = false;
ManagedObjectReference host = null;
@ -121,13 +121,13 @@ public class DeployVM
vmFolderMor,vmxPath,getVmName(),false,resourcePool,host);
String result = cb.getServiceUtil().waitForTask(taskmor);
if (result.equalsIgnoreCase("Sucess"))
if (result.equalsIgnoreCase("Sucess")) // sic
{
registered = true;
}
else
{
System.out.println("Some Exception While Registering The VM");
System.out.println("Exception registering the VM");
registered = false;
}
return registered;
@ -247,6 +247,11 @@ public class DeployVM
{
String[] disks = pXML.getDisk();
if(disks.length==1 && disks[0].equals(""))
{
return;
}
VirtualDeviceConfigSpec [] vdiskSpecArray = new VirtualDeviceConfigSpec[disks.length];
for(int i=0;i<disks.length;i++)
@ -302,11 +307,17 @@ public class DeployVM
void configureNetwork()
{
// Firt, let's find out the number of NICs to be removed
String[] nics = pXML.getNet();
if(nics.length==1 && nics[0].equals(""))
{
return;
}
// First, let's find out the number of NICs to be removed
VirtualDevice [] test = vmConfigInfo.getHardware().getDevice();
String[] nics = pXML.getNet();
// Lenth of array is #nicsToBeRemoved-#nicsToBeAdded
@ -371,20 +382,18 @@ public class DeployVM
}
*/
DeployVM(String[] args, String hostName, String _vmName, ParseXML _pXML) throws Exception
{
cb = AppUtil.initialize("RegisterVM", null, args);
cb.connect();
DeployVM(AppUtil _cb, String hostName, ParseXML _pXML) throws Exception
{
cb = _cb;
// TODO get this dynamically
datastoreName = "datastore1";
datacenterName = "ha-datacenter";
vmName = _vmName;
vmName = _pXML.getName();
pXML = _pXML;
// Get reference to host
hostMor = cb.getServiceUtil().getDecendentMoRef(null,"HostSystem",
hostName);
@ -394,8 +403,5 @@ public class DeployVM
service = sc.getService();
}
protected void finalize() throws Throwable
{
cb.disConnect();
}
}

View File

@ -25,10 +25,11 @@ import com.vmware.apputils.vim.*;
class OneVmmVmware extends Thread
{
private String[] argsWithHost;
OperationsOverVM oVM;
private String[] arguments;
// Entry point - main procedure
// Helpers from VI samples
static AppUtil cb = null;
public static void main(String[] args)
{
@ -46,7 +47,36 @@ class OneVmmVmware extends Thread
// Constructor
OneVmmVmware(String[] args)
{
arguments = args;
argsWithHost = new String[args.length+2];
for(int i=0;i<args.length;i++)
{
argsWithHost[i] = args[i];
}
argsWithHost[args.length] = "--url";
// TODO this is just for testing
// argsWithHost[arguments.length + 1 ] = "https://" + hostName + ":443/sdk";
argsWithHost[args.length + 1 ] = "https://localhost:8008/sdk";
try
{
cb = AppUtil.initialize("OneVmmVmware", null, argsWithHost);
cb.connect();
oVM = new OperationsOverVM(cb);
}
catch(Exception e)
{
System.out.println("Error stablishing connection to ESX host. Reason: " +
e.getMessage());
System.exit(-1);
}
}
protected void finalize() throws Throwable
{
cb.disConnect();
}
@ -101,7 +131,8 @@ class OneVmmVmware extends Thread
{
synchronized (System.err)
{
System.err.println("FAILURE Unknown command");
System.out.println("FAILURE Wrong number of arguments for DEPLOY action. Number args = [" +
str_split.length + "].");
}
}
else
@ -111,38 +142,33 @@ class OneVmmVmware extends Thread
fileName = str_split[3];
try
{
// First, register the VM
String[] argsWithHost = new String[arguments.length+2];
for(int i=0;i<arguments.length;i++)
{
argsWithHost[i] = arguments[i];
}
argsWithHost[arguments.length] = "--url";
// TODO this is just for testing
// argsWithHost[arguments.length + 1 ] = "https://" + hostName + ":443/sdk";
argsWithHost[arguments.length + 1 ] = "https://localhost:8008/sdk";
{
// let's read the XML file and extract needed info
ParseXML pXML = new ParseXML(fileName);
DeployVM dVM = new DeployVM(argsWithHost, hostName, "one-" + vid_str,pXML);
// First, register the VM
DeployVM dVM = new DeployVM(cb, hostName, pXML);
if(!dVM.registerVirtualMachine())
{
throw new Exception("Error registering VM.");
throw new Exception("Error registering VM(" + pXML.getName() + ").");
}
// Now, proceed with the reconfiguration
if(!dVM.shapeVM())
{
throw new Exception("Error reconfiguring VM.");
throw new Exception("Error reconfiguring VM (" + pXML.getName() + ").");
}
if(!oVM.powerOn(pXML.getName()))
{
throw new Exception("Error powering on VM(" + pXML.getName() + ").");
}
System.err.println("DEPLOY SUCCESS " + vid_str + " " + pXML.getName());
continue;
}
@ -157,6 +183,127 @@ class OneVmmVmware extends Thread
} // catch
} // else if (str_split.length != 4)
} // if (action.equals("DEPLOY"))
if (action.equals("SHUTDOWN"))
{
if (str_split.length < 3 )
{
synchronized (System.err)
{
System.out.println("FAILURE Wrong number of arguments for SHUTDOWN action. Number args = [" +
str_split.length + "].");
}
}
else
{
vid_str = str_split[1];
hostName = str_split[2];
String vmName = str_split[3];
if(!oVM.powerOff(vmName))
{
System.err.println("SHUTDOWN FAILURE " + vid_str + " Failed shutdown VM in host " +
hostName);
}
else
{
System.err.println("SHUTDOWN SUCCESS " + vid_str);
}
}
continue;
} // if (action.equals("SHUTDOWN"))
if (action.equals("SHUTDOWN") || action.equals("CANCEL"))
{
if (str_split.length < 3 )
{
synchronized (System.err)
{
System.out.println("FAILURE Wrong number of arguments for " + action +
" action. Number args = [" +
str_split.length + "].");
}
}
else
{
vid_str = str_split[1];
hostName = str_split[2];
String vmName = str_split[3];
if(!oVM.powerOff(vmName))
{
System.err.println(action + " FAILURE " + vid_str + " Failed shutdown VM in host " +
hostName);
}
else
{
System.err.println(action + " SUCCESS " + vid_str);
}
}
continue;
} // if (action.equals("SHUTDOWN or CANCEL"))
if (action.equals("SUSPEND"))
{
if (str_split.length < 4)
{
synchronized (System.err)
{
System.out.println("FAILURE Wrong number of arguments for SUSPEND action. Number args = [" +
str_split.length + "].");
}
}
else
{
vid_str = str_split[1];
hostName = str_split[2];
String vmName = str_split[3];
if(!oVM.suspend(vmName))
{
System.err.println(action + " FAILURE " + vid_str + " Failed suspending VM in host " +
hostName);
}
else
{
System.err.println(action + " SUCCESS " + vid_str);
}
continue;
}
} // if (action.equals("SUSPEND"))
if (action.equals("CHECKPOINT"))
{
vid_str = str_split[1];
System.err.println(action + " FAILURE " + vid_str + " Action not supported");
continue;
} // if (action.equals("CHECKPOINT"))
if (action.equals("RESTORE"))
{
vid_str = str_split[1];
System.err.println(action + " FAILURE " + vid_str + " Action not supported");
continue;
} // if (action.equals("RESTORE"))
if (action.equals("MIGRATE"))
{
vid_str = str_split[1];
System.err.println(action + " FAILURE " + vid_str + " Action not supported");
continue;
} // if (action.equals("MIGRATE"))
} // else if (action.equals("FINALIZE"))
} // while(!fin)
} // loop

View File

@ -0,0 +1,141 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2009, Distributed Systems Architecture Group, Universidad */
/* Complutense de Madrid (dsa-research.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
import com.vmware.vim.*;
import com.vmware.apputils.*;
import com.vmware.apputils.vim.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.rmi.RemoteException;
/*
* Used to enforce an operaton over a VM
*/
public class OperationsOverVM
{
// Helpers from VI samples
private static AppUtil cb = null;
ManagedObjectReference virtualMachine;
private String datacenterName = "";
private String datastoreName = "";
private boolean getTaskInfo(ManagedObjectReference taskmor) throws Exception
{
DynamicProperty[] scsiArry = getDynamicProperties(taskmor,"info");
TaskInfo tinfo = ((TaskInfo)(scsiArry[0]).getVal());
String res = cb.getServiceUtil().waitForTask(taskmor);
if(res.equalsIgnoreCase("sucess"))
{
return true;
}
else
{
return false;
}
}
private DynamicProperty[] getDynamicProperties
(ManagedObjectReference mobjRef,String pName )throws Exception
{
ObjectContent[] objContent =
cb.getServiceUtil().getObjectProperties(null, mobjRef,
new String[] { pName });
ObjectContent contentObj = objContent[0];
DynamicProperty[] objArr = contentObj.getPropSet();
return objArr;
}
public boolean powerOn(String vmName) throws Exception
{
ManagedObjectReference taskmor = null;
ManagedObjectReference virtualMachine
= cb.getServiceUtil().getDecendentMoRef(null, "VirtualMachine", vmName);
taskmor = cb.getConnection().getService().powerOnVM_Task(virtualMachine, null);
boolean result = getTaskInfo(taskmor);
return result;
}
public boolean powerOff(String vmName)
{
try
{
ManagedObjectReference taskmor = null;
ManagedObjectReference virtualMachine
= cb.getServiceUtil().getDecendentMoRef(null, "VirtualMachine", vmName);
taskmor = cb.getConnection().getService().powerOffVM_Task(virtualMachine);
boolean result = getTaskInfo(taskmor);
return result;
}
catch(Exception e)
{
System.out.println("Error powering off VirtualMachine [" + vmName + "]. Reason:" + e.getMessage());
return false;
}
}
public boolean suspend(String vmName)
{
try
{
ManagedObjectReference taskmor = null;
ManagedObjectReference virtualMachine
= cb.getServiceUtil().getDecendentMoRef(null, "VirtualMachine", vmName);
taskmor = cb.getConnection().getService().suspendVM_Task(virtualMachine);
boolean result = getTaskInfo(taskmor);
return result;
}
catch(Exception e)
{
System.out.println("Error suspending VirtualMachine [" + vmName + "]. Reason:" + e.getMessage());
return false;
}
}
OperationsOverVM(AppUtil _cb) throws Exception
{
cb = _cb;
// TODO get this dynamically
datastoreName = "datastore1";
datacenterName = "ha-datacenter";
}
}

View File

@ -13,11 +13,11 @@ import org.xml.sax.SAXParseException;
public class ParseXML
{
private String name;
private String cpu;
private String[] disk;
private String memory;
private String[] macs;
private String name = "";
private String cpu = "";
private String[] disk = {""};
private String memory = "";
private String[] macs = {""};
/**
* Parses the XML file and fills the values
@ -29,54 +29,46 @@ public class ParseXML
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File(fileName));
doc.getDocumentElement().normalize();
doc.getDocumentElement().normalize();
NodeList vm = doc.getElementsByTagName("VM");
if(vm.getLength()!=1)
NodeList vmNL = doc.getElementsByTagName("VM");
if(vmNL.getLength()!=1)
{
throw new Exception("Number of VM tags different of 1: [" + vm.getLength() + "]");
throw new Exception("Number of VM tags different of 1: [" + vmNL.getLength() + "]");
}
Element vm = (Element)(vmNL.item(0));
// Name
NodeList nameNL = doc.getElementsByTagName("NAME");
NodeList nameNL = vm.getElementsByTagName("NAME");
if(nameNL.getLength()!=1)
{
throw new Exception("Number of NAME tags different of 1: [" + nameNL.getLength() + "]");
}
name = ((Node)nameNL.item(0)).getFirstChild().getNodeValue().trim();
name = ((Node)nameNL.item(0)).getNodeValue().trim();
// CPU
NodeList cpuNL = doc.getElementsByTagName("CPU");
NodeList cpuNL = vm.getElementsByTagName("CPU");
if(cpuNL.getLength()!=1)
{
throw new Exception("Number of CPU tags different of 1: [" + cpuNL.getLength() + "]");
}
cpu = ((Node)cpuNL.item(0)).getNodeValue().trim();
cpu = ((Node)cpuNL.item(0)).getFirstChild().getNodeValue().trim();
// Memory
NodeList memoryNL = doc.getElementsByTagName("MEMORY");
NodeList memoryNL = vm.getElementsByTagName("MEMORY");
if(memoryNL.getLength()!=1)
{
throw new Exception("Number of MEMORY tags different of 1: [" + memoryNL.getLength() + "]");
}
memory = ((Node)memoryNL.item(0)).getNodeValue().trim();
memory = ((Node)memoryNL.item(0)).getFirstChild().getNodeValue().trim();
// DISK
NodeList diskNL = doc.getElementsByTagName("DISK");
NodeList diskNL = vm.getElementsByTagName("DISK");
if(diskNL.getLength()!=0)
{
@ -86,13 +78,13 @@ public class ParseXML
{
NodeList sourceNode = ((Element)diskNL).getElementsByTagName("SOURCE");
disk[i] = ((Node)sourceNode.item(0)).getNodeValue().trim();
disk[i] = ((Node)sourceNode.item(0)).getFirstChild().getNodeValue().trim();
}
}
// Network
NodeList nwNL = doc.getElementsByTagName("NIC");
NodeList nwNL = vm.getElementsByTagName("NIC");
if(nwNL.getLength()!=0)
{
@ -102,14 +94,14 @@ public class ParseXML
{
NodeList mac = ((Element)nwNL).getElementsByTagName("MAC");
macs[i] = ((Node)mac.item(0)).getNodeValue().trim();
macs[i] = ((Node)mac.item(0)).getFirstChild().getNodeValue().trim();
}
}
}
catch (SAXParseException err)
{
throw new Exception("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
+ err.getLineNumber () + ", uri " + err.getSystemId ());
}
}// end ParseXML

5
src/vmm_mad/vmware/one_vmm_vmware Normal file → Executable file
View File

@ -24,13 +24,18 @@ DRIVER_NAME=`basename $0`
if [ -z "${ONE_LOCATION}" ]; then
DRIVERRC=/etc/one/${DRIVER_NAME}/${DRIVER_NAME}rc
MADCOMMON=/usr/lib/one/mads/madcommon.sh
VAR_LOCATION=/var/lib/one
else
DRIVERRC=$ONE_LOCATION/etc/${DRIVER_NAME}/${DRIVER_NAME}rc
MADCOMMON=$ONE_LOCATION/lib/mads/madcommon.sh
VAR_LOCATION=$ONE_LOCATION/var
fi
. $MADCOMMON
# Go to VAR_LOCATION
cd $VAR_LOCATION
# Export the im_mad specific rc
export_rc_vars $DRIVERRC