-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathcreate_random_marvel_vms.py
More file actions
executable file
·227 lines (191 loc) · 8.42 KB
/
create_random_marvel_vms.py
File metadata and controls
executable file
·227 lines (191 loc) · 8.42 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
# Author: William Lam
# Website: www.williamlam.com
# Product: VMware vSphere
# Description: vSphere SDK for Python (pyvmomi) program for creating tiny VMs (1vCPU/128MB) with random names using the Marvel Comics API
# Reference: http://www.williamlam.com/2014/02/having-some-fun-with-marvel-comics-api.html
"""
vSphere SDK for Python program for creating tiny VMs (1vCPU/128MB) with random names using the Marvel Commics API
"""
from optparse import OptionParser, make_option
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, VmomiSupport
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from pyVmomi import vmodl
from pyVmomi import vim
from pprint import pprint
import requests
import json
import time
import hashlib
import random
import argparse
import atexit
import sys
import string
import ssl
# Marvel API keys
marvel_public_key = ''
marvel_private_key = ''
def GetArgs():
"""
Supports the command-line arguments listed below.
"""
parser = argparse.ArgumentParser(
description='Process args for retrieving all the Virtual Machines')
parser.add_argument('-s', '--host', required=True,
action='store', help='Remote host to connect to')
parser.add_argument('-o', '--port', type=int, default=443,
action='store', help='Port to connect on')
parser.add_argument('-u', '--user', required=True, action='store',
help='User name to use when connecting to host')
parser.add_argument('-p', '--password', required=True,
action='store', help='Password to use when connecting to host')
parser.add_argument('-c', '--count', required=True,
action='store', help='Number of VMs to create')
parser.add_argument('-d', '--datastore', required=True,
action='store', help='Name of Datastore to create VM in')
parser.add_argument('-e', '--datacenter', required=True,
action='store', help='Name of Datastore to create VM in')
parser.add_argument('-f', '--opid', default='create-marvel-vm',
action='store', help='Name of Datastore to create VM in')
args = parser.parse_args()
return args
def find_datacenter(service_instance, datacenter_name):
content = service_instance.RetrieveContent()
# Traverse the inventory hierarchy starting from the root folder
for child_entity in content.rootFolder.childEntity:
if isinstance(child_entity, vim.Datacenter):
if child_entity.name == datacenter_name:
return child_entity
# Datacenter not found
return None
def getMarvelCharacters(number_of_characters):
timestamp = str(int(time.time()))
# hash is required as part of request which is md5(timestamp + private + public key)
hash_value = hashlib.md5(
(timestamp + marvel_private_key + marvel_public_key).encode('utf-8')).hexdigest()
characters = []
for x in range(number_of_characters):
# randomly select one of the 1402 Marvel character
offset = random.randrange(1, 1402)
limit = '1'
# GET /v1/public/characters
url = 'http://gateway.marvel.com:80/v1/public/characters?limit=' + limit + '&offset=' + \
str(offset) + '&apikey=' + marvel_public_key + \
'&ts=' + timestamp + '&hash=' + hash_value
headers = {'content-type': 'application/json'}
request = requests.get(url, headers=headers)
data = json.loads(request.content)
# retrieve character name & replace spaces with underscore so we don't have stupid spaces in our VM names
character = 'marvelvm-' + \
data['data']['results'][0]['name'].strip().replace(' ', '_')
characters.append(character.lower())
return characters
def CreateDummyVM(name, si, vmFolder, rp, datastore):
datastorePath = f"[{datastore}]"
# bare minimum VM shell, no disks. Feel free to edit
file = vim.vm.FileInfo(logDirectory=None, snapshotDirectory=None,
suspendDirectory=None, vmPathName=datastorePath)
config = vim.vm.ConfigSpec(
name=name, memoryMB=128, numCPUs=1, files=file, guestId='dosGuest', version='vmx-13')
print("Creating VM " + name + " ...")
task = vmFolder.CreateVM_Task(config=config, pool=rp)
WaitForTasks([task], si)
# borrowed from poweronvm.py sample
def WaitForTasks(tasks, si):
"""
Given the service instance si and tasks, it returns after all the
tasks are complete
"""
pc = si.content.propertyCollector
taskList = [str(task) for task in tasks]
# Create filter
objSpecs = [vmodl.query.PropertyCollector.ObjectSpec(obj=task)
for task in tasks]
propSpec = vmodl.query.PropertyCollector.PropertySpec(type=vim.Task, pathSet=[], all=True)
filterSpec = vmodl.query.PropertyCollector.FilterSpec()
filterSpec.objectSet = objSpecs
filterSpec.propSet = [propSpec]
filter = pc.CreateFilter(filterSpec, True)
try:
version, state = None, None
# Loop looking for updates till the state moves to a completed state.
while len(taskList):
update = pc.WaitForUpdates(version)
for filterSet in update.filterSet:
for objSet in filterSet.objectSet:
task = objSet.obj
for change in objSet.changeSet:
if change.name == 'info':
state = change.val.state
elif change.name == 'info.state':
state = change.val
else:
continue
if not str(task) in taskList:
continue
if state == vim.TaskInfo.State.success:
# Remove task from taskList
taskList.remove(str(task))
elif state == vim.TaskInfo.State.error:
raise task.info.error
# Move to next version
version = update.version
finally:
if filter:
filter.Destroy()
def main():
"""
Simple command-line program for creating Dummy VM based on Marvel character names
"""
args = GetArgs()
# Set custom OpID for this connection
reqCtx = VmomiSupport.GetRequestContext()
reqCtx["operationID"] = args.opid
try:
context = None
try:
if sys.version_info[:3] > (2, 7, 8):
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
# Disabling the annoying InsecureRequestWarning message
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
si = SmartConnect(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port),
sslContext=context)
except IOError as e:
pass
if not si:
print(
"Could not connect to the specified host using specified username and password")
return -1
atexit.register(Disconnect, si)
content = si.RetrieveContent()
dc = find_datacenter(si, args.datacenter)
vmFolder = dc.vmFolder
hosts = dc.hostFolder.childEntity
rp = hosts[0].resourcePool
# Ensure user sets up Marvel API keys
if marvel_public_key == '' or marvel_private_key == '':
print("\nPlease configure your Marvel Public/Private API Key by setting marvel_public_key and marvel_private_key variable\n")
print("Generating random VM name for now ...")
characters = ['randomvm-' + ''.join(random.choice(string.ascii_letters)
for _ in range(10)) for _ in range(int(args.count))]
else:
print("Connecting to Marvel API and retrieving " + args.count + " random character(s) ...")
characters = getMarvelCharacters(int(args.count))
for name in characters:
CreateDummyVM(name, si, vmFolder, rp, args.datastore)
except vmodl.MethodFault as e:
print("Caught vmodl fault : " + e.msg)
return -1
except Exception as e:
print("Caught exception : " + str(e))
return -1
return 0
# Start program
if __name__ == "__main__":
main()