blob: 4275524bee49061d213f573a6adc8458576fcc39 [file] [log] [blame]
Jon Hall3b489db2015-10-05 14:38:37 -07001#!/usr/bin/env python
Jon Halle9b1fa32015-12-08 15:32:21 -08002"""
Jon Hall3b489db2015-10-05 14:38:37 -07003 Generate the partitions json file from the $OC* environment variables
4
Jon Hallb07cdd22016-06-17 16:53:13 -07005 Usage: onos-gen-partitions [output file] [node_ip ...]
Jon Hall3b489db2015-10-05 14:38:37 -07006 If output file is not provided, the json is written to stdout.
Jon Halle9b1fa32015-12-08 15:32:21 -08007"""
Jon Hall3b489db2015-10-05 14:38:37 -07008
9from os import environ
10from collections import deque, OrderedDict
11import re
12import json
13import sys
Jon Halle9b1fa32015-12-08 15:32:21 -080014import hashlib
Jon Hall3b489db2015-10-05 14:38:37 -070015
16convert = lambda text: int(text) if text.isdigit() else text.lower()
17alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
18
19def 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 Hallb07cdd22016-06-17 16:53:13 -070026def get_nodes(ips=None, port=9876):
Jon Hall37677302015-10-23 12:17:49 -070027 node = lambda k: { 'id': k, 'ip': k, 'port': port }
Jon Hallb07cdd22016-06-17 16:53:13 -070028 if not ips:
29 ips = [ environ[v] for v in get_OC_vars() ]
30 return [ node(v) for v in ips ]
Jon Hall3b489db2015-10-05 14:38:37 -070031
Jon Hallb07cdd22016-06-17 16:53:13 -070032def generate_partitions(nodes, k):
Jon Hall3b489db2015-10-05 14:38:37 -070033 l = deque(nodes)
Jon Hall37677302015-10-23 12:17:49 -070034 perms = []
Jon Hall3b489db2015-10-05 14:38:37 -070035 for i in range(1, len(nodes)+1):
Jon Hall37677302015-10-23 12:17:49 -070036 part = {
Jon Hall9288fdd2016-01-19 11:51:27 -080037 'id': i,
Jon Hall37677302015-10-23 12:17:49 -070038 'members': list(l)[:k]
39 }
40 perms.append(part)
Jon Hall3b489db2015-10-05 14:38:37 -070041 l.rotate(-1)
Jon Hall37677302015-10-23 12:17:49 -070042 return perms
Jon Hall3b489db2015-10-05 14:38:37 -070043
Jon Hallb07cdd22016-06-17 16:53:13 -070044def generate_partitions_HA(nodes, k):
Jon Hall3b489db2015-10-05 14:38:37 -070045 l = deque(nodes)
Jon Hall37677302015-10-23 12:17:49 -070046 perms = []
Jon Hall3b489db2015-10-05 14:38:37 -070047 for i in range(1, (len(nodes) + 1) / 2 + 1):
Jon Hall37677302015-10-23 12:17:49 -070048 part = {
Jon Hall9288fdd2016-01-19 11:51:27 -080049 'id': i,
Jon Hall37677302015-10-23 12:17:49 -070050 'members': list(l)[:k]
51 }
52 perms.append(part)
Jon Hall3b489db2015-10-05 14:38:37 -070053 l.rotate(-2)
Jon Hall37677302015-10-23 12:17:49 -070054 return perms
Jon Hall3b489db2015-10-05 14:38:37 -070055
56if __name__ == '__main__':
Jon Hallb07cdd22016-06-17 16:53:13 -070057 nodes = get_nodes(sys.argv[2:])
58 partitions = generate_partitions_HA([v.get('id') for v in nodes], 3)
59 m = hashlib.sha256()
Jon Halle9b1fa32015-12-08 15:32:21 -080060 for node in nodes:
Jon Hallb07cdd22016-06-17 16:53:13 -070061 m.update(node['ip'])
62 name = int(m.hexdigest()[:8], base=16) # 32-bit int based on SHA256 digest
Jon Hall3b489db2015-10-05 14:38:37 -070063 data = {
Jon Halle9b1fa32015-12-08 15:32:21 -080064 'name': name,
Jon Hall3b489db2015-10-05 14:38:37 -070065 '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