use of com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfParser 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);
}
Aggregations