Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 3 | Generate the partitions json file from the $OC* environment variables |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 4 | |
Madan Jampani | 80244e5 | 2016-05-17 20:03:43 -0700 | [diff] [blame^] | 5 | Usage: onos-gen-partitions [output file] [node_ip ...] |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 6 | If output file is not provided, the json is written to stdout. |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 7 | """ |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 8 | |
| 9 | from os import environ |
| 10 | from collections import deque, OrderedDict |
| 11 | import re |
| 12 | import json |
| 13 | import sys |
Thomas Vachuska | b56b917 | 2015-11-24 11:24:23 -0800 | [diff] [blame] | 14 | import hashlib |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 15 | |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 16 | convert = lambda text: int(text) if text.isdigit() else text.lower() |
| 17 | alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 18 | |
| 19 | def get_OC_vars(): |
| 20 | vars = [] |
| 21 | for var in environ: |
| 22 | if re.match(r"OC[0-9]+", var): |
| 23 | vars.append(var) |
| 24 | return sorted(vars, key=alphanum_key) |
| 25 | |
Brian O'Connor | fb8a275 | 2016-05-16 16:48:23 -0700 | [diff] [blame] | 26 | def get_nodes(ips=None, port=9876): |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 27 | node = lambda k: { 'id': k, 'ip': k, 'port': port } |
Brian O'Connor | fb8a275 | 2016-05-16 16:48:23 -0700 | [diff] [blame] | 28 | if not ips: |
| 29 | ips = [ environ[v] for v in get_OC_vars() ] |
| 30 | return [ node(v) for v in ips ] |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 31 | |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 32 | def generate_base_partition(nodes): |
| 33 | return { |
| 34 | 'id': 0, |
| 35 | 'members': nodes |
| 36 | } |
| 37 | |
| 38 | def generate_extended_partitions(nodes, k): |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 39 | l = deque(nodes) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 40 | perms = [] |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 41 | for i in range(1, len(nodes)+1): |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 42 | part = { |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 43 | 'id': i, |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 44 | 'members': list(l)[:k] |
| 45 | } |
| 46 | perms.append(part) |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 47 | l.rotate(-1) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 48 | return perms |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 49 | |
| 50 | if __name__ == '__main__': |
Madan Jampani | 80244e5 | 2016-05-17 20:03:43 -0700 | [diff] [blame^] | 51 | nodes = get_nodes(sys.argv[2:]) |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 52 | base_partition = generate_base_partition([v.get('id') for v in nodes]) |
| 53 | extended_partitions = generate_extended_partitions([v.get('id') for v in nodes], 3) |
| 54 | partitions = [] |
| 55 | partitions.append(base_partition) |
| 56 | partitions.extend(extended_partitions) |
Brian O'Connor | fb8a275 | 2016-05-16 16:48:23 -0700 | [diff] [blame] | 57 | m = hashlib.sha256() |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 58 | for node in nodes: |
Brian O'Connor | fb8a275 | 2016-05-16 16:48:23 -0700 | [diff] [blame] | 59 | m.update(node['ip']) |
| 60 | name = int(m.hexdigest()[:8], base=16) # 32-bit int based on SHA256 digest |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 61 | data = { |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 62 | 'name': name, |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 63 | 'nodes': nodes, |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 64 | 'partitions': partitions |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 65 | } |
| 66 | output = json.dumps(data, indent=4) |
| 67 | |
| 68 | if len(sys.argv) == 2: |
| 69 | filename = sys.argv[1] |
| 70 | with open(filename, 'w') as f: |
| 71 | f.write(output) |
| 72 | else: |
| 73 | print output |