-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateinstance.py
More file actions
executable file
·60 lines (48 loc) · 1.6 KB
/
createinstance.py
File metadata and controls
executable file
·60 lines (48 loc) · 1.6 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
#!/usr/bin/python
import sys
import CloudStack
import time
from termcolor import colored
from time import sleep
from urllib2 import urlopen, HTTPError
import cloudconstants as k
cloudstack = CloudStack.Client(API_URI, YOUR_API_KEY, YOUR_API_SECRET)
projectid = k.proj_testautomation
if len(sys.argv) < 2:
sys.exit('You must provide a valid hostname. Usage: ./createinstance.py hostname')
else:
vmname = sys.argv[1]
vms = cloudstack.listVirtualMachines({'name': vmname, 'projectid': projectid})
if len(vms) > 0:
sys.exit('\nThere is already a VM with this name in this project. Please pick another name.\n')
else:
args = {
'serviceofferingid': k.svc_medium_hourly,
'templateid': k.tmpl_win2k8r2_std,
'zoneid': k.zone_amers1a,
'networkids': k.net_amers1a,
'projectid': projectid,
'name': vmname
}
try:
job = cloudstack.deployVirtualMachine(args)
jobid = job['jobid']
asyncargs = {
'jobid': jobid
}
async = cloudstack.queryAsyncJobResult(asyncargs)
ready = async['jobstatus']
print '\nBuilding instance'
while (ready == 0):
print '\b.',
sys.stdout.flush()
time.sleep(5)
async = cloudstack.queryAsyncJobResult(asyncargs)
ready = async['jobstatus']
result = async['jobresult']
vmdetails = result['virtualmachine']
print "\nInstance created. Instance name is %s. Password = %s" % (vmdetails['name'], vmdetails['password'])
except HTTPError, e:
print e
# print e.__dict__
print "\n"