-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcreate_and_remove_snapshot.py
184 lines (144 loc) · 5.74 KB
/
create_and_remove_snapshot.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
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
'''
Copyright 2013-2014 Reubenur Rahman
All Rights Reserved
@author: [email protected]
'''
import atexit
import argparse
import sys
import time
import ssl
from pyVmomi import vim, vmodl
from pyVim import connect
from pyVim.connect import Disconnect, SmartConnect, GetSi
inputs = {'vcenter_ip': '15.10.10.211',
'vcenter_password': 'Password123',
'vcenter_user': 'Administrator',
'vm_name' : 'reuben-test',
#create, remove or list
'operation' : 'create',
'snapshot_name' : 'my_test_snapshot',
'ignore_ssl': 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 get_snapshots(vm):
return get_snapshots_recursively(vm.snapshot.rootSnapshotList, '')
def get_snapshots_recursively(snapshots, snapshot_location):
snapshot_paths = []
if not snapshots:
return snapshot_paths
for snapshot in snapshots:
if snapshot_location:
current_snapshot_path = snapshot_location + '/' + snapshot.name
else:
current_snapshot_path = snapshot.name
snapshot_paths.append(current_snapshot_path)
snapshot_paths = snapshot_paths + get_snapshots_recursively(snapshot.childSnapshotList, current_snapshot_path)
return snapshot_paths
def main():
try:
si = None
try:
print "Trying to connect to VCENTER SERVER . . ."
context = None
if inputs['ignore_ssl']:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_NONE
#si = connect.Connect(args.host, int(args.port), args.user, args.password, service="hostd")
si = connect.Connect(inputs['vcenter_ip'], 443, inputs['vcenter_user'], inputs['vcenter_password'],
sslContext=context)
except IOError, e:
pass
atexit.register(Disconnect, si)
print "Connected to VCENTER SERVER !"
content = si.RetrieveContent()
operation = inputs['operation']
vm_name = inputs['vm_name']
snapshot_name = inputs['snapshot_name']
vm = get_obj(content, [vim.VirtualMachine], vm_name)
if operation == 'create':
description = "Test snapshot"
# Read about dumpMemory : http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html#createSnapshot
dumpMemory = False
quiesce = True
invoke_and_track(vm.CreateSnapshot(snapshot_name, description, dumpMemory, quiesce))
elif operation == 'remove':
snapshots = vm.snapshot.rootSnapshotList
for snapshot in snapshots:
if snapshot_name == snapshot.name:
snap_obj = snapshot.snapshot
print "Removing snapshot ", snap_obj
invoke_and_track(snap_obj.RemoveSnapshot_Task(True))
else:
print "Couldn't find any snapshots"
if operation == 'list':
print 'Display list of snapshots on virtual machine ' + vm.name
snapshot_paths = get_snapshots(vm)
for snapshot_path in snapshot_paths:
print snapshot_path
except vmodl.MethodFault, e:
print "Caught vmodl fault: %s" % e.msg
return 1
except Exception, e:
if str(e).startswith("'vim.Task'"):
return 1
print "Caught exception: %s" % str(e)
return 1
# Start program
if __name__ == "__main__":
main()