Search in sources :

Example 1 with InvalidNameFaultMsg

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

the class HypervisorHostHelper method importVmFromOVF.

/**
 * deploys a new VM from a ovf spec. It ignores network, defaults locale to 'US'
 * @throws Exception shoud be a VmwareResourceException
 */
public static void importVmFromOVF(VmwareHypervisorHost host, String ovfFilePath, String vmName, DatastoreMO dsMo, String diskOption, ManagedObjectReference morRp, ManagedObjectReference morHost, String configurationId) throws CloudRuntimeException, IOException {
    assert (morRp != null);
    OvfCreateImportSpecParams importSpecParams = new OvfCreateImportSpecParams();
    importSpecParams.setHostSystem(morHost);
    importSpecParams.setLocale("US");
    importSpecParams.setEntityName(vmName);
    String deploymentOption = StringUtils.isNotBlank(configurationId) ? configurationId : "";
    importSpecParams.setDeploymentOption(deploymentOption);
    // diskOption: thin, thick, etc
    importSpecParams.setDiskProvisioning(diskOption);
    String ovfDescriptor = removeOVFNetwork(HttpNfcLeaseMO.readOvfContent(ovfFilePath));
    VmwareContext context = host.getContext();
    OvfCreateImportSpecResult ovfImportResult = null;
    try {
        ovfImportResult = context.getService().createImportSpec(context.getServiceContent().getOvfManager(), ovfDescriptor, morRp, dsMo.getMor(), importSpecParams);
    } catch (ConcurrentAccessFaultMsg | FileFaultFaultMsg | InvalidDatastoreFaultMsg | InvalidStateFaultMsg | RuntimeFaultFaultMsg | TaskInProgressFaultMsg | VmConfigFaultFaultMsg error) {
        throw new CloudRuntimeException("ImportSpec creation failed", error);
    }
    if (ovfImportResult == null) {
        String msg = "createImportSpec() failed. ovfFilePath: " + ovfFilePath + ", vmName: " + vmName + ", diskOption: " + diskOption;
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    }
    if (!ovfImportResult.getError().isEmpty()) {
        for (LocalizedMethodFault fault : ovfImportResult.getError()) {
            s_logger.error("createImportSpec error: " + fault.getLocalizedMessage());
        }
        throw new CloudRuntimeException("Failed to create an import spec from " + ovfFilePath + ". Check log for details.");
    }
    if (!ovfImportResult.getWarning().isEmpty()) {
        for (LocalizedMethodFault fault : ovfImportResult.getError()) {
            s_logger.warn("createImportSpec warning: " + fault.getLocalizedMessage());
        }
    }
    DatacenterMO dcMo = null;
    try {
        dcMo = new DatacenterMO(context, host.getHyperHostDatacenter());
    } catch (Exception e) {
        throw new CloudRuntimeException(String.format("no datacenter for host '%s' available in context", context.getServerAddress()), e);
    }
    ManagedObjectReference folderMO = null;
    try {
        folderMO = dcMo.getVmFolder();
    } catch (Exception e) {
        throw new CloudRuntimeException("no management handle for VmFolder", e);
    }
    ManagedObjectReference morLease = null;
    try {
        morLease = context.getService().importVApp(morRp, ovfImportResult.getImportSpec(), folderMO, morHost);
    } catch (DuplicateNameFaultMsg | FileFaultFaultMsg | InsufficientResourcesFaultFaultMsg | InvalidDatastoreFaultMsg | InvalidNameFaultMsg | OutOfBoundsFaultMsg | RuntimeFaultFaultMsg | VmConfigFaultFaultMsg fault) {
        throw new CloudRuntimeException("import vApp failed", fault);
    }
    if (morLease == null) {
        String msg = "importVApp() failed. ovfFilePath: " + ovfFilePath + ", vmName: " + vmName + ", diskOption: " + diskOption;
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    }
    boolean importSuccess = true;
    final HttpNfcLeaseMO leaseMo = new HttpNfcLeaseMO(context, morLease);
    HttpNfcLeaseState state = null;
    try {
        state = leaseMo.waitState(new HttpNfcLeaseState[] { HttpNfcLeaseState.READY, HttpNfcLeaseState.ERROR });
    } catch (Exception e) {
        throw new CloudRuntimeException("exception while waiting for leaseMO", e);
    }
    try {
        if (state == HttpNfcLeaseState.READY) {
            final long totalBytes = HttpNfcLeaseMO.calcTotalBytes(ovfImportResult);
            File ovfFile = new File(ovfFilePath);
            HttpNfcLeaseInfo httpNfcLeaseInfo = null;
            try {
                httpNfcLeaseInfo = leaseMo.getLeaseInfo();
            } catch (Exception e) {
                throw new CloudRuntimeException("error waiting for lease info", e);
            }
            List<HttpNfcLeaseDeviceUrl> deviceUrls = httpNfcLeaseInfo.getDeviceUrl();
            long bytesAlreadyWritten = 0;
            final HttpNfcLeaseMO.ProgressReporter progressReporter = leaseMo.createProgressReporter();
            try {
                for (HttpNfcLeaseDeviceUrl deviceUrl : deviceUrls) {
                    String deviceKey = deviceUrl.getImportKey();
                    for (OvfFileItem ovfFileItem : ovfImportResult.getFileItem()) {
                        if (deviceKey.equals(ovfFileItem.getDeviceId())) {
                            String absoluteFile = ovfFile.getParent() + File.separator + ovfFileItem.getPath();
                            s_logger.info("Uploading file: " + absoluteFile);
                            File f = new File(absoluteFile);
                            if (f.exists()) {
                                String urlToPost = deviceUrl.getUrl();
                                urlToPost = resolveHostNameInUrl(dcMo, urlToPost);
                                context.uploadVmdkFile(ovfFileItem.isCreate() ? "PUT" : "POST", urlToPost, absoluteFile, bytesAlreadyWritten, new ActionDelegate<Long>() {

                                    @Override
                                    public void action(Long param) {
                                        progressReporter.reportProgress((int) (param * 100 / totalBytes));
                                    }
                                });
                                bytesAlreadyWritten += ovfFileItem.getSize();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                String erroMsg = "File upload task failed to complete due to: " + e.getMessage();
                s_logger.error(erroMsg);
                // Set flag to cleanup the stale template left due to failed import operation, if any
                importSuccess = false;
                throw new CloudRuntimeException(erroMsg, e);
            } catch (Throwable th) {
                String errorMsg = "throwable caught during file upload task: " + th.getMessage();
                s_logger.error(errorMsg);
                // Set flag to cleanup the stale template left due to failed import operation, if any
                importSuccess = false;
                throw new CloudRuntimeException(errorMsg, th);
            } finally {
                progressReporter.close();
            }
            if (bytesAlreadyWritten == totalBytes) {
                try {
                    leaseMo.updateLeaseProgress(100);
                } catch (Exception e) {
                    throw new CloudRuntimeException("error while waiting for lease update", e);
                }
            }
        } else if (state == HttpNfcLeaseState.ERROR) {
            LocalizedMethodFault error = null;
            try {
                error = leaseMo.getLeaseError();
            } catch (Exception e) {
                throw new CloudRuntimeException("error getting lease error", e);
            }
            MethodFault fault = error.getFault();
            String erroMsg = "Object creation on vCenter failed due to: Exception: " + fault.getClass().getName() + ", message: " + error.getLocalizedMessage();
            s_logger.error(erroMsg);
            throw new CloudRuntimeException(erroMsg);
        }
    } finally {
        try {
            if (!importSuccess) {
                s_logger.error("Aborting the lease on " + vmName + " after import operation failed.");
                leaseMo.abortLease();
            } else {
                leaseMo.completeLease();
            }
        } catch (Exception e) {
            throw new CloudRuntimeException("error completing lease", e);
        }
    }
}
Also used : OvfCreateImportSpecResult(com.vmware.vim25.OvfCreateImportSpecResult) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) InsufficientResourcesFaultFaultMsg(com.vmware.vim25.InsufficientResourcesFaultFaultMsg) ConcurrentAccessFaultMsg(com.vmware.vim25.ConcurrentAccessFaultMsg) InvalidDatastoreFaultMsg(com.vmware.vim25.InvalidDatastoreFaultMsg) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) FileFaultFaultMsg(com.vmware.vim25.FileFaultFaultMsg) InvalidNameFaultMsg(com.vmware.vim25.InvalidNameFaultMsg) HttpNfcLeaseDeviceUrl(com.vmware.vim25.HttpNfcLeaseDeviceUrl) OvfFileItem(com.vmware.vim25.OvfFileItem) MethodFault(com.vmware.vim25.MethodFault) LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) TaskInProgressFaultMsg(com.vmware.vim25.TaskInProgressFaultMsg) InvalidParameterException(java.security.InvalidParameterException) CloudException(com.cloud.exception.CloudException) TransformerException(javax.xml.transform.TransformerException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OvfCreateImportSpecParams(com.vmware.vim25.OvfCreateImportSpecParams) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) InvalidStateFaultMsg(com.vmware.vim25.InvalidStateFaultMsg) VmConfigFaultFaultMsg(com.vmware.vim25.VmConfigFaultFaultMsg) DuplicateNameFaultMsg(com.vmware.vim25.DuplicateNameFaultMsg) HttpNfcLeaseInfo(com.vmware.vim25.HttpNfcLeaseInfo) OvfFile(com.vmware.vim25.OvfFile) File(java.io.File) OutOfBoundsFaultMsg(com.vmware.vim25.OutOfBoundsFaultMsg) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) HttpNfcLeaseState(com.vmware.vim25.HttpNfcLeaseState)

Aggregations

CloudException (com.cloud.exception.CloudException)1 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ConcurrentAccessFaultMsg (com.vmware.vim25.ConcurrentAccessFaultMsg)1 DuplicateNameFaultMsg (com.vmware.vim25.DuplicateNameFaultMsg)1 FileFaultFaultMsg (com.vmware.vim25.FileFaultFaultMsg)1 HttpNfcLeaseDeviceUrl (com.vmware.vim25.HttpNfcLeaseDeviceUrl)1 HttpNfcLeaseInfo (com.vmware.vim25.HttpNfcLeaseInfo)1 HttpNfcLeaseState (com.vmware.vim25.HttpNfcLeaseState)1 InsufficientResourcesFaultFaultMsg (com.vmware.vim25.InsufficientResourcesFaultFaultMsg)1 InvalidDatastoreFaultMsg (com.vmware.vim25.InvalidDatastoreFaultMsg)1 InvalidNameFaultMsg (com.vmware.vim25.InvalidNameFaultMsg)1 InvalidStateFaultMsg (com.vmware.vim25.InvalidStateFaultMsg)1 LocalizedMethodFault (com.vmware.vim25.LocalizedMethodFault)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 MethodFault (com.vmware.vim25.MethodFault)1 OutOfBoundsFaultMsg (com.vmware.vim25.OutOfBoundsFaultMsg)1 OvfCreateImportSpecParams (com.vmware.vim25.OvfCreateImportSpecParams)1 OvfCreateImportSpecResult (com.vmware.vim25.OvfCreateImportSpecResult)1 OvfFile (com.vmware.vim25.OvfFile)1