blob: ef8f2f0b0d6d73a2811b1d1840d2cff3dfd96454 [file] [log] [blame]
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -08001#!/usr/bin/env python
2"""
Jordan Halterman00e92da2018-05-22 23:05:52 -07003usage: onos-gen-config [-h] [-s PARTITION_SIZE] [-n NUM_PARTITIONS]
4 [filename] [node_ip [node_ip ...]]
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -08005
Jordan Halterman00e92da2018-05-22 23:05:52 -07006Generate the partitions json file given a list of IPs or from the $OCC*
Thiago Santos9eb23c12016-09-20 14:03:34 -03007environment 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 $OC* environment
Jordan Halterman00e92da2018-05-22 23:05:52 -070014 variables. NOTE: these arguments are only processed
Thiago Santos9eb23c12016-09-20 14:03:34 -030015 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.
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080026"""
Brian O'Connor6e192432015-02-26 15:17:23 -080027
28from os import environ
Thiago Santos9eb23c12016-09-20 14:03:34 -030029import argparse
Brian O'Connor6e192432015-02-26 15:17:23 -080030import re
31import json
Brian O'Connor6e192432015-02-26 15:17:23 -080032
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080033convert = lambda text: int(text) if text.isdigit() else text.lower()
34alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
Brian O'Connor6e192432015-02-26 15:17:23 -080035
Jordan Halterman00e92da2018-05-22 23:05:52 -070036def get_OCC_vars():
Brian O'Connor6e192432015-02-26 15:17:23 -080037 vars = []
38 for var in environ:
Jordan Halterman00e92da2018-05-22 23:05:52 -070039 if re.match(r"OCC[0-9]+", var):
Brian O'Connor6e192432015-02-26 15:17:23 -080040 vars.append(var)
41 return sorted(vars, key=alphanum_key)
42
Jordan Halterman00e92da2018-05-22 23:05:52 -070043def get_nodes(ips=None, default_port=5679):
Ray Milkeyff18b6e2017-07-13 08:34:19 -070044 node = lambda id, ip, port : { 'id': id, 'ip': ip, 'port': port }
45 result = []
46 if not ips:
Jordan Halterman00e92da2018-05-22 23:05:52 -070047 ips = [ environ[v] for v in get_OCC_vars() ]
48 i = 1
Ray Milkeyff18b6e2017-07-13 08:34:19 -070049 for ip_string in ips:
50 address_tuple = ip_string.split(":")
51 if len(address_tuple) == 3:
52 id=address_tuple[0]
53 ip=address_tuple[1]
54 port=int(address_tuple[2])
55 else:
Jordan Halterman00e92da2018-05-22 23:05:52 -070056 id='atomix-{}'.format(i)
57 i += 1
Ray Milkeyff18b6e2017-07-13 08:34:19 -070058 ip=ip_string
59 port=default_port
60 result.append(node(id, ip, port))
61 return result
Brian O'Connor6e192432015-02-26 15:17:23 -080062
Brian O'Connor6e192432015-02-26 15:17:23 -080063if __name__ == '__main__':
Thiago Santos9eb23c12016-09-20 14:03:34 -030064 parser = argparse.ArgumentParser(
65 description="Generate the partitions json file given a list of IPs or from the $OC* environment variables.")
66 parser.add_argument(
67 '-s', '--partition-size', type=int, default=3,
68 help="Number of nodes per partition. Note that partition sizes smaller than 3 are not fault tolerant. Defaults to 3." )
69 parser.add_argument(
70 '-n', '--num-partitions', type=int,
71 help="Number of partitions. Defaults to the number of nodes in the cluster." )
72 # TODO: make filename and nodes independent. This will break backwards compatibility with existing usage.
73 parser.add_argument(
74 'filename', metavar='filename', type=str, nargs='?',
75 help='File to write output to. If none is provided, output is written to stdout.')
76 parser.add_argument(
77 'nodes', metavar='node_ip', type=str, nargs='*',
78 help='IP Address(es) of the node(s) in the cluster. If no IPs are given, ' +
79 'will use the $OC* environment variables. NOTE: these arguemnts' +
80 ' are only processed after the filename argument.')
81
82 args = parser.parse_args()
83 filename = args.filename
84 partition_size = args.partition_size
Jordan Halterman00e92da2018-05-22 23:05:52 -070085 cluster = get_nodes(args.nodes)
Thiago Santos9eb23c12016-09-20 14:03:34 -030086 num_partitions = args.num_partitions
87 if not num_partitions:
Jordan Halterman00e92da2018-05-22 23:05:52 -070088 num_partitions = len(cluster)
Thiago Santos9eb23c12016-09-20 14:03:34 -030089
Madan Jampaniec1df022015-10-13 21:23:03 -070090 data = {
Jordan Halterman00e92da2018-05-22 23:05:52 -070091 'name': 'onos',
92 'cluster': cluster
Brian O'Connor6e192432015-02-26 15:17:23 -080093 }
94 output = json.dumps(data, indent=4)
95
Thiago Santos9eb23c12016-09-20 14:03:34 -030096 if filename:
Brian O'Connor6e192432015-02-26 15:17:23 -080097 with open(filename, 'w') as f:
98 f.write(output)
99 else:
100 print output