forked from jenkinsci/vsphere-cloud-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVSphere.java
More file actions
756 lines (635 loc) · 24.7 KB
/
VSphere.java
File metadata and controls
756 lines (635 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* Copyright 2013, MANDIANT, Eric Lordahl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jenkinsci.plugins.vsphere.tools;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.TaskInfoState;
import com.vmware.vim25.VirtualMachineCloneSpec;
import com.vmware.vim25.VirtualMachineConfigSpec;
import com.vmware.vim25.VirtualMachinePowerState;
import com.vmware.vim25.VirtualMachineQuestionInfo;
import com.vmware.vim25.VirtualMachineRelocateSpec;
import com.vmware.vim25.VirtualMachineSnapshotInfo;
import com.vmware.vim25.VirtualMachineSnapshotTree;
import com.vmware.vim25.VirtualMachineToolsStatus;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Datastore;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ResourcePool;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.Task;
import com.vmware.vim25.mo.VirtualMachine;
import com.vmware.vim25.mo.VirtualMachineSnapshot;
import org.apache.commons.lang.StringUtils;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
public class VSphere {
private final URL url;
private final String session;
private VSphere(@Nonnull String url, @Nonnull String user, @CheckForNull String pw) throws VSphereException{
try {
//TODO - change ignoreCert to be configurable
this.url = new URL(url);
this.session = (new ServiceInstance(this.url, user, pw, true)).getServerConnection().getSessionStr();
} catch (Exception e) {
throw new VSphereException(e);
}
}
private ServiceInstance getServiceInstance() throws RemoteException, MalformedURLException{
return new ServiceInstance(url, session, true);
}
/**
* Initiates Connection to vSphere Server
* @param server Server URL
* @throws VSphereException
*/
public static VSphere connect(@Nonnull String server, @Nonnull String user, @CheckForNull String pw) throws VSphereException {
return new VSphere(server, user, pw);
}
/**
* Disconnect from vSphere server
*/
public void disconnect() {
try {
this.getServiceInstance().getServerConnection().logout();
} catch (Exception e) {
System.out.println("Caught exception when trying to disconnect vSphere." + String.format("%n") + e);
}
}
public static String vSphereOutput(String msg){
return (Messages.VSphereLogger_title()+": ").concat(msg);
}
/**
* Deploys a new VM from a given template with a given name.
*
* @param cloneName - name of VM to be created
* @param sourceName - name of VM or template to be cloned
* @param linkedClone - true if you want to re-use disk backings
* @param resourcePoolName - resource pool to use
* @param cluster - ComputeClusterResource to use
* @param datastoreName - Datastore to use
* @throws VSphereException
*/
public void deployVm(String cloneName, String sourceName, boolean linkedClone, String resourcePoolName, String cluster, String datastoreName, PrintStream jLogger) throws VSphereException {
boolean DO_NOT_USE_SNAPSHOTS = false;
logMessage(jLogger, "Deploying new vm \""+ cloneName + "\" from template \""+sourceName+"\"");
cloneOrDeployVm(cloneName, sourceName, linkedClone, resourcePoolName, cluster, datastoreName, DO_NOT_USE_SNAPSHOTS, jLogger);
}
/**
* Clones a new VM from a given vm or template with a given name.
*
* @param cloneName - name of VM to be created
* @param sourceName - name of VM or template to be cloned
* @param linkedClone - true if you want to re-use disk backings
* @param resourcePoolName - resource pool to use
* @param cluster - ComputeClusterResource to use
* @param datastoreName - Datastore to use
* @throws VSphereException
*/
public void cloneVm(String cloneName, String sourceName, boolean linkedClone, String resourcePoolName, String cluster, String datastoreName, PrintStream jLogger) throws VSphereException {
boolean DO_USE_SNAPSHOTS = true;
logMessage(jLogger, "Creating a shallow clone of \""+ sourceName + "\" to \""+cloneName+"\"");
cloneOrDeployVm(cloneName, sourceName, linkedClone, resourcePoolName, cluster, datastoreName, DO_USE_SNAPSHOTS, jLogger);
}
private void cloneOrDeployVm(String cloneName, String sourceName, boolean linkedClone, String resourcePoolName, String cluster, String datastoreName, boolean useSnapshot, PrintStream jLogger) throws VSphereException {
try{
VirtualMachine sourceVm = getVmByName(sourceName);
if(sourceVm==null) {
throw new VSphereException("VM or template \"" + sourceName + "\" not found");
}
if(getVmByName(cloneName)!=null){
throw new VSphereException("VM \"" + cloneName + "\" already exists");
}
VirtualMachineRelocateSpec rel = createRelocateSpec(jLogger, linkedClone, resourcePoolName, cluster, datastoreName);
VirtualMachineCloneSpec cloneSpec = createCloneSpec(rel);
cloneSpec.setTemplate(false);
if (useSnapshot) {
//TODO add config to allow state of VM or snapshot
if(sourceVm.getCurrentSnapShot()==null){
throw new VSphereException("Source VM or Template \"" + sourceName + "\" requires at least one snapshot!");
}
cloneSpec.setSnapshot(sourceVm.getCurrentSnapShot().getMOR());
}
Task task = sourceVm.cloneVM_Task((Folder) sourceVm.getParent(),
cloneName, cloneSpec);
logMessage(jLogger, "Started cloning of VM. Please wait ...");
String status = task.waitForTask();
if(!TaskInfoState.success.toString().equals(status)) {
throw new VSphereException("Couldn't clone \""+ sourceName +"\"! Does \""+cloneName+"\" already exist? " +
"Clone task ended with status " + status);
}
} catch(Exception e){
throw new VSphereException(e);
}
}
private VirtualMachineCloneSpec createCloneSpec(VirtualMachineRelocateSpec rel) {
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setLocation(rel);
cloneSpec.setTemplate(false);
cloneSpec.setPowerOn(true);
return cloneSpec;
}
private VirtualMachineRelocateSpec createRelocateSpec(PrintStream jLogger, boolean linkedClone, String resourcePoolName,
String cluster, String datastoreName) throws RemoteException, MalformedURLException, VSphereException {
VirtualMachineRelocateSpec rel = new VirtualMachineRelocateSpec();
if(linkedClone){
rel.setDiskMoveType("createNewChildDiskBacking");
}else{
rel.setDiskMoveType("moveAllDiskBackingsAndDisallowSharing");
}
ClusterComputeResource clusterResource = getClusterByName(cluster);
// probably only of interest if someone actually entered a cluster name
if (clusterResource == null && StringUtils.isNotBlank(cluster)) {
logMessage(jLogger, "Cluster resource " + cluster + " does not exist, root folder will be used for getting resource pool and datastore");
}
ResourcePool resourcePool = getResourcePoolByName(resourcePoolName, clusterResource);
if (resourcePool == null) {
throw new VSphereException("Resource pool \"" + resourcePoolName + "\" not found");
}
rel.setPool(resourcePool.getMOR());
if (datastoreName != null && !datastoreName.isEmpty()) {
Datastore datastore = getDatastoreByName(datastoreName, clusterResource);
if (datastore==null){
throw new VSphereException("Datastore \"" + datastoreName + "\" not found!");
}
rel.setDatastore(datastore.getMOR());
}
return rel;
}
public void reconfigureVm(String name, VirtualMachineConfigSpec spec) throws VSphereException {
VirtualMachine vm = getVmByName(name);
if(vm==null) {
throw new VSphereException("No VM or template " + name + " found");
}
System.out.println("Reconfiguring VM. Please wait ...");
try {
Task task = vm.reconfigVM_Task(spec);
String status = task.waitForTask();
if(status.equals(TaskInfoState.success.toString())) {
return;
}
} catch(Exception e){
throw new VSphereException("VM cannot be reconfigured:" + e.getMessage(), e);
}
throw new VSphereException("Couldn't reconfigure \""+ name +"\"!");
}
/**
* @param name - Name of VM to start
* @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;
if(vm.getConfig().template)
throw new VSphereException("VM represents a template!");
Task task = vm.powerOnVM_Task(null);
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;
}
if (task.getTaskInfo().getState()==TaskInfoState.running ||
task.getTaskInfo().getState()==TaskInfoState.queued){
Thread.sleep(5000);
}
//Check for copied/moved question
VirtualMachineQuestionInfo q = vm.getRuntime().getQuestion();
if(q!=null && q.getId().equals("_vmx1")){
vm.answerVM(q.getId(), q.getChoice().getDefaultIndex().toString());
return;
}
}
}catch(Exception e){
throw new VSphereException("VM cannot be started:", e);
}
throw new VSphereException("VM cannot be started");
}
private ManagedObjectReference findSnapshotInTree(
VirtualMachineSnapshotTree[] snapTree, String snapName) {
for (VirtualMachineSnapshotTree node : snapTree) {
if (snapName.equals(node.getName())) {
return node.getSnapshot();
} else {
VirtualMachineSnapshotTree[] childTree =
node.getChildSnapshotList();
if (childTree != null) {
ManagedObjectReference mor = findSnapshotInTree(
childTree, snapName);
if (mor != null) {
return mor;
}
}
}
}
return null;
}
public VirtualMachineSnapshot getSnapshotInTree(
VirtualMachine vm, String snapName) {
if (vm == null || snapName == null) {
return null;
}
VirtualMachineSnapshotInfo info = vm.getSnapshot();
if (info != null)
{
VirtualMachineSnapshotTree[] snapTree =
info.getRootSnapshotList();
if (snapTree != null) {
ManagedObjectReference mor = findSnapshotInTree(
snapTree, snapName);
if (mor != null) {
return new VirtualMachineSnapshot(
vm.getServerConnection(), mor);
}
}
}
return null;
}
public void revertToSnapshot(String vmName, String snapName) throws VSphereException{
VirtualMachine vm = getVmByName(vmName);
VirtualMachineSnapshot snap = getSnapshotInTree(vm, snapName);
if (snap == null) {
throw new VSphereException("Virtual Machine snapshot cannot be found");
}
try{
Task task = snap.revertToSnapshot_Task(null);
if (!task.waitForTask().equals(Task.SUCCESS)) {
throw new VSphereException("Could not revert to snapshot");
}
}catch(Exception e){
throw new VSphereException(e);
}
}
public void deleteSnapshot(String vmName, String snapName, boolean consolidate, boolean failOnNoExist) throws VSphereException{
VirtualMachine vm = getVmByName(vmName);
VirtualMachineSnapshot snap = getSnapshotInTree(vm, snapName);
if (snap == null && failOnNoExist) {
throw new VSphereException("Virtual Machine snapshot cannot be found");
}
try{
Task task;
if (snap!=null){
//Does not delete subtree; Implicitly consolidates disk
task = snap.removeSnapshot_Task(false);
if (!task.waitForTask().equals(Task.SUCCESS)) {
throw new VSphereException("Could not delete snapshot");
}
}
if(!consolidate)
return;
//This might be redundant, but I think it consolidates all disks,
//where as the removeSnapshot only consolidates the individual disk
task = vm.consolidateVMDisks_Task();
if (!task.waitForTask().equals(Task.SUCCESS)) {
throw new VSphereException("Could not consolidate VM disks");
}
}catch(Exception e){
throw new VSphereException(e);
}
}
public void takeSnapshot(String vmName, String snapshot, String description, boolean snapMemory) throws VSphereException{
try {
Task task = getVmByName(vmName).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("Could not take snapshot");
}
public void markAsTemplate(String vmName, String snapName, boolean force) throws VSphereException {
try{
VirtualMachine vm = getVmByName(vmName);
if(vm.getConfig().template)
return;
if(isPoweredOff(vm) || force){
powerOffVm(vm, force, false);
vm.markAsTemplate();
return;
}
}catch(Exception e){
throw new VSphereException("Could not convert to Template", e);
}
throw new VSphereException("Could not mark as Template. Check it's power state or select \"force.\"");
}
public void markAsVm(String name, String resourcePool, String cluster) throws VSphereException{
try{
VirtualMachine vm = getVmByName(name);
if(vm.getConfig().template){
vm.markAsVirtualMachine(
getResourcePoolByName(resourcePool, getClusterByName(cluster)),
null
);
}
}catch(Exception e){
throw new VSphereException("Could not convert to VM", e);
}
}
/**
* Shortcut
*
* @param vm - VirtualMachine name of which IP is returned
* @return - String containing IP address
* @throws VSphereException
*/
public String getIp(VirtualMachine vm, int timeout) throws VSphereException {
if (vm==null)
throw new VSphereException("VM is null");
//Determine how many attempts will be made to fetch the IP address
final int waitSeconds = 5;
final int maxTries;
if (timeout<=waitSeconds)
maxTries = 1;
else
maxTries = (int) Math.round((double)timeout / waitSeconds);
for(int count=0; count<maxTries; count++){
//get IP
if(vm.getGuest().getIpAddress()!=null){
return vm.getGuest().getIpAddress();
}
try {
//wait
Thread.sleep(waitSeconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
/**
* @param vmName - name of VM object to retrieve
* @return - VirtualMachine object
* @throws InvalidProperty
* @throws RuntimeFault
* @throws RemoteException
* @throws MalformedURLException
* @throws VSphereException
*/
public VirtualMachine getVmByName(String vmName) throws VSphereException {
try {
return (VirtualMachine) new InventoryNavigator(
getServiceInstance().getRootFolder()).searchManagedEntity(
"VirtualMachine", vmName);
} catch (Exception e) {
throw new VSphereException(e);
}
}
private Datastore getDatastoreByName(final String datastoreName, ManagedEntity rootEntity) throws RemoteException, MalformedURLException {
if (rootEntity == null) {
rootEntity = getServiceInstance().getRootFolder();
}
Datastore datastore = (Datastore) new InventoryNavigator(rootEntity).searchManagedEntity("Datastore", datastoreName);
if (datastore != null) {
return datastore;
}
if (rootEntity == null || !(rootEntity instanceof ClusterComputeResource)) {
return null;
}
// try to fetch data store directly from cluster if above approach doesn't work
ClusterComputeResource clusterResource = (ClusterComputeResource) rootEntity;
for (Datastore dataStore : clusterResource.getDatastores()) {
if (dataStore.getName().equals(datastoreName)) {
return dataStore;
}
}
return null;
}
/**
* @return - ManagedEntity array of Datastore
* @throws InvalidProperty
* @throws RuntimeFault
* @throws RemoteException
* @throws MalformedURLException
* @throws VSphereException
*/
public ManagedEntity[] getDatastores() throws VSphereException {
try {
return new InventoryNavigator(
getServiceInstance().getRootFolder()).searchManagedEntities(
"Datastore");
} catch (Exception e) {
throw new VSphereException(e);
}
}
/**
* @param poolName - Name of pool to use
* @return - ResourcePool object
* @throws InvalidProperty
* @throws RuntimeFault
* @throws RemoteException
* @throws MalformedURLException
* @throws VSphereException
*/
private ResourcePool getResourcePoolByName(final String poolName, ManagedEntity rootEntity) throws InvalidProperty, RuntimeFault, RemoteException, MalformedURLException {
if (rootEntity==null) rootEntity=getServiceInstance().getRootFolder();
return (ResourcePool) new InventoryNavigator(
rootEntity).searchManagedEntity(
"ResourcePool", poolName);
}
/**
* @param clusterName - Name of cluster name to find
* @param rootEntity - managed entity to search
* @return - ClusterComputeResource object
* @throws InvalidProperty
* @throws RuntimeFault
* @throws RemoteException
* @throws MalformedURLException
* @throws VSphereException
*/
private ClusterComputeResource getClusterByName(final String clusterName, ManagedEntity rootEntity) throws InvalidProperty, RuntimeFault, RemoteException, MalformedURLException {
if (rootEntity==null) rootEntity=getServiceInstance().getRootFolder();
return (ClusterComputeResource) new InventoryNavigator(
rootEntity).searchManagedEntity(
"ClusterComputeResource", clusterName);
}
/**
* @param clusterName - Name of cluster name to find
* @return - ClusterComputeResource object
* @throws InvalidProperty
* @throws RuntimeFault
* @throws RemoteException
* @throws MalformedURLException
* @throws VSphereException
*/
private ClusterComputeResource getClusterByName(final String clusterName) throws InvalidProperty, RuntimeFault, RemoteException, MalformedURLException {
return getClusterByName(clusterName, null);
}
/**
* Detroys the VM in vSphere
* @param name - VM object to destroy
* @throws VSphereException
*/
public void destroyVm(String name, boolean failOnNoExist) throws VSphereException{
try{
VirtualMachine vm = getVmByName(name);
if(vm==null){
if(failOnNoExist) throw new VSphereException("VM does not exist");
System.out.println("VM does not exist, or already deleted!");
return;
}
if(!vm.getConfig().template) {
powerOffVm(vm, true, false);
}
String status = vm.destroy_Task().waitForTask();
if(status.equals(Task.SUCCESS))
{
System.out.println("VM was deleted successfully.");
return;
}
}catch(Exception e){
throw new VSphereException(e.getMessage());
}
throw new VSphereException("Could not delete VM!");
}
/**
* Renames a VM Snapshot
* @param oldName the current name of the vm
* @param newName the new name of the vm
* @param newDescription the new description of the vm
* @throws VSphereException
*/
public void renameVmSnapshot(String vmName, String oldName, String newName, String newDescription) throws VSphereException{
try{
VirtualMachine vm = getVmByName(vmName);
if(vm==null){
throw new VSphereException("VM does not exist");
}
VirtualMachineSnapshot snapshot = getSnapshotInTree(vm, oldName);
snapshot.renameSnapshot(newName, newDescription);
System.out.println("VM Snapshot was renamed successfully.");
return;
}catch(Exception e){
throw new VSphereException(e.getMessage());
}
}
/**
* Renames the VM vSphere
* @param oldName the current name of the vm
* @param newName the new name of the vm
* @throws VSphereException
*/
public void renameVm(String oldName, String newName) throws VSphereException{
try{
VirtualMachine vm = getVmByName(oldName);
if(vm==null){
throw new VSphereException("VM does not exist");
}
String status = vm.rename_Task(newName).waitForTask();
if(status.equals(Task.SUCCESS))
{
System.out.println("VM was renamed successfully.");
return;
}
}catch(Exception e){
throw new VSphereException(e.getMessage());
}
throw new VSphereException("Could not rename VM!");
}
private boolean isSuspended(VirtualMachine vm){
return (vm.getRuntime().getPowerState() == VirtualMachinePowerState.suspended);
}
private boolean isPoweredOn(VirtualMachine vm){
return (vm.getRuntime().getPowerState() == VirtualMachinePowerState.poweredOn);
}
private boolean isPoweredOff(VirtualMachine vm){
return (vm.getRuntime() != null && vm.getRuntime().getPowerState() == VirtualMachinePowerState.poweredOff);
}
public boolean vmToolIsEnabled(VirtualMachine vm) {
VirtualMachineToolsStatus status = vm.getGuest().toolsStatus;
return ((status == VirtualMachineToolsStatus.toolsOk) || (status == VirtualMachineToolsStatus.toolsOld));
}
public void powerOffVm(VirtualMachine vm, boolean evenIfSuspended, boolean shutdownGracefully) throws VSphereException{
if(vm.getConfig().template)
throw new VSphereException("VM represents a template!");
if (isPoweredOn(vm) || (evenIfSuspended && isSuspended(vm))) {
boolean doHardShutdown = true;
String status;
try {
if (!isSuspended(vm) && shutdownGracefully && vmToolIsEnabled(vm)) {
System.out.println("Requesting guest shutdown");
vm.shutdownGuest();
// Wait for up to 180 seconds for a shutdown - then shutdown hard.
for (int i = 0; i <= 180; i++) {
Thread.sleep(1000);
if (isPoweredOff(vm)) {
doHardShutdown = false;
System.out.println("VM gracefully powered down successfully.");
return;
}
}
}
if (doHardShutdown) {
System.out.println("Powering off the VM");
status = vm.powerOffVM_Task().waitForTask();
if(status.equals(Task.SUCCESS)) {
System.out.println("VM was powered down successfully.");
return;
}
}
} catch (Exception e) {
throw new VSphereException(e);
}
}
else if (isPoweredOff(vm)){
System.out.println("Machine is already off.");
return;
}
throw new VSphereException("Machine could not be powered down!");
}
public void suspendVm(VirtualMachine vm) throws VSphereException{
if (isPoweredOn(vm)) {
String status;
try {
//TODO is this better?
//vm.shutdownGuest()
status = vm.suspendVM_Task().waitForTask();
} catch (Exception e) {
throw new VSphereException(e);
}
if(Task.SUCCESS.equals(status)) {
System.out.println("VM was suspended successfully.");
return;
}
}
else {
System.out.println("Machine not powered on.");
return;
}
throw new VSphereException("Machine could not be suspended!");
}
private void logMessage(PrintStream jLogger, String message) {
if (jLogger != null) {
VSphereLogger.vsLogger(jLogger, message);
} else {
System.out.println(message);
}
}
}