Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jon Hall | e9b1fa3 | 2015-12-08 15:32:21 -0800 | [diff] [blame] | 2 | """ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 3 | Generate the partitions json file from the $OC* environment variables |
| 4 | |
Jon Hall | b07cdd2 | 2016-06-17 16:53:13 -0700 | [diff] [blame] | 5 | Usage: onos-gen-partitions [output file] [node_ip ...] |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 6 | If output file is not provided, the json is written to stdout. |
Jon Hall | e9b1fa3 | 2015-12-08 15:32:21 -0800 | [diff] [blame] | 7 | """ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 8 | |
| 9 | from os import environ |
| 10 | from collections import deque, OrderedDict |
| 11 | import re |
| 12 | import json |
| 13 | import sys |
Jon Hall | e9b1fa3 | 2015-12-08 15:32:21 -0800 | [diff] [blame] | 14 | import hashlib |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 15 | |
| 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)] |
| 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 | |
Jon Hall | b07cdd2 | 2016-06-17 16:53:13 -0700 | [diff] [blame] | 26 | def get_nodes(ips=None, port=9876): |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 27 | node = lambda k: { 'id': k, 'ip': k, 'port': port } |
Jon Hall | b07cdd2 | 2016-06-17 16:53:13 -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 ] |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 31 | |
Jon Hall | b07cdd2 | 2016-06-17 16:53:13 -0700 | [diff] [blame] | 32 | def generate_partitions(nodes, k): |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 33 | l = deque(nodes) |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 34 | perms = [] |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 35 | for i in range(1, len(nodes)+1): |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 36 | part = { |
Jon Hall | 9288fdd | 2016-01-19 11:51:27 -0800 | [diff] [blame] | 37 | 'id': i, |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 38 | 'members': list(l)[:k] |
| 39 | } |
| 40 | perms.append(part) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 41 | l.rotate(-1) |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 42 | return perms |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 43 | |
Jon Hall | b07cdd2 | 2016-06-17 16:53:13 -0700 | [diff] [blame] | 44 | def generate_partitions_HA(nodes, k): |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 45 | l = deque(nodes) |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 46 | perms = [] |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 47 | for i in range(1, (len(nodes) + 1) / 2 + 1): |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 48 | part = { |
Jon Hall | 9288fdd | 2016-01-19 11:51:27 -0800 | [diff] [blame] | 49 | 'id': i, |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 50 | 'members': list(l)[:k] |
| 51 | } |
| 52 | perms.append(part) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 53 | l.rotate(-2) |
Jon Hall | 3767730 | 2015-10-23 12:17:49 -0700 | [diff] [blame] | 54 | return perms |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 55 | |
| 56 | if __name__ == '__main__': |
Jon Hall | b07cdd2 | 2016-06-17 16:53:13 -0700 | [diff] [blame] | 57 | nodes = get_nodes(sys.argv[2:]) |
| 58 | partitions = generate_partitions_HA([v.get('id') for v in nodes], 3) |
| 59 | m = hashlib.sha256() |
Jon Hall | e9b1fa3 | 2015-12-08 15:32:21 -0800 | [diff] [blame] | 60 | for node in nodes: |
Jon Hall | b07cdd2 | 2016-06-17 16:53:13 -0700 | [diff] [blame] | 61 | m.update(node['ip']) |
| 62 | name = int(m.hexdigest()[:8], base=16) # 32-bit int based on SHA256 digest |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 63 | data = { |
Jon Hall | e9b1fa3 | 2015-12-08 15:32:21 -0800 | [diff] [blame] | 64 | 'name': name, |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 65 | 'nodes': nodes, |
| 66 | 'partitions': partitions |
| 67 | } |
| 68 | output = json.dumps(data, indent=4) |
| 69 | |
| 70 | if len(sys.argv) == 2: |
| 71 | filename = sys.argv[1] |
| 72 | with open(filename, 'w') as f: |
| 73 | f.write(output) |
| 74 | else: |
| 75 | print output |