diff --git a/.gitignore b/.gitignore index 51dfa8e1..d6a3a32a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ work .project .classpath .settings +.idea +vsphere-cloud.iml +*.log /nb-configuration.xml -/nbactions.xml \ No newline at end of file +/nbactions.xml diff --git a/pom.xml b/pom.xml index 0368cc48..72947747 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.jenkins-ci.plugins plugin - 1.609.2 + 2.6 vsphere-cloud @@ -69,24 +69,48 @@ org.jenkins-ci.plugins credentials - 1.15 + 1.28 jar - - com.vmware - vijava - 5.1 - - - org.jenkins-ci.plugins - node-iterator-api - 1.1 - - - org.jenkins-ci.plugins - ssh-slaves - 1.10 - + + com.toastcoders + yavijava + 6.0.03 + + + org.jenkins-ci.plugins + node-iterator-api + 1.5 + + + org.jenkins-ci.plugins + ssh-slaves + 1.11 + + + org.jenkins-ci.plugins.workflow + workflow-basic-steps + 2.0 + - + + + + + org.jenkins-ci.tools + maven-hpi-plugin + + true + + + + org.codehaus.mojo + findbugs-maven-plugin + + true + + + + + diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/VSphereBuildStep.java b/src/main/java/org/jenkinsci/plugins/vsphere/VSphereBuildStep.java index f0b90734..3dd30b92 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/VSphereBuildStep.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/VSphereBuildStep.java @@ -16,18 +16,20 @@ import hudson.DescriptorExtensionList; import hudson.ExtensionPoint; +import hudson.FilePath; import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.Describable; -import hudson.model.AbstractBuild; -import hudson.model.Descriptor; -import hudson.model.Hudson; -import hudson.slaves.Cloud; +import hudson.model.*; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vSphereCloud; import org.jenkinsci.plugins.vsphere.builders.Messages; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; +import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +import javax.annotation.Nonnull; +import java.io.IOException; /** * Define a base class for all vSphere build steps. All vSphere build steps should extend @@ -45,12 +47,18 @@ public void setVsphere(VSphere vsphere) { this.vsphere = vsphere; } + public String getIP() { + return ""; + } + public static DescriptorExtensionList all() { return Hudson.getInstance().getDescriptorList(VSphereBuildStep.class); } public abstract boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws Exception; + public abstract void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException; + public VSphereBuildStepDescriptor getDescriptor() { return (VSphereBuildStepDescriptor)Hudson.getInstance().getDescriptor(getClass()); } diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/VSphereBuildStepContainer.java b/src/main/java/org/jenkinsci/plugins/vsphere/VSphereBuildStepContainer.java index 2065e570..9e30a709 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/VSphereBuildStepContainer.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/VSphereBuildStepContainer.java @@ -14,24 +14,19 @@ */ package org.jenkinsci.plugins.vsphere; -import hudson.DescriptorExtensionList; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; +import hudson.*; import hudson.init.InitMilestone; import hudson.init.Initializer; -import hudson.model.BuildListener; -import hudson.model.Items; -import hudson.model.AbstractBuild; -import hudson.model.AbstractProject; -import hudson.model.Hudson; +import hudson.model.*; import hudson.slaves.Cloud; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.Builder; import hudson.util.ListBoxModel; +import java.io.IOException; import java.io.PrintStream; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vSphereCloud; import org.jenkinsci.plugins.vsphere.VSphereBuildStep.VSphereBuildStepDescriptor; import org.jenkinsci.plugins.vsphere.builders.Messages; @@ -40,7 +35,9 @@ import org.jenkinsci.plugins.vsphere.tools.VSphereLogger; import org.kohsuke.stapler.DataBoundConstructor; -public class VSphereBuildStepContainer extends Builder { +import javax.annotation.Nonnull; + +public class VSphereBuildStepContainer extends Builder implements SimpleBuildStep { public static final String SELECTABLE_SERVER_NAME = "${VSPHERE_CLOUD_NAME}"; @@ -68,13 +65,15 @@ public VSphereBuildStep getBuildStep() { } @Override - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { VSphere vsphere = null; try { - EnvVars env = build.getEnvironment(listener); - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedServerName = env.expand(serverName); - + String expandedServerName = serverName; + if (run instanceof AbstractBuild) { + EnvVars env = (run.getEnvironment(listener)); + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedServerName = env.expand(serverName); + } startLogs(listener.getLogger(), expandedServerName); //Need to ensure this server is same as one that was previously saved. //TODO - also need to improve logging here. @@ -87,16 +86,19 @@ public boolean perform(final AbstractBuild build, final Launcher launcher, } buildStep.setVsphere(vsphere); + if (run instanceof AbstractBuild) { + buildStep.perform(((AbstractBuild) run), launcher, (BuildListener) listener); + } else { + buildStep.perform(run, filePath, launcher, listener); + } - return buildStep.perform(build, launcher, listener); } catch (Exception e) { - VSphereLogger.vsLogger(listener.getLogger(), e); + throw new AbortException(e.getMessage()); } finally { if (vsphere != null) { vsphere.disconnect(); } } - return false; } private void startLogs(PrintStream logger, String serverName){ diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Clone.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Clone.java index 3f251d06..3f45dd6b 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Clone.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Clone.java @@ -14,16 +14,17 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; +import hudson.*; import hudson.model.BuildListener; import hudson.model.AbstractBuild; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; @@ -38,45 +39,46 @@ public class Clone extends VSphereBuildStep { - private final String sourceName; - private final String clone; - private final boolean linkedClone; - private final String resourcePool; - private final String cluster; + private final String sourceName; + private final String clone; + private final boolean linkedClone; + private final String resourcePool; + private final String cluster; private final String datastore; private final boolean powerOn; - - @DataBoundConstructor - public Clone(String sourceName, String clone, boolean linkedClone, - String resourcePool, String cluster, String datastore, boolean powerOn) throws VSphereException { - this.sourceName = sourceName; - this.clone = clone; - this.linkedClone = linkedClone; - this.resourcePool=resourcePool; - this.cluster=cluster; + private String IP; + + @DataBoundConstructor + public Clone(String sourceName, String clone, boolean linkedClone, + String resourcePool, String cluster, String datastore, boolean powerOn) throws VSphereException { + this.sourceName = sourceName; + this.clone = clone; + this.linkedClone = linkedClone; + this.resourcePool=resourcePool; + this.cluster=cluster; this.datastore=datastore; - this.powerOn=powerOn; - } + this.powerOn=powerOn; + } - public String getSourceName() { - return sourceName; - } + public String getSourceName() { + return sourceName; + } - public String getClone() { - return clone; - } + public String getClone() { + return clone; + } - public boolean isLinkedClone() { - return linkedClone; - } + public boolean isLinkedClone() { + return linkedClone; + } - public String getCluster() { - return cluster; - } + public String getCluster() { + return cluster; + } - public String getResourcePool() { - return resourcePool; - } + public String getResourcePool() { + return resourcePool; + } public String getDatastore() { return datastore; @@ -86,118 +88,147 @@ public boolean isPowerOn() { return powerOn; } + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + cloneFromSource(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - return cloneFromSource(build, launcher, listener); - //TODO throw AbortException instead of returning value - } - - private boolean cloneFromSource(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - PrintStream jLogger = listener.getLogger(); - - EnvVars env; - try { - env = build.getEnvironment(listener); - } catch (Exception e) { - throw new VSphereException(e); - } - - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - - String expandedClone = env.expand(clone), expandedSource = env.expand(sourceName), - expandedCluster = env.expand(cluster), expandedDatastore = env.expand(datastore), - expandedResourcePool = env.expand(resourcePool); - - vsphere.cloneVm(expandedClone, expandedSource, linkedClone, expandedResourcePool, expandedCluster, - expandedDatastore, powerOn, jLogger); - VSphereLogger.vsLogger(jLogger, "\""+expandedClone+"\" successfully cloned!"); - - return true; - } - - @Extension - public static final class CloneDescriptor extends VSphereBuildStepDescriptor { - - public CloneDescriptor() { - load(); - } - - @Override - public String getDisplayName() { - return Messages.vm_title_Clone(); - } - - public FormValidation doCheckSource(@QueryParameter String value) - throws IOException, ServletException { - if (value.length() == 0) - return FormValidation.error("Please enter the sourceName name"); - return FormValidation.ok(); - } - - public FormValidation doCheckClone(@QueryParameter String value) - throws IOException, ServletException { - if (value.length() == 0) - return FormValidation.error(Messages.validation_required("the clone name")); - return FormValidation.ok(); - } - - public FormValidation doCheckResourcePool(@QueryParameter String value, - @QueryParameter String serverName, - @QueryParameter String sourceName) - throws IOException, ServletException { - try { - VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance(); - - VirtualMachine virtualMachine = vsphere.getVmByName(sourceName); - if (virtualMachine == null) { - return FormValidation.error("The source VM \""+sourceName+"\"was not found cannot check the configuration."); - } - if ((virtualMachine.getConfig().template) && (value.length() == 0)) { - return FormValidation.error(Messages.validation_required("the resource pool")); - } - } catch (VSphereException ve) { - return FormValidation.error("Cannot connect to vsphere. "+ve.getMessage()); - } - return FormValidation.ok(); - } - - public FormValidation doCheckCluster(@QueryParameter String value) - throws IOException, ServletException { - if (value.length() == 0) - return FormValidation.error(Messages.validation_required("the cluster")); - return FormValidation.ok(); - } - - public FormValidation doTestData(@QueryParameter String serverName, - @QueryParameter String sourceName, @QueryParameter String clone, - @QueryParameter String resourcePool, @QueryParameter String cluster) { - try { - if (sourceName.length() == 0 || clone.length()==0 || serverName.length()==0 - || cluster.length()==0 ) - return FormValidation.error(Messages.validation_requiredValues()); - - VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance(); - - //TODO what if clone name is variable? - VirtualMachine cloneVM = vsphere.getVmByName(clone); - if (cloneVM != null) - return FormValidation.error(Messages.validation_exists("clone")); - - if (sourceName.indexOf('$') >= 0) - return FormValidation.warning(Messages.validation_buildParameter("sourceName")); - - VirtualMachine vm = vsphere.getVmByName(sourceName); - if (vm == null) - return FormValidation.error(Messages.validation_notFound("sourceName")); - - VirtualMachineSnapshot snap = vm.getCurrentSnapShot(); - if (snap == null) - return FormValidation.error(Messages.validation_noSnapshots()); - - return FormValidation.ok(Messages.validation_success()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } -} + @Override + public String getIP() { + return IP; + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = cloneFromSource(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + private boolean cloneFromSource(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { + PrintStream jLogger = listener.getLogger(); + String expandedClone = clone; + String expandedSource = sourceName; + String expandedCluster = cluster; + String expandedDatastore = datastore; + String expandedResourcePool = resourcePool; + EnvVars env; + try { + env = run.getEnvironment(listener); + } catch (Exception e) { + throw new VSphereException(e); + } + + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedClone = env.expand(clone); + expandedSource = env.expand(sourceName); + expandedCluster = env.expand(cluster); + expandedDatastore = env.expand(datastore); + expandedResourcePool = env.expand(resourcePool); + } + vsphere.cloneVm(expandedClone, expandedSource, linkedClone, expandedResourcePool, expandedCluster, + expandedDatastore, powerOn, jLogger); + if (powerOn) { + IP = vsphere.getIp(vsphere.getVmByName(expandedClone), 60); + } + VSphereLogger.vsLogger(jLogger, "\""+expandedClone+"\" successfully cloned!"); + + return true; + } + + @Extension + public static final class CloneDescriptor extends VSphereBuildStepDescriptor { + + public CloneDescriptor() { + load(); + } + + @Override + public String getDisplayName() { + return Messages.vm_title_Clone(); + } + + public FormValidation doCheckSource(@QueryParameter String value) + throws IOException, ServletException { + if (value.length() == 0) + return FormValidation.error("Please enter the sourceName name"); + return FormValidation.ok(); + } + + public FormValidation doCheckClone(@QueryParameter String value) + throws IOException, ServletException { + if (value.length() == 0) + return FormValidation.error(Messages.validation_required("the clone name")); + return FormValidation.ok(); + } + + public FormValidation doCheckResourcePool(@QueryParameter String value, + @QueryParameter String serverName, + @QueryParameter String sourceName) + throws IOException, ServletException { + try { + VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance(); + + VirtualMachine virtualMachine = vsphere.getVmByName(sourceName); + if (virtualMachine == null) { + return FormValidation.error("The source VM \""+sourceName+"\"was not found cannot check the configuration."); + } + if ((virtualMachine.getConfig().template) && (value.length() == 0)) { + return FormValidation.error(Messages.validation_required("the resource pool")); + } + } catch (VSphereException ve) { + return FormValidation.error("Cannot connect to vsphere. "+ve.getMessage()); + } + return FormValidation.ok(); + } + + public FormValidation doCheckCluster(@QueryParameter String value) + throws IOException, ServletException { + if (value.length() == 0) + return FormValidation.error(Messages.validation_required("the cluster")); + return FormValidation.ok(); + } + + public FormValidation doTestData(@QueryParameter String serverName, + @QueryParameter String sourceName, @QueryParameter String clone, + @QueryParameter String resourcePool, @QueryParameter String cluster) { + try { + if (sourceName.length() == 0 || clone.length()==0 || serverName.length()==0 + || cluster.length()==0 ) + return FormValidation.error(Messages.validation_requiredValues()); + + VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance(); + + //TODO what if clone name is variable? + VirtualMachine cloneVM = vsphere.getVmByName(clone); + if (cloneVM != null) + return FormValidation.error(Messages.validation_exists("clone")); + + if (sourceName.indexOf('$') >= 0) + return FormValidation.warning(Messages.validation_buildParameter("sourceName")); + + VirtualMachine vm = vsphere.getVmByName(sourceName); + if (vm == null) + return FormValidation.error(Messages.validation_notFound("sourceName")); + + VirtualMachineSnapshot snap = vm.getCurrentSnapShot(); + if (snap == null) + return FormValidation.error(Messages.validation_noSnapshots()); + + return FormValidation.ok(Messages.validation_success()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ConvertToTemplate.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ConvertToTemplate.java index e2251f09..1a9c0459 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ConvertToTemplate.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ConvertToTemplate.java @@ -14,11 +14,11 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; +import hudson.*; import hudson.model.BuildListener; import hudson.model.AbstractBuild; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.util.FormValidation; import java.io.IOException; @@ -26,6 +26,7 @@ import java.text.SimpleDateFormat; import java.util.Date; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; @@ -37,90 +38,109 @@ public class ConvertToTemplate extends VSphereBuildStep { - private final String vm; - private final boolean force; - - @DataBoundConstructor - public ConvertToTemplate(String vm, boolean force) throws VSphereException { - this.force = force; - this.vm = vm; - } - - public String getVm() { - return vm; - } - - public boolean isForce() { - return force; - } - - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - return convert(build, launcher, listener); - } - - private boolean convert(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - PrintStream jLogger = listener.getLogger(); - VSphereLogger.vsLogger(jLogger, "Converting VM to template. Please wait ..."); - - EnvVars env; - try { - env = build.getEnvironment(listener); - } catch (Exception e) { - throw new VSphereException(e); - } - - Date date = new Date(); - SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa"); - - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedVm = env.expand(vm); - - vsphere.markAsTemplate(expandedVm, df.format(date), force); - VSphereLogger.vsLogger(jLogger, "\""+expandedVm+"\" is now a template."); - - return true; - } - - @Extension - public static final class ConvertToTemplateDescriptor extends VSphereBuildStepDescriptor { - - public ConvertToTemplateDescriptor() { - load(); - } - - /** - * This human readable name is used in the configuration screen. - */ - @Override - public String getDisplayName() { - return Messages.vm_title_ConvertToTemplate(); - } - - public FormValidation doCheckVm(@QueryParameter String value) - throws IOException, ServletException { - if (value.length() == 0) - return FormValidation.error(Messages.validation_required("the VM name")); - return FormValidation.ok(); - } - - public FormValidation doTestData(@QueryParameter String serverName, - @QueryParameter String vm) { - try { - - if (serverName.length() == 0 || vm.length() == 0) - return FormValidation.error(Messages.validation_requiredValues()); - - if (vm.indexOf('$') >= 0) - return FormValidation.warning(Messages.validation_buildParameter("VM")); - - VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance(); - if (vsphere.getVmByName(vm) == null) - return FormValidation.error(Messages.validation_notFound("VM")); - - return FormValidation.ok(Messages.validation_success()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } + private final String vm; + private final boolean force; + + @DataBoundConstructor + public ConvertToTemplate(String vm, boolean force) throws VSphereException { + this.force = force; + this.vm = vm; + } + + public String getVm() { + return vm; + } + + public boolean isForce() { + return force; + } + + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + convert(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = convert(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + private boolean convert(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { + PrintStream jLogger = listener.getLogger(); + VSphereLogger.vsLogger(jLogger, "Converting VM to template. Please wait ..."); + String expandedVm = vm; + EnvVars env; + try { + env = run.getEnvironment(listener); + } catch (Exception e) { + throw new VSphereException(e); + } + + Date date = new Date(); + SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa"); + + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedVm = env.expand(vm); + } + + vsphere.markAsTemplate(expandedVm, df.format(date), force); + VSphereLogger.vsLogger(jLogger, "\""+expandedVm+"\" is now a template."); + + return true; + } + + @Extension + public static final class ConvertToTemplateDescriptor extends VSphereBuildStepDescriptor { + + public ConvertToTemplateDescriptor() { + load(); + } + + /** + * This human readable name is used in the configuration screen. + */ + @Override + public String getDisplayName() { + return Messages.vm_title_ConvertToTemplate(); + } + + public FormValidation doCheckVm(@QueryParameter String value) + throws IOException, ServletException { + if (value.length() == 0) + return FormValidation.error(Messages.validation_required("the VM name")); + return FormValidation.ok(); + } + + public FormValidation doTestData(@QueryParameter String serverName, + @QueryParameter String vm) { + try { + + if (serverName.length() == 0 || vm.length() == 0) + return FormValidation.error(Messages.validation_requiredValues()); + + if (vm.indexOf('$') >= 0) + return FormValidation.warning(Messages.validation_buildParameter("VM")); + + VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance(); + if (vsphere.getVmByName(vm) == null) + return FormValidation.error(Messages.validation_notFound("VM")); + + return FormValidation.ok(Messages.validation_success()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } } \ No newline at end of file diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ConvertToVm.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ConvertToVm.java index 6df2d3f6..aaf8bfb6 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ConvertToVm.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ConvertToVm.java @@ -14,16 +14,17 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; +import hudson.*; import hudson.model.BuildListener; import hudson.model.AbstractBuild; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; @@ -37,112 +38,133 @@ public class ConvertToVm extends VSphereBuildStep { - private final String template; - private final String resourcePool; - private final String cluster; - - @DataBoundConstructor - public ConvertToVm(String template, String resourcePool, String cluster) throws VSphereException { - this.template = template; - this.resourcePool = resourcePool; - this.cluster = cluster; - } - - public String getTemplate() { - return template; - } - - public String getCluster() { - return cluster; - } - - public String getResourcePool() { - return resourcePool; - } - - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - return convert(build, launcher, listener); - } - - private boolean convert(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - PrintStream jLogger = listener.getLogger(); - VSphereLogger.vsLogger(jLogger, "Converting template to VM. Please wait ..."); - - EnvVars env; - try { - env = build.getEnvironment(listener); - } catch (Exception e) { - throw new VSphereException(e); - } - - //TODO: take in a comma delimited list and convert all - env.overrideAll(build.getBuildVariables()); // Add in matrix axis.. - String expandedTemplate = env.expand(template), expandedCluster = env.expand(cluster), - expandedResourcePool = env.expand(resourcePool); - - vsphere.markAsVm(expandedTemplate, expandedResourcePool, expandedCluster); - VSphereLogger.vsLogger(jLogger, "\""+expandedTemplate+"\" is a VM!"); - - return true; - } - - @Extension - public static final class ConvertToVmDescriptor extends VSphereBuildStepDescriptor { - - public ConvertToVmDescriptor() { - load(); - } - - @Override - public String getDisplayName() { - return Messages.vm_title_ConvertToVM(); - } - - public FormValidation doCheckTemplate(@QueryParameter String value) - throws IOException, ServletException { - if (value.length() == 0) - return FormValidation.error(Messages.validation_required("the Template name")); - return FormValidation.ok(); - } - - public FormValidation doCheckResourcePool(@QueryParameter String value) - throws IOException, ServletException { - if (value.length() == 0) - return FormValidation.error(Messages.validation_required("the resource pool")); - return FormValidation.ok(); - } - - public FormValidation doCheckCluster(@QueryParameter String value) - throws IOException, ServletException { - if (value.length() == 0) - return FormValidation.error(Messages.validation_required("the cluster")); - return FormValidation.ok(); - } - - public FormValidation doTestData(@QueryParameter String serverName, - @QueryParameter String template, @QueryParameter String resourcePool, - @QueryParameter String cluster) { - try { - - if (serverName.length() == 0 || template.length() == 0 - || resourcePool.length() == 0 || cluster.length() == 0) - return FormValidation.error(Messages.validation_requiredValues()); - - if (template.indexOf('$') >= 0) - return FormValidation.warning(Messages.validation_buildParameter("Template")); - - VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance(); - VirtualMachine vm = vsphere.getVmByName(template); - if (vm == null) - return FormValidation.error(Messages.validation_notFound("template")); - - if(!vm.getConfig().template) - return FormValidation.error(Messages.validation_alreadySet("template", "VM")); - - return FormValidation.ok(Messages.validation_success()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } -} + private final String template; + private final String resourcePool; + private final String cluster; + + @DataBoundConstructor + public ConvertToVm(String template, String resourcePool, String cluster) throws VSphereException { + this.template = template; + this.resourcePool = resourcePool; + this.cluster = cluster; + } + + public String getTemplate() { + return template; + } + + public String getCluster() { + return cluster; + } + + public String getResourcePool() { + return resourcePool; + } + + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + convert(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = convert(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + private boolean convert(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { + PrintStream jLogger = listener.getLogger(); + VSphereLogger.vsLogger(jLogger, "Converting template to VM. Please wait ..."); + String expandedTemplate = template; + String expandedCluster = cluster; + String expandedResourcePool = resourcePool; + EnvVars env; + try { + env = run.getEnvironment(listener); + } catch (Exception e) { + throw new VSphereException(e); + } + + //TODO: take in a comma delimited list and convert all + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedTemplate = env.expand(template); + expandedCluster = env.expand(cluster); + expandedResourcePool = env.expand(resourcePool); + } + + vsphere.markAsVm(expandedTemplate, expandedResourcePool, expandedCluster); + VSphereLogger.vsLogger(jLogger, "\""+expandedTemplate+"\" is a VM!"); + + return true; + } + + @Extension + public static final class ConvertToVmDescriptor extends VSphereBuildStepDescriptor { + + public ConvertToVmDescriptor() { + load(); + } + + @Override + public String getDisplayName() { + return Messages.vm_title_ConvertToVM(); + } + + public FormValidation doCheckTemplate(@QueryParameter String value) + throws IOException, ServletException { + if (value.length() == 0) + return FormValidation.error(Messages.validation_required("the Template name")); + return FormValidation.ok(); + } + + public FormValidation doCheckResourcePool(@QueryParameter String value) + throws IOException, ServletException { + if (value.length() == 0) + return FormValidation.error(Messages.validation_required("the resource pool")); + return FormValidation.ok(); + } + + public FormValidation doCheckCluster(@QueryParameter String value) + throws IOException, ServletException { + if (value.length() == 0) + return FormValidation.error(Messages.validation_required("the cluster")); + return FormValidation.ok(); + } + + public FormValidation doTestData(@QueryParameter String serverName, + @QueryParameter String template, @QueryParameter String resourcePool, + @QueryParameter String cluster) { + try { + + if (serverName.length() == 0 || template.length() == 0 + || resourcePool.length() == 0 || cluster.length() == 0) + return FormValidation.error(Messages.validation_requiredValues()); + + if (template.indexOf('$') >= 0) + return FormValidation.warning(Messages.validation_buildParameter("Template")); + + VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance(); + VirtualMachine vm = vsphere.getVmByName(template); + if (vm == null) + return FormValidation.error(Messages.validation_notFound("template")); + + if(!vm.getConfig().template) + return FormValidation.error(Messages.validation_alreadySet("template", "VM")); + + return FormValidation.ok(Messages.validation_success()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Delete.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Delete.java index 31a5dccb..422d3e9e 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Delete.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Delete.java @@ -14,18 +14,19 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.AbstractBuild; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; +import javax.annotation.Nonnull; import javax.servlet.ServletException; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -54,27 +55,49 @@ public boolean isFailOnNoExist(){ return failOnNoExist; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - - if(allowDelete()) - return killVm(build, launcher, listener); - else - VSphereLogger.vsLogger(listener.getLogger(), "Deletion is disabled!"); + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + if(allowDelete()) { + killVm(run, launcher, listener); + } else { + VSphereLogger.vsLogger(listener.getLogger(), "Deletion is disabled!"); + } + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } - return false; + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + if(allowDelete()) { + retVal = killVm(build, launcher, listener); + } else { + VSphereLogger.vsLogger(listener.getLogger(), "Deletion is disabled!"); + } + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value } - private boolean killVm(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { + private boolean killVm(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { PrintStream jLogger = listener.getLogger(); + String expandedVm = vm; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedVm = env.expand(vm); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild)run).getBuildVariables()); // Add in matrix axes.. + expandedVm = env.expand(vm); + } VSphereLogger.vsLogger(jLogger, "Destroying VM \""+expandedVm+".\" Please wait ..."); vsphere.destroyVm(expandedVm, failOnNoExist); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/DeleteSnapshot.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/DeleteSnapshot.java index ca21394b..b827aee2 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/DeleteSnapshot.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/DeleteSnapshot.java @@ -14,18 +14,19 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.AbstractBuild; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; +import javax.annotation.Nonnull; import javax.servlet.ServletException; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -35,7 +36,7 @@ import com.vmware.vim25.mo.VirtualMachineSnapshot; -public class DeleteSnapshot extends VSphereBuildStep { +public class DeleteSnapshot extends VSphereBuildStep implements SimpleBuildStep { private final String vm; private final String snapshotName; @@ -66,23 +67,63 @@ public boolean isFailOnNoExist() { return failOnNoExist; } - public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException { - return deleteSnapshot(build, launcher, listener); + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + deleteSnapshot(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = deleteSnapshot(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; //TODO throw AbortException instead of returning value } - private boolean deleteSnapshot(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException{ + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + private boolean deleteSnapshot(final Run run, Launcher launcher, final TaskListener listener) throws VSphereException{ PrintStream jLogger = listener.getLogger(); + String expandedSnap = snapshotName; + String expandedVm = vm; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedSnap = env.expand(snapshotName); - String expandedVm = env.expand(vm); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedSnap = env.expand(snapshotName); + expandedVm = env.expand(vm); + } VSphereLogger.vsLogger(jLogger, "Deleting snapshot \""+expandedSnap+"\" of VM "+expandedVm+"..."); vsphere.deleteSnapshot(expandedVm, expandedSnap, consolidate, failOnNoExist); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Deploy.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Deploy.java index b7a81beb..811143e2 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Deploy.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Deploy.java @@ -14,18 +14,23 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.AbstractBuild; +import com.vmware.vim25.StringExpression; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import javax.annotation.Nonnull; import javax.servlet.ServletException; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -36,7 +41,7 @@ import com.vmware.vim25.mo.VirtualMachine; import com.vmware.vim25.mo.VirtualMachineSnapshot; -public class Deploy extends VSphereBuildStep { +public class Deploy extends VSphereBuildStep implements SimpleBuildStep { private final String template; private final String clone; @@ -45,6 +50,7 @@ public class Deploy extends VSphereBuildStep { private final String cluster; private final String datastore; private final boolean powerOn; + private String IP; @DataBoundConstructor public Deploy(String template, String clone, boolean linkedClone, @@ -52,10 +58,10 @@ public Deploy(String template, String clone, boolean linkedClone, this.template = template; this.clone = clone; this.linkedClone = linkedClone; - this.resourcePool=resourcePool; + this.resourcePool= (resourcePool != null) ? resourcePool : ""; this.cluster=cluster; this.datastore=datastore; - this.powerOn=powerOn; + this.powerOn=powerOn; } public String getTemplate() { @@ -86,27 +92,75 @@ public boolean isPowerOn() { return powerOn; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - return deployFromTemplate(build, launcher, listener); + @Override + public String getIP() { + return IP; + } + + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + deployFromTemplate(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = deployFromTemplate(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; //TODO throw AbortException instead of returning value } - private boolean deployFromTemplate(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - PrintStream jLogger = listener.getLogger(); + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + private boolean deployFromTemplate(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { + PrintStream jLogger = listener.getLogger(); + String expandedClone = clone; + String expandedTemplate = template; + String expandedCluster = cluster; + String expandedDatastore = datastore; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedClone = env.expand(clone), expandedTemplate = env.expand(template), - expandedCluster = env.expand(cluster), expandedDatastore = env.expand(datastore); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedClone = env.expand(clone); + expandedTemplate = env.expand(template); + expandedCluster = env.expand(cluster); + expandedDatastore = env.expand(datastore); + } String resourcePoolName; - if (resourcePool.length() == 0) { + if ("".equals(resourcePool) || (resourcePool.length() == 0)) { // Not all installations are using resource pools. But there is always a hidden "Resources" resource // pool, even if not visible in the vSphere Client. resourcePoolName = "Resources"; @@ -116,8 +170,23 @@ private boolean deployFromTemplate(final AbstractBuild build, final Launch vsphere.deployVm(expandedClone, expandedTemplate, linkedClone, resourcePoolName, expandedCluster, expandedDatastore, powerOn, jLogger); VSphereLogger.vsLogger(jLogger, "\""+expandedClone+"\" successfully deployed!"); + IP = vsphere.getIp(vsphere.getVmByName(expandedClone), 60); + + if(IP!=null) { + VSphereLogger.vsLogger(jLogger, "Successfully retrieved IP for \"" + expandedClone + "\" : " + IP); + VSphereLogger.vsLogger(jLogger, "Exposing " + IP + " as environment variable VSPHERE_IP"); + + if (run instanceof AbstractBuild) { + VSphereEnvAction envAction = new VSphereEnvAction(); + envAction.add("VSPHERE_IP", IP); + run.addAction(envAction); + } - return true; + return true; + } else { + VSphereLogger.vsLogger(jLogger, "Error: Timed out after waiting 60 seconds to get IP for \""+expandedClone+"\" "); + return false; + } } @Extension @@ -193,4 +262,27 @@ public FormValidation doTestData(@QueryParameter String serverName, } } } + /** + * This class is used to inject the IP value into the build environment + * as a variable so that it can be used with other plugins. (Copied from PowerOn builder) + * + * @author Lordahl + */ + private static class VSphereEnvAction implements EnvironmentContributingAction { + // Decided not to record this data in build.xml, so marked transient: + private transient Map data = new HashMap(); + + private void add(String key, String val) { + if (data==null) return; + data.put(key, val); + } + + public void buildEnvVars(AbstractBuild build, EnvVars env) { + if (data!=null) env.putAll(data); + } + + public String getIconFileName() { return null; } + public String getDisplayName() { return null; } + public String getUrlName() { return null; } + } } diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ExposeGuestInfo.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ExposeGuestInfo.java index caf52888..d5a64a32 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ExposeGuestInfo.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ExposeGuestInfo.java @@ -3,13 +3,11 @@ import com.vmware.vim25.GuestInfo; import com.vmware.vim25.mo.VirtualMachine; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.AbstractBuild; -import hudson.model.BuildListener; -import hudson.model.EnvironmentContributingAction; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -17,26 +15,26 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Expose guest info for the named VM as environmental variables. * Information on variables can be found here. * https://www.vmware.com/support/developer/converter-sdk/conv55_apireference/vim.vm.GuestInfo.html */ -public class ExposeGuestInfo extends VSphereBuildStep { +public class ExposeGuestInfo extends VSphereBuildStep implements SimpleBuildStep { private final String vm; private final String envVariablePrefix; + private String IP; + private Map envVars = new HashMap<>(); @DataBoundConstructor public ExposeGuestInfo(final String vm, final String envVariablePrefix) throws VSphereException { @@ -53,16 +51,68 @@ public String getEnvVariablePrefix() { } @Override - public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws Exception { + public String getIP() { + return IP; + } + + public Map getVars() { + return envVars; + } + + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + exposeInfo(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = exposeInfo(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + public boolean exposeInfo(Run run, Launcher launcher, TaskListener listener) throws Exception { PrintStream jLogger = listener.getLogger(); + String vmName = vm; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String vmName = env.expand(vm); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + vmName = env.expand(vm); + } VSphereLogger.vsLogger(jLogger, "Exposing guest info for VM \"" + vmName + "\" as environment variables"); @@ -71,7 +121,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen throw new RuntimeException(Messages.validation_notFound("vm " + vmName)); } VSphereEnvAction envAction = createGuestInfoEnvAction(vsphereVm, jLogger); - build.addAction(envAction); + run.addAction(envAction); VSphereLogger.vsLogger(jLogger, "Successfully exposed guest info for VM \"" + vmName + "\""); return true; @@ -107,6 +157,10 @@ private VSphereEnvAction createGuestInfoEnvAction(VirtualMachine vsphereVm, Prin continue; } + if ("IpAddress".equals(variableName)) { + IP = String.valueOf(value); + } + envVars.put(envVariablePrefix + "_" + variableName, String.valueOf(value)); String environmentVariableName = envVariablePrefix + "_" + variableName; String environmentVariableValue = String.valueOf(value); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOff.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOff.java index 6986b6cc..75cc6e47 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOff.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOff.java @@ -14,18 +14,20 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.AbstractBuild; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStep; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; +import javax.annotation.Nonnull; import javax.servlet.ServletException; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -35,7 +37,7 @@ import com.vmware.vim25.mo.VirtualMachine; -public class PowerOff extends VSphereBuildStep { +public class PowerOff extends VSphereBuildStep implements SimpleBuildStep { private final String vm; private final boolean evenIfSuspended; @@ -58,22 +60,61 @@ public String getVm() { return vm; } - public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException { - return powerOff(build, launcher, listener); - //TODO throw AbortException instead of returning value + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; } - private boolean powerOff(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException{ - PrintStream jLogger = listener.getLogger(); - EnvVars env; + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { try { - env = build.getEnvironment(listener); + powerOff(run, launcher, listener); } catch (Exception e) { - throw new VSphereException(e); + throw new AbortException(e.getMessage()); } + } + + @Override + public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = powerOff(build, launcher, listener); + } catch (VSphereException e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedVm = env.expand(vm); + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + private boolean powerOff(final Run run, Launcher launcher, final TaskListener listener) throws VSphereException{ + PrintStream jLogger = listener.getLogger(); + EnvVars env; + String expandedVm = vm; + if (run instanceof AbstractBuild) { + try { + env = run.getEnvironment(listener); + } catch (Exception e) { + throw new VSphereException(e); + } + + env.overrideAll(((AbstractBuild)run).getBuildVariables()); // Add in matrix axes.. + expandedVm = env.expand(vm); + } VSphereLogger.vsLogger(jLogger, "Shutting Down VM " + expandedVm + "..."); VirtualMachine vsphereVm = vsphere.getVmByName(expandedVm); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOn.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOn.java index d494112c..cbc7b748 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOn.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOn.java @@ -15,22 +15,10 @@ package org.jenkinsci.plugins.vsphere.builders; import com.google.common.base.Stopwatch; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.EnvironmentContributingAction; -import hudson.model.AbstractBuild; +import com.vmware.vim25.mo.VirtualMachine; +import hudson.*; +import hudson.model.*; import hudson.util.FormValidation; - -import java.io.IOException; -import java.io.PrintStream; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import javax.servlet.ServletException; - import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -38,12 +26,19 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; -import com.vmware.vim25.mo.VirtualMachine; +import javax.annotation.Nonnull; +import javax.servlet.ServletException; +import java.io.IOException; +import java.io.PrintStream; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; public class PowerOn extends VSphereBuildStep { private final String vm; private final int timeoutInSeconds; + private String IP; @DataBoundConstructor public PowerOn(final String vm, final int timeoutInSeconds) throws VSphereException { @@ -59,49 +54,71 @@ public int getTimeoutInSeconds() { return timeoutInSeconds; } - public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException { - return powerOn(build, launcher, listener); - //TODO throw AbortException instead of returning value + @Override + public String getIP() { + return IP; } - private boolean powerOn(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException{ - PrintStream jLogger = listener.getLogger(); - EnvVars env; + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + powerOn(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) { + boolean retVal = false; try { - env = build.getEnvironment(listener); + retVal = powerOn(build, launcher, listener); } catch (Exception e) { - throw new VSphereException(e); + e.printStackTrace(); } + return retVal; + //TODO throw AbortException instead of returning value + } + + private boolean powerOn(final Run run, Launcher launcher, final TaskListener listener) throws VSphereException, IOException, InterruptedException { + PrintStream jLogger = listener.getLogger(); + EnvVars env; + String expandedVm = vm; - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedVm = env.expand(vm); + env = run.getEnvironment(listener); - VSphereLogger.vsLogger(jLogger, "Waiting for VM " + expandedVm + " to start (VM may be restarted during this time)"); - VSphereLogger.vsLogger(jLogger, "Timeout set to " + timeoutInSeconds + " seconds"); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild)run).getBuildVariables()); // Add in matrix axes.. + expandedVm = env.expand(vm); + } Stopwatch stopwatch = new Stopwatch().start(); vsphere.startVm(expandedVm, timeoutInSeconds); long elapsedTime = stopwatch.elapsedTime(TimeUnit.SECONDS); - VSphereLogger.vsLogger(jLogger, "VM started in " + elapsedTime + " seconds"); int secondsToWaitForIp = (int) (timeoutInSeconds - elapsedTime); - String vmIP = vsphere.getIp(vsphere.getVmByName(expandedVm), secondsToWaitForIp); + IP = vsphere.getIp(vsphere.getVmByName(expandedVm), secondsToWaitForIp); - if(vmIP==null){ + if(IP==null){ VSphereLogger.vsLogger(jLogger, "Error: Timed out after waiting " + secondsToWaitForIp + " seconds to get IP for \""+expandedVm+"\" "); return false; } - VSphereLogger.vsLogger(jLogger, "Successfully retrieved IP for \""+expandedVm+"\" : "+vmIP); - VSphereLogger.vsLogger(jLogger, "Complete startup took " + stopwatch.elapsedTime(TimeUnit.SECONDS) + " seconds"); + VSphereLogger.vsLogger(jLogger, "Successfully retrieved IP for \""+expandedVm+"\" : "+IP); stopwatch.stop(); // useful to tell user about the environment variable - VSphereLogger.vsLogger(jLogger, "Exposing " + vmIP + " as environment variable VSPHERE_IP"); - VSphereEnvAction envAction = new VSphereEnvAction(); - envAction.add("VSPHERE_IP", vmIP); - build.addAction(envAction); + VSphereLogger.vsLogger(jLogger, "Exposing " + IP + " as environment variable VSPHERE_IP"); + + if (run instanceof AbstractBuild) { + VSphereEnvAction envAction = new VSphereEnvAction(); + envAction.add("VSPHERE_IP", IP); + run.addAction(envAction); + } else { + env.put("VSPHERE_IP", IP); + } + return true; } diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Reconfigure.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Reconfigure.java index 16cc2486..8d56730f 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Reconfigure.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Reconfigure.java @@ -16,13 +16,11 @@ import com.vmware.vim25.VirtualMachineConfigSpec; import com.vmware.vim25.mo.VirtualMachine; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.AbstractBuild; -import hudson.model.BuildListener; -import hudson.model.Hudson; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -30,12 +28,14 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; import java.util.List; -public class Reconfigure extends VSphereBuildStep { +public class Reconfigure extends VSphereBuildStep implements SimpleBuildStep{ private final List reconfigureSteps; private final String vm; @@ -54,21 +54,62 @@ public List getReconfigureSteps() { return reconfigureSteps; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { - return reconfigureVm(build, launcher, listener); + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + reconfigureVm(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = reconfigureVm(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value } - private boolean reconfigureVm(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + private boolean reconfigureVm(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException, IOException, InterruptedException { PrintStream jLogger = listener.getLogger(); + String expandedVm = vm; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); - String expandedVm = env.expand(vm); + + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedVm = env.expand(vm); + } VirtualMachine realVM = vsphere.getVmByName(expandedVm); @@ -78,7 +119,7 @@ private boolean reconfigureVm(final AbstractBuild build, final Launcher la actionStep.setVsphere(getVsphere()); actionStep.setVM(realVM); actionStep.setVirtualMachineConfigSpec(spec); - actionStep.perform(build, launcher, listener); + actionStep.perform(run, null, launcher, listener); } vsphere.reconfigureVm(expandedVm, spec); VSphereLogger.vsLogger(jLogger, "Finished!"); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureCpu.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureCpu.java index 6d838055..1eb68c9c 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureCpu.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureCpu.java @@ -15,17 +15,19 @@ package org.jenkinsci.plugins.vsphere.builders; import com.vmware.vim25.*; -import hudson.EnvVars; +import hudson.*; import hudson.Extension; -import hudson.Launcher; import hudson.model.AbstractBuild; import hudson.model.BuildListener; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.util.FormValidation; import org.jenkinsci.plugins.vsphere.tools.VSphereException; import org.jenkinsci.plugins.vsphere.tools.VSphereLogger; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintStream; @@ -50,18 +52,44 @@ public String getCoresPerSocket() { return coresPerSocket; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + reconfigureCPU(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = reconfigureCPU(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + public boolean reconfigureCPU (final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { PrintStream jLogger = listener.getLogger(); + String expandedCPUCores = cpuCores; + String expandedCoresPerSocket = coresPerSocket; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); - String expandedCPUCores = env.expand(cpuCores); - String expandedCoresPerSocket = env.expand(coresPerSocket); + + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedCPUCores = env.expand(cpuCores); + expandedCoresPerSocket = env.expand(coresPerSocket); + } VSphereLogger.vsLogger(jLogger, "Preparing reconfigure: CPU"); spec.setNumCPUs(Integer.valueOf(expandedCPUCores)); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureDisk.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureDisk.java index c5db3d87..d91de6fb 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureDisk.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureDisk.java @@ -20,11 +20,12 @@ import com.vmware.vim25.mo.Task; import com.vmware.vim25.mo.VirtualMachine; -import hudson.EnvVars; +import hudson.*; import hudson.Extension; -import hudson.Launcher; import hudson.model.AbstractBuild; import hudson.model.BuildListener; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.util.FormValidation; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -32,6 +33,7 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.IOException; @@ -62,16 +64,39 @@ public String getDataStore() { return datastore; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + reconfigureDisk(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = reconfigureDisk(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + 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 = build.getEnvironment(listener); - env.overrideAll(build.getBuildVariables()); - int diskSize = Integer.parseInt(env.expand(this.diskSize)); - + env = run.getEnvironment(listener); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + diskSize = Integer.parseInt(env.expand(this.diskSize)); + } VirtualDeviceConfigSpec vdiskSpec = createAddDiskConfigSpec(vm, diskSize, jLogger); VirtualDeviceConfigSpec [] vdiskSpecArray = {vdiskSpec}; diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureMemory.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureMemory.java index af52ab65..78ab0057 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureMemory.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureMemory.java @@ -18,17 +18,18 @@ import com.vmware.vim25.SharesInfo; import com.vmware.vim25.VirtualDevice; import com.vmware.vim25.VirtualEthernetCard; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; +import hudson.*; import hudson.model.AbstractBuild; import hudson.model.BuildListener; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.util.FormValidation; import org.jenkinsci.plugins.vsphere.tools.VSphereException; import org.jenkinsci.plugins.vsphere.tools.VSphereLogger; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintStream; @@ -46,17 +47,41 @@ public String getMemorySize() { return memorySize; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + reconfigureMemory(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = reconfigureMemory(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + public boolean reconfigureMemory(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { PrintStream jLogger = listener.getLogger(); + String expandedMemorySize = memorySize; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); - String expandedMemorySize = env.expand(memorySize); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedMemorySize = env.expand(memorySize); + } VSphereLogger.vsLogger(jLogger, "Preparing reconfigure: Memory"); spec.setMemoryMB(Long.valueOf(expandedMemorySize)); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureNetworkAdapters.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureNetworkAdapters.java index ef39fc86..3013cd8a 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureNetworkAdapters.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureNetworkAdapters.java @@ -18,17 +18,19 @@ import com.vmware.vim25.mo.DistributedVirtualPortgroup; import com.vmware.vim25.mo.DistributedVirtualSwitch; import com.vmware.vim25.mo.Network; -import hudson.EnvVars; +import hudson.*; import hudson.Extension; -import hudson.Launcher; import hudson.model.AbstractBuild; import hudson.model.BuildListener; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.util.FormValidation; import org.jenkinsci.plugins.vsphere.tools.VSphereException; import org.jenkinsci.plugins.vsphere.tools.VSphereLogger; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintStream; @@ -92,22 +94,49 @@ public String getDistributedPortId() { return distributedPortId; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + reconfigureNetwork(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = reconfigureNetwork(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + public boolean reconfigureNetwork(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { PrintStream jLogger = listener.getLogger(); + String expandedDeviceLabel = deviceLabel; + String expandedMacAddress = macAddress; + String expandedPortGroup = portGroup; + String expandedDistributedPortGroup = distributedPortGroup; + String expandedDistributedPortId = distributedPortId; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); - String expandedDeviceLabel = env.expand(deviceLabel); - String expandedMacAddress = env.expand(macAddress); - String expandedPortGroup = env.expand(portGroup); - String expandedDistributedPortGroup = env.expand(distributedPortGroup); - String expandedDistributedPortId = env.expand(distributedPortId); - + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedDeviceLabel = env.expand(deviceLabel); + expandedMacAddress = env.expand(macAddress); + expandedPortGroup = env.expand(portGroup); + expandedDistributedPortGroup = env.expand(distributedPortGroup); + expandedDistributedPortId = env.expand(distributedPortId); + } VSphereLogger.vsLogger(jLogger, "Preparing reconfigure: "+ deviceAction.getLabel() +" Network Adapter \"" + expandedDeviceLabel + "\""); VirtualEthernetCard vEth = null; if (deviceAction == DeviceAction.ADD) { diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureStep.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureStep.java index 9c9b9c8f..66638cbe 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureStep.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/ReconfigureStep.java @@ -17,15 +17,14 @@ import com.vmware.vim25.VirtualDevice; import com.vmware.vim25.VirtualMachineConfigSpec; import com.vmware.vim25.mo.VirtualMachine; -import hudson.DescriptorExtensionList; -import hudson.ExtensionList; -import hudson.ExtensionPoint; -import hudson.Launcher; +import hudson.*; import hudson.model.*; import jenkins.model.Jenkins; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; +import javax.annotation.Nonnull; +import java.io.IOException; import java.util.List; /** @@ -68,6 +67,8 @@ public static List all() { public abstract boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException; + public abstract void perform(@Nonnull Run run, FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException; + protected VirtualDevice findDeviceByLabel(VirtualDevice[] devices, String label) { for(VirtualDevice d : devices) { if(d.getDeviceInfo().getLabel().contentEquals(label)) { diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Rename.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Rename.java index b8d12c19..76a31486 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/Rename.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/Rename.java @@ -15,12 +15,11 @@ package org.jenkinsci.plugins.vsphere.builders; import com.vmware.vim25.mo.VirtualMachine; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.AbstractBuild; -import hudson.model.BuildListener; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -28,11 +27,13 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; -public class Rename extends VSphereBuildStep { +public class Rename extends VSphereBuildStep implements SimpleBuildStep { private final String oldName; private final String newName; @@ -51,18 +52,63 @@ public String getNewName() { return newName; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + rename(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + @Override + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = rename(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + public boolean rename(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { PrintStream jLogger = listener.getLogger(); + String expandedOldName = oldName; + String expandedNewName = newName; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedOldName = env.expand(oldName); - String expandedNewName = env.expand(newName); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild)run).getBuildVariables()); // Add in matrix axes.. + expandedOldName = env.expand(oldName); + expandedNewName = env.expand(newName); + } VSphereLogger.vsLogger(jLogger, "Renaming VM \""+expandedOldName+".\" to \"" + expandedNewName + "\" Please wait ..."); vsphere.renameVm(expandedOldName, expandedNewName); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/RenameSnapshot.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/RenameSnapshot.java index 30962ecd..9898c533 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/RenameSnapshot.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/RenameSnapshot.java @@ -15,12 +15,11 @@ package org.jenkinsci.plugins.vsphere.builders; import com.vmware.vim25.mo.VirtualMachine; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.AbstractBuild; -import hudson.model.BuildListener; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -28,11 +27,13 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; +import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; -public class RenameSnapshot extends VSphereBuildStep { +public class RenameSnapshot extends VSphereBuildStep implements SimpleBuildStep { private final String vm; private final String oldName; @@ -63,23 +64,73 @@ public String getNewDescription() { return newDescription; } - public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws VSphereException { + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + renameSnapshot(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = renameSnapshot(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; + //TODO throw AbortException instead of returning value + } + + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + public boolean renameSnapshot(final Run run, final Launcher launcher, final TaskListener listener) throws VSphereException { PrintStream jLogger = listener.getLogger(); + String expandedVm = vm; + String expandedOldName = oldName; + String expandedNewName = newName; + String expandedNewDescription = newDescription; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedVm = env.expand(vm); - String expandedOldName = env.expand(oldName); - String expandedNewName = env.expand(newName); - String expandedNewDescription = env.expand(newDescription); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild) run).getBuildVariables()); // Add in matrix axes.. + expandedVm = env.expand(vm); + expandedOldName = env.expand(oldName); + expandedNewName = env.expand(newName); + expandedNewDescription = env.expand(newDescription); + } VSphereLogger.vsLogger(jLogger, "Renaming snapshot of VM \""+expandedVm+"\" from \""+expandedOldName+"\" to \"" + expandedNewName + "\" with description \""+ expandedNewDescription +"\". Please wait ..."); + try { vsphere.renameVmSnapshot(expandedVm, expandedOldName, expandedNewName, expandedNewDescription); + } catch (Exception e) { + throw new VSphereException(e); + } VSphereLogger.vsLogger(jLogger, "Renamed!"); return true; diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/RevertToSnapshot.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/RevertToSnapshot.java index 24ebb20f..d3a3cd85 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/RevertToSnapshot.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/RevertToSnapshot.java @@ -14,18 +14,19 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.AbstractBuild; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; +import javax.annotation.Nonnull; import javax.servlet.ServletException; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -35,7 +36,7 @@ import com.vmware.vim25.mo.VirtualMachineSnapshot; -public class RevertToSnapshot extends VSphereBuildStep { +public class RevertToSnapshot extends VSphereBuildStep implements SimpleBuildStep { private final String vm; private final String snapshotName; @@ -54,23 +55,63 @@ public String getSnapshotName() { return snapshotName; } - public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException { - return revertToSnapshot(build, launcher, listener); + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + revertToSnapshot (run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + @Override + public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = revertToSnapshot(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; //TODO throw AbortException instead of returning value } - private boolean revertToSnapshot(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException{ + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + private boolean revertToSnapshot(final Run run, Launcher launcher, final TaskListener listener) throws VSphereException{ PrintStream jLogger = listener.getLogger(); + String expandedSnap = snapshotName; + String expandedVm = vm; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedSnap = env.expand(snapshotName); - String expandedVm = env.expand(vm); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild)run).getBuildVariables()); // Add in matrix axes.. + expandedSnap = env.expand(snapshotName); + expandedVm = env.expand(vm); + } VSphereLogger.vsLogger(jLogger, "Reverting to snapshot \""+expandedSnap+"\" for VM "+expandedVm+"..."); vsphere.revertToSnapshot(expandedVm, expandedSnap); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/SuspendVm.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/SuspendVm.java index 895765d8..1c09c79c 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/SuspendVm.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/SuspendVm.java @@ -14,18 +14,19 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.AbstractBuild; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; +import javax.annotation.Nonnull; import javax.servlet.ServletException; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -33,7 +34,7 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; -public class SuspendVm extends VSphereBuildStep { +public class SuspendVm extends VSphereBuildStep implements SimpleBuildStep { private final String vm; @@ -46,22 +47,59 @@ public String getVm() { return vm; } - public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException { - return suspend(build, launcher, listener); + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + suspend(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + retVal = suspend(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; //TODO throw AbortException instead of returning value } - private boolean suspend(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException{ + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + private boolean suspend(final Run run, Launcher launcher, final TaskListener listener) throws VSphereException{ PrintStream jLogger = listener.getLogger(); + String expandedVm = vm; EnvVars env; try { - env = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. - String expandedVm = env.expand(vm); + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild)run).getBuildVariables()); // Add in matrix axes.. + expandedVm = env.expand(vm); + } VSphereLogger.vsLogger(jLogger, "Suspending VM..."); vsphere.suspendVm( vsphere.getVmByName(expandedVm)); diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/builders/TakeSnapshot.java b/src/main/java/org/jenkinsci/plugins/vsphere/builders/TakeSnapshot.java index ae92a95f..10a6c62d 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/builders/TakeSnapshot.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/builders/TakeSnapshot.java @@ -14,18 +14,19 @@ */ package org.jenkinsci.plugins.vsphere.builders; -import hudson.EnvVars; -import hudson.Extension; -import hudson.Launcher; -import hudson.model.BuildListener; -import hudson.model.AbstractBuild; +import hudson.*; +import hudson.model.*; +import hudson.tasks.BuildStepMonitor; import hudson.util.FormValidation; import java.io.IOException; import java.io.PrintStream; +import java.util.Collection; +import javax.annotation.Nonnull; import javax.servlet.ServletException; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.vsphere.VSphereBuildStep; import org.jenkinsci.plugins.vsphere.tools.VSphere; import org.jenkinsci.plugins.vsphere.tools.VSphereException; @@ -33,7 +34,7 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; -public class TakeSnapshot extends VSphereBuildStep { +public class TakeSnapshot extends VSphereBuildStep implements SimpleBuildStep { private final String vm; private final String snapshotName; @@ -64,24 +65,67 @@ public boolean isIncludeMemory(){ return includeMemory; } - public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException { - return takeSnapshot(build, launcher, listener); + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + try { + takeSnapshot(run, launcher, listener); + } catch (Exception e) { + throw new AbortException(e.getMessage()); + } + } + + @Override + public boolean prebuild(AbstractBuild abstractBuild, BuildListener buildListener) { + return false; + } + + public boolean perform(final AbstractBuild build, Launcher launcher, final BuildListener listener) { + boolean retVal = false; + try { + return takeSnapshot(build, launcher, listener); + } catch (Exception e) { + e.printStackTrace(); + } + return retVal; //TODO throw AbortException instead of returning value } - private boolean takeSnapshot(final AbstractBuild build, Launcher launcher, final BuildListener listener) throws VSphereException{ + @Override + public Action getProjectAction(AbstractProject abstractProject) { + return null; + } + + @Override + public Collection getProjectActions(AbstractProject abstractProject) { + return null; + } + + @Override + public BuildStepMonitor getRequiredMonitorService() { + return null; + } + + 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 = build.getEnvironment(listener); + env = run.getEnvironment(listener); } catch (Exception e) { throw new VSphereException(e); } - env.overrideAll(build.getBuildVariables()); // Add in matrix axes.. + if (run instanceof AbstractBuild) { + env.overrideAll(((AbstractBuild)run).getBuildVariables()); // Add in matrix axes.. + expandedVm = env.expand(vm); + expandedSnapshotName = env.expand(snapshotName); + expandedDescription = env.expand(description); + } VSphereLogger.vsLogger(jLogger, "Taking snapshot..."); - vsphere.takeSnapshot(env.expand(vm), env.expand(snapshotName), env.expand(description), includeMemory); + vsphere.takeSnapshot(expandedVm, expandedSnapshotName, expandedDescription, includeMemory); VSphereLogger.vsLogger(jLogger, "Complete."); return true; diff --git a/src/main/java/org/jenkinsci/plugins/vsphere/tools/VSphere.java b/src/main/java/org/jenkinsci/plugins/vsphere/tools/VSphere.java index 859bb47b..b3620d88 100644 --- a/src/main/java/org/jenkinsci/plugins/vsphere/tools/VSphere.java +++ b/src/main/java/org/jenkinsci/plugins/vsphere/tools/VSphere.java @@ -40,7 +40,6 @@ import com.vmware.vim25.mo.ClusterComputeResource; import com.vmware.vim25.mo.Datastore; import com.vmware.vim25.mo.Folder; -import com.vmware.vim25.mo.HostSystem; import com.vmware.vim25.mo.InventoryNavigator; import com.vmware.vim25.mo.ManagedEntity; import com.vmware.vim25.mo.ResourcePool; @@ -261,12 +260,10 @@ public void startVm(String name, int timeoutInSeconds) throws VSphereException { int timesToCheck = timeoutInSeconds / 5; // add one extra time for remainder timesToCheck++; - System.out.println("Checking " + timesToCheck + " times for vm to be powered on"); for (int i=0; i getBuildSteps() { + return Hudson.getInstance().getDescriptorList(VSphereBuildStep.class); + } + } + + public static final class vSphereExecution extends AbstractSynchronousNonBlockingStepExecution { + + private static final long serialVersionUID = 1; + + private transient VSphereBuildStepContainer vSphereBSC; + + @Inject + private transient vSphereStep step; + + @StepContextParameter + private transient Run run; + + @StepContextParameter + private transient FilePath filePath; + + @StepContextParameter + private transient Launcher launcher; + + @StepContextParameter + private transient TaskListener listener; + + @StepContextParameter + private transient EnvVars envVars; + + @Override + protected String run() throws Exception { + String IP = ""; + + vSphereBSC = new VSphereBuildStepContainer(step.getBuildStep(), step.getServerName()); + vSphereBSC.perform(run, filePath, launcher, listener); + if (step.getBuildStep().getClass().toString().contains("PowerOn") || + step.getBuildStep().getClass().toString().contains("Deploy") || + step.getBuildStep().getClass().toString().contains("Clone") || + step.getBuildStep().getClass().toString().contains("ExposeGuestInfo")) { + IP = step.getBuildStep().getIP(); + envVars.put("VSPHERE_IP", IP); + + if (step.getBuildStep().getClass().toString().contains("ExposeGuestInfo")) { + Map envVars = ((ExposeGuestInfo)step.getBuildStep()).getVars(); + for (Map.Entry envVar: envVars.entrySet()) { + envVars.put(envVar.getKey(), envVar.getValue()); + } + } + } + vSphereBSC = null; + + return IP; + } + } +} \ No newline at end of file diff --git a/src/main/resources/org/jenkinsci/plugins/workflow/vSphereStep/config.jelly b/src/main/resources/org/jenkinsci/plugins/workflow/vSphereStep/config.jelly new file mode 100644 index 00000000..fea2d138 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/workflow/vSphereStep/config.jelly @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/main/resources/org/jenkinsci/plugins/workflow/vSphereStep/help.html b/src/main/resources/org/jenkinsci/plugins/workflow/vSphereStep/help.html new file mode 100644 index 00000000..e24fd066 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/workflow/vSphereStep/help.html @@ -0,0 +1,3 @@ +
+ Execute vCenter actions. Reusing vSphereBuildStep forms, cut properly exposing VSPHERE_IP for PowerOn, Deploy and Clone options. +
\ No newline at end of file