Search in sources :

Example 1 with OvfDeployer

use of com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfDeployer 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);
}
Also used : OvfRetriever(com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfRetriever) OvfNetworkMapping(com.vmware.vim25.OvfNetworkMapping) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) OvfDeployer(com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfDeployer) ClientUtils.findMatchingVirtualDevice(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.findMatchingVirtualDevice) ArrayOfVirtualDevice(com.vmware.vim25.ArrayOfVirtualDevice) VirtualDevice(com.vmware.vim25.VirtualDevice) OvfParser(com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfParser) ArrayOfVirtualDevice(com.vmware.vim25.ArrayOfVirtualDevice) VirtualMachineDefinedProfileSpec(com.vmware.vim25.VirtualMachineDefinedProfileSpec) Document(org.w3c.dom.Document) URI(java.net.URI) FinderException(com.vmware.photon.controller.model.adapters.vsphere.util.finders.FinderException) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ClientUtils.findMatchingVirtualDevice (com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.findMatchingVirtualDevice)1 OvfDeployer (com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfDeployer)1 OvfParser (com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfParser)1 OvfRetriever (com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfRetriever)1 GetMoRef (com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef)1 FinderException (com.vmware.photon.controller.model.adapters.vsphere.util.finders.FinderException)1 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)1 ArrayOfVirtualDevice (com.vmware.vim25.ArrayOfVirtualDevice)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 OvfNetworkMapping (com.vmware.vim25.OvfNetworkMapping)1 VirtualDevice (com.vmware.vim25.VirtualDevice)1 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)1 VirtualEthernetCard (com.vmware.vim25.VirtualEthernetCard)1 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)1 VirtualMachineDefinedProfileSpec (com.vmware.vim25.VirtualMachineDefinedProfileSpec)1 URI (java.net.URI)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Document (org.w3c.dom.Document)1