-
Notifications
You must be signed in to change notification settings - Fork 60
/
vm_power_ops.py
144 lines (115 loc) · 4.2 KB
/
vm_power_ops.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
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
'''
Copyright 2013-2014 Reubenur Rahman
All Rights Reserved
@author: [email protected]
'''
import atexit
import argparse
import sys
import time
from pyVmomi import vim, vmodl
from pyVim import connect
from pyVim.connect import Disconnect, SmartConnect, GetSi
inputs = {'vcenter_ip': '10.10.10.211',
'vcenter_password': 'Password123',
'vcenter_user': 'Administrator',
'vm_name' : 'reuben-test',
#Start, Stop(force), Suspend(force)
'operation' : 'suspend',
'force' : True,
}
def get_obj(content, vimtype, name):
"""
Get the vsphere object associated with a given text name
"""
obj = None
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return obj
def wait_for_task(task, raiseOnError=True, si=None, pc=None):
if si is None:
si = GetSi()
if pc is None:
sc = si.RetrieveContent()
pc = sc.propertyCollector
# First create the object specification as the task object.
objspec = vmodl.Query.PropertyCollector.ObjectSpec()
objspec.SetObj(task)
# Next, create the property specification as the state.
propspec = vmodl.Query.PropertyCollector.PropertySpec()
propspec.SetType(vim.Task);
propspec.SetPathSet(["info.state"]);
propspec.SetAll(True)
# Create a filter spec with the specified object and property spec.
filterspec = vmodl.Query.PropertyCollector.FilterSpec()
filterspec.SetObjectSet([objspec])
filterspec.SetPropSet([propspec])
# Create the filter
filter = pc.CreateFilter(filterspec, True)
# Loop looking for updates till the state moves to a completed state.
taskName = task.GetInfo().GetName()
update = pc.WaitForUpdates(None)
state = task.GetInfo().GetState()
while state != vim.TaskInfo.State.success and \
state != vim.TaskInfo.State.error:
if (state == 'running') and (taskName.info.name != "Destroy"):
# check to see if VM needs to ask a question, thow exception
vm = task.GetInfo().GetEntity()
if vm is not None and isinstance(vm, vim.VirtualMachine):
qst = vm.GetRuntime().GetQuestion()
if qst is not None:
raise Exception("Task blocked, User Intervention required")
update = pc.WaitForUpdates(update.GetVersion())
state = task.GetInfo().GetState()
filter.Destroy()
if state == "error" and raiseOnError:
raise task.GetInfo().GetError()
return state
def invoke_and_track(func, *args, **kw):
try :
task = func(*args, **kw)
wait_for_task(task)
except:
raise
def main():
try:
si = None
try:
print "Trying to connect to VCENTER SERVER . . ."
si = connect.Connect(inputs['vcenter_ip'], 443, inputs['vcenter_user'], inputs['vcenter_password'])
except IOError, e:
pass
atexit.register(Disconnect, si)
print "Connected to VCENTER SERVER !"
content = si.RetrieveContent()
if inputs['operation'] == 'stop' or inputs['operation'] == 'suspend':
force = inputs['force']
vm = get_obj(content, [vim.VirtualMachine], inputs['vm_name'])
#current_state = vm.runtime.powerState
if inputs['operation'] == 'start':
invoke_and_track(vm.PowerOn, None)
elif inputs['operation'] == 'stop':
if not force:
invoke_and_track(vm.ShutdownGuest)
else:
invoke_and_track(vm. PowerOff)
elif inputs['operation'] == 'suspend':
if not force:
invoke_and_track(vm.StandbyGuest)
else:
invoke_and_track(vm. Suspend)
#wait_for_task(task, si)
except vmodl.MethodFault, e:
print "Caught vmodl fault: %s" % e.msg
return 1
except Exception, e:
if str(e) == 'Query':
return 1
print "Caught exception: %s" % str(e)
return 1
# Start program
if __name__ == "__main__":
main()