Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

PowerOn: Use timeout value for all parts of power on #32

Merged
merged 1 commit into from
Jun 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -231,10 +231,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 @@ -243,11 +246,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 @@ -259,7 +267,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 @@ -660,7 +668,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