-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansible.py
executable file
·103 lines (88 loc) · 3.61 KB
/
ansible.py
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
from config import *
from schema import *
from flask import jsonify, Response
@app.route("/ansible/list")
def ansible_list():
ansible_groups=dict()
for role in Role.query.all():
ansible_group=dict()
ansible_group["hosts"]=[]
for host in Host.query.filter(Host.role_id==role.id):
ansible_group["hosts"].append(host.name)
ansible_group["vars"]={"smi_role":role.name}
ansible_group["children"]=[]
ansible_groups[role.name]=ansible_group
for group in Group.query.all():
ansible_group=dict()
ansible_group["hosts"]=[]
for membership in HostGroupMemebership.query.filter(HostGroupMemebership.group_id==group.id):
ansible_group["hosts"].append(membership.host.name)
ansible_group["vars"]={"smi_group":group.name}
ansible_group["children"]=[]
ansible_groups[group.name]=ansible_group
for system in System.query.all():
ansible_group=dict()
ansible_group["hosts"]=[]
for host in Host.query.filter(Host.system_id==system.id):
ansible_group["hosts"].append(host.name)
ansible_group["vars"]={"smi_system":system.name}
ansible_group["children"]=[]
ansible_groups[system.name]=ansible_group
return jsonify(ansible_groups)
@app.route("/ansible/host/<hostname>")
def ansible_host(hostname):
ansible_vars = dict()
host = Host.query.filter(Host.name==hostname).first()
loc = Location.query.filter(Location.id==host.location_id).first()
disks = Disk.query.filter(Disk.host_id==host.id)
if disks.count()>0:
primary_disk_size=disks.first().size
else:
primary_disk_size=0
if not host:
return "HTTP/404 Not Found", 404
if host.ansible_vars:
for ansible_var in host.ansible_vars.split(" "):
if len(ansible_var.split("="))==2: # yes, this is weak but I'm not gonna use yaml/json in frontend to store key-value pairs... or maybe I am but not right now
ansible_vars[ansible_var.split("=")[0]]=ansible_var.split("=")[1]
else:
ansible_vars[ansible_var]=None
ansible_vars['ansible_host']=host.ip
ansible_vars['cpu']=host.cpu
ansible_vars['mem']=host.ram
ansible_vars['dsk']=primary_disk_size
ansible_vars['hv'] =loc.hypervisor
return jsonify(ansible_vars)
@app.route("/ansible/inventory.txt")
def ansible_inventory():
inventory="# Generated by SMI\n"
inventory+="\n# Hosts\n\n"
for host in Host.query.all():
loc = Location.query.filter(Location.id==host.location_id).first()
disks = Disk.query.filter(Disk.host_id==host.id)
if disks.count()>0:
primary_disk_size=disks.first().size
else:
primary_disk_size=0
inventory+=("%s ansible_host=%s cpu=%d mem=%d dsk=%d hv=%s \n"%(host.name, host.ip, host.cpu, host.ram, primary_disk_size, loc.hypervisor))
inventory+="\n## SMI Roles\n\n"
for role in Role.query.all():
inventory+=("[%s]\n"%(role.name))
for host in Host.query.filter(Host.role_id==role.id):
inventory+=("%s smi_role=%s\n"%(host.name, role.name))
inventory+="\n"
inventory+="\n## SMI Groups\n\n"
for group in Group.query.all():
inventory+=("[%s]\n"%(group.name))
for membership in HostGroupMemebership.query.filter(HostGroupMemebership.group_id==group.id):
if membership.group_id==group.id:
host=Host.query.filter(Host.id==membership.host_id).first()
inventory+=("%s smi_group=%s\n"%(host.name, group.name))
inventory+="\n"
inventory+="\n## SMI Systems\n\n"
for system in System.query.all():
inventory+=("[%s]\n"%(system.name))
for host in Host.query.filter(Host.system_id==system.id):
inventory+=("%s smi_system=%s\n"%(host.name, system.name))
inventory+="\n"
return Response(inventory, mimetype='text/plain')