Search in sources :

Example 6 with VirtualMachineCloneSpec

use of com.vmware.vim25.VirtualMachineCloneSpec in project CloudStack-archive by CloudStack-extras.

the class VirtualMachineMO method createFullClone.

public boolean createFullClone(String cloneName, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool, ManagedObjectReference morDs) throws Exception {
    VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
    VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
    cloneSpec.setLocation(relocSpec);
    cloneSpec.setPowerOn(false);
    cloneSpec.setTemplate(false);
    relocSpec.setDatastore(morDs);
    relocSpec.setPool(morResourcePool);
    ManagedObjectReference morTask = _context.getService().cloneVM_Task(_mor, morFolder, cloneName, cloneSpec);
    String result = _context.getServiceUtil().waitForTask(morTask);
    if (result.equals("sucess")) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware cloneVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
Also used : VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) VirtualMachineCloneSpec(com.vmware.vim25.VirtualMachineCloneSpec) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 7 with VirtualMachineCloneSpec

use of com.vmware.vim25.VirtualMachineCloneSpec in project cloudstack by apache.

the class VirtualMachineMO method createFullClone.

public boolean createFullClone(String cloneName, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool, ManagedObjectReference morDs) throws Exception {
    VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
    VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
    cloneSpec.setLocation(relocSpec);
    cloneSpec.setPowerOn(false);
    cloneSpec.setTemplate(false);
    relocSpec.setDatastore(morDs);
    relocSpec.setPool(morResourcePool);
    ManagedObjectReference morTask = _context.getService().cloneVMTask(_mor, morFolder, cloneName, cloneSpec);
    boolean result = _context.getVimClient().waitForTask(morTask);
    if (result) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware cloneVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
Also used : VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) VirtualMachineCloneSpec(com.vmware.vim25.VirtualMachineCloneSpec) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 8 with VirtualMachineCloneSpec

use of com.vmware.vim25.VirtualMachineCloneSpec in project cloudstack by apache.

the class VirtualMachineMO method createFullCloneWithSpecificDisk.

public VirtualMachineMO createFullCloneWithSpecificDisk(String cloneName, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool, VirtualDisk requiredDisk) throws Exception {
    assert (morFolder != null);
    assert (morResourcePool != null);
    VirtualMachineRuntimeInfo runtimeInfo = getRuntimeInfo();
    HostMO hostMo = new HostMO(_context, runtimeInfo.getHost());
    DatacenterMO dcMo = new DatacenterMO(_context, hostMo.getHyperHostDatacenter());
    DatastoreMO dsMo = new DatastoreMO(_context, morResourcePool);
    VirtualMachineRelocateSpec rSpec = new VirtualMachineRelocateSpec();
    VirtualDisk[] vmDisks = getAllDiskDevice();
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
    s_logger.debug(String.format("Removing the disks other than the required disk with key %s to the cloned VM", requiredDisk.getKey()));
    for (VirtualDisk disk : vmDisks) {
        s_logger.debug(String.format("Original disk with key %s found in the VM %s", disk.getKey(), getName()));
        if (requiredDisk.getKey() != disk.getKey()) {
            VirtualDeviceConfigSpec virtualDeviceConfigSpec = new VirtualDeviceConfigSpec();
            virtualDeviceConfigSpec.setDevice(disk);
            virtualDeviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
            vmConfigSpec.getDeviceChange().add(virtualDeviceConfigSpec);
        }
    }
    rSpec.setPool(morResourcePool);
    VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
    cloneSpec.setPowerOn(false);
    cloneSpec.setTemplate(false);
    cloneSpec.setLocation(rSpec);
    cloneSpec.setMemory(false);
    cloneSpec.setConfig(vmConfigSpec);
    ManagedObjectReference morTask = _context.getService().cloneVMTask(_mor, morFolder, cloneName, cloneSpec);
    boolean result = _context.getVimClient().waitForTask(morTask);
    if (result) {
        _context.waitForTaskProgressDone(morTask);
        VirtualMachineMO clonedVm = dcMo.findVm(cloneName);
        if (clonedVm == null) {
            s_logger.error(String.format("Failed to clone VM %s", cloneName));
            return null;
        }
        s_logger.debug(String.format("Cloned VM: %s as %s", getName(), cloneName));
        clonedVm.tagAsWorkerVM();
        makeSureVMHasOnlyRequiredDisk(clonedVm, requiredDisk, dsMo, dcMo);
        return clonedVm;
    } else {
        s_logger.error("VMware cloneVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        return null;
    }
}
Also used : VirtualMachineRuntimeInfo(com.vmware.vim25.VirtualMachineRuntimeInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDisk(com.vmware.vim25.VirtualDisk) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) VirtualMachineCloneSpec(com.vmware.vim25.VirtualMachineCloneSpec) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 9 with VirtualMachineCloneSpec

use of com.vmware.vim25.VirtualMachineCloneSpec in project cloudstack by apache.

the class VirtualMachineMO method createFullClone.

public boolean createFullClone(String cloneName, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool, ManagedObjectReference morDs, Storage.ProvisioningType diskProvisioningType) throws Exception {
    VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
    VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
    cloneSpec.setLocation(relocSpec);
    cloneSpec.setPowerOn(false);
    cloneSpec.setTemplate(false);
    relocSpec.setDatastore(morDs);
    relocSpec.setPool(morResourcePool);
    setDiskProvisioningType(relocSpec, morDs, diskProvisioningType);
    ManagedObjectReference morTask = _context.getService().cloneVMTask(_mor, morFolder, cloneName, cloneSpec);
    boolean result = _context.getVimClient().waitForTask(morTask);
    if (result) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware cloneVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
Also used : VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) VirtualMachineCloneSpec(com.vmware.vim25.VirtualMachineCloneSpec) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 10 with VirtualMachineCloneSpec

use of com.vmware.vim25.VirtualMachineCloneSpec in project photon-model by vmware.

the class InstanceClient method replicateVMTemplate.

private ManagedObjectReference replicateVMTemplate(ManagedObjectReference resourcePool, ManagedObjectReference datastore, List<VirtualMachineDefinedProfileSpec> pbmSpec, ManagedObjectReference vmFolder, String vmName, ManagedObjectReference vm, GetMoRef get) throws Exception {
    logger.info("Template lives on a different datastore, looking for a local copy of: {}.", vmName);
    String replicatedName = vmName;
    if (datastore != null) {
        replicatedName = replicatedName + "_" + datastore.getValue();
    }
    ManagedObjectReference repVm = findTemplateByName(replicatedName, get);
    if (repVm != null) {
        return repVm;
    }
    logger.info("Replicating {} ({}) to {}", vmName, vm.getValue(), replicatedName);
    Lock lock = getLock(replicatedName);
    lock.lock();
    try {
        VirtualMachineRelocateSpec spec = new VirtualMachineRelocateSpec();
        spec.setPool(resourcePool);
        if (datastore != null) {
            spec.setDatastore(datastore);
        }
        if (pbmSpec != null) {
            pbmSpec.stream().forEach(sp -> {
                spec.getProfile().add(sp);
            });
        }
        spec.setFolder(vmFolder);
        spec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING.value());
        VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
        cloneSpec.setLocation(spec);
        cloneSpec.setTemplate(false);
        cloneSpec.setPowerOn(false);
        ManagedObjectReference cloneTask = getVimPort().cloneVMTask(vm, vmFolder, replicatedName, cloneSpec);
        TaskInfo info = VimUtils.waitTaskEnd(this.connection, cloneTask);
        if (info.getState() == TaskInfoState.ERROR) {
            MethodFault fault = info.getError().getFault();
            if (fault instanceof DuplicateName) {
                logger.info("Template is being replicated by another thread, waiting for {} to be ready", replicatedName);
                return awaitVM(replicatedName, vmFolder, get);
            } else {
                return VimUtils.rethrow(info.getError());
            }
        }
        ManagedObjectReference rvm = (ManagedObjectReference) info.getResult();
        logger.info("Replicated {} ({}) to {} ({})", vmName, vm.getValue(), replicatedName, rvm.getValue());
        logger.info("Creating initial snapshot for linked clones on {}", rvm.getValue());
        ManagedObjectReference snapshotTask = getVimPort().createSnapshotTask(rvm, "initial", null, false, false);
        VimUtils.waitTaskEnd(this.connection, snapshotTask);
        logger.info("Created initial snapshot for linked clones on {}", rvm.getValue());
        return rvm;
    } finally {
        lock.unlock();
    }
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) MethodFault(com.vmware.vim25.MethodFault) VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) VirtualMachineCloneSpec(com.vmware.vim25.VirtualMachineCloneSpec) DuplicateName(com.vmware.vim25.DuplicateName) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Aggregations

VirtualMachineCloneSpec (com.vmware.vim25.VirtualMachineCloneSpec)15 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)13 VirtualMachineRelocateSpec (com.vmware.vim25.VirtualMachineRelocateSpec)13 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)10 VirtualDisk (com.vmware.vim25.VirtualDisk)5 MethodFault (com.vmware.vim25.MethodFault)4 TaskInfo (com.vmware.vim25.TaskInfo)4 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)4 VirtualMachineRelocateSpecDiskLocator (com.vmware.vim25.VirtualMachineRelocateSpecDiskLocator)4 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)3 DuplicateName (com.vmware.vim25.DuplicateName)3 FileAlreadyExists (com.vmware.vim25.FileAlreadyExists)3 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)3 Lock (java.util.concurrent.locks.Lock)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 RESOURCE_GROUP_NAME (com.vmware.photon.controller.model.ComputeProperties.RESOURCE_GROUP_NAME)2 ComputeInstanceRequest (com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest)2 VM_PATH_FORMAT (com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.VM_PATH_FORMAT)2 ClientUtils.createCdrom (com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.createCdrom)2