Example #1
0
  /**
   * 克隆虚拟机,CloneVM_Task<br>
   * <br>
   * 是否转换为模板
   *
   * @param resultMap
   * @param url
   * @param username
   * @param password
   * @param dcId
   * @param vmMor
   * @param cloneName
   * @param isTemplate
   * @param rpId
   * @return
   */
  private static void cloneVM(
      HashMap<String, Object> resultMap,
      String url,
      String username,
      String password,
      String dcId,
      ManagedObjectReference vmMor,
      String cloneName,
      boolean isTemplate,
      String rpId) {
    try {
      VIMCache cache = VIMCache.getInstance(url, username, password);

      ManagedObjectReference dcMor = cache.getDatacenter(dcId);
      // ManagedObjectReference vmMor = cache.getVirtualMachine(vmId);
      ManagedObjectReference vmFolderMor = cache.getVMFolder(dcId);
      ManagedObjectReference rpMor = null;
      if (!Util.normalizeString(rpId).equals("")) {
        rpMor = cache.getResourcePool(rpId);
      }

      if ((dcMor != null) && (vmMor != null) && (vmFolderMor != null)) {
        ExtendedAppUtil ecb = getECB(url, username, password);

        VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
        VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
        cloneSpec.setLocation(relocSpec);
        cloneSpec.setPowerOn(false);
        cloneSpec.setTemplate(isTemplate);
        // 如果是从模板部署为虚拟机需要指定资源池
        if (rpMor != null) {
          relocSpec.setPool(rpMor);
        }

        ManagedObjectReference taskMor =
            ecb.getServiceConnection3()
                .getService()
                .cloneVM_Task(vmMor, vmFolderMor, cloneName, cloneSpec);

        execTaskAndRecordResultMap(ecb, taskMor, resultMap);
      } else {
        if (dcMor == null) {
          recordResultMapError(resultMap, "数据中心'" + dcId + "'不存在");
        }
        if (vmMor == null) {
          // recordResultMapError(resultMap, "虚拟机'" + vmId
          // + "'不存在");
          recordResultMapError(resultMap, "虚拟机'" + "'不存在");
        }
        if (vmFolderMor == null) {
          recordResultMapError(resultMap, "数据中心'" + dcId + "'的vmFolder不存在");
        }
      }
    } catch (Exception e) {
      LOGGER.error("cloneVM error, ", e);
      recordResultMapException(resultMap, e);
    }
  }
  private VirtualMachineRelocateSpec createRelocateSpec(
      PrintStream jLogger,
      boolean linkedClone,
      String resourcePoolName,
      String cluster,
      String datastoreName,
      boolean isResourcePoolRequired)
      throws RemoteException, MalformedURLException, VSphereException {
    VirtualMachineRelocateSpec rel = new VirtualMachineRelocateSpec();

    if (linkedClone) {
      rel.setDiskMoveType("createNewChildDiskBacking");
    } else {
      rel.setDiskMoveType("moveAllDiskBackingsAndDisallowSharing");
    }

    ClusterComputeResource clusterResource = getClusterByName(cluster);

    // probably only of interest if someone actually entered a cluster name
    if (clusterResource == null && StringUtils.isNotBlank(cluster)) {
      logMessage(
          jLogger,
          "Cluster resource "
              + cluster
              + " does not exist, root folder will be used for getting resource pool and datastore");
    }

    if (resourcePoolName != null && !resourcePoolName.isEmpty()) {
      ResourcePool resourcePool = getResourcePoolByName(resourcePoolName, clusterResource);

      if (resourcePool == null) {
        throw new VSphereException("Resource pool \"" + resourcePoolName + "\" not found");
      }

      rel.setPool(resourcePool.getMOR());
    } else if (isResourcePoolRequired) {
      throw new VSphereException("You must specify a resource  pool  when using a template");
    }

    if (datastoreName != null && !datastoreName.isEmpty()) {
      Datastore datastore = getDatastoreByName(datastoreName, clusterResource);
      if (datastore == null) {
        throw new VSphereException("Datastore \"" + datastoreName + "\" not found!");
      }
      rel.setDatastore(datastore.getMOR());
    }

    return rel;
  }