From cb16385ddbd8a8a58a5e0b036ddb9db72151cc00 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Thu, 29 Nov 2018 15:04:27 -0500 Subject: [PATCH 1/4] Add confluent2ansible Utility that wil take a noderange and generate an appropriate ansible hosts file, including carrying over pertinent groups. --- confluent_client/bin/confluent2ansible | 69 ++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 confluent_client/bin/confluent2ansible diff --git a/confluent_client/bin/confluent2ansible b/confluent_client/bin/confluent2ansible new file mode 100644 index 00000000..de9f7b99 --- /dev/null +++ b/confluent_client/bin/confluent2ansible @@ -0,0 +1,69 @@ +#!/usr/bin/env python +import csv +import optparse +import signal +import sys +import os +try: + signal.signal(signal.SIGPIPE, signal.SIG_DFL) +except AttributeError: + pass + +path = os.path.dirname(os.path.realpath(__file__)) +path = os.path.realpath(os.path.join(path, '..', 'lib', 'python')) +if path.startswith('/opt'): + sys.path.append(path) + +import confluent.client as client +import confluent.sortutil as sortutil + + +def lookupdata(data, key): + ret = data.get(key, {}).get('value', '') + if ret is None: + ret = '' + return ret + +def main(): + argparser = optparse.OptionParser( + usage='''\n %prog noderange -o ansible.hosts + \n ''') + argparser.add_option('-o', '--output', + help='Ansible hosts file') + (options, args) = argparser.parse_args() + try: + noderange = args[0] + except IndexError: + argparser.print_help() + sys.exit(1) + if not options.output: + sys.stderr.write('Output file must be specified by -o\n') + sys.exit(1) + sess = client.Command() + databynode = {} + for res in sess.read('/noderange/{0}/attributes/all'.format(noderange)): + for node in res.get('databynode', {}): + if node not in databynode: + databynode[node] = {} + databynode[node].update(res['databynode'][node]) + nodesbygroup = {} + with open(options.output, 'w') as importfile: + for node in sortutil.natural_sort(databynode): + data = databynode[node] + if not data.get('groups', []): + importfile.write(node + '\n') + for g in data.get('groups', []): + if g not in nodesbygroup: + nodesbygroup[g] = set([node]) + else: + nodesbygroup[g].add(node) + for group in sortutil.natural_sort(nodesbygroup): + importfile.write('[{0}]\n'.format(group)) + for node in sortutil.natural_sort(nodesbygroup[group]): + importfile.write('{0}\n'.format(node)) + + + + +if __name__ == '__main__': + main() From 01d73308ed4d4f8b8c45b451fa18bea8bf2edca3 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Thu, 29 Nov 2018 15:04:56 -0500 Subject: [PATCH 2/4] Pull back spurious blank lines --- confluent_client/bin/confluent2ansible | 2 -- 1 file changed, 2 deletions(-) diff --git a/confluent_client/bin/confluent2ansible b/confluent_client/bin/confluent2ansible index de9f7b99..20cbd5e6 100644 --- a/confluent_client/bin/confluent2ansible +++ b/confluent_client/bin/confluent2ansible @@ -63,7 +63,5 @@ def main(): importfile.write('{0}\n'.format(node)) - - if __name__ == '__main__': main() From 22503e7e11b6e28c7748ba965cf114e7639824ab Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Thu, 29 Nov 2018 15:21:37 -0500 Subject: [PATCH 3/4] Inject blank lines in ansible hosts file This looks a bit more like a natural hosts file. --- confluent_client/bin/confluent2ansible | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/confluent_client/bin/confluent2ansible b/confluent_client/bin/confluent2ansible index 20cbd5e6..2f16dd84 100644 --- a/confluent_client/bin/confluent2ansible +++ b/confluent_client/bin/confluent2ansible @@ -48,19 +48,24 @@ def main(): databynode[node].update(res['databynode'][node]) nodesbygroup = {} with open(options.output, 'w') as importfile: + needempty = False for node in sortutil.natural_sort(databynode): data = databynode[node] if not data.get('groups', []): importfile.write(node + '\n') + needempty = True for g in data.get('groups', []): if g not in nodesbygroup: nodesbygroup[g] = set([node]) else: nodesbygroup[g].add(node) + if needempty: + importfile.write('\n') for group in sortutil.natural_sort(nodesbygroup): importfile.write('[{0}]\n'.format(group)) for node in sortutil.natural_sort(nodesbygroup[group]): importfile.write('{0}\n'.format(node)) + importfile.write('\n') if __name__ == '__main__': From a9375113544f0f7e8ee2efca89920e343ddd6bdb Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Fri, 30 Nov 2018 16:09:47 -0500 Subject: [PATCH 4/4] Fix nodesetboot argument parsing --- confluent_client/bin/nodesetboot | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/confluent_client/bin/nodesetboot b/confluent_client/bin/nodesetboot index 7954a2bf..1ec19099 100755 --- a/confluent_client/bin/nodesetboot +++ b/confluent_client/bin/nodesetboot @@ -53,8 +53,8 @@ except IndexError: argparser.print_help() sys.exit(1) bootdev = None -if len(sys.argv) > 2: - bootdev = sys.argv[2] +if len(args) > 1: + bootdev = args[1] if bootdev in ('net', 'pxe'): bootdev = 'network' session = client.Command() @@ -65,4 +65,4 @@ else: bootmode = 'uefi' sys.exit(session.simple_noderange_command(noderange, '/boot/nextdevice', bootdev, bootmode=bootmode, - persistent=options.persist)) \ No newline at end of file + persistent=options.persist))