-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsmm
executable file
·93 lines (80 loc) · 2.71 KB
/
lsmm
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
#!/usr/bin/python3
import sys, subprocess, json
from tabulate import tabulate
import click
def load_machines(profile, param):
args = []
args.append("maas")
args.append(profile)
args.append("machines")
args.append("read")
for x in param:
args.append(x)
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
output = proc.stdout.read()
try:
machine_json = json.loads(output)
return machine_json
except:
print("couldn't load json correctly; exiting")
sys.exit()
def print_summary(machine_json):
data = []
for x in machine_json:
try:
data.append(
[
x["hostname"] + "\n" + x["system_id"],
str(x["power_state"]) + "\n" + str(x["power_type"]),
x["status_name"] + "\n" + x["hardware_info"]["system_vendor"],
str(x["owner"]) + "\n" + str(x["pool"]["name"]),
x["zone"]["name"],
str(x["boot_interface"]["vlan"]["fabric"])
+ "\n"
+ str(x["boot_interface"]["vlan"]["name"]),
str(x["boot_interface"]["links"][0]["subnet"]["name"])
+ "\n"
+ str(x["interface_set"][0]["mac_address"]),
]
)
except:
continue
output_string = tabulate(
data,
headers=[
"Hostname\nSystem ID",
"Power\nType",
"Status\nVendor",
"Owner\nPool",
"Zone\nSpaces",
"Fabric\nVLAN",
"Subnet\nMAC Address",
],
tablefmt="grid",
)
print(output_string)
@click.command(help="lsmm - list MAAS machines")
@click.option(
"-p", "profile", default="admin", help='MAAS profile name, defaults to "admin"'
)
@click.option("-s", "status", help='Machine status ("new","ready",...); omit for all')
@click.option("-h", "hostname", help="Machine hostname; omit for all")
@click.option("-i", "systemid", help="Machine system ID; omit for all")
@click.option("-z", "zone", help="Machines in zone ZONE; omit for all")
@click.option("-r", "rpool", help="Machines in resources pool RPOOL; omit for all")
def cli(profile, status, hostname, systemid, zone, rpool):
param = []
if status != None:
param.append("status=" + status)
if hostname != None:
param.append("hostname=" + hostname)
if systemid != None:
param.append("id=" + systemid)
if zone != None:
param.append("zone=" + zone)
if rpool != None:
param.append("pool=" + rpool)
machine_json = load_machines(profile, param)
print_summary(machine_json)
if __name__ == "__main__":
cli()