forked from vmware/pyvmomi-community-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreboot_vm.py
executable file
·72 lines (60 loc) · 2.08 KB
/
reboot_vm.py
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
#!/usr/bin/env python
#
# Written by Michael Rice
# Github: https://github.com/michaelrice
# Website: https://michaelrice.github.io/
# Blog: http://www.errr-online.com/
#
# This code is released under the terms of the Apache 2
# http://www.apache.org/licenses/LICENSE-2.0.html
#
# Example script to reboot a VirtualMachine
import atexit
from pyVim import connect
from tools import cli
from tools import tasks
def setup_args():
"""Adds additional ARGS to allow the vm name or uuid to
be set.
"""
parser = cli.build_arg_parser()
# using j here because -u is used for user
parser.add_argument('-j', '--uuid',
help='UUID of the VirtualMachine you want to reboot.')
parser.add_argument('-n', '--name',
help='DNS Name of the VirtualMachine you want to '
'reboot.')
parser.add_argument('-i', '--ip',
help='IP Address of the VirtualMachine you want to '
'reboot')
my_args = parser.parse_args()
return cli.prompt_for_password(my_args)
ARGS = setup_args()
SI = None
try:
SI = connect.SmartConnect(host=ARGS.host,
user=ARGS.user,
pwd=ARGS.password,
port=ARGS.port)
atexit.register(connect.Disconnect, SI)
except IOError as ex:
pass
if not SI:
raise SystemExit("Unable to connect to host with supplied info.")
VM = None
if ARGS.uuid:
VM = SI.content.searchIndex.FindByUuid(None, ARGS.uuid,
True,
True)
elif ARGS.name:
VM = SI.content.searchIndex.FindByDnsName(None, ARGS.name,
True)
elif ARGS.ip:
VM = SI.content.searchIndex.FindByIp(None, ARGS.ip, True)
if VM is None:
raise SystemExit("Unable to locate VirtualMachine.")
print("Found: {0}".format(VM.name))
print("The current powerState is: {0}".format(VM.runtime.powerState))
TASK = VM.ResetVM_Task()
tasks.wait_for_tasks(SI, [TASK])
print("its done.")