Skip to content

Commit

Permalink
Merge pull request #32 from damienbiggs/topic/powerOn
Browse files Browse the repository at this point in the history
PowerOn: Use timeout value for all parts of power on
  • Loading branch information
jswager committed Jun 10, 2015
2 parents 4f1eb75 + 5840d54 commit 88a15ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void launch(SlaveComputer slaveComputer, TaskListener taskListener)
case suspended:
// Power the VM up.
vSphereCloud.Log(slaveComputer, taskListener, "Powering on VM");
v.startVm(vmName);
v.startVm(vmName, 60);
break;
}

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOn.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.jenkinsci.plugins.vsphere.builders;

import com.google.common.base.Stopwatch;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
Expand All @@ -26,6 +27,7 @@
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;

Expand Down Expand Up @@ -74,16 +76,26 @@ private boolean powerOn(final AbstractBuild<?, ?> build, Launcher launcher, fina
env.overrideAll(build.getBuildVariables()); // Add in matrix axes..
String expandedVm = env.expand(vm);

VSphereLogger.vsLogger(jLogger, "Waiting for IP (VM may be restarted during this time)");
vsphere.startVm(expandedVm);
String vmIP = vsphere.getIp(vsphere.getVmByName(expandedVm), getTimeoutInSeconds());
VSphereLogger.vsLogger(jLogger, "Waiting for VM " + expandedVm + " to start (VM may be restarted during this time)");
VSphereLogger.vsLogger(jLogger, "Timeout set to " + timeoutInSeconds + " seconds");

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);

if(vmIP==null){
VSphereLogger.vsLogger(jLogger, "Error: Could not get IP for \""+expandedVm+"\" ");
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");
stopwatch.stop();

// useful to tell user about the environment variable
VSphereLogger.vsLogger(jLogger, "Exposing " + vmIP + " as environment variable VSPHERE_IP");
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/jenkinsci/plugins/vsphere/tools/VSphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,13 @@ public void reconfigureVm(String name, VirtualMachineConfigSpec spec) throws VSp
* @param name - Name of VM to start
* @throws VSphereException
*/
public void startVm(String name) throws VSphereException {
public void startVm(String name, int timeoutInSeconds) throws VSphereException {

try{
VirtualMachine vm = getVmByName(name);
if (vm == null) {
throw new VSphereException("Vm " + name + " was not found");
}
if(isPoweredOn(vm))
return;

Expand All @@ -250,11 +253,16 @@ public void startVm(String name) throws VSphereException {

Task task = vm.powerOnVM_Task(null);

for (int i=0, j=12; i<j; i++){
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<timesToCheck; i++){

if(task.getTaskInfo().getState()==TaskInfoState.success){
System.out.println("VM was powered up successfully.");
return;
return;
}

if (task.getTaskInfo().getState()==TaskInfoState.running ||
Expand All @@ -266,7 +274,7 @@ public void startVm(String name) throws VSphereException {
VirtualMachineQuestionInfo q = vm.getRuntime().getQuestion();
if(q!=null && q.getId().equals("_vmx1")){
vm.answerVM(q.getId(), q.getChoice().getDefaultIndex().toString());
return;
return;
}
}
}catch(Exception e){
Expand Down Expand Up @@ -673,7 +681,7 @@ private boolean isPoweredOn(VirtualMachine vm){
}

private boolean isPoweredOff(VirtualMachine vm){
return (vm.getRuntime().getPowerState() == VirtualMachinePowerState.poweredOff);
return (vm.getRuntime() != null && vm.getRuntime().getPowerState() == VirtualMachinePowerState.poweredOff);
}

public boolean vmToolIsEnabled(VirtualMachine vm) {
Expand Down

0 comments on commit 88a15ac

Please sign in to comment.