Skip to content

Commit

Permalink
Merge pull request #59 from stephenc/jenkins-39232
Browse files Browse the repository at this point in the history
[JENKINS-39232] Make vSphereCloudLauncher inherit from DelegatingComputerLauncher
  • Loading branch information
jswager authored Oct 26, 2016
2 parents dd3f70f + 5b4ff26 commit 5ed468a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
62 changes: 45 additions & 17 deletions src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import hudson.model.TaskListener;
import hudson.model.Descriptor;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.DelegatingComputerLauncher;
import hudson.slaves.SlaveComputer;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.util.Calendar;

import org.jenkinsci.plugins.vsphere.tools.VSphere;
Expand All @@ -28,9 +30,10 @@
*
* @author Admin
*/
public class vSphereCloudLauncher extends ComputerLauncher {
public class vSphereCloudLauncher extends DelegatingComputerLauncher {

private final ComputerLauncher delegate;
@Deprecated
private transient ComputerLauncher delegate;
private final Boolean overrideLaunchSupported;
private final String vsDescription;
private final String vmName;
Expand All @@ -51,13 +54,12 @@ public enum MACHINE_ACTION {
}

@DataBoundConstructor
public vSphereCloudLauncher(ComputerLauncher delegate,
public vSphereCloudLauncher(ComputerLauncher launcher,
String vsDescription, String vmName,
Boolean overrideLaunchSupported, Boolean waitForVMTools,
String snapName, String launchDelay, String idleOption,
String LimitedTestRunCount) {
super();
this.delegate = delegate;
super(launcher);
this.overrideLaunchSupported = overrideLaunchSupported;
this.vsDescription = vsDescription;
this.vmName = vmName;
Expand All @@ -82,6 +84,33 @@ public vSphereCloudLauncher(ComputerLauncher delegate,
this.LimitedTestRunCount = Util.tryParseNumber(LimitedTestRunCount, 0).intValue();
}

private vSphereCloudLauncher(ComputerLauncher launcher, Boolean overrideLaunchSupported, String vsDescription,
String vmName, Boolean waitForVMTools, String snapName, int launchDelay,
MACHINE_ACTION idleAction, int limitedTestRunCount) {
super(launcher);
this.overrideLaunchSupported = overrideLaunchSupported;
this.vsDescription = vsDescription;
this.vmName = vmName;
this.waitForVMTools = waitForVMTools;
this.snapName = snapName;
this.launchDelay = launchDelay;
this.idleAction = idleAction;
LimitedTestRunCount = limitedTestRunCount;
}

/**
* Migrates instances from the old parent class to the new parent class.
* @return the deserialized instance.
* @throws ObjectStreamException if something went wrong.
*/
private Object readResolve() throws ObjectStreamException {
if (delegate != null) {
return new vSphereCloudLauncher(delegate, overrideLaunchSupported, vsDescription, vmName, waitForVMTools,
snapName, launchDelay, idleAction, LimitedTestRunCount);
}
return this;
}

public vSphereCloud findOurVsInstance() throws RuntimeException {
if (vsDescription != null && vmName != null) {
for (vSphereCloud cloud : vSphereCloud.findAllVsphereClouds()) {
Expand Down Expand Up @@ -193,15 +222,15 @@ public void launch(SlaveComputer slaveComputer, TaskListener taskListener)
/* At this point we have told vSphere to get the VM going.
* Now we wait our launch delay amount before trying to connect.
*/
if (delegate.isLaunchSupported()) {
if (launcher.isLaunchSupported()) {
if (launchDelay > 0) {
vSphereCloud.Log(slaveComputer, taskListener, "Waiting for " + launchDelay
+ " seconds before asking " + delegate + " to launch slave.");
+ " seconds before asking " + launcher + " to launch slave.");
// Delegate is going to do launch.
Thread.sleep(launchDelay * 1000);
}
vSphereCloud.Log(slaveComputer, taskListener, "Asking " + delegate.getClass().getSimpleName() + " to launch slave.");
delegate.launch(slaveComputer, taskListener);
vSphereCloud.Log(slaveComputer, taskListener, "Asking " + launcher.getClass().getSimpleName() + " to launch slave.");
super.launch(slaveComputer, taskListener);
} else {
vSphereCloud.Log(slaveComputer, taskListener, "Waiting for up to " + launchDelay
+ " seconds for slave to come online.");
Expand Down Expand Up @@ -263,7 +292,7 @@ public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListen
VSphere v = null;
try {
vSphereCloud.Log(slaveComputer, taskListener, "Running disconnect procedure...");
delegate.afterDisconnect(slaveComputer, taskListener);
super.afterDisconnect(slaveComputer, taskListener);
vSphereCloud.Log(slaveComputer, taskListener, "Shutting down Virtual Machine...");
MACHINE_ACTION localIdle = idleAction;
if (localIdle == null) {
Expand Down Expand Up @@ -323,8 +352,12 @@ public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListen
}
}

/**
* @deprecated use {@link #getLauncher()}
*/
@Deprecated
public ComputerLauncher getDelegate() {
return delegate;
return launcher;
}

public String getVmName() {
Expand Down Expand Up @@ -354,17 +387,12 @@ public Integer getLimitedTestRunCount() {
@Override
public boolean isLaunchSupported() {
if (this.overrideLaunchSupported == null) {
return delegate.isLaunchSupported();
return launcher.isLaunchSupported();
} else {
return overrideLaunchSupported;
}
}

@Override
public void beforeDisconnect(SlaveComputer slaveComputer, TaskListener taskListener) {
delegate.beforeDisconnect(slaveComputer, taskListener); //this call does nothing.
}

@Override
public Descriptor<ComputerLauncher> getDescriptor() {
// Don't allow creation of launcher from UI
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jenkinsci/plugins/vSphereCloudSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public boolean EndLimitedTestRun(Run r) {
* @return original launcher
*/
public ComputerLauncher getDelegateLauncher() {
return ((vSphereCloudLauncher) getLauncher()).getDelegate();
return ((vSphereCloudLauncher) getLauncher()).getLauncher();
}

@Extension
Expand Down

0 comments on commit 5ed468a

Please sign in to comment.