Search in sources :

Example 11 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException 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 12 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class vSphereCloudSlaveTemplate method provision.

private vSphereCloudProvisionedSlave provision(final String cloneName, final PrintStream logger, final Map<String, String> resolvedExtraConfigParameters, final VSphere vSphere) throws VSphereException, FormException, IOException {
    final boolean POWER_ON = true;
    final boolean useCurrentSnapshot;
    final String snapshotToUse;
    if (getUseSnapshot()) {
        final String sn = getSnapshotName();
        if (sn != null && !sn.isEmpty()) {
            useCurrentSnapshot = false;
            snapshotToUse = sn;
        } else {
            useCurrentSnapshot = true;
            snapshotToUse = null;
        }
    } else {
        useCurrentSnapshot = false;
        snapshotToUse = null;
    }
    try {
        vSphere.cloneOrDeployVm(cloneName, this.masterImageName, this.linkedClone, this.resourcePool, this.cluster, this.datastore, this.folder, useCurrentSnapshot, snapshotToUse, POWER_ON, resolvedExtraConfigParameters, this.customizationSpec, logger);
        LOGGER.log(Level.FINE, "Created new VM {0} from image {1}", new Object[] { cloneName, this.masterImageName });
    } catch (VSphereDuplicateException ex) {
        final String vmJenkinsUrl = findWhichJenkinsThisVMBelongsTo(vSphere, cloneName);
        if (vmJenkinsUrl == null) {
            LOGGER.log(Level.SEVERE, "VM {0} name clashes with one we wanted to use, but it wasn't started by this plugin.", cloneName);
            throw ex;
        }
        final String ourJenkinsUrl = Jenkins.getActiveInstance().getRootUrl();
        if (vmJenkinsUrl.equals(ourJenkinsUrl)) {
            LOGGER.log(Level.INFO, "Found existing VM {0} that we started previously (and must have either lost track of it or failed to delete it).", cloneName);
        } else {
            LOGGER.log(Level.SEVERE, "VM {0} name clashes with one we wanted to use, but it doesn't belong to this Jenkins server: it belongs to {1}.  You MUST reconfigure one of these Jenkins servers to use a different naming strategy so that we no longer get clashes within vSphere host {2}. i.e. change the cloneNamePrefix on one/both to ensure uniqueness.", new Object[] { cloneName, vmJenkinsUrl, this.getParent().getVsHost() });
            throw ex;
        }
    } catch (VSphereException ex) {
        // if anything else went wrong, attempt to tidy up
        try {
            vSphere.destroyVm(cloneName, false);
        } catch (Exception logOnly) {
            LOGGER.log(Level.SEVERE, "Unable to create and power-on new VM " + cloneName + " (cloned from image " + this.masterImageName + ") and, worse, bits of the VM may still exist as the attempt to delete the remains also failed.", logOnly);
        }
        throw ex;
    }
    vSphereCloudProvisionedSlave slave = null;
    try {
        final ComputerLauncher configuredLauncher = determineLauncher(vSphere, cloneName);
        final RetentionStrategy<?> configuredStrategy = determineRetention();
        final String snapshotNameForLauncher = "";
        /* we don't make the launcher do anything with snapshots because our clone won't be created with any */
        slave = new vSphereCloudProvisionedSlave(cloneName, this.templateDescription, this.remoteFS, String.valueOf(this.numberOfExecutors), this.mode, this.labelString, configuredLauncher, configuredStrategy, this.nodeProperties, this.parent.getVsDescription(), cloneName, this.forceVMLaunch, this.waitForVMTools, snapshotNameForLauncher, String.valueOf(this.launchDelay), null, String.valueOf(this.limitedRunCount));
    } finally {
        // if anything went wrong, try to tidy up
        if (slave == null) {
            LOGGER.log(Level.FINER, "Creation of slave failed after cloning VM: destroying clone {0}", cloneName);
            vSphere.destroyVm(cloneName, false);
        }
    }
    return slave;
}
Also used : ComputerLauncher(hudson.slaves.ComputerLauncher) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) FormException(hudson.model.Descriptor.FormException) IOException(java.io.IOException) VSphereDuplicateException(org.jenkinsci.plugins.vsphere.tools.VSphereDuplicateException) VSphereDuplicateException(org.jenkinsci.plugins.vsphere.tools.VSphereDuplicateException)

Example 13 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class TakeSnapshot method takeSnapshot.

private boolean takeSnapshot(final Run<?, ?> run, Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    String expandedVm = vm;
    String expandedSnapshotName = snapshotName;
    String expandedDescription = description;
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    if (run instanceof AbstractBuild) {
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedVm = env.expand(vm);
        expandedSnapshotName = env.expand(snapshotName);
        expandedDescription = env.expand(description);
    }
    VSphereLogger.vsLogger(jLogger, "Taking snapshot...");
    vsphere.takeSnapshot(expandedVm, expandedSnapshotName, expandedDescription, includeMemory);
    VSphereLogger.vsLogger(jLogger, "Complete.");
    return true;
}
Also used : PrintStream(java.io.PrintStream) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Example 14 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class SuspendVm method suspend.

private boolean suspend(final Run<?, ?> run, Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    String expandedVm = vm;
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    if (run instanceof AbstractBuild) {
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedVm = env.expand(vm);
    }
    VSphereLogger.vsLogger(jLogger, "Suspending VM...");
    vsphere.suspendVm(vsphere.getVmByName(expandedVm));
    VSphereLogger.vsLogger(jLogger, "Successfully suspended \"" + expandedVm + "\"");
    return true;
}
Also used : PrintStream(java.io.PrintStream) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Example 15 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class ReconfigureDisk method reconfigureDisk.

public boolean reconfigureDisk(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    int diskSize = Integer.parseInt(this.diskSize);
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
        if (run instanceof AbstractBuild) {
            // Add in matrix axes..
            env.overrideAll(((AbstractBuild) run).getBuildVariables());
            diskSize = Integer.parseInt(env.expand(this.diskSize));
        }
        VirtualDeviceConfigSpec vdiskSpec = createAddDiskConfigSpec(vm, diskSize, jLogger);
        VirtualDeviceConfigSpec[] vdiskSpecArray = { vdiskSpec };
        spec.setDeviceChange(vdiskSpecArray);
        VSphereLogger.vsLogger(jLogger, "Configuration done");
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    return true;
}
Also used : PrintStream(java.io.PrintStream) AbstractBuild(hudson.model.AbstractBuild) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) ServletException(javax.servlet.ServletException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Aggregations

VSphereException (org.jenkinsci.plugins.vsphere.tools.VSphereException)22 IOException (java.io.IOException)19 PrintStream (java.io.PrintStream)18 AbstractBuild (hudson.model.AbstractBuild)7 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)4 ServletException (javax.servlet.ServletException)4 OptionValue (com.vmware.vim25.OptionValue)1 VirtualMachineConfigInfo (com.vmware.vim25.VirtualMachineConfigInfo)1 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)1 Datastore (com.vmware.vim25.mo.Datastore)1 DistributedVirtualPortgroup (com.vmware.vim25.mo.DistributedVirtualPortgroup)1 DistributedVirtualSwitch (com.vmware.vim25.mo.DistributedVirtualSwitch)1 ManagedEntity (com.vmware.vim25.mo.ManagedEntity)1 Network (com.vmware.vim25.mo.Network)1 Task (com.vmware.vim25.mo.Task)1 FormException (hudson.model.Descriptor.FormException)1 ComputerLauncher (hudson.slaves.ComputerLauncher)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1