use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class InstanceClient method cloneVm.
private ManagedObjectReference cloneVm(ManagedObjectReference template) throws Exception {
ManagedObjectReference folder = getVmFolder();
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.bootDisk);
ManagedObjectReference datastore = getDataStoreForDisk(this.bootDisk, pbmSpec);
ManagedObjectReference resourcePool = getResourcePool();
Map<String, Object> props = this.get.entityProps(template, VimPath.vm_config_hardware_device);
ArrayOfVirtualDevice devices = (ArrayOfVirtualDevice) props.get(VimPath.vm_config_hardware_device);
VirtualDisk vd = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).findFirst().orElse(null);
VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
relocSpec.setDatastore(datastore);
if (pbmSpec != null) {
pbmSpec.stream().forEach(spec -> {
relocSpec.getProfile().add(spec);
});
}
relocSpec.setFolder(folder);
relocSpec.setPool(resourcePool);
relocSpec.setDiskMoveType(computeDiskMoveType().value());
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setLocation(relocSpec);
// Set the provisioning type of the parent disk.
VirtualMachineRelocateSpecDiskLocator diskProvisionTypeLocator = setProvisioningType(vd, datastore, pbmSpec);
if (diskProvisionTypeLocator != null) {
cloneSpec.getLocation().getDisk().add(diskProvisionTypeLocator);
}
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
String displayName = this.ctx.child.name;
ManagedObjectReference cloneTask = getVimPort().cloneVMTask(template, folder, displayName, cloneSpec);
TaskInfo info = waitTaskEnd(cloneTask);
if (info.getState() == TaskInfoState.ERROR) {
MethodFault fault = info.getError().getFault();
if (fault instanceof FileAlreadyExists) {
// a .vmx file already exists, assume someone won the race to create the vm
return null;
} else {
return VimUtils.rethrow(info.getError());
}
}
return (ManagedObjectReference) info.getResult();
}
use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class InstanceClient method deployOvf.
private ManagedObjectReference deployOvf(URI ovfUri) throws Exception {
OvfDeployer deployer = new OvfDeployer(this.connection);
CustomProperties cust = CustomProperties.of(this.ctx.child.description);
URI archiveUri = cust.getUri(OvfParser.PROP_OVF_ARCHIVE_URI);
if (archiveUri != null) {
logger.info("Prefer ova {} uri to ovf {}", archiveUri, ovfUri);
OvfRetriever retriever = deployer.getRetriever();
ovfUri = retriever.downloadIfOva(archiveUri);
}
ManagedObjectReference folder = getVmFolder();
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.bootDisk);
ManagedObjectReference ds = getDataStoreForDisk(this.bootDisk, pbmSpec);
ManagedObjectReference resourcePool = getResourcePool();
String vmName = "pmt-" + deployer.getRetriever().hash(ovfUri);
GetMoRef get = new GetMoRef(this.connection);
ManagedObjectReference vm = findTemplateByName(vmName, get);
if (vm == null) {
String config = cust.getString(OvfParser.PROP_OVF_CONFIGURATION);
Lock lock = getLock(vmName);
lock.lock();
try {
vm = findTemplateByName(vmName, get);
if (vm == null) {
OvfParser parser = new OvfParser();
Document ovfDoc = parser.retrieveDescriptor(ovfUri);
List<OvfNetworkMapping> networks = mapNetworks(parser.extractNetworks(ovfDoc), ovfDoc, this.ctx.nics);
vm = deployer.deployOvf(ovfUri, getHost(), folder, vmName, networks, ds, Collections.emptyList(), config, resourcePool);
logger.info("Removing NICs from deployed template: {} ({})", vmName, vm.getValue());
ArrayOfVirtualDevice devices = get.entityProp(vm, VimPath.vm_config_hardware_device);
if (devices != null) {
VirtualMachineConfigSpec reconfig = new VirtualMachineConfigSpec();
for (VirtualDevice device : devices.getVirtualDevice()) {
if (device instanceof VirtualEthernetCard) {
VirtualDeviceConfigSpec spec = new VirtualDeviceConfigSpec();
spec.setDevice(device);
spec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
reconfig.getDeviceChange().add(spec);
}
}
ManagedObjectReference reconfigTask = getVimPort().reconfigVMTask(vm, reconfig);
VimUtils.waitTaskEnd(this.connection, reconfigTask);
}
ManagedObjectReference snapshotTask = getVimPort().createSnapshotTask(vm, "initial", null, false, false);
VimUtils.waitTaskEnd(this.connection, snapshotTask);
}
} catch (Exception e) {
logger.warn("Error deploying Ovf for template [" + vmName + "],reason:", e);
vm = awaitVM(vmName, folder, get);
} finally {
lock.unlock();
}
}
if (!isSameDatastore(ds, vm, get)) {
// make sure the original VM template is ready
Object snapshot = get.entityProp(vm, VimPath.vm_snapshot);
if (snapshot == null) {
vm = awaitVM(vmName, folder, get);
}
vm = replicateVMTemplate(resourcePool, ds, pbmSpec, folder, vmName, vm, get);
}
return cloneOvfBasedTemplate(vm, ds, folder, resourcePool, pbmSpec);
}
use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class InstanceClient method deleteInstance.
public void deleteInstance(ServiceHost serviceHost) throws Exception {
ManagedObjectReference vm = CustomProperties.of(this.ctx.child).getMoRef(CustomProperties.MOREF);
if (vm == null) {
logger.info("No moref associated with the given instance, skipping delete.");
return;
}
ArrayOfVirtualDevice devices = this.get.entityProp(vm, VimPath.vm_config_hardware_device);
// Handle disks of the VM during VM deletion based on its persistent flag.
handleVirtualDiskCleanup(serviceHost, vm, devices, this.ctx.disks);
TaskInfo info;
// power off
ManagedObjectReference task = getVimPort().powerOffVMTask(vm);
info = waitTaskEnd(task);
ignoreError("Ignore error powering off VM", info);
// delete vm
task = getVimPort().destroyTask(vm);
info = waitTaskEnd(task);
ignoreError("Ignore error deleting VM", info);
// Handle CD ROM folder clean up.
handleVirtualCDRomCleanup(this.ctx.disks);
}
use of com.vmware.vim25.ArrayOfVirtualDevice in project photon-model by vmware.
the class InstanceClient method handleVirtualDiskCleanup.
/**
* For every disk states in the compute based on the persistent flag if it is set as TRUE,
* then disk will be detached before we delete the vm.
*/
private void handleVirtualDiskCleanup(ServiceHost serviceHost, ManagedObjectReference vm, ArrayOfVirtualDevice devices, List<DiskStateExpanded> disks) throws Exception {
if (CollectionUtils.isEmpty(disks)) {
return;
}
List<DiskStateExpanded> persistDisks = disks.stream().filter(disk -> disk.type == DiskType.HDD).filter(disk -> disk.persistent != null && disk.persistent).collect(Collectors.toList());
if (CollectionUtils.isEmpty(persistDisks)) {
return;
}
List<Operation> diskUpdateOps = new ArrayList<>(persistDisks.size());
for (DiskStateExpanded ds : persistDisks) {
VirtualDisk vd = (VirtualDisk) findMatchingVirtualDevice(getListOfVirtualDisk(devices), ds);
if (vd != null) {
detachDisk(this.connection, vd, vm, getVimPort());
}
// Now update the status of the persistent disks to be AVAILABLE
ds.status = DiskService.DiskStatus.AVAILABLE;
CustomProperties.of(ds).put(DISK_CONTROLLER_NUMBER, (String) null).put(PROVIDER_DISK_UNIQUE_ID, (String) null);
ds.id = UriUtils.getLastPathSegment(ds.documentSelfLink);
diskUpdateOps.add(Operation.createPut(PhotonModelUriUtils.createInventoryUri(serviceHost, ds.documentSelfLink)).setReferer(serviceHost.getUri()).setBody(ds));
disks.remove(ds);
}
// call patch operations on the disk states
OperationJoin.create(diskUpdateOps).setCompletion((os, errors) -> {
if (errors != null && !errors.isEmpty()) {
logger.warn(String.format("Exception in updating persistent disk during deletion of VM." + " Error : %s", Utils.toString(errors)));
}
}).sendWith(serviceHost);
}
Aggregations