Search in sources :

Example 16 with VirtualMachineFileInfo

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

the class HypervisorHostHelper method createWorkerVM.

public static VirtualMachineMO createWorkerVM(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName) throws Exception {
    // Allow worker VM to float within cluster so that we will have better chance to
    // create it successfully
    ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
    if (morCluster != null)
        hyperHost = new ClusterMO(hyperHost.getContext(), morCluster);
    VirtualMachineMO workingVM = null;
    VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
    vmConfig.setName(vmName);
    vmConfig.setMemoryMB((long) 4);
    vmConfig.setNumCPUs(1);
    vmConfig.setGuestId(VirtualMachineGuestOsIdentifier.OTHER_GUEST.value());
    VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
    fileInfo.setVmPathName(dsMo.getDatastoreRootPath());
    vmConfig.setFiles(fileInfo);
    VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
    scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
    scsiController.setBusNumber(0);
    scsiController.setKey(1);
    VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
    scsiControllerSpec.setDevice(scsiController);
    scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    vmConfig.getDeviceChange().add(scsiControllerSpec);
    if (hyperHost.createVm(vmConfig)) {
        // Ugly work-around, it takes time for newly created VM to appear
        for (int i = 0; i < 10 && workingVM == null; i++) {
            workingVM = hyperHost.findVmOnHyperHost(vmName);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                s_logger.debug("[ignored] interupted while waiting to config vm.");
            }
        }
    }
    if (workingVM != null) {
        workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER, "true");
        String workerTag = String.format("%d-%s", System.currentTimeMillis(), hyperHost.getContext().getStockObject("noderuninfo"));
        workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER_TAG, workerTag);
    }
    return workingVM;
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) VirtualLsiLogicController(com.vmware.vim25.VirtualLsiLogicController) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 17 with VirtualMachineFileInfo

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

the class VirtualMachineMO method getVmxHttpAccessUrl.

public String getVmxHttpAccessUrl() throws Exception {
    Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
    VirtualMachineFileInfo fileInfo = getFileInfo();
    String vmxFilePath = fileInfo.getVmPathName();
    String[] vmxPathTokens = vmxFilePath.split("\\[|\\]|/");
    StringBuffer sb = new StringBuffer("https://" + _context.getServerAddress() + "/folder/");
    sb.append(URLEncoder.encode(vmxPathTokens[2].trim(), "UTF-8"));
    sb.append("/");
    sb.append(URLEncoder.encode(vmxPathTokens[3].trim(), "UTF-8"));
    sb.append("?dcPath=");
    sb.append(URLEncoder.encode(dcInfo.second(), "UTF-8"));
    sb.append("&dsName=");
    sb.append(URLEncoder.encode(vmxPathTokens[1].trim(), "UTF-8"));
    return sb.toString();
}
Also used : VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo)

Example 18 with VirtualMachineFileInfo

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

the class VirtualMachineMO method redoRegistration.

public void redoRegistration(ManagedObjectReference morHost) throws Exception {
    String vmName = getVmName();
    VirtualMachineFileInfo vmFileInfo = getFileInfo();
    boolean isTemplate = isTemplate();
    HostMO hostMo;
    if (morHost != null)
        hostMo = new HostMO(getContext(), morHost);
    else
        hostMo = getRunningHost();
    ManagedObjectReference morFolder = getParentMor();
    ManagedObjectReference morPool = hostMo.getHyperHostOwnerResourcePool();
    _context.getService().unregisterVM(_mor);
    ManagedObjectReference morTask = _context.getService().registerVMTask(morFolder, vmFileInfo.getVmPathName(), vmName, false, morPool, hostMo.getMor());
    boolean result = _context.getVimClient().waitForTask(morTask);
    if (!result) {
        throw new Exception("Unable to register template due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    } else {
        _context.waitForTaskProgressDone(morTask);
        if (isTemplate) {
            VirtualMachineMO vmNewRegistration = hostMo.findVmOnHyperHost(vmName);
            assert (vmNewRegistration != null);
            vmNewRegistration.markAsTemplate();
        }
    }
}
Also used : VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 19 with VirtualMachineFileInfo

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

the class VirtualMachineMO method setSnapshotDirectory.

// snapshot directory in format of: /vmfs/volumes/<datastore name>/<path>
@Deprecated
public void setSnapshotDirectory(String snapshotDir) throws Exception {
    VirtualMachineFileInfo fileInfo = getFileInfo();
    Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
    String vmxUrl = _context.composeDatastoreBrowseUrl(dcInfo.second(), fileInfo.getVmPathName());
    byte[] vmxContent = _context.getResourceContent(vmxUrl);
    BufferedReader in = null;
    BufferedWriter out = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    boolean replaced = false;
    try {
        in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(vmxContent), "UTF-8"));
        out = new BufferedWriter(new OutputStreamWriter(bos, "UTF-8"));
        String line;
        while ((line = in.readLine()) != null) {
            if (line.startsWith("workingDir")) {
                replaced = true;
                out.write(String.format("workingDir=\"%s\"", snapshotDir));
                out.newLine();
            } else {
                out.write(line);
                out.newLine();
            }
        }
        if (!replaced) {
            out.newLine();
            out.write(String.format("workingDir=\"%s\"", snapshotDir));
            out.newLine();
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
    _context.uploadResourceContent(vmxUrl, bos.toByteArray());
// It seems that I don't need to do re-registration. VMware has bug in writing the correct snapshot's VMDK path to
// its disk backing info anyway.
// redoRegistration();
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) BufferedWriter(java.io.BufferedWriter)

Example 20 with VirtualMachineFileInfo

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

the class VirtualMachineMO method getSnapshotDescriptorDatastorePath.

public String getSnapshotDescriptorDatastorePath() throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.getPathSet().add("name");
    pSpec.getPathSet().add("config.files");
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.FALSE);
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);
    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
    assert (ocs != null);
    String vmName = null;
    VirtualMachineFileInfo fileInfo = null;
    assert (ocs.size() == 1);
    for (ObjectContent oc : ocs) {
        List<DynamicProperty> props = oc.getPropSet();
        if (props != null) {
            assert (props.size() == 2);
            for (DynamicProperty prop : props) {
                if (prop.getName().equals("name")) {
                    vmName = prop.getVal().toString();
                } else {
                    fileInfo = (VirtualMachineFileInfo) prop.getVal();
                }
            }
        }
    }
    assert (vmName != null);
    assert (fileInfo != null);
    // .vmsd file exists at the same directory of .vmx file
    DatastoreFile vmxFile = new DatastoreFile(fileInfo.getVmPathName());
    return vmxFile.getCompanionPath(vmName + ".vmsd");
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) DynamicProperty(com.vmware.vim25.DynamicProperty) ArrayList(java.util.ArrayList) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo)

Aggregations

VirtualMachineFileInfo (com.vmware.vim25.VirtualMachineFileInfo)20 Pair (com.cloud.utils.Pair)8 ArrayList (java.util.ArrayList)8 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)6 SnapshotInfo (com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo)4 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)4 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)4 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)4 VirtualMachineSnapshotInfo (com.vmware.vim25.VirtualMachineSnapshotInfo)4 Ternary (com.cloud.utils.Ternary)2 DynamicProperty (com.vmware.vim25.DynamicProperty)2 ObjectContent (com.vmware.vim25.ObjectContent)2 ObjectSpec (com.vmware.vim25.ObjectSpec)2 VirtualDevice (com.vmware.vim25.VirtualDevice)2 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)2 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)2 VirtualLsiLogicController (com.vmware.vim25.VirtualLsiLogicController)2 VirtualMachineVideoCard (com.vmware.vim25.VirtualMachineVideoCard)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2