From 264b654e3246bf0176b6bc783faffef0ce12d545 Mon Sep 17 00:00:00 2001 From: Peter Darton Date: Tue, 18 Aug 2020 17:26:28 +0100 Subject: [PATCH 1/4] Removed unnecessary code in vSphereCloudProvisionedSlave It shouldn't need a descriptor visibility filter as it says isInstantiable=false. It can remove most descriptor code just by inheriting from vSphereCloudSlave's descriptor instead. --- .../plugins/vSphereCloudProvisionedSlave.java | 82 ++----------------- 1 file changed, 6 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/vSphereCloudProvisionedSlave.java b/src/main/java/org/jenkinsci/plugins/vSphereCloudProvisionedSlave.java index 2037b9a5..20ac7235 100644 --- a/src/main/java/org/jenkinsci/plugins/vSphereCloudProvisionedSlave.java +++ b/src/main/java/org/jenkinsci/plugins/vSphereCloudProvisionedSlave.java @@ -100,89 +100,19 @@ public void preLaunch(Computer c, TaskListener taskListener) throws IOException, } @Extension - public static class DescriptorVisibilityFilterImpl extends DescriptorVisibilityFilter { - @Override - public boolean filter(@CheckForNull Object context, @NonNull Descriptor descriptor) { - return !(descriptor instanceof DescriptorImpl); - } - } - - @Extension - public static final class DescriptorImpl extends SlaveDescriptor { - - public DescriptorImpl() { - load(); - } - + public static final class DescriptorImpl extends vSphereCloudSlave.DescriptorImpl { @Override public String getDisplayName() { - return "Slave created from a vSphere Cloud slave template"; + return super.getDisplayName()+", auto-provisioned by Jenkins from cloud template"; } @Override public boolean isInstantiable() { + /* + * This type of agent can't be directly created by the user through the UI. + * The user defines a vSphere agent template and _that_ then creates these "on demand". + */ return false; } - - public List getvSphereClouds() { - List result = new ArrayList(); - for (Cloud cloud : Jenkins.getInstance().clouds) { - if (cloud instanceof vSphereCloud) { - result.add((vSphereCloud) cloud); - } - } - return result; - } - - public vSphereCloud getSpecificvSphereCloud(String vsDescription) - throws Exception { - for (vSphereCloud vs : getvSphereClouds()) { - if (vs.getVsDescription().equals(vsDescription)) { - return vs; - } - } - throw new Exception("The vSphere Cloud doesn't exist"); - } - - public List getIdleOptions() { - List options = new ArrayList(); - options.add("Shutdown"); - options.add("Shutdown and Revert"); - options.add("Revert and Restart"); - options.add("Revert and Reset"); - options.add("Suspend"); - options.add("Reset"); - options.add("Reconnect and Revert"); - options.add("Nothing"); - return options; - } - - public FormValidation doCheckLaunchDelay(@QueryParameter String value) { - return FormValidation.validatePositiveInteger(value); - } - - @RequirePOST - public FormValidation doTestConnection(@AncestorInPath ItemGroup context, - @QueryParameter String vsDescription, - @QueryParameter String vmName, - @QueryParameter String snapName) { - throwUnlessUserHasPermissionToConfigureSlave(context); - try { - vSphereCloud vsC = getSpecificvSphereCloud(vsDescription); - VirtualMachine vm = vsC.vSphereInstance().getVmByName(vmName); - if (vm == null) { - return FormValidation.error("Virtual Machine was not found"); - } - if (!snapName.isEmpty()) { - VirtualMachineSnapshot snap = vsC.vSphereInstance().getSnapshotInTree(vm, snapName); - if (snap == null) { - return FormValidation.error("Virtual Machine snapshot was not found"); - } - } - return FormValidation.ok("Virtual Machine found successfully"); - } catch (Exception e) { - throw new RuntimeException(e); - } - } } } From 79b39fc590113d8216898d7a17e7bfe25daf709c Mon Sep 17 00:00:00 2001 From: Peter Darton Date: Tue, 18 Aug 2020 17:29:08 +0100 Subject: [PATCH 2/4] Removed unnecessary code in vSphereCloudSlave It shouldn't need a descriptor visibility filter as should be visible. It doesn't need to specify isInstantiable=true as that's the default. --- .../jenkinsci/plugins/vSphereCloudSlave.java | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java b/src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java index 9737e1c8..1670b59f 100644 --- a/src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java +++ b/src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java @@ -2,17 +2,13 @@ import static org.jenkinsci.plugins.vsphere.tools.PermissionUtils.throwUnlessUserHasPermissionToConfigureSlave; -import edu.umd.cs.findbugs.annotations.CheckForNull; -import edu.umd.cs.findbugs.annotations.NonNull; import hudson.AbortException; import hudson.Extension; import hudson.Util; -import hudson.model.DescriptorVisibilityFilter; import hudson.model.Queue.BuildableItem; import hudson.model.Result; import hudson.model.TaskListener; import hudson.model.Computer; -import hudson.model.Descriptor; import hudson.model.Descriptor.FormException; import hudson.model.Run; import hudson.model.queue.CauseOfBlockage; @@ -368,28 +364,14 @@ public void preLaunch(Computer c, TaskListener taskListener) throws IOException, } @Extension - public static class DescriptorVisibilityFilterImpl extends DescriptorVisibilityFilter { - @Override - public boolean filter(@CheckForNull Object context, @NonNull Descriptor descriptor) { - return !(descriptor instanceof DescriptorImpl); - } - } - - @Extension - public static final class DescriptorImpl extends SlaveDescriptor { - + public static class DescriptorImpl extends SlaveDescriptor { public DescriptorImpl() { load(); } @Override public String getDisplayName() { - return "Slave virtual computer running under vSphere Cloud"; - } - - @Override - public boolean isInstantiable() { - return true; + return "Agent running within a vSphere hypervisor"; } public List getvSphereClouds() { From abf926c883ef3ed7def90563492c691c99b801b6 Mon Sep 17 00:00:00 2001 From: Peter Darton Date: Tue, 18 Aug 2020 17:31:01 +0100 Subject: [PATCH 3/4] Bugfix: Make vSphereCloudSlave jelly match DumbSlave Don't call getComputerLauncherDescriptors() as that doesn't exist anymore, call computerLauncherDescriptors(it) instead. --- .../jenkinsci/plugins/vSphereCloudSlave/configure-entries.jelly | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/configure-entries.jelly b/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/configure-entries.jelly index 036bbb48..13541933 100644 --- a/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/configure-entries.jelly +++ b/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/configure-entries.jelly @@ -57,7 +57,7 @@ - + From bc70875d567ad4a472fd195720ae5a501aec64ff Mon Sep 17 00:00:00 2001 From: Peter Darton Date: Tue, 18 Aug 2020 17:32:00 +0100 Subject: [PATCH 4/4] Add descriptive text for creating a new vSphere slave Uses the same code pattern as used in DumbSlave in the core code. --- .../plugins/vSphereCloudSlave/newInstanceDetail.jelly | 4 ++++ .../plugins/vSphereCloudSlave/newInstanceDetail.properties | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/newInstanceDetail.jelly create mode 100644 src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/newInstanceDetail.properties diff --git a/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/newInstanceDetail.jelly b/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/newInstanceDetail.jelly new file mode 100644 index 00000000..799b5dd3 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/newInstanceDetail.jelly @@ -0,0 +1,4 @@ + +
+ ${%detail} +
diff --git a/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/newInstanceDetail.properties b/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/newInstanceDetail.properties new file mode 100644 index 00000000..81a697a3 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/vSphereCloudSlave/newInstanceDetail.properties @@ -0,0 +1,5 @@ +detail=\ + Adds a permanent managed vSphere agent to Jenkins. \ + This is called "managed" because Jenkins can perform vSphere-specific actions (e.g. to revert the agent back to a previous state) upon build completion etc. \ + Use this if you have defined permanent virtual machines in your vSphere cloud and want Jenkins to handle them. \ + If you want Jenkins to create, use and destroy machines "on demand" then you should use the Cloud functionality and define a Jenkins agent template instead.