blob: 9f31deee3726f7e3c5cd7b37ef66b97c27cdc817 [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
Brian O'Connor6e192432015-02-26 15:17:23 -080036
Jordan Halterman19c123a2018-07-30 13:57:19 -070037def get_vars(type):
38 types = {'controller': 'OC', 'storage': 'OCC'}
39 vars = []
40 for var in environ:
41 if re.match(r"{}[0-9]+".format(types[type]), var):
42 vars.append(var)
43 return sorted(vars, key=alphanum_key)
44
45
46def get_nodes(type, ips=None, default_port=5679):
47 node = lambda id, ip, port: {'id': id, 'ip': ip, 'port': port}
48 prefixes = {'controller': 'onos', 'storage': 'atomix'}
Ray Milkeyff18b6e2017-07-13 08:34:19 -070049 result = []
50 if not ips:
Jordan Halterman19c123a2018-07-30 13:57:19 -070051 ips = [environ[v] for v in get_vars(type)]
Jordan Halterman00e92da2018-05-22 23:05:52 -070052 i = 1
Ray Milkeyff18b6e2017-07-13 08:34:19 -070053 for ip_string in ips:
54 address_tuple = ip_string.split(":")
55 if len(address_tuple) == 3:
Jordan Halterman19c123a2018-07-30 13:57:19 -070056 id = address_tuple[0]
57 ip = address_tuple[1]
58 port = int(address_tuple[2])
Ray Milkeyff18b6e2017-07-13 08:34:19 -070059 else:
Jordan Halterman19c123a2018-07-30 13:57:19 -070060 id = '{}-{}'.format(prefixes[type], i)
Jordan Halterman00e92da2018-05-22 23:05:52 -070061 i += 1
Jordan Halterman19c123a2018-07-30 13:57:19 -070062 ip = ip_string
63 port = default_port
Ray Milkeyff18b6e2017-07-13 08:34:19 -070064 result.append(node(id, ip, port))
65 return result
Brian O'Connor6e192432015-02-26 15:17:23 -080066
Jordan Halterman19c123a2018-07-30 13:57:19 -070067
Brian O'Connor6e192432015-02-26 15:17:23 -080068if __name__ == '__main__':
Jordan Halterman19c123a2018-07-30 13:57:19 -070069 parser = argparse.ArgumentParser(
70 description="Generate the partitions json file given a list of IPs or from environment variables.")
71 parser.add_argument(
72 'filename', metavar='filename', type=str, nargs='?',
73 help='File to write output to. If none is provided, output is written to stdout.')
74 parser.add_argument(
75 '--controller-nodes', '-c', metavar='node_ip', type=str, nargs='+',
76 help='IP Address(es) of the controller nodes. If no IPs are given, ' +
77 'will use the $OC* environment variables. NOTE: these arguemnts' +
78 ' are only processed after the filename argument.')
79 parser.add_argument(
80 '--storage-nodes', '-s', metavar='node_ip', type=str, nargs='+',
81 help='IP Address(es) of the storage nodes. If no IPs are given, ' +
82 'will use the $OCC* environment variables. NOTE: these arguemnts' +
83 ' are only processed after the filename argument.')
Thiago Santos9eb23c12016-09-20 14:03:34 -030084
Jordan Halterman19c123a2018-07-30 13:57:19 -070085 args = parser.parse_args()
86 filename = args.filename
87 controller = get_nodes('controller', args.controller_nodes)
88 storage = get_nodes('storage', args.storage_nodes)
Thiago Santos9eb23c12016-09-20 14:03:34 -030089
Jordan Halterman19c123a2018-07-30 13:57:19 -070090 if len(storage) > 0:
91 data = {
92 'name': 'onos',
93 'storage': storage
94 }
95 else:
96 data = {
97 'name': 'onos',
98 'controller': controller
99 }
100 output = json.dumps(data, indent=4)
Brian O'Connor6e192432015-02-26 15:17:23 -0800101
Jordan Halterman19c123a2018-07-30 13:57:19 -0700102 if filename:
103 with open(filename, 'w') as f:
104 f.write(output)
105 else:
106 print output