use of com.vmware.vim25.OvfNetworkMapping in project photon-model by vmware.
the class InstanceClient method mapNetworks.
private List<OvfNetworkMapping> mapNetworks(List<String> ovfNetworkNames, Document ovfDoc, List<NetworkInterfaceStateWithDetails> nics) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
List<OvfNetworkMapping> networks = new ArrayList<>();
if (ovfNetworkNames.isEmpty() || nics.isEmpty()) {
return networks;
}
CustomProperties custProp;
ManagedObjectReference moRef;
NetworkInterfaceStateWithDetails nic = nics.iterator().next();
if (nic.subnet != null) {
custProp = CustomProperties.of(nic.subnet);
} else {
custProp = CustomProperties.of(nic.network);
}
moRef = custProp.getMoRef(CustomProperties.MOREF);
if (moRef == null) {
moRef = this.finder.networkList("*").iterator().next().object;
}
final ManagedObjectReference finalMoRef = moRef;
ovfNetworkNames.forEach(n -> {
OvfNetworkMapping nm = new OvfNetworkMapping();
nm.setName(n);
nm.setNetwork(finalMoRef);
networks.add(nm);
});
return networks;
}
use of com.vmware.vim25.OvfNetworkMapping in project photon-model by vmware.
the class OvfDeployer method deployOvf.
public ManagedObjectReference deployOvf(URI ovfUri, ManagedObjectReference host, ManagedObjectReference vmFolder, String vmName, List<OvfNetworkMapping> networks, ManagedObjectReference datastore, Collection<KeyValue> ovfProps, String deploymentConfig, ManagedObjectReference resourcePool) throws Exception {
String ovfDescriptor = getRetriever().retrieveAsString(ovfUri);
OvfCreateImportSpecParams params = new OvfCreateImportSpecParams();
params.setHostSystem(host);
params.setLocale("US");
params.setEntityName(vmName);
if (deploymentConfig == null) {
deploymentConfig = "";
}
params.setDeploymentOption(deploymentConfig);
params.getNetworkMapping().addAll(networks);
params.setDiskProvisioning(OvfCreateImportSpecParamsDiskProvisioningType.THIN.name());
if (ovfProps != null) {
params.getPropertyMapping().addAll(ovfProps);
}
ManagedObjectReference ovfManager = this.connection.getServiceContent().getOvfManager();
OvfCreateImportSpecResult importSpecResult = getVimPort().createImportSpec(ovfManager, ovfDescriptor, resourcePool, datastore, params);
if (!importSpecResult.getError().isEmpty()) {
return VimUtils.rethrow(importSpecResult.getError().get(0));
}
long totalBytes = getImportSizeBytes(importSpecResult);
ManagedObjectReference lease = getVimPort().importVApp(resourcePool, importSpecResult.getImportSpec(), vmFolder, host);
LeaseProgressUpdater leaseUpdater = new LeaseProgressUpdater(this.connection, lease, totalBytes);
GetMoRef get = new GetMoRef(this.connection);
HttpNfcLeaseInfo httpNfcLeaseInfo;
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
try {
leaseUpdater.awaitReady();
logger.info("Lease ready");
// start updating the lease
leaseUpdater.start(executorService);
httpNfcLeaseInfo = get.entityProp(lease, PROP_INFO);
List<HttpNfcLeaseDeviceUrl> deviceUrls = httpNfcLeaseInfo.getDeviceUrl();
String ip = this.connection.getURI().getHost();
String basePath = extractBasePath(ovfUri);
for (HttpNfcLeaseDeviceUrl deviceUrl : deviceUrls) {
String deviceKey = deviceUrl.getImportKey();
for (OvfFileItem ovfFileItem : importSpecResult.getFileItem()) {
if (deviceKey.equals(ovfFileItem.getDeviceId())) {
logger.debug("Importing device id: {}", deviceKey);
String sourceUri = computeDiskSourceUri(basePath, ovfFileItem);
String uploadUri = makUploadUri(ip, deviceUrl);
uploadVmdkFile(ovfFileItem, sourceUri, uploadUri, leaseUpdater, this.ovfRetriever.getClient());
logger.info("Completed uploading VMDK file {}", sourceUri);
}
}
}
// complete lease
leaseUpdater.complete();
} catch (Exception e) {
leaseUpdater.abort(VimUtils.convertExceptionToFault(e));
logger.info("Error importing ovf", e);
throw e;
} finally {
executorService.shutdown();
}
httpNfcLeaseInfo = get.entityProp(lease, PROP_INFO);
ManagedObjectReference entity = httpNfcLeaseInfo.getEntity();
// as this is an OVF it makes sense to enable the OVF transport
// only the guestInfo is enabled by default
VmConfigSpec spec = new VmConfigSpec();
spec.getOvfEnvironmentTransport().add(TRANSPORT_GUESTINFO);
spec.getOvfEnvironmentTransport().add(TRANSPORT_ISO);
VirtualMachineConfigSpec reconfig = new VirtualMachineConfigSpec();
reconfig.setVAppConfig(spec);
ManagedObjectReference reconfigTask = getVimPort().reconfigVMTask(entity, reconfig);
VimUtils.waitTaskEnd(this.connection, reconfigTask);
return entity;
}
use of com.vmware.vim25.OvfNetworkMapping 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