-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestroyinstance.py
More file actions
executable file
·50 lines (38 loc) · 1.11 KB
/
destroyinstance.py
File metadata and controls
executable file
·50 lines (38 loc) · 1.11 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
#!/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)
vmname = sys.argv[1]
args = {
'name': vmname,
'projectid': k.proj_testautomation
}
vms = cloudstack.listVirtualMachines(args)
for vm in vms:
print "Destroying %s with id %s in project %s." % (vm['name'], vm['id'], vm['project'])
try:
job = cloudstack.destroyVirtualMachine({'id': vm['id']})
except HTTPError, e:
print e
jobid = job['jobid']
asyncargs = {
'jobid': jobid
}
async = cloudstack.queryAsyncJobResult(asyncargs)
ready = async['jobstatus']
print '\nDestroying 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 '\nProject %s instance %s destroyed.' % (vmdetails['project'], vmdetails['displayname'])
print '\n'