Search in sources :

Example 1 with OvfCreateImportSpecResult

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

the class HttpNfcLeaseMO method calcTotalBytes.

public static long calcTotalBytes(OvfCreateImportSpecResult ovfImportResult) {
    List<OvfFileItem> fileItemArr = ovfImportResult.getFileItem();
    long totalBytes = 0;
    if (fileItemArr != null) {
        for (OvfFileItem fi : fileItemArr) {
            totalBytes += fi.getSize();
        }
    }
    return totalBytes;
}
Also used : OvfFileItem(com.vmware.vim25.OvfFileItem)

Example 2 with OvfCreateImportSpecResult

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

the class HttpNfcLeaseMO method calcTotalBytes.

public static long calcTotalBytes(OvfCreateImportSpecResult ovfImportResult) {
    OvfFileItem[] fileItemArr = ovfImportResult.getFileItem();
    long totalBytes = 0;
    if (fileItemArr != null) {
        for (OvfFileItem fi : fileItemArr) {
            totalBytes += fi.getSize();
        }
    }
    return totalBytes;
}
Also used : OvfFileItem(com.vmware.vim25.OvfFileItem)

Example 3 with OvfCreateImportSpecResult

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

the class HypervisorHostHelper method importVmFromOVF.

public static void importVmFromOVF(VmwareHypervisorHost host, String ovfFilePath, String vmName, DatastoreMO dsMo, String diskOption, ManagedObjectReference morRp, ManagedObjectReference morHost) throws Exception {
    assert (morRp != null);
    OvfCreateImportSpecParams importSpecParams = new OvfCreateImportSpecParams();
    importSpecParams.setHostSystem(morHost);
    importSpecParams.setLocale("US");
    importSpecParams.setEntityName(vmName);
    importSpecParams.setDeploymentOption("");
    // diskOption: thin, thick, etc
    importSpecParams.setDiskProvisioning(diskOption);
    importSpecParams.setPropertyMapping(null);
    String ovfDescriptor = HttpNfcLeaseMO.readOvfContent(ovfFilePath);
    VmwareContext context = host.getContext();
    OvfCreateImportSpecResult ovfImportResult = context.getService().createImportSpec(context.getServiceContent().getOvfManager(), ovfDescriptor, morRp, dsMo.getMor(), importSpecParams);
    if (ovfImportResult == null) {
        String msg = "createImportSpec() failed. ovfFilePath: " + ovfFilePath + ", vmName: " + vmName + ", diskOption: " + diskOption;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    DatacenterMO dcMo = new DatacenterMO(context, host.getHyperHostDatacenter());
    ManagedObjectReference morLease = context.getService().importVApp(morRp, ovfImportResult.getImportSpec(), dcMo.getVmFolder(), morHost);
    if (morLease == null) {
        String msg = "importVApp() failed. ovfFilePath: " + ovfFilePath + ", vmName: " + vmName + ", diskOption: " + diskOption;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    final HttpNfcLeaseMO leaseMo = new HttpNfcLeaseMO(context, morLease);
    HttpNfcLeaseState state = leaseMo.waitState(new HttpNfcLeaseState[] { HttpNfcLeaseState.ready, HttpNfcLeaseState.error });
    try {
        if (state == HttpNfcLeaseState.ready) {
            final long totalBytes = HttpNfcLeaseMO.calcTotalBytes(ovfImportResult);
            File ovfFile = new File(ovfFilePath);
            HttpNfcLeaseInfo httpNfcLeaseInfo = leaseMo.getLeaseInfo();
            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();
                            String urlToPost = deviceUrl.getUrl();
                            urlToPost = resolveHostNameInUrl(dcMo, urlToPost);
                            context.uploadVmdkFile(ovfFileItem.isCreate() ? "PUT" : "POST", urlToPost, absoluteFile, bytesAlreadyWritten, new ActionDelegate<Long>() {

                                public void action(Long param) {
                                    progressReporter.reportProgress((int) (param * 100 / totalBytes));
                                }
                            });
                            bytesAlreadyWritten += ovfFileItem.getSize();
                        }
                    }
                }
            } finally {
                progressReporter.close();
            }
            leaseMo.updateLeaseProgress(100);
        }
    } finally {
        leaseMo.completeLease();
    }
}
Also used : OvfFileItem(com.vmware.vim25.OvfFileItem) OvfCreateImportSpecResult(com.vmware.vim25.OvfCreateImportSpecResult) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) OvfCreateImportSpecParams(com.vmware.vim25.OvfCreateImportSpecParams) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) HttpNfcLeaseInfo(com.vmware.vim25.HttpNfcLeaseInfo) File(java.io.File) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) HttpNfcLeaseState(com.vmware.vim25.HttpNfcLeaseState) HttpNfcLeaseDeviceUrl(com.vmware.vim25.HttpNfcLeaseDeviceUrl)

Example 4 with OvfCreateImportSpecResult

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

the class HypervisorHostHelper method importVmFromOVF.

public static void importVmFromOVF(VmwareHypervisorHost host, String ovfFilePath, String vmName, DatastoreMO dsMo, String diskOption, ManagedObjectReference morRp, ManagedObjectReference morHost) throws Exception {
    assert (morRp != null);
    OvfCreateImportSpecParams importSpecParams = new OvfCreateImportSpecParams();
    importSpecParams.setHostSystem(morHost);
    importSpecParams.setLocale("US");
    importSpecParams.setEntityName(vmName);
    importSpecParams.setDeploymentOption("");
    // diskOption: thin, thick, etc
    importSpecParams.setDiskProvisioning(diskOption);
    String ovfDescriptor = removeOVFNetwork(HttpNfcLeaseMO.readOvfContent(ovfFilePath));
    VmwareContext context = host.getContext();
    OvfCreateImportSpecResult ovfImportResult = context.getService().createImportSpec(context.getServiceContent().getOvfManager(), ovfDescriptor, morRp, dsMo.getMor(), importSpecParams);
    if (ovfImportResult == null) {
        String msg = "createImportSpec() failed. ovfFilePath: " + ovfFilePath + ", vmName: " + vmName + ", diskOption: " + diskOption;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    if (!ovfImportResult.getError().isEmpty()) {
        for (LocalizedMethodFault fault : ovfImportResult.getError()) {
            s_logger.error("createImportSpec error: " + fault.getLocalizedMessage());
        }
        throw new CloudException("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 = new DatacenterMO(context, host.getHyperHostDatacenter());
    ManagedObjectReference morLease = context.getService().importVApp(morRp, ovfImportResult.getImportSpec(), dcMo.getVmFolder(), morHost);
    if (morLease == null) {
        String msg = "importVApp() failed. ovfFilePath: " + ovfFilePath + ", vmName: " + vmName + ", diskOption: " + diskOption;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    boolean importSuccess = true;
    final HttpNfcLeaseMO leaseMo = new HttpNfcLeaseMO(context, morLease);
    HttpNfcLeaseState state = leaseMo.waitState(new HttpNfcLeaseState[] { HttpNfcLeaseState.READY, HttpNfcLeaseState.ERROR });
    try {
        if (state == HttpNfcLeaseState.READY) {
            final long totalBytes = HttpNfcLeaseMO.calcTotalBytes(ovfImportResult);
            File ovfFile = new File(ovfFilePath);
            HttpNfcLeaseInfo httpNfcLeaseInfo = leaseMo.getLeaseInfo();
            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();
                            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 Exception(erroMsg);
            } 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 Exception(errorMsg, th);
            } finally {
                progressReporter.close();
            }
            if (bytesAlreadyWritten == totalBytes) {
                leaseMo.updateLeaseProgress(100);
            }
        } else if (state == HttpNfcLeaseState.ERROR) {
            LocalizedMethodFault error = leaseMo.getLeaseError();
            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 Exception(erroMsg);
        }
    } finally {
        if (!importSuccess) {
            s_logger.error("Aborting the lease on " + vmName + " after import operation failed.");
            leaseMo.abortLease();
        } else {
            leaseMo.completeLease();
        }
    }
}
Also used : OvfFileItem(com.vmware.vim25.OvfFileItem) LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) MethodFault(com.vmware.vim25.MethodFault) LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) OvfCreateImportSpecResult(com.vmware.vim25.OvfCreateImportSpecResult) CloudException(com.cloud.exception.CloudException) URISyntaxException(java.net.URISyntaxException) InvalidParameterException(java.security.InvalidParameterException) CloudException(com.cloud.exception.CloudException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OvfCreateImportSpecParams(com.vmware.vim25.OvfCreateImportSpecParams) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) HttpNfcLeaseInfo(com.vmware.vim25.HttpNfcLeaseInfo) File(java.io.File) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) HttpNfcLeaseState(com.vmware.vim25.HttpNfcLeaseState) HttpNfcLeaseDeviceUrl(com.vmware.vim25.HttpNfcLeaseDeviceUrl)

Aggregations

OvfFileItem (com.vmware.vim25.OvfFileItem)4 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 HttpNfcLeaseDeviceUrl (com.vmware.vim25.HttpNfcLeaseDeviceUrl)2 HttpNfcLeaseInfo (com.vmware.vim25.HttpNfcLeaseInfo)2 HttpNfcLeaseState (com.vmware.vim25.HttpNfcLeaseState)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 OvfCreateImportSpecParams (com.vmware.vim25.OvfCreateImportSpecParams)2 OvfCreateImportSpecResult (com.vmware.vim25.OvfCreateImportSpecResult)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 CloudException (com.cloud.exception.CloudException)1 LocalizedMethodFault (com.vmware.vim25.LocalizedMethodFault)1 MethodFault (com.vmware.vim25.MethodFault)1 IOException (java.io.IOException)1 InvalidParameterException (java.security.InvalidParameterException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 SAXException (org.xml.sax.SAXException)1