Search in sources :

Example 1 with VirtualMachineImportSpec

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

the class HypervisorHostHelper method readOVF.

public static List<Pair<String, Boolean>> readOVF(VmwareHypervisorHost host, String ovfFilePath, DatastoreMO dsMo) throws Exception {
    List<Pair<String, Boolean>> ovfVolumeInfos = new ArrayList<Pair<String, Boolean>>();
    List<String> files = new ArrayList<String>();
    ManagedObjectReference morRp = host.getHyperHostOwnerResourcePool();
    assert (morRp != null);
    ManagedObjectReference morHost = host.getMor();
    String importEntityName = UUID.randomUUID().toString();
    OvfCreateImportSpecParams importSpecParams = new OvfCreateImportSpecParams();
    importSpecParams.setHostSystem(morHost);
    importSpecParams.setLocale("US");
    importSpecParams.setEntityName(importEntityName);
    importSpecParams.setDeploymentOption("");
    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;
        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());
        }
    }
    VirtualMachineImportSpec importSpec = (VirtualMachineImportSpec) ovfImportResult.getImportSpec();
    if (importSpec == null) {
        String msg = "createImportSpec() failed to create import specification for OVF template at " + ovfFilePath;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    File ovfFile = new File(ovfFilePath);
    for (OvfFileItem ovfFileItem : ovfImportResult.getFileItem()) {
        String absFile = ovfFile.getParent() + File.separator + ovfFileItem.getPath();
        files.add(absFile);
    }
    int osDiskSeqNumber = 0;
    VirtualMachineConfigSpec config = importSpec.getConfigSpec();
    String paramVal = getOVFParamValue(config);
    if (paramVal != null && !paramVal.isEmpty()) {
        try {
            osDiskSeqNumber = getOsDiskFromOvfConf(config, paramVal);
        } catch (Exception e) {
            osDiskSeqNumber = 0;
        }
    }
    int diskCount = 0;
    int deviceCount = 0;
    List<VirtualDeviceConfigSpec> deviceConfigList = config.getDeviceChange();
    for (VirtualDeviceConfigSpec deviceSpec : deviceConfigList) {
        Boolean osDisk = false;
        VirtualDevice device = deviceSpec.getDevice();
        if (device instanceof VirtualDisk) {
            if ((osDiskSeqNumber == 0 && diskCount == 0) || osDiskSeqNumber == deviceCount) {
                osDisk = true;
            }
            Pair<String, Boolean> ovfVolumeInfo = new Pair<String, Boolean>(files.get(diskCount), osDisk);
            ovfVolumeInfos.add(ovfVolumeInfo);
            diskCount++;
        }
        deviceCount++;
    }
    return ovfVolumeInfos;
}
Also used : OvfFileItem(com.vmware.vim25.OvfFileItem) LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) ArrayList(java.util.ArrayList) VirtualDevice(com.vmware.vim25.VirtualDevice) OvfCreateImportSpecResult(com.vmware.vim25.OvfCreateImportSpecResult) CloudException(com.cloud.exception.CloudException) 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) VirtualDisk(com.vmware.vim25.VirtualDisk) OvfCreateImportSpecParams(com.vmware.vim25.OvfCreateImportSpecParams) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualMachineImportSpec(com.vmware.vim25.VirtualMachineImportSpec) OvfFile(com.vmware.vim25.OvfFile) File(java.io.File) Pair(com.cloud.utils.Pair) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

CloudException (com.cloud.exception.CloudException)1 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)1 Pair (com.cloud.utils.Pair)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)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 OvfFile (com.vmware.vim25.OvfFile)1 OvfFileItem (com.vmware.vim25.OvfFileItem)1 VirtualDevice (com.vmware.vim25.VirtualDevice)1 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)1 VirtualDisk (com.vmware.vim25.VirtualDisk)1 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)1 VirtualMachineImportSpec (com.vmware.vim25.VirtualMachineImportSpec)1 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 InvalidParameterException (java.security.InvalidParameterException)1 ArrayList (java.util.ArrayList)1