This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fuel.py
executable file
·94 lines (79 loc) · 2.8 KB
/
fuel.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
#! /usr/bin/env python
import sys
import os
import subprocess
import optparse
import json
from collections import defaultdict
from ConfigParser import ConfigParser
fuel = "/usr/bin/fuel"
fuel_cmd = "{0} node --json".format(fuel)
inventory_path = os.path.dirname(os.path.realpath(__file__))
inventory_ini = inventory_path + os.path.sep + 'fuel.ini'
inventory_cfg = {
'skip_deleting': False,
'skip_offline': False,
'skip_deploying': False,
}
def _read_config():
if not os.path.exists(inventory_ini):
return
c = ConfigParser()
c.read(inventory_ini)
if not c.has_section('fuel'):
return
for option in ('skip_deleting', 'skip_offline', 'skip_deploying'):
if c.has_option('fuel', option):
inventory_cfg[option] = c.getboolean('fuel', option)
def _listnodes():
p = subprocess.Popen(fuel_cmd.split(), stdout=subprocess.PIPE).stdout
return json.load(p)
def fuel_inventory():
inventory = defaultdict(list)
inventory['_meta'] = {
'hostvars': {},
}
for node in _listnodes():
# skip deleting, offline, deploying and discovering/unprovisioned nodes
if node['pending_deletion'] and inventory_cfg['skip_deleting']:
continue
if not node['online'] and inventory_cfg['skip_offline']:
continue
if node['status'] == 'deploying' and inventory_cfg['skip_deploying']:
continue
if node['status'] == 'discover':
continue
hostname = node['name']
cluster_id = node['cluster']
hw_vendor = node['meta']['system']['manufacturer'].lower()
[inventory[role.strip()].append(hostname) for role in node['roles'].split(",")]
inventory["cluster-{0}".format(cluster_id)].append(hostname)
inventory["hw-{0}-servers".format(hw_vendor)].append(hostname)
nodemeta = {
'online': node['online'],
'os_platform': node['os_platform'],
'status': node['status'],
'ip': node['ip'],
'mac': node['mac'],
'cluster' : cluster_id,
'ansible_ssh_host': node['ip']
}
inventory["node-{}".format(node['id'])].append(hostname)
inventory['_meta']['hostvars'][hostname] = nodemeta
return inventory
if __name__ == '__main__':
if not os.path.exists(fuel):
print json.dumps({})
sys.exit(1)
cli_parser = optparse.OptionParser()
cli_parser.add_option('--list', action='store_true')
cli_parser.add_option('--host', action='store', type='string')
(options, args) = cli_parser.parse_args()
_read_config()
inventory = fuel_inventory()
if options.list:
data = json.dumps(inventory)
print data
elif options.host:
data = json.dumps(inventory['_meta']['hostvars'][options.host])
print data