From 22382726fa69f40c74611a79b99845df1bd3076f Mon Sep 17 00:00:00 2001 From: Andre keedy Date: Wed, 3 Feb 2016 15:42:05 -0500 Subject: [PATCH 1/3] Add rackHd inventory script - Allow ansible to get hosts inventory from rackHD by node id --- contrib/inventory/rackhd.py | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 contrib/inventory/rackhd.py diff --git a/contrib/inventory/rackhd.py b/contrib/inventory/rackhd.py new file mode 100755 index 0000000000..f27fd5905e --- /dev/null +++ b/contrib/inventory/rackhd.py @@ -0,0 +1,73 @@ +#!/usr/bin/python +import json +import requests +import os +import argparse +import types + +MONORAIL_URL = 'http://localhost:8080' + +class OnRackInventory(object): + def __init__(self, nodeids): + self._inventory = {} + for nodeid in nodeids: + self._load_inventory_data(nodeid) + output = '{\n' + for nodeid,info in self._inventory.iteritems(): + output += self._format_output(nodeid, info) + output += ',\n' + output = output[:-2] + output += '}\n' + print output + + def _load_inventory_data(self, nodeid): + info = {} + info['ohai'] = MONORAIL_URL + '/api/common/nodes/{0}/catalogs/ohai'.format(nodeid ) + info['lookup'] = MONORAIL_URL + '/api/common/lookups/?q={0}'.format(nodeid) + + results = {} + for key,url in info.iteritems(): + r = requests.get( url, verify=False) + results[key] = r.text + + self._inventory[nodeid] = results + + def _format_output(self, nodeid, info): + output = '' + try: + node_info = json.loads(info['lookup']) + ipaddress = '' + if len(node_info) > 0: + ipaddress = node_info[0]["ipAddress"] + output += ' "' + nodeid + '" : {\n' + output += ' "hosts": [ "' + ipaddress + '" ],\n' + output += ' "vars" : {\n' + for key,result in info.iteritems(): + output += ' "' + key + '": ' + json.dumps(json.loads(result), sort_keys=True, indent=2) + ',\n' + output += ' "ansible_ssh_user": "renasar"\n' + output += ' }\n' + output += ' }\n' + except KeyError: + pass + return output + +try: + #check if monorail url(ie:10.1.1.45:8080) is specified in the environment + MONORAIL_URL = 'http://' + str(os.environ['MONORAIL']) +except: + #use default values + pass + +# Use the nodeid specified in the environment to limit the data returned +# or return data for all available nodes +nodeids = [] +try: + nodeids += os.environ['nodeid'].split(',') +except KeyError: + url = MONORAIL_URL + '/api/common/nodes' + r = requests.get( url, verify=False) + data = json.loads(r.text) + for entry in data: + if entry['type'] == 'compute': + nodeids.append(entry['id']) +OnRackInventory(nodeids) From 083530d8faf00c84b49c3a5cff15d5d9706d4076 Mon Sep 17 00:00:00 2001 From: Andre keedy Date: Wed, 3 Feb 2016 17:00:58 -0500 Subject: [PATCH 2/3] Fix erros --- contrib/inventory/rackhd.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/contrib/inventory/rackhd.py b/contrib/inventory/rackhd.py index f27fd5905e..df74eb332b 100755 --- a/contrib/inventory/rackhd.py +++ b/contrib/inventory/rackhd.py @@ -5,9 +5,9 @@ import os import argparse import types -MONORAIL_URL = 'http://localhost:8080' +RACKHD_URL = 'http://localhost:8080' -class OnRackInventory(object): +class RackhdInventory(object): def __init__(self, nodeids): self._inventory = {} for nodeid in nodeids: @@ -18,12 +18,12 @@ class OnRackInventory(object): output += ',\n' output = output[:-2] output += '}\n' - print output + print (output) def _load_inventory_data(self, nodeid): info = {} - info['ohai'] = MONORAIL_URL + '/api/common/nodes/{0}/catalogs/ohai'.format(nodeid ) - info['lookup'] = MONORAIL_URL + '/api/common/lookups/?q={0}'.format(nodeid) + info['ohai'] = RACKHD_URL + '/api/common/nodes/{0}/catalogs/ohai'.format(nodeid ) + info['lookup'] = RACKHD_URL + '/api/common/lookups/?q={0}'.format(nodeid) results = {} for key,url in info.iteritems(): @@ -52,8 +52,8 @@ class OnRackInventory(object): return output try: - #check if monorail url(ie:10.1.1.45:8080) is specified in the environment - MONORAIL_URL = 'http://' + str(os.environ['MONORAIL']) + #check if rackhd url(ie:10.1.1.45:8080) is specified in the environment + RACKHD_URL = 'http://' + str(os.environ['RACKHD_URL']) except: #use default values pass @@ -64,10 +64,10 @@ nodeids = [] try: nodeids += os.environ['nodeid'].split(',') except KeyError: - url = MONORAIL_URL + '/api/common/nodes' + url = RACKHD_URL + '/api/common/nodes' r = requests.get( url, verify=False) data = json.loads(r.text) for entry in data: if entry['type'] == 'compute': nodeids.append(entry['id']) -OnRackInventory(nodeids) +RackhdInventory(nodeids) From e46074c79132e0181b5284642f8ad03faf6209ff Mon Sep 17 00:00:00 2001 From: Andre keedy Date: Thu, 4 Feb 2016 16:58:10 -0500 Subject: [PATCH 3/3] Address comments --- contrib/inventory/rackhd.py | 57 ++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/contrib/inventory/rackhd.py b/contrib/inventory/rackhd.py index df74eb332b..e7db24d320 100755 --- a/contrib/inventory/rackhd.py +++ b/contrib/inventory/rackhd.py @@ -1,4 +1,5 @@ #!/usr/bin/python + import json import requests import os @@ -12,13 +13,8 @@ class RackhdInventory(object): self._inventory = {} for nodeid in nodeids: self._load_inventory_data(nodeid) - output = '{\n' for nodeid,info in self._inventory.iteritems(): - output += self._format_output(nodeid, info) - output += ',\n' - output = output[:-2] - output += '}\n' - print (output) + print(json.dumps(self._format_output(nodeid, info))) def _load_inventory_data(self, nodeid): info = {} @@ -29,28 +25,29 @@ class RackhdInventory(object): for key,url in info.iteritems(): r = requests.get( url, verify=False) results[key] = r.text - self._inventory[nodeid] = results def _format_output(self, nodeid, info): - output = '' try: node_info = json.loads(info['lookup']) ipaddress = '' if len(node_info) > 0: - ipaddress = node_info[0]["ipAddress"] - output += ' "' + nodeid + '" : {\n' - output += ' "hosts": [ "' + ipaddress + '" ],\n' - output += ' "vars" : {\n' + ipaddress = node_info[0]['ipAddress'] + output = {nodeid:{ 'hosts':[ipaddress],'vars':{}}} for key,result in info.iteritems(): - output += ' "' + key + '": ' + json.dumps(json.loads(result), sort_keys=True, indent=2) + ',\n' - output += ' "ansible_ssh_user": "renasar"\n' - output += ' }\n' - output += ' }\n' + output[nodeid]['vars'][key] = json.loads(result) + output[nodeid]['vars']['ansible_ssh_user'] = 'monorail' except KeyError: pass return output + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--host') + parser.add_argument('--list', action='store_true') + return parser.parse_args() + try: #check if rackhd url(ie:10.1.1.45:8080) is specified in the environment RACKHD_URL = 'http://' + str(os.environ['RACKHD_URL']) @@ -61,13 +58,21 @@ except: # Use the nodeid specified in the environment to limit the data returned # or return data for all available nodes nodeids = [] -try: - nodeids += os.environ['nodeid'].split(',') -except KeyError: - url = RACKHD_URL + '/api/common/nodes' - r = requests.get( url, verify=False) - data = json.loads(r.text) - for entry in data: - if entry['type'] == 'compute': - nodeids.append(entry['id']) -RackhdInventory(nodeids) + +if (parse_args().host): + try: + nodeids += parse_args().host.split(',') + RackhdInventory(nodeids) + except: + pass +if (parse_args().list): + try: + url = RACKHD_URL + '/api/common/nodes' + r = requests.get( url, verify=False) + data = json.loads(r.text) + for entry in data: + if entry['type'] == 'compute': + nodeids.append(entry['id']) + RackhdInventory(nodeids) + except: + pass