Browse Source

yanic: Add merge script + cron job for nodes.json files.

Signed-off-by: Maximilian Wilhelm <max@rfc2324.org>
Maximilian Wilhelm 6 years ago
parent
commit
427f7c02b7
3 changed files with 57 additions and 0 deletions
  1. 42 0
      yanic/ff_merge_nodes_json
  2. 5 0
      yanic/ff_merge_nodes_json.cron
  3. 10 0
      yanic/init.sls

+ 42 - 0
yanic/ff_merge_nodes_json

@@ -0,0 +1,42 @@
+#!/usr/bin/python
+#
+# Maximilian Wilhelm <max@rfc2324.org>
+#  --  Tue 20 Jun 2017 06:40:18 PM CEST
+#
+
+import argparse
+import json
+import os
+import sys
+
+parser = argparse.ArgumentParser (description = 'Merge nodes.json files')
+parser.add_argument ('files', help = 'Path for nodes.json file(s)', nargs = '+')
+parser.add_argument ('--pretty-print', help = 'Pretty-print JSON output', action = 'store_true')
+args = parser.parse_args ()
+
+all_nodes = {}
+uberdict = {}
+
+# Read all nodes lists into all_nodes dict, thereby dropping any duplicate nodes.
+for file_path in args.files:
+	try:
+		with open (file_path, 'rb') as fh:
+			nodes = json.load (fh)
+	except IOError as (errno, strerror):
+		print "Error while reading file '%s': %s" % (file_path, strerror)
+		sys.exit (1)
+
+	for node in nodes['nodes']:
+		all_nodes[node['nodeinfo']['node_id']] = node
+
+	for key in nodes.keys ():
+		if key != 'nodes':
+			uberdict[key] = nodes[key]
+
+uberdict['nodes'] = all_nodes.values ()
+
+# Print merged nodes.json's to stdout
+if args.pretty_print:
+	print (json.dumps (uberdict, sort_keys = True, indent = 4, separators = (',', ': ')))
+else:
+	print (json.dumps (uberdict))

+ 5 - 0
yanic/ff_merge_nodes_json.cron

@@ -0,0 +1,5 @@
+#
+# Merge all nodes.json into one ubernodes.json
+#
+
+*/5 *   * * *	root	/usr/local/bin/ff_merge_nodes_json /srv/yanic/data/*/nodes.json > /srv/yanic/data/nodes.json

+ 10 - 0
yanic/init.sls

@@ -67,3 +67,13 @@ yanic@{{site}}:
       - file: /srv/yanic/{{site}}.conf
       - file: yanic
 {% endfor %}
+
+
+/usr/local/bin/ff_merge_nodes_json:
+  file.managed:
+    - source: salt://yanic/ff_merge_nodes_json
+    - mode: 755
+
+/etc/cron.d/ff_merge_nodes_json:
+  file.managed:
+    - source: salt://yanic/ff_merge_nodes_json.cron