-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent-hosting-ha-router.py
executable file
·116 lines (107 loc) · 4.33 KB
/
agent-hosting-ha-router.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
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Usage:
#
# $ export OS_XXXXX
# ...
#
# $ ./agent-hosting-ha-router.py <[json]|yaml|yml|table>
#
# >json
#
# >yaml|yml
#
# >table (requires terminaltables)
# +--------------------+--------------------------------------+----------------------------------+---------------------+
# | name | id | project | agents |
# +--------------------+--------------------------------------+----------------------------------+---------------------+
# | router3 | 039ae521-0cdb-4b61-b7a5-97e333dcb35b | 68a93cc709b44de08cfd11e6bdac2b9b | - ha_state: active |
# | | | | host: osnet001 |
# | | | | - ha_state: standby |
# | | | | host: osnet002 |
# | | | | |
# | router2 | 97d2ab1d-0cec-49d5-856f-0a1a3c9a5156 | 68a93cc709b44de08cfd11e6bdac2b9b | - ha_state: active |
# | | | | host: osnet001 |
# | | | | - ha_state: standby |
# | | | | host: osnet002 |
# | | | | |
# | router1 | f1c23964-7025-4ded-ab14-992f636b3485 | 8f77be9ac1ef49b6ad033e84000ec182 | - ha_state: null |
# | | | | host: osnet001 |
# +--------------------+--------------------------------------+----------------------------------+---------------------+
import sys
import json
import yaml
try:
from terminaltables import AsciiTable
NO_TABLE=False
except ImportError:
NO_TABLE=True
pass
try:
import openstack
except ImportError:
print('[ERR] `openstacksdk` required')
sys.exit(1)
try:
CONN = openstack.connect()
except Exception as ex:
print('[ERR] Probably missing OS_* environment variables: %s' % ex)
def list_routers_on_l3_agents():
res = []
for rt in CONN.list_routers():
it = {
'router': {
'id': rt.id,
'name': rt.name,
'project': rt.project_id,
'active': 0
},
'agents': []
}
ags = CONN.network.routers_hosting_l3_agents(rt)
for ag in ags:
# HA States: ['active', 'standby', 'standalone']
ha_state = 'standalone'
if ag.ha_state is not None :
ha_state = ag.ha_state
if ha_state in ['active', 'standalone']:
it['router']['active'] += 1
it['agents'].append({
'host': ag.host,
'ha_state': ha_state,
})
res.append(it)
return res
def main(output):
if output == 'json':
print(json.dumps(list_routers_on_l3_agents()))
elif output in ['yaml', 'yml']:
print(yaml.safe_dump(list_routers_on_l3_agents()))
elif output == 'table':
if NO_TABLE:
print('[ERR] Cannot use table rendre, missing `terminaltables`')
sys.exit(1)
tbl = [
['name', 'id', 'project', 'active', 'agents'],
]
for line in list_routers_on_l3_agents():
tbl.append([
line['router']['name'],
line['router']['id'],
line['router']['project'],
line['router']['active'],
yaml.safe_dump(line['agents'])
])
table = AsciiTable(tbl)
print(table.table)
else:
print('[ERR] %s is not a valid output. Choices: [table], json, yaml|yml' % output)
sys.exit(1)
if __name__ == '__main__':
if not NO_TABLE:
output = 'table'
else:
output = 'json'
if len(sys.argv) > 1:
output = sys.argv[1]
main(output)