Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-fix JENKINS-24661 #58

Merged
merged 2 commits into from
Sep 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 103 additions & 28 deletions src/main/java/org/jenkinsci/plugins/vSphereCloudSlaveTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,60 @@

package org.jenkinsci.plugins;

import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.domains.SchemeRequirement;

import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.ItemGroup;
import hudson.model.TaskListener;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Descriptor.FormException;
import hudson.model.Label;
import hudson.model.Node.Mode;
import hudson.model.labels.LabelAtom;
import hudson.plugins.sshslaves.SSHLauncher;
import hudson.security.ACL;
import hudson.security.AccessControlled;
import hudson.slaves.NodeProperty;
import hudson.slaves.CommandLauncher;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.JNLPLauncher;
import hudson.slaves.RetentionStrategy;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;
import jenkins.slaves.JnlpSlaveAgentProtocol;

import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials;
import hudson.model.Descriptor.FormException;
import hudson.model.Label;
import hudson.model.Node.Mode;
import hudson.model.TaskListener;
import hudson.model.labels.LabelAtom;
import hudson.slaves.NodeProperty;

import java.io.IOException;
import java.io.PrintStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.jenkinsci.plugins.vsphere.VSphereCloudRetentionStrategy;
import org.jenkinsci.plugins.vsphere.RunOnceCloudRetentionStrategy;
import org.jenkinsci.plugins.vsphere.VSphereCloudRetentionStrategy;
import org.jenkinsci.plugins.vsphere.VSphereConnectionConfig;
import org.jenkinsci.plugins.vsphere.VSphereGuestInfoProperty;
import org.jenkinsci.plugins.vsphere.builders.Messages;
import org.jenkinsci.plugins.vsphere.tools.CloudProvisioningState;
import org.jenkinsci.plugins.vsphere.tools.VSphere;
import org.jenkinsci.plugins.vsphere.tools.VSphereException;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.domains.SchemeRequirement;
import com.vmware.vim25.mo.VirtualMachine;

/**
*
Expand All @@ -84,6 +83,7 @@ public class vSphereCloudSlaveTemplate implements Describable<vSphereCloudSlaveT

private final String cloneNamePrefix;
private final String masterImageName;
private Boolean useSnapshot; // almost final
private final String snapshotName;
private final boolean linkedClone;
private final String cluster;
Expand Down Expand Up @@ -119,6 +119,7 @@ public class vSphereCloudSlaveTemplate implements Describable<vSphereCloudSlaveT
@DataBoundConstructor
public vSphereCloudSlaveTemplate(final String cloneNamePrefix,
final String masterImageName,
final Boolean useSnapshot,
final String snapshotName,
final boolean linkedClone,
final String cluster,
Expand All @@ -145,6 +146,7 @@ public vSphereCloudSlaveTemplate(final String cloneNamePrefix,
this.cloneNamePrefix = cloneNamePrefix;
this.masterImageName = masterImageName;
this.snapshotName = snapshotName;
this.useSnapshot = useSnapshot;
this.linkedClone = linkedClone;
this.cluster = cluster;
this.resourcePool = resourcePool;
Expand Down Expand Up @@ -178,6 +180,10 @@ public String getMasterImageName() {
return this.masterImageName;
}

public boolean getUseSnapshot() {
return useSnapshot.booleanValue();
}

public String getSnapshotName() {
return this.snapshotName;
}
Expand Down Expand Up @@ -293,6 +299,9 @@ protected Object readResolve() {
if(this.templateInstanceCap == 0) {
this.templateInstanceCap = Integer.MAX_VALUE;
}
if ( this.useSnapshot == null ) {
this.useSnapshot = Boolean.valueOf(this.snapshotName!=null);
}
/*
* If we've upgraded from an earlier version of the plugin
* where things were hard-coded instead of configurable
Expand Down Expand Up @@ -333,7 +342,22 @@ public vSphereCloudProvisionedSlave provision(final CloudProvisioningState algor
final PrintStream logger = listener.getLogger();
final VSphere vSphere = getParent().vSphereInstance();
final boolean POWER_ON = true;
vSphere.cloneVm(cloneName, this.masterImageName, this.linkedClone, this.resourcePool, this.cluster, this.datastore, POWER_ON, logger);
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;
}
vSphere.cloneOrDeployVm(cloneName, this.masterImageName, this.linkedClone, this.resourcePool, this.cluster, this.datastore, useCurrentSnapshot, snapshotToUse, POWER_ON, logger);
try {
if( this.guestInfoProperties!=null && !this.guestInfoProperties.isEmpty()) {
final Map<String, String> resolvedGuestInfoProperties = calculateGuestInfoProperties(cloneName, listener);
Expand All @@ -344,7 +368,8 @@ public vSphereCloudProvisionedSlave provision(final CloudProvisioningState algor
}
final ComputerLauncher configuredLauncher = determineLauncher(vSphere, cloneName);
final RetentionStrategy<?> configuredStrategy = determineRetention();
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, snapshotName, String.valueOf(this.launchDelay), null, String.valueOf(this.limitedRunCount));
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 ) {
Expand Down Expand Up @@ -411,10 +436,60 @@ public FormValidation doCheckNumberOfExecutors(@QueryParameter String numberOfEx
return FormValidation.validatePositiveInteger(numberOfExecutors);
}

public FormValidation doCheckLinkedClone(@QueryParameter boolean linkedClone, @QueryParameter boolean useSnapshot) {
final boolean noSnapshot = !useSnapshot;
if (linkedClone && noSnapshot) {
return FormValidation.warning("Linked clones are based upon a snapshot.");
}
return FormValidation.ok();
}

public FormValidation doCheckLaunchDelay(@QueryParameter String launchDelay) {
return FormValidation.validateNonNegativeInteger(launchDelay);
}

public FormValidation doTestCloneParameters(@QueryParameter String vsHost, @QueryParameter String vsDescription,
@QueryParameter String credentialsId, @QueryParameter String masterImageName,
@QueryParameter boolean linkedClone, @QueryParameter boolean useSnapshot,
@QueryParameter String snapshotName) {
try {
final VSphereConnectionConfig config = new VSphereConnectionConfig(vsHost, credentialsId);
final String effectiveUsername = config.getUsername();
final String effectivePassword = config.getPassword();
final VSphere vsphere = VSphere.connect(vsHost + "/sdk", effectiveUsername, effectivePassword);
try {
final VirtualMachine vm = vsphere.getVmByName(masterImageName);
if (vm == null) {
return FormValidation.error(Messages.validation_notFound("master image \"" + masterImageName
+ "\""));
}
if (useSnapshot) {
if (snapshotName != null && !snapshotName.isEmpty()) {
final Object snapshot = vsphere.getSnapshotInTree(vm, snapshotName);
if (snapshot == null) {
return FormValidation.error(Messages.validation_notFound("snapshot \"" + snapshotName
+ "\""));
}
} else {
final Object snapshot = vm.getCurrentSnapShot();
if (snapshot == null) {
return FormValidation.error("No snapshots found.");
}
}
} else {
if (linkedClone) {
return FormValidation.warning("vSphere doesn't like creating linked clones without a snapshot");
}
}
return FormValidation.ok(Messages.validation_success());
} finally {
vsphere.disconnect();
}
} catch (Exception e) {
return FormValidation.error(e, "Problem validating");
}
}

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup<?> context) {
if(!(context instanceof AccessControlled ? (AccessControlled) context : Jenkins.getInstance()).hasPermission(Computer.CONFIGURE)) {
return new ListBoxModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import hudson.slaves.CloudRetentionStrategy;
import hudson.slaves.EphemeralNode;
import hudson.slaves.RetentionStrategy;
import hudson.util.FormValidation;
import hudson.util.TimeUnit2;

import java.io.IOException;
Expand All @@ -38,7 +37,6 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import hudson.model.Descriptor;
import hudson.slaves.CloudRetentionStrategy;
import hudson.slaves.RetentionStrategy;
import hudson.util.FormValidation;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

public class VSphereCloudRetentionStrategy extends CloudRetentionStrategy {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.jenkinsci.plugins.vsphere.tools;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.jenkinsci.plugins.vSphereCloudSlaveTemplate;
Expand Down
Loading