return description;
}
public void execute(){
VsphereServer vsphereServer=new VsphereServer(vsphereUrl, vsphereUsername,vspherePassword);
try {
vsphereServer.connect();
ArrayList<String> datacenters=vsphereServer.listDataCenters();
ArrayList<String> datastores=vsphereServer.listDataStores();
ArrayList<String> networks=vsphereServer.listNetworks();
ArrayList<String> clusters=vsphereServer.listClusters();
//Check cluster
if (!clusters.contains(vsphereClusterName)) {
System.err.println("cluster "+vsphereClusterName+" does not exist");
System.exit(-1);
}
//Check datastore
if (!datastores.contains(vsphereDataStoreName)) {
System.err.println("datastore "+vsphereDataStoreName+" does not exist");
System.exit(-1);
}
//Check datacenter
if (!datacenters.contains(vsphereDataCenterName)) {
System.err.println("datacenter "+vsphereDataCenterName+" does not exist");
System.exit(-1);
}
//Check networks to be used
for (int i=0; i< vmNics.length; i++) {
if (! networks.contains(vmNics[i].getNetwork())) {
System.err.println("network "+vmNics[i].getNetwork()+" does not exist");
System.exit(-1);
}
}
//verify cdrom
//Find if vm by name vmname already exists
VirtualMachine existingVm=vsphereServer.findVmByName(vmName);
if (existingVm!=null) {
System.out.println("Machine exists with name:"+existingVm.getName());
//vsphereServer.showVmDetail(existingVm);
//Where was this used for?
// VirtualMachineCapability vmc = existingVm.getCapability();
// VirtualMachineSummary summary = (VirtualMachineSummary) (existingVm.getSummary());
if(vmOverwrite) {
if (vsphereServer.isVmPoweredOn(existingVm))
{
vsphereServer.powerOffVm(existingVm);
}
vsphereServer.destroyVm(existingVm);
} else {
System.out.println("use --overwrite true to overwrite");
System.exit(1);
}
}
VirtualMachine newVm=vsphereServer.createVm(vmName,vmMemory,
vmCpus,vmOsType,vmDisks,
vmNics,vsphereDataCenterName,vsphereDataStoreName,vsphereClusterName);
if (vmCdromIsoPath!=null) {
vsphereServer.setCdromVm(newVm,vmCdromIsoPath,vmCdromDataStoreName);
}
if (vmVncActivate) {
vsphereServer.vncActivateVm(newVm,vmVncPort,vmVncPassword);
}
String pxeInterface="";
//Determine the PXE interface
for (int i=0; i< vmNics.length; i++) {
if (vmNics[i].isPxe()) {
System.out.println("enabling pxe on interface"+vmNics[i].getName());
pxeInterface="Network Adapter "+(i+1);
vsphereServer.setVmPxebootInterface(newVm,pxeInterface);
}
}
//Enough config, let's prepare the server for it's first boot
vsphereServer.setEnterBiosVm(newVm,true);
if (newVm!=null) {
vsphereServer.powerOnVm(newVm);
}
//it is now in bios waiting , so we can power it off
if (newVm!=null) {
vsphereServer.powerOffVm(newVm);
}
//flip the enterbios flag
vsphereServer.setEnterBiosVm(newVm,false);
if (vmBootOrder!=null) {
//allow:net
vsphereServer.setBootOrderVm(newVm, vmBootOrder);
}
//if (vmPxeInterface!=null) {
// vsphereServer.setVmPxebootInterface(newVm,vmPxeInterface);
//}
//vsphereServer.listNicsVm(newVm);
if (vmOmapiRegister) {
System.out.println("registering nic:"+vmPxeInterface);
OmapiServer omapiServer=new OmapiServer(omapiHost,omapiPort,omapiKeyName, omapiKeyValue);
String macAddress=vsphereServer.getMacAddress(pxeInterface,newVm);
omapiServer.updateDHCP(vmName, macAddress,omapiOverWrite);
System.out.println(macAddress);
}
if (registermac) {
String command = String.format(registermacCommand, vsphereServer.getMacAddress(pxeInterface,newVm));
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
process.waitFor();
}
vsphereServer.powerOnVm(newVm);
} catch (RemoteException ex) {
System.err.println("eror logging in the vsphere, username, password?");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();