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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations