Search in sources :

Example 1 with VSphereDuplicateException

use of org.jenkinsci.plugins.vsphere.tools.VSphereDuplicateException 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)

Aggregations

FormException (hudson.model.Descriptor.FormException)1 ComputerLauncher (hudson.slaves.ComputerLauncher)1 IOException (java.io.IOException)1 VSphereDuplicateException (org.jenkinsci.plugins.vsphere.tools.VSphereDuplicateException)1 VSphereException (org.jenkinsci.plugins.vsphere.tools.VSphereException)1