Skip to content

Commit

Permalink
PowerOn: Check if guest info is null while waiting for ip
Browse files Browse the repository at this point in the history
I noticed in our environment that the guest info can sometimes be null and lead
to  a null pointer exception.
Handle incorrect name for power off as well in a more explicit manner.
It currently throws a Null Pointer Exception.
  • Loading branch information
damienbiggs committed May 26, 2015
1 parent 4953e1a commit 32d861c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
VSphereLogger.vsLogger(jLogger, "Exposing guest info for VM \"" + vmName + "\" as environment variables");

VirtualMachine vsphereVm = vsphere.getVmByName(vmName);
if (vsphereVm == null) {
throw new RuntimeException(Messages.validation_notFound("vm " + vmName));
}
VSphereEnvAction envAction = createGuestInfoEnvAction(vsphereVm, jLogger);
build.addAction(envAction);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ private boolean powerOff(final AbstractBuild<?, ?> build, Launcher launcher, fin
env.overrideAll(build.getBuildVariables()); // Add in matrix axes..
String expandedVm = env.expand(vm);

VSphereLogger.vsLogger(jLogger, "Shutting Down VM...");
vsphere.powerOffVm( vsphere.getVmByName(expandedVm), evenIfSuspended, shutdownGracefully);
VSphereLogger.vsLogger(jLogger, "Shutting Down VM " + expandedVm + "...");
VirtualMachine vsphereVm = vsphere.getVmByName(expandedVm);
if (vsphereVm == null) {
throw new RuntimeException(Messages.validation_notFound("vm " + expandedVm));
}
vsphere.powerOffVm( vsphereVm, evenIfSuspended, shutdownGracefully);

VSphereLogger.vsLogger(jLogger, "Successfully shutdown \""+expandedVm+"\"");

Expand Down
31 changes: 22 additions & 9 deletions src/main/java/org/jenkinsci/plugins/vsphere/tools/VSphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
*/
package org.jenkinsci.plugins.vsphere.tools;

import com.vmware.vim25.FileFault;
import com.vmware.vim25.GuestInfo;
import com.vmware.vim25.InvalidName;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.InvalidState;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.SnapshotFault;
import com.vmware.vim25.TaskInProgress;
import com.vmware.vim25.TaskInfoState;
import com.vmware.vim25.VirtualMachineCloneSpec;
import com.vmware.vim25.VirtualMachineConfigSpec;
Expand All @@ -26,6 +32,7 @@
import com.vmware.vim25.VirtualMachineSnapshotInfo;
import com.vmware.vim25.VirtualMachineSnapshotTree;
import com.vmware.vim25.VirtualMachineToolsStatus;
import com.vmware.vim25.VmConfigFault;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Datastore;
import com.vmware.vim25.mo.Folder;
Expand Down Expand Up @@ -263,7 +270,7 @@ public void startVm(String name) throws VSphereException {
}
}
}catch(Exception e){
throw new VSphereException("VM cannot be started:", e);
throw new VSphereException("VM cannot be started: " + e.getMessage(), e);
}

throw new VSphereException("VM cannot be started");
Expand Down Expand Up @@ -369,16 +376,20 @@ public void deleteSnapshot(String vmName, String snapName, boolean consolidate,

public void takeSnapshot(String vmName, String snapshot, String description, boolean snapMemory) throws VSphereException{

try {
Task task = getVmByName(vmName).createSnapshot_Task(snapshot, description, snapMemory, !snapMemory);
VirtualMachine vmToSnapshot = getVmByName(vmName);
if (vmToSnapshot == null) {
throw new VSphereException("Vm " + vmName + " was not found");
}
try {
Task task = vmToSnapshot.createSnapshot_Task(snapshot, description, snapMemory, !snapMemory);
if (task.waitForTask().equals(Task.SUCCESS)) {
return;
}
} catch (Exception e) {
throw new VSphereException("Could not take snapshot", e);
}
throw new VSphereException(e);
}

throw new VSphereException("Could not take snapshot");
throw new VSphereException("Could not take snapshot");
}

public void markAsTemplate(String vmName, String snapName, boolean force) throws VSphereException {
Expand Down Expand Up @@ -436,9 +447,11 @@ public String getIp(VirtualMachine vm, int timeout) throws VSphereException {

for(int count=0; count<maxTries; count++){

//get IP
if(vm.getGuest().getIpAddress()!=null){
return vm.getGuest().getIpAddress();
GuestInfo guestInfo = vm.getGuest();

// guest info can be null sometimes
if (guestInfo != null && guestInfo.getIpAddress() != null){
return guestInfo.getIpAddress();
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public static void vsLogger(PrintStream logger, Exception e){
return;
}

if (e.getMessage() != null) {
logger.println("["+Messages.VSphereLogger_title()+"] Exception: " + e.getMessage());
if (e.getMessage() != null && (!(e instanceof RuntimeException) && e.getCause() == null)) {
logger.println("["+Messages.VSphereLogger_title()+"] " + e.getMessage());
} else {
logger.println("["+Messages.VSphereLogger_title()+"] Exception message was null, stack trace");
logger.println("["+Messages.VSphereLogger_title()+"] Exception");
e.printStackTrace(logger);
}
}
Expand Down

0 comments on commit 32d861c

Please sign in to comment.