Search in sources :

Example 16 with FileInfo

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

the class DatastoreMO method folderExists.

public boolean folderExists(String folderParentDatastorePath, String folderName) throws Exception {
    HostDatastoreBrowserMO browserMo = getHostDatastoreBrowserMO();
    HostDatastoreBrowserSearchResults results = browserMo.searchDatastore(folderParentDatastorePath, folderName, true);
    if (results != null) {
        List<FileInfo> info = results.getFile();
        if (info != null && info.size() > 0) {
            s_logger.info("Folder " + folderName + " exists on datastore");
            return true;
        }
    }
    s_logger.info("Folder " + folderName + " does not exist on datastore");
    return false;
}
Also used : HostDatastoreBrowserSearchResults(com.vmware.vim25.HostDatastoreBrowserSearchResults) FileInfo(com.vmware.vim25.FileInfo)

Example 17 with FileInfo

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

the class DatastoreMO method searchFileInSubFolders.

public String searchFileInSubFolders(String fileName, boolean caseInsensitive) throws Exception {
    String datastorePath = "[" + getName() + "]";
    String rootDirectoryFilePath = String.format("%s %s", datastorePath, fileName);
    if (fileExists(rootDirectoryFilePath)) {
        return rootDirectoryFilePath;
    }
    String parentFolderPath = null;
    String absoluteFileName = null;
    s_logger.info("Searching file " + fileName + " in " + datastorePath);
    HostDatastoreBrowserMO browserMo = getHostDatastoreBrowserMO();
    ArrayList<HostDatastoreBrowserSearchResults> results = browserMo.searchDatastoreSubFolders("[" + getName() + "]", fileName, caseInsensitive);
    if (results != null && results.size() > 1) {
        s_logger.warn("Multiple files with name " + fileName + " exists in datastore " + datastorePath + ". Trying to choose first file found in search attempt.");
    } else if (results == null) {
        String msg = "No file found with name " + fileName + " found in datastore " + datastorePath;
        s_logger.error(msg);
        throw new CloudException(msg);
    }
    for (HostDatastoreBrowserSearchResults result : results) {
        List<FileInfo> info = result.getFile();
        if (info != null && info.size() > 0) {
            for (FileInfo fi : info) {
                absoluteFileName = parentFolderPath = result.getFolderPath();
                s_logger.info("Found file " + fileName + " in datastore at " + absoluteFileName);
                if (parentFolderPath.endsWith("]"))
                    absoluteFileName += " ";
                absoluteFileName += fi.getPath();
                break;
            }
        }
    }
    return absoluteFileName;
}
Also used : HostDatastoreBrowserSearchResults(com.vmware.vim25.HostDatastoreBrowserSearchResults) FileInfo(com.vmware.vim25.FileInfo) CloudException(com.cloud.exception.CloudException)

Example 18 with FileInfo

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

the class HypervisorHostHelper method createBlankVm.

public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, String vmInternalCSName, int cpuCount, int cpuSpeedMHz, int cpuReservedMHz, boolean limitCpuUse, int memoryMB, int memoryReserveMB, String guestOsIdentifier, ManagedObjectReference morDs, boolean snapshotDirToParent, Pair<String, String> controllerInfo, Boolean systemVm) throws Exception {
    if (s_logger.isInfoEnabled())
        s_logger.info("Create blank VM. cpuCount: " + cpuCount + ", cpuSpeed(MHz): " + cpuSpeedMHz + ", mem(Mb): " + memoryMB);
    VirtualDeviceConfigSpec controllerSpec = null;
    // VM config basics
    VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
    vmConfig.setName(vmName);
    if (vmInternalCSName == null)
        vmInternalCSName = vmName;
    VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse);
    String recommendedController = host.getRecommendedDiskController(guestOsIdentifier);
    String newRootDiskController = controllerInfo.first();
    String newDataDiskController = controllerInfo.second();
    if (DiskControllerType.getType(controllerInfo.first()) == DiskControllerType.osdefault) {
        newRootDiskController = recommendedController;
    }
    if (DiskControllerType.getType(controllerInfo.second()) == DiskControllerType.osdefault) {
        newDataDiskController = recommendedController;
    }
    Pair<String, String> updatedControllerInfo = new Pair<String, String>(newRootDiskController, newDataDiskController);
    String scsiDiskController = HypervisorHostHelper.getScsiController(updatedControllerInfo, recommendedController);
    // If there is requirement for a SCSI controller, ensure to create those.
    if (scsiDiskController != null) {
        int busNum = 0;
        int maxControllerCount = VmwareHelper.MAX_SCSI_CONTROLLER_COUNT;
        if (systemVm) {
            maxControllerCount = 1;
        }
        while (busNum < maxControllerCount) {
            VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
            scsiControllerSpec = getControllerSpec(DiskControllerType.getType(scsiDiskController).toString(), busNum);
            vmConfig.getDeviceChange().add(scsiControllerSpec);
            busNum++;
        }
    }
    if (guestOsIdentifier.startsWith("darwin")) {
        //Mac OS
        s_logger.debug("Add USB Controller device for blank Mac OS VM " + vmName);
        //For Mac OS X systems, the EHCI+UHCI controller is enabled by default and is required for USB mouse and keyboard access.
        VirtualDevice usbControllerDevice = VmwareHelper.prepareUSBControllerDevice();
        VirtualDeviceConfigSpec usbControllerSpec = new VirtualDeviceConfigSpec();
        usbControllerSpec.setDevice(usbControllerDevice);
        usbControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        vmConfig.getDeviceChange().add(usbControllerSpec);
    }
    VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
    DatastoreMO dsMo = new DatastoreMO(host.getContext(), morDs);
    fileInfo.setVmPathName(String.format("[%s]", dsMo.getName()));
    vmConfig.setFiles(fileInfo);
    VirtualMachineVideoCard videoCard = new VirtualMachineVideoCard();
    videoCard.setControllerKey(100);
    videoCard.setUseAutoDetect(true);
    VirtualDeviceConfigSpec videoDeviceSpec = new VirtualDeviceConfigSpec();
    videoDeviceSpec.setDevice(videoCard);
    videoDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    vmConfig.getDeviceChange().add(videoDeviceSpec);
    if (host.createVm(vmConfig)) {
        // Here, when attempting to find the VM, we need to use the name
        // with which we created it. This is the only such place where
        // we need to do this. At all other places, we always use the
        // VM's internal cloudstack generated name. Here, we cannot use
        // the internal name because we can set the internal name into the
        // VM's custom field CLOUD_VM_INTERNAL_NAME only after we create
        // the VM.
        VirtualMachineMO vmMo = host.findVmOnHyperHost(vmName);
        assert (vmMo != null);
        vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME, vmInternalCSName);
        int ideControllerKey = -1;
        while (ideControllerKey < 0) {
            ideControllerKey = vmMo.tryGetIDEDeviceControllerKey();
            if (ideControllerKey >= 0)
                break;
            s_logger.info("Waiting for IDE controller be ready in VM: " + vmInternalCSName);
            Thread.sleep(1000);
        }
        return true;
    }
    return false;
}
Also used : VirtualMachineVideoCard(com.vmware.vim25.VirtualMachineVideoCard) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) Pair(com.cloud.utils.Pair)

Example 19 with FileInfo

use of com.vmware.vim25.FileInfo 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 20 with FileInfo

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

Aggregations

VirtualMachineFileInfo (com.vmware.vim25.VirtualMachineFileInfo)11 FileInfo (com.vmware.vim25.FileInfo)6 HostDatastoreBrowserSearchResults (com.vmware.vim25.HostDatastoreBrowserSearchResults)6 FileInfo (com.intellij.openapi.util.io.win32.FileInfo)4 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)3 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)3 CloudException (com.cloud.exception.CloudException)2 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)2 FileAttributes (com.intellij.openapi.util.io.FileAttributes)2 VirtualFileWithId (com.intellij.openapi.vfs.VirtualFileWithId)2 DynamicProperty (com.vmware.vim25.DynamicProperty)2 ObjectContent (com.vmware.vim25.ObjectContent)2 ObjectSpec (com.vmware.vim25.ObjectSpec)2 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)2 PropertySpec (com.vmware.vim25.PropertySpec)2 VirtualLsiLogicController (com.vmware.vim25.VirtualLsiLogicController)2 VirtualMachineVideoCard (com.vmware.vim25.VirtualMachineVideoCard)2 THashMap (gnu.trove.THashMap)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2