Skip to content

Commit

Permalink
Merge pull request #15 from Mithweth/master
Browse files Browse the repository at this point in the history
Start VM even if policy is set on BEFORE_JOB
  • Loading branch information
Maxime C authored Aug 27, 2021
2 parents 9388b3e + 7af1b6f commit ddd63b5
Showing 1 changed file with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,29 @@ public boolean isLaunchSupported() {
return overrideLaunchSupported;
}

public void revertSnapshot(SlaveComputer slaveComputer, TaskListener taskListener) throws IOException, InterruptedException {
public void startSlaveIfNeeded(TaskListener taskListener) throws IOException, InterruptedException {
String taskId = null;
JSONObject taskStatus = null;
try {
Datacenter datacenter = findDatacenterInstance();
Connector pve = datacenter.proxmoxInstance();
Boolean isvmIdRunning = pve.isQemuMachineRunning(datacenterNode, virtualMachineId);
if (!isvmIdRunning) {
taskListener.getLogger().println("Starting virtual machine...");
taskId = pve.startQemuMachine(datacenterNode, virtualMachineId);
taskStatus = pve.waitForTaskToFinish(datacenterNode, taskId);
taskListener.getLogger().println("Task finished! Status object: " + taskStatus.toString());
}
} catch (IOException e) {
taskListener.getLogger().println("ERROR: IOException: " + e.getMessage());
} catch (JSONException e) {
taskListener.getLogger().println("ERROR: Parsing JSON: " + e.getMessage());
} catch (LoginException e) {
taskListener.getLogger().println("ERROR: Login failed: " + e.getMessage());
}
}

public void revertSnapshot(SlaveComputer slaveComputer, TaskListener taskListener) throws InterruptedException {
String taskId = null;
JSONObject taskStatus = null;

Expand All @@ -115,11 +137,8 @@ public void revertSnapshot(SlaveComputer slaveComputer, TaskListener taskListene
}

Boolean isvmIdRunning = pve.isQemuMachineRunning(datacenterNode, virtualMachineId);
if (startVM && !isvmIdRunning) {
taskListener.getLogger().println("Starting virtual machine...");
taskId = pve.startQemuMachine(datacenterNode, virtualMachineId);
taskStatus = pve.waitForTaskToFinish(datacenterNode, taskId);
taskListener.getLogger().println("Task finished! Status object: " + taskStatus.toString());
if (startVM) {
startSlaveIfNeeded(taskListener);
}

} catch (IOException e) {
Expand All @@ -138,8 +157,13 @@ public void revertSnapshot(SlaveComputer slaveComputer, TaskListener taskListene

@Override
public void launch(SlaveComputer slaveComputer, TaskListener taskListener) throws IOException, InterruptedException {
if(revertPolicy == RevertPolicy.AFTER_CONNECT)
revertSnapshot(slaveComputer, taskListener);
if (revertPolicy == RevertPolicy.AFTER_CONNECT) {
revertSnapshot(slaveComputer, taskListener);
} else {
if (startVM) {
startSlaveIfNeeded(taskListener);
}
}

delegate.launch(slaveComputer, taskListener);
}
Expand Down

0 comments on commit ddd63b5

Please sign in to comment.