blob: 5e42dfc9ab8a4e88242122c968cd3907ec7f756d [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001#!/usr/bin/env python
2"""
3usage: atomix-gen-config [-h] [-s PARTITION_SIZE] [-n NUM_PARTITIONS]
4 [node_ip] [filename] [node_ip [node_ip ...]]
5
6Generate the partitions json file given a list of IPs or from the $OCC*
7environment variables.
8
9positional arguments:
10 filename File to write output to. If none is provided, output
11 is written to stdout.
12 node_ip IP Address(es) of the node(s) in the cluster. If no
13 IPs are given, will use the $OCC* environment
14 variables. NOTE: these arguemnts are only processed
15 after the filename argument.
16
17optional arguments:
18 -h, --help show this help message and exit
19 -s PARTITION_SIZE, --partition-size PARTITION_SIZE
20 Number of nodes per partition. Note that partition
21 sizes smaller than 3 are not fault tolerant. Defaults
22 to 3.
23 -n NUM_PARTITIONS, --num-partitions NUM_PARTITIONS
24 Number of partitions. Defaults to the number of nodes
25 in the cluster.
26"""
27
28from os import environ
29import argparse
30import re
31import json
32
33convert = lambda text: int(text) if text.isdigit() else text.lower()
34alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
35
36def get_OCC_vars():
37 vars = []
38 for var in environ:
39 if re.match(r"OCC[0-9]+", var):
40 vars.append(var)
41 return sorted(vars, key=alphanum_key)
42
43def get_local_node(node, ips=None):
44 if not ips:
45 ips = [ environ[v] for v in get_OCC_vars() ]
46 return 'atomix-{}'.format(ips.index(node) + 1)
47
48def get_nodes(ips=None, default_port=5679):
49 node = lambda id, ip, port: {'id': id, 'address': '{}:{}'.format(ip, port)}
50 result = []
51 if not ips:
52 ips = [ environ[v] for v in get_OCC_vars() ]
53 i = 1
54 for ip_string in ips:
55 address_tuple = ip_string.split(":")
56 if len(address_tuple) == 3:
57 id=address_tuple[0]
58 ip=address_tuple[1]
59 port=int(address_tuple[2])
60 else:
61 id='atomix-{}'.format(i)
62 i += 1
63 ip=ip_string
64 port=default_port
65 result.append(node(id, ip, port))
66 return result
67
68def get_local_address(node, ips=None, default_port=5679):
69 result = []
70 if not ips:
71 ips = [ environ[v] for v in get_OCC_vars() ]
72 i = 1
73 for ip_string in ips:
74 address_tuple = ip_string.split(":")
75 if len(address_tuple) == 3:
76 id=address_tuple[0]
77 ip=address_tuple[1]
78 port=int(address_tuple[2])
79 if node == id or node == ip:
80 return '{}:{}'.format(ip, port)
81 return '{}:{}'.format(node, default_port)
82
83if __name__ == '__main__':
84 parser = argparse.ArgumentParser(
85 description="Generate the partitions json file given a list of IPs or from the $OCC* environment variables.")
86 parser.add_argument(
87 '-s', '--partition-size', type=int, default=3,
88 help="Number of nodes per partition. Note that partition sizes smaller than 3 are not fault tolerant. Defaults to 3." )
89 parser.add_argument(
90 '-n', '--num-partitions', type=int,
91 help="Number of partitions. Defaults to the number of nodes in the cluster." )
92 # TODO: make filename and nodes independent. This will break backwards compatibility with existing usage.
93 parser.add_argument(
94 'node', metavar='node_ip', type=str, help='IP address of the node for which to generate the configuration')
95 parser.add_argument(
96 'filename', metavar='filename', type=str, nargs='?',
97 help='File to write output to. If none is provided, output is written to stdout.')
98 parser.add_argument(
99 'nodes', metavar='node_ip', type=str, nargs='*',
100 help='IP Address(es) of the node(s) in the cluster. If no IPs are given, ' +
101 'will use the $OCC* environment variables. NOTE: these arguemnts' +
102 ' are only processed after the filename argument.')
103
104 args = parser.parse_args()
105 filename = args.filename
106 partition_size = args.partition_size
107 local_member_id = get_local_node(args.node)
108 local_member_address = get_local_address(args.node, args.nodes)
109 nodes = get_nodes(args.nodes)
110 num_partitions = args.num_partitions
111 if not num_partitions:
112 num_partitions = len(nodes)
113
114 data = {
115 'cluster': {
116 'clusterId': 'onos',
117 'node': {
118 'id': local_member_id,
119 'address': local_member_address
120 },
121 'discovery': {
122 'type': 'bootstrap',
123 'nodes': nodes
124 }
125 },
126 'managementGroup': {
127 'type': 'raft',
128 'partitions': 1,
129 'partitionSize': len(nodes),
130 'members': [node['id'] for node in nodes]
131 },
132 'partitionGroups': {
133 'raft': {
134 'type': 'raft',
135 'partitions': num_partitions,
136 'partitionSize': partition_size,
137 'members': [node['id'] for node in nodes]
138 }
139 }
140 }
141 output = json.dumps(data, indent=4)
142
143 if filename:
144 with open(filename, 'w') as f:
145 f.write(output)
146 else:
147 print output