Search in sources :

Example 11 with VmConfigSpec

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

the class VirtualMachineMO method tearDownDevices.

public void tearDownDevices(Class<?>[] deviceClasses) throws Exception {
    VirtualDevice[] devices = getMatchedDevices(deviceClasses);
    if (devices.length > 0) {
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[devices.length];
        for (int i = 0; i < devices.length; i++) {
            deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
            deviceConfigSpecArray[i].setDevice(devices[i]);
            deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
            vmConfigSpec.getDeviceChange().add(deviceConfigSpecArray[i]);
        }
        if (!configureVm(vmConfigSpec)) {
            throw new Exception("Failed to detach devices");
        }
    }
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice)

Example 12 with VmConfigSpec

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

the class InstanceClient method populateVAppProperties.

private boolean populateVAppProperties(VirtualMachineConfigSpec spec, ArrayOfVAppPropertyInfo currentProps) {
    if (this.bootDisk == null) {
        return false;
    }
    boolean customizationsApplied = false;
    int nextKey = 1;
    if (currentProps != null) {
        nextKey = currentProps.getVAppPropertyInfo().stream().mapToInt(VAppPropertyInfo::getKey).max().orElse(1);
        nextKey++;
    }
    String ovfEnv = getFileItemByPath(this.bootDisk, OVF_PROPERTY_ENV);
    if (ovfEnv != null) {
        @SuppressWarnings("unchecked") Map<String, String> map = Utils.fromJson(ovfEnv, Map.class);
        if (!map.isEmpty()) {
            customizationsApplied = true;
            VmConfigSpec configSpec = new VmConfigSpec();
            configSpec.getOvfEnvironmentTransport().add(OvfDeployer.TRANSPORT_ISO);
            if (currentProps == null) {
                currentProps = new ArrayOfVAppPropertyInfo();
            }
            currentProps.getVAppPropertyInfo().forEach(pi -> {
                if (map.containsKey(pi.getId())) {
                    VAppPropertySpec ps = new VAppPropertySpec();
                    ps.setOperation(ArrayUpdateOperation.EDIT);
                    pi.setValue(map.remove(pi.getId()));
                    ps.setInfo(pi);
                    configSpec.getProperty().add(ps);
                }
            });
            // only new key/values
            for (Entry<String, String> entry : map.entrySet()) {
                VAppPropertyInfo pi = new VAppPropertyInfo();
                pi.setId(entry.getKey());
                pi.setType("string");
                pi.setKey(nextKey++);
                pi.setValue(entry.getValue());
                VAppPropertySpec ps = new VAppPropertySpec();
                ps.setOperation(ArrayUpdateOperation.ADD);
                ps.setInfo(pi);
                configSpec.getProperty().add(ps);
            }
            spec.setVAppConfig(configSpec);
        }
    }
    return customizationsApplied;
}
Also used : VmConfigSpec(com.vmware.vim25.VmConfigSpec) ArrayOfVAppPropertyInfo(com.vmware.vim25.ArrayOfVAppPropertyInfo) VAppPropertySpec(com.vmware.vim25.VAppPropertySpec) VAppPropertyInfo(com.vmware.vim25.VAppPropertyInfo) ArrayOfVAppPropertyInfo(com.vmware.vim25.ArrayOfVAppPropertyInfo)

Example 13 with VmConfigSpec

use of com.vmware.vim25.VmConfigSpec 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;
}
Also used : OvfFileItem(com.vmware.vim25.OvfFileItem) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) OvfCreateImportSpecResult(com.vmware.vim25.OvfCreateImportSpecResult) FinderException(com.vmware.photon.controller.model.adapters.vsphere.util.finders.FinderException) IOException(java.io.IOException) GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) OvfCreateImportSpecParams(com.vmware.vim25.OvfCreateImportSpecParams) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) HttpNfcLeaseInfo(com.vmware.vim25.HttpNfcLeaseInfo) VmConfigSpec(com.vmware.vim25.VmConfigSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) HttpNfcLeaseDeviceUrl(com.vmware.vim25.HttpNfcLeaseDeviceUrl)

Example 14 with VmConfigSpec

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

the class VirtualMachineMO method tearDownDevices.

public void tearDownDevices(Class<?>[] deviceClasses) throws Exception {
    VirtualDevice[] devices = getMatchedDevices(deviceClasses);
    if (devices.length > 0) {
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[devices.length];
        for (int i = 0; i < devices.length; i++) {
            deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
            deviceConfigSpecArray[i].setDevice(devices[i]);
            deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.remove);
        }
        vmConfigSpec.setDeviceChange(deviceConfigSpecArray);
        if (!configureVm(vmConfigSpec)) {
            throw new Exception("Failed to detach devices");
        }
    }
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice)

Example 15 with VmConfigSpec

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

the class VirtualMachineMO method setVncConfigInfo.

public boolean setVncConfigInfo(boolean enableVnc, String vncPassword, int vncPort, String keyboard) throws Exception {
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
    OptionValue[] vncOptions = VmwareHelper.composeVncOptions(null, enableVnc, vncPassword, vncPort, keyboard);
    vmConfigSpec.setExtraConfig(vncOptions);
    ManagedObjectReference morTask = _context.getService().reconfigVM_Task(_mor, vmConfigSpec);
    String result = _context.getServiceUtil().waitForTask(morTask);
    if (result.equals("sucess")) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware reconfigVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) OptionValue(com.vmware.vim25.OptionValue) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)19 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)17 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)11 VirtualDevice (com.vmware.vim25.VirtualDevice)10 IOException (java.io.IOException)8 CloudException (com.cloud.exception.CloudException)7 InternalErrorException (com.cloud.exception.InternalErrorException)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 ConnectException (java.net.ConnectException)7 RemoteException (java.rmi.RemoteException)7 ConfigurationException (javax.naming.ConfigurationException)7 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)5 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)5 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)5 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)5 OptionValue (com.vmware.vim25.OptionValue)4 VirtualDisk (com.vmware.vim25.VirtualDisk)4 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)3 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)3