Search in sources :

Example 6 with VirtualMachineConfigInfo

use of com.vmware.vim25.VirtualMachineConfigInfo in project vsphere-cloud-plugin by jenkinsci.

the class vSphereCloudSlaveTemplate method findWhichJenkinsThisVMBelongsTo.

private static String findWhichJenkinsThisVMBelongsTo(final VSphere vSphere, String cloneName) {
    final VirtualMachine vm;
    try {
        vm = vSphere.getVmByName(cloneName);
    } catch (VSphereException e) {
        return null;
    }
    final VirtualMachineConfigInfo config = vm.getConfig();
    if (config == null) {
        return null;
    }
    final OptionValue[] extraConfigs = config.extraConfig;
    if (extraConfigs == null) {
        return null;
    }
    String vmJenkinsUrl = null;
    for (final OptionValue ec : extraConfigs) {
        final String configName = ec.key;
        final String configValue = ec.value == null ? null : ec.value.toString();
        if (VSPHERE_ATTR_FOR_JENKINSURL.equals(configName)) {
            vmJenkinsUrl = configValue;
        }
    }
    return vmJenkinsUrl;
}
Also used : OptionValue(com.vmware.vim25.OptionValue) VirtualMachineConfigInfo(com.vmware.vim25.VirtualMachineConfigInfo) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 7 with VirtualMachineConfigInfo

use of com.vmware.vim25.VirtualMachineConfigInfo in project vsphere-cloud-plugin by jenkinsci.

the class VSphere method cloneOrDeployVm.

/**
 * Creates a new VM by cloning an existing VM or Template.
 *
 * @param cloneName
 *            The name for the new VM.
 * @param sourceName
 *            The name of the VM or Template that is to be cloned.
 * @param linkedClone
 *            If true then the clone will be defined as a delta from the
 *            original, rather than a "full fat" copy. If this is true then
 *            you will need to use a snapshot.
 * @param resourcePoolName
 *            (Optional) The name of the resource pool to use, or null.
 * @param cluster
 *            (Optional) The name of the cluster, or null.
 * @param datastoreName
 *            (Optional) The name of the data store, or null.
 * @param folderName
 *            (Optional) The name or path of the VSphere folder, or null
 * @param useCurrentSnapshot
 *            If true then the clone will be created from the source VM's
 *            "current" snapshot. This means that the VM <em>must</em> have
 *            at least one snapshot.
 * @param namedSnapshot
 *            If set then the clone will be created from the source VM's
 *            snapshot of this name. If this is set then
 *            <code>useCurrentSnapshot</code> must not be set.
 * @param powerOn
 *            If true then the new VM will be switched on after it has been
 *            created.
 * @param extraConfigParameters
 *            (Optional) parameters to set in the VM's "extra config"
 *            object. This data can then be read back at a later stage.In
 *            the case of parameters whose name starts "guestinfo.", the
 *            parameter can be read by the VMware Tools on the client OS.
 *            e.g. a variable named "guestinfo.Foo" with value "Bar" could
 *            be read on the guest using the command-line
 *            <tt>vmtoolsd --cmd "info-get guestinfo.Foo"</tt>.
 * @param customizationSpec
 *            (Optional) Customization spec to use for this VM, or null
 * @param jLogger
 *            Where to log to.
 * @throws VSphereException
 *             if anything goes wrong.
 */
public void cloneOrDeployVm(String cloneName, String sourceName, boolean linkedClone, String resourcePoolName, String cluster, String datastoreName, String folderName, boolean useCurrentSnapshot, final String namedSnapshot, boolean powerOn, Map<String, String> extraConfigParameters, String customizationSpec, PrintStream jLogger) throws VSphereException {
    try {
        final VirtualMachine sourceVm = getVmByName(sourceName);
        if (sourceVm == null) {
            throw new VSphereNotFoundException("VM or template", sourceName);
        }
        if (getVmByName(cloneName) != null) {
            throw new VSphereDuplicateException("VM", cloneName);
        }
        final VirtualMachineConfigInfo vmConfig = sourceVm.getConfig();
        final boolean sourceIsATemplate = vmConfig.template;
        final String sourceType = sourceIsATemplate ? "Template" : "VM";
        final VirtualMachineRelocateSpec rel = createRelocateSpec(jLogger, linkedClone, resourcePoolName, cluster, datastoreName, sourceIsATemplate);
        final VirtualMachineCloneSpec cloneSpec = createCloneSpec(rel);
        cloneSpec.setTemplate(false);
        cloneSpec.powerOn = powerOn;
        if (namedSnapshot != null && !namedSnapshot.isEmpty()) {
            if (useCurrentSnapshot) {
                throw new IllegalArgumentException("It is not valid to request a clone of " + sourceType + "  \"" + sourceName + "\" based on its snapshot \"" + namedSnapshot + "\" AND also specify that the latest snapshot should be used.  Either choose to use the latest snapshot, or name a snapshot, or neither, but not both.");
            }
            final VirtualMachineSnapshot namedVMSnapshot = getSnapshotInTree(sourceVm, namedSnapshot);
            if (namedVMSnapshot == null) {
                throw new VSphereNotFoundException("Snapshot", namedSnapshot, "Source " + sourceType + "  \"" + sourceName + "\" has no snapshot called \"" + namedSnapshot + "\".");
            }
            logMessage(jLogger, "Clone of " + sourceType + " \"" + sourceName + "\" will be based on named snapshot \"" + namedSnapshot + "\".");
            cloneSpec.setSnapshot(namedVMSnapshot.getMOR());
        }
        if (useCurrentSnapshot) {
            final VirtualMachineSnapshot currentSnapShot = sourceVm.getCurrentSnapShot();
            if (currentSnapShot == null) {
                throw new VSphereNotFoundException("Snapshot", null, "Source " + sourceType + "  \"" + sourceName + "\" requires at least one snapshot.");
            }
            logMessage(jLogger, "Clone of " + sourceType + " \"" + sourceName + "\" will be based on current snapshot \"" + currentSnapShot.toString() + "\".");
            cloneSpec.setSnapshot(currentSnapShot.getMOR());
        }
        if (extraConfigParameters != null && !extraConfigParameters.isEmpty()) {
            logMessage(jLogger, "Clone of " + sourceType + " \"" + sourceName + "\" will have extra configuration parameters " + extraConfigParameters + ".");
            VirtualMachineConfigSpec cs = createVMConfigSpecFromExtraConfigParameters(extraConfigParameters);
            cloneSpec.setConfig(cs);
        }
        if (customizationSpec != null && customizationSpec.length() > 0) {
            logMessage(jLogger, "Clone of " + sourceType + " \"" + sourceName + "\" will use customization specification \"" + customizationSpec + "\".");
            CustomizationSpecItem spec = getCustomizationSpecByName(customizationSpec);
            cloneSpec.setCustomization(spec.getSpec());
        }
        Folder folder;
        if (folderName == null || folderName.isEmpty() || folderName.equals(" ")) {
            // same folder as source
            folder = (Folder) sourceVm.getParent();
        } else if (!folderExists(folderName)) {
            folder = (Folder) sourceVm.getParent();
            logMessage(jLogger, "Unable to find the specified folder. Creating VM in the same folder as its parent ");
        } else {
            folder = getFolder(folderName);
        }
        final Task task = sourceVm.cloneVM_Task(folder, cloneName, cloneSpec);
        logMessage(jLogger, "Started cloning of " + sourceType + " \"" + sourceName + "\". Please wait ...");
        final String status = task.waitForTask();
        if (!TaskInfoState.success.toString().equals(status)) {
            throw newVSphereException(task.getTaskInfo(), "Couldn't clone \"" + sourceName + "\". " + "Clone task ended with status " + status + ".");
        }
        logMessage(jLogger, "Successfully cloned VM \"" + sourceName + "\" to create \"" + cloneName + "\".");
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException(e);
    }
}
Also used : Task(com.vmware.vim25.mo.Task) VirtualMachineConfigInfo(com.vmware.vim25.VirtualMachineConfigInfo) Folder(com.vmware.vim25.mo.Folder) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachineSnapshot(com.vmware.vim25.mo.VirtualMachineSnapshot) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) CustomizationSpecItem(com.vmware.vim25.CustomizationSpecItem) VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) VirtualMachineCloneSpec(com.vmware.vim25.VirtualMachineCloneSpec) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 8 with VirtualMachineConfigInfo

use of com.vmware.vim25.VirtualMachineConfigInfo in project vsphere-cloud-plugin by jenkinsci.

the class ReconfigureDisk method addSCSIController.

private VirtualLsiLogicController addSCSIController(VirtualMachine vm) throws Exception {
    VirtualMachineConfigInfo vmConfig = vm.getConfig();
    VirtualPCIController pci = null;
    Set<Integer> scsiBuses = new HashSet<Integer>();
    for (VirtualDevice vmDevice : vmConfig.getHardware().getDevice()) {
        if (vmDevice instanceof VirtualPCIController) {
            pci = (VirtualPCIController) vmDevice;
        } else if (vmDevice instanceof VirtualSCSIController) {
            VirtualSCSIController ctrl = (VirtualSCSIController) vmDevice;
            scsiBuses.add(ctrl.getBusNumber());
        }
    }
    if (pci == null) {
        throw new VSphereException("No PCI controller found");
    }
    VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec deviceSpec = new VirtualDeviceConfigSpec();
    deviceSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
    VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController();
    scsiCtrl.setControllerKey(pci.getKey());
    scsiCtrl.setSharedBus(VirtualSCSISharing.noSharing);
    for (int i = 0; ; ++i) {
        if (!scsiBuses.contains(Integer.valueOf(i))) {
            scsiCtrl.setBusNumber(i);
            break;
        }
    }
    deviceSpec.setDevice(scsiCtrl);
    vmSpec.setDeviceChange(new VirtualDeviceConfigSpec[] { deviceSpec });
    Task task = vm.reconfigVM_Task(vmSpec);
    task.waitForTask();
    return scsiCtrl;
}
Also used : Task(com.vmware.vim25.mo.Task) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) HashSet(java.util.HashSet)

Aggregations

VirtualMachineConfigInfo (com.vmware.vim25.VirtualMachineConfigInfo)7 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)4 OptionValue (com.vmware.vim25.OptionValue)3 VirtualDevice (com.vmware.vim25.VirtualDevice)3 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)3 Pair (com.cloud.utils.Pair)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 StorageIOAllocationInfo (com.vmware.vim25.StorageIOAllocationInfo)2 VirtualDisk (com.vmware.vim25.VirtualDisk)2 Task (com.vmware.vim25.mo.Task)2 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)2 ArrayList (java.util.ArrayList)2 VSphereException (org.jenkinsci.plugins.vsphere.tools.VSphereException)2 VolumeVO (com.cloud.storage.VolumeVO)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 CustomizationSpecItem (com.vmware.vim25.CustomizationSpecItem)1 SharesInfo (com.vmware.vim25.SharesInfo)1 VirtualDeviceFileBackingInfo (com.vmware.vim25.VirtualDeviceFileBackingInfo)1 VirtualMachineCloneSpec (com.vmware.vim25.VirtualMachineCloneSpec)1 VirtualMachineRelocateSpec (com.vmware.vim25.VirtualMachineRelocateSpec)1