Example #1
0
 private static boolean customValidation() throws Exception {
   boolean validate = false;
   String datacenterName = datacenter;
   String datastoreName = datastore;
   if (datacenterName.length() != 0
       && datacenterName != null
       && datastoreName.length() != 0
       && datastoreName != null) {
     ManagedObjectReference dcmor = getDatacenterByName(datacenter);
     if (dcmor != null) {
       List<DynamicProperty> datastoresList = getDynamicProarray(dcmor, "Datacenter", "datastore");
       ArrayOfManagedObjectReference ds =
           ((ArrayOfManagedObjectReference) (datastoresList.get(0)).getVal());
       List<ManagedObjectReference> datastores = ds.getManagedObjectReference();
       if (datastores.size() != 0) {
         for (int i = 0; i < datastores.size(); i++) {
           DatastoreSummary dsSummary = getDataStoreSummary(datastores.get(i));
           if (dsSummary.getName().equalsIgnoreCase(datastoreName)) {
             i = datastores.size() + 1;
             validate = true;
           }
         }
         if (!validate) {
           System.out.println(
               "Specified Datastore with name "
                   + datastore
                   + " was not"
                   + " found in specified Datacenter");
         }
         return validate;
       } else {
         System.out.println("No Datastore found in specified Datacenter");
         return validate;
       }
     } else {
       System.out.println("Specified Datacenter with name " + datacenter + " not Found");
       return validate;
     }
   }
   return validate;
 }
Example #2
0
  private static boolean registerVirtualMachine() throws Exception {
    boolean registered = false;
    ManagedObjectReference resourcePoolRef = new ManagedObjectReference();
    System.out.println("Registering The Virtual Machine ..........");
    ManagedObjectReference host = null;
    // Get The Data Center
    ManagedObjectReference dcmor = getDatacenterByName(datacenter);
    List<DynamicProperty> vmFolderList = getDynamicProarray(dcmor, "Datacenter", "vmFolder");
    ManagedObjectReference vmFolderMor = (ManagedObjectReference) (vmFolderList.get(0)).getVal();

    List<DynamicProperty> hostFolderList = getDynamicProarray(dcmor, "Datacenter", "hostFolder");
    ManagedObjectReference hostFolderMor = (ManagedObjectReference) hostFolderList.get(0).getVal();

    List<DynamicProperty> computeResourceList =
        getDynamicProarray(hostFolderMor, "Folder", "childEntity");
    List<ManagedObjectReference> csMoRefList = new ArrayList<ManagedObjectReference>();
    List<ManagedObjectReference> cSList = new ArrayList<ManagedObjectReference>();
    for (int i = 0; i < computeResourceList.size(); i++) {
      ArrayOfManagedObjectReference moArray =
          (ArrayOfManagedObjectReference) (computeResourceList.get(0)).getVal();
      csMoRefList.addAll(moArray.getManagedObjectReference());
      for (int j = 0; j < csMoRefList.size(); j++) {
        if (csMoRefList.get(j).getType().toString().contains("ComputeResource")) {
          cSList.add(csMoRefList.get(j));
        }
      }
    }
    List<ManagedObjectReference> hosts = new ArrayList<ManagedObjectReference>();
    for (int k = 0; k < cSList.size(); k++) {
      List<DynamicProperty> hostSystemList =
          getDynamicProarray(cSList.get(k), "ComputeResource", "host");
      ArrayOfManagedObjectReference hostsArray =
          (ArrayOfManagedObjectReference) (hostSystemList.get(0)).getVal();
      ManagedObjectReference hostSystem = hostsArray.getManagedObjectReference().get(0);
      hosts.add(hostSystem);
    }

    if (hosts.size() < 1) {
      System.out.println("No host found in datacenter to" + " register the Virtual Machine");
      return registered;
    } else {
      boolean hostFound = false;
      for (int i = 0; i < hosts.size(); i++) {
        ManagedObjectReference hostMor = (ManagedObjectReference) hosts.get(i);
        List<DynamicProperty> datastoresList =
            getDynamicProarray(hostMor, "HostSystem", "datastore");
        ArrayOfManagedObjectReference hostSystemArray =
            ((ArrayOfManagedObjectReference) (datastoresList.get(0)).getVal());
        List<ManagedObjectReference> datastores = hostSystemArray.getManagedObjectReference();

        long dirSize = getDirSize(localPath);
        for (int j = 0; j < datastores.size(); j++) {
          DatastoreSummary dataStoreSummary = getDataStoreSummary(datastores.get(j));

          if (dataStoreSummary.getName().equalsIgnoreCase(datastore)) {
            DatastoreInfo datastoreInfo = getDataStoreInfo(datastores.get(j));
            if (datastoreInfo.getFreeSpace() > dirSize) {
              host = hostMor;
              hostFound = true;
              i = hosts.size() + 1;
              j = datastores.size() + 1;
            }
          }
        }
      }
      if (hostFound) {
        // Get The vmx path
        String vmxPath = "[" + datastore + "] " + vmName + "/" + vmName + ".vmx";
        ManagedObjectReference vmRef = new ManagedObjectReference();
        List<DynamicProperty> vmsPropList = getDynamicProarray(host, "HostSystem", "vm");
        ArrayOfManagedObjectReference vmsPropArray =
            (ArrayOfManagedObjectReference) (vmsPropList.get(0)).getVal();
        List<ManagedObjectReference> vmsList = vmsPropArray.getManagedObjectReference();
        for (int i = 0; i < vmsList.size(); i++) {
          if (vmsList.get(i).getType().equalsIgnoreCase("VirtualMachine")) {
            vmRef = vmsList.get(i);
            break;
          }
        }
        List<DynamicProperty> rpRef = getDynamicProarray(vmRef, "VirtualMachine", "resourcePool");
        resourcePoolRef = (ManagedObjectReference) rpRef.get(0).getVal();
        // Registering The Virtual machine
        ManagedObjectReference taskmor =
            vimPort.registerVMTask(vmFolderMor, vmxPath, vmName, false, resourcePoolRef, host);

        String result = waitForTask(taskmor);
        if (result != null) {
          if (result.equalsIgnoreCase("sucess")) {
            System.out.println("Registering The Virtual Machine ..........Done");
            registered = true;
          } else {
            System.out.println("Some Exception While Registering The VM");
            registered = false;
          }
        } else {
          System.out.println("Some Exception While Registering The VM");
          registered = false;
        }
        return registered;
      } else {
        System.out.println(
            "No host in datacenter has got the" + " specified datastore and free space");
        return registered;
      }
    }
  }
Example #3
0
  /**
   * Creates the vm config spec object.
   *
   * @param vmName the vm name
   * @param datastoreName the datastore name
   * @param diskSizeMB the disk size in mb
   * @param computeResMor the compute res moref
   * @param hostMor the host mor
   * @return the virtual machine config spec object
   * @throws Exception the exception
   */
  VirtualMachineConfigSpec createVmConfigSpec(
      String vmName,
      String datastoreName,
      int diskSizeMB,
      ManagedObjectReference computeResMor,
      ManagedObjectReference hostMor)
      throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {

    ConfigTarget configTarget = getConfigTargetForHost(computeResMor, hostMor);
    List<VirtualDevice> defaultDevices = getDefaultDevices(computeResMor, hostMor);
    VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
    String networkName = null;
    if (configTarget.getNetwork() != null) {
      for (int i = 0; i < configTarget.getNetwork().size(); i++) {
        VirtualMachineNetworkInfo netInfo = configTarget.getNetwork().get(i);
        NetworkSummary netSummary = netInfo.getNetwork();
        if (netSummary.isAccessible()) {
          networkName = netSummary.getName();
          break;
        }
      }
    }
    ManagedObjectReference datastoreRef = null;
    if (datastoreName != null) {
      boolean flag = false;
      for (int i = 0; i < configTarget.getDatastore().size(); i++) {
        VirtualMachineDatastoreInfo vdsInfo = configTarget.getDatastore().get(i);
        DatastoreSummary dsSummary = vdsInfo.getDatastore();
        if (dsSummary.getName().equals(datastoreName)) {
          flag = true;
          if (dsSummary.isAccessible()) {
            datastoreRef = dsSummary.getDatastore();
          } else {
            throw new RuntimeException("Specified Datastore is not accessible");
          }
          break;
        }
      }
      if (!flag) {
        throw new RuntimeException("Specified Datastore is not Found");
      }
    } else {
      boolean flag = false;
      for (int i = 0; i < configTarget.getDatastore().size(); i++) {
        VirtualMachineDatastoreInfo vdsInfo = configTarget.getDatastore().get(i);
        DatastoreSummary dsSummary = vdsInfo.getDatastore();
        if (dsSummary.isAccessible()) {
          datastoreName = dsSummary.getName();
          datastoreRef = dsSummary.getDatastore();
          flag = true;
          break;
        }
      }
      if (!flag) {
        throw new RuntimeException("No Datastore found on host");
      }
    }
    String datastoreVolume = getVolumeName(datastoreName);
    VirtualMachineFileInfo vmfi = new VirtualMachineFileInfo();
    vmfi.setVmPathName(datastoreVolume);
    configSpec.setFiles(vmfi);
    // Add a scsi controller
    int diskCtlrKey = 1;
    VirtualDeviceConfigSpec scsiCtrlSpec = new VirtualDeviceConfigSpec();
    scsiCtrlSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController();
    scsiCtrl.setBusNumber(0);
    scsiCtrlSpec.setDevice(scsiCtrl);
    scsiCtrl.setKey(diskCtlrKey);
    scsiCtrl.setSharedBus(VirtualSCSISharing.NO_SHARING);
    String ctlrType = scsiCtrl.getClass().getName();
    ctlrType = ctlrType.substring(ctlrType.lastIndexOf(".") + 1);

    // Find the IDE controller
    VirtualDevice ideCtlr = null;
    for (int di = 0; di < defaultDevices.size(); di++) {
      if (defaultDevices.get(di) instanceof VirtualIDEController) {
        ideCtlr = defaultDevices.get(di);
        break;
      }
    }

    // Add a floppy
    VirtualDeviceConfigSpec floppySpec = new VirtualDeviceConfigSpec();
    floppySpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    VirtualFloppy floppy = new VirtualFloppy();
    VirtualFloppyDeviceBackingInfo flpBacking = new VirtualFloppyDeviceBackingInfo();
    flpBacking.setDeviceName("/dev/fd0");
    floppy.setBacking(flpBacking);
    floppy.setKey(3);
    floppySpec.setDevice(floppy);

    // Add a cdrom based on a physical device
    VirtualDeviceConfigSpec cdSpec = null;

    if (ideCtlr != null) {
      cdSpec = new VirtualDeviceConfigSpec();
      cdSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
      VirtualCdrom cdrom = new VirtualCdrom();
      VirtualCdromIsoBackingInfo cdDeviceBacking = new VirtualCdromIsoBackingInfo();
      cdDeviceBacking.setDatastore(datastoreRef);
      cdDeviceBacking.setFileName(datastoreVolume + "testcd.iso");
      cdrom.setBacking(cdDeviceBacking);
      cdrom.setKey(20);
      cdrom.setControllerKey(new Integer(ideCtlr.getKey()));
      cdrom.setUnitNumber(new Integer(0));
      cdSpec.setDevice(cdrom);
    }

    // Create a new disk - file based - for the vm
    VirtualDeviceConfigSpec diskSpec = null;
    diskSpec = createVirtualDisk(datastoreName, diskCtlrKey, datastoreRef, diskSizeMB);

    // Add a NIC. the network Name must be set as the device name to create the NIC.
    VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
    if (networkName != null) {
      nicSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
      VirtualEthernetCard nic = new VirtualPCNet32();
      VirtualEthernetCardNetworkBackingInfo nicBacking =
          new VirtualEthernetCardNetworkBackingInfo();
      nicBacking.setDeviceName(networkName);
      nic.setAddressType("generated");
      nic.setBacking(nicBacking);
      nic.setKey(4);
      nicSpec.setDevice(nic);
    }

    List<VirtualDeviceConfigSpec> deviceConfigSpec = new ArrayList<VirtualDeviceConfigSpec>();
    deviceConfigSpec.add(scsiCtrlSpec);
    deviceConfigSpec.add(floppySpec);
    deviceConfigSpec.add(diskSpec);
    if (ideCtlr != null) {
      deviceConfigSpec.add(cdSpec);
      deviceConfigSpec.add(nicSpec);
    } else {
      deviceConfigSpec = new ArrayList<VirtualDeviceConfigSpec>();
      deviceConfigSpec.add(nicSpec);
    }
    configSpec.getDeviceChange().addAll(deviceConfigSpec);
    return configSpec;
  }