Search in sources :

Example 1 with MethodFault

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

the class VmwareHelper method getExceptionMessage.

public static String getExceptionMessage(Throwable e, boolean printStack) {
    if (e instanceof MethodFault) {
        final StringWriter writer = new StringWriter();
        writer.append("Exception: " + e.getClass().getName() + "\n");
        writer.append("message: " + ((MethodFault) e).getFaultString() + "\n");
        if (printStack) {
            writer.append("stack: ");
            e.printStackTrace(new PrintWriter(writer));
        }
        return writer.toString();
    }
    return ExceptionUtil.toString(e, printStack);
}
Also used : MethodFault(com.vmware.vim25.MethodFault) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 2 with MethodFault

use of com.vmware.vim25.MethodFault 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)

Example 3 with MethodFault

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

the class VmwareHelper method getExceptionMessage.

public static String getExceptionMessage(Throwable e, boolean printStack) {
    // from individual exception through getFaultInfo, so we have to use reflection here to get MethodFault information.
    try {
        Class<? extends Throwable> cls = e.getClass();
        Method mth = cls.getDeclaredMethod("getFaultInfo", (Class<?>) null);
        if (mth != null) {
            Object fault = mth.invoke(e, (Object[]) null);
            if (fault instanceof MethodFault) {
                final StringWriter writer = new StringWriter();
                writer.append("Exception: " + fault.getClass().getName() + "\n");
                writer.append("message: " + ((MethodFault) fault).getFaultMessage() + "\n");
                if (printStack) {
                    writer.append("stack: ");
                    e.printStackTrace(new PrintWriter(writer));
                }
                return writer.toString();
            }
        }
    } catch (Exception ex) {
        s_logger.info("[ignored]" + "failed toi get message for exception: " + e.getLocalizedMessage());
    }
    return ExceptionUtil.toString(e, printStack);
}
Also used : MethodFault(com.vmware.vim25.MethodFault) StringWriter(java.io.StringWriter) Method(java.lang.reflect.Method) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

MethodFault (com.vmware.vim25.MethodFault)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 CloudException (com.cloud.exception.CloudException)1 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 HttpNfcLeaseDeviceUrl (com.vmware.vim25.HttpNfcLeaseDeviceUrl)1 HttpNfcLeaseInfo (com.vmware.vim25.HttpNfcLeaseInfo)1 HttpNfcLeaseState (com.vmware.vim25.HttpNfcLeaseState)1 LocalizedMethodFault (com.vmware.vim25.LocalizedMethodFault)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 OvfCreateImportSpecParams (com.vmware.vim25.OvfCreateImportSpecParams)1 OvfCreateImportSpecResult (com.vmware.vim25.OvfCreateImportSpecResult)1 OvfFileItem (com.vmware.vim25.OvfFileItem)1 File (java.io.File)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1 InvalidParameterException (java.security.InvalidParameterException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1