-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistvms.py
More file actions
executable file
·68 lines (54 loc) · 2.11 KB
/
listvms.py
File metadata and controls
executable file
·68 lines (54 loc) · 2.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
import sys
import CloudStack
import time
from termcolor import colored
from urllib2 import urlopen, HTTPError
import cloudconstants as k
# Initialize the API
cloudstack = CloudStack.Client(API_URI, YOUR_API_KEY, YOUR_API_SECRET)
# Fetch all the private cloud projects associated with the API key
projects = cloudstack.listProjects()
# This is so we can format the output to screen better - tokenized field names
header = ['IP Address', 'State', 'Project Name', 'Template', 'Instance', 'Hostname']
# Print the header for the output
print "\nVirtual Machines"
print "============================\n"
print colored(header[0].ljust(15) + header[1].center(11) + header[2].center(23)+ header[3].center(29)+ header[4].center(8)+ header[5].center(37) + '\n', attrs=['bold', 'underline'])
# Iterate through the projects to retrieve all the VMs associated
for project in projects:
args = {'projectid' : project['id']}
vms = cloudstack.listVirtualMachines(args)
# Iterate through each VM in each project and format and print the details to screen
for vm in vms:
for nic in vm['nic']:
vmip=nic['ipaddress']
print "%s" % (vmip).ljust(15),
sys.stdout.flush()
# Color-code the VM status - red if down and green, if up
if vm['state'] == 'Stopped':
print "%s" % colored(vm['state'][:7].center(9), 'yellow', 'on_red'),
sys.stdout.flush()
else:
print "%s" % colored(vm['state'][:7].center(9), 'blue', 'on_green'),
sys.stdout.flush()
# Parse through the service instance and get the basic details
svctype = vm['serviceofferingname'].lower().split()
if 'hourly' in svctype:
t = 'HR'
elif 'daily' in svctype:
t = 'DY'
else:
t = 'UN'
if 'micro' in svctype:
sz = 'MIC'
elif 'small' in svctype:
sz = 'SML'
elif 'medium' in svctype:
sz = 'MED'
elif 'large' in svctype:
sz = 'LRG'
else:
sz = 'UNK'
svcstr = t + '-' + sz
print "%s %s %s %s\n" % (project['name'][:22].ljust(22), vm['templatename'][:28].ljust(28), svcstr.ljust(8), vm['name'])