blob: e214d2fc5d6aea193555ebadd7171f7df649f611 [file] [log] [blame]
pierventre69db90c2022-05-06 23:06:20 +02001#!/usr/bin/env python3
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -08002"""
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 Haltermand326de12018-07-31 11:57:21 -070037def get_vars_by_type(type):
Jordan Halterman19c123a2018-07-30 13:57:19 -070038 vars = []
39 for var in environ:
Jordan Haltermand326de12018-07-31 11:57:21 -070040 if re.match(r"{}[0-9]+".format(type), var):
Jordan Halterman19c123a2018-07-30 13:57:19 -070041 vars.append(var)
42 return sorted(vars, key=alphanum_key)
43
44
Jordan Haltermand326de12018-07-31 11:57:21 -070045def get_vars():
46 vars = get_vars_by_type('OCC')
47 if len(vars) == 0:
48 vars = get_vars_by_type('OC')
49 return vars
50
51
52def get_nodes(ips=None, default_port=5679):
Jordan Halterman19c123a2018-07-30 13:57:19 -070053 node = lambda id, ip, port: {'id': id, 'ip': ip, 'port': port}
Ray Milkeyff18b6e2017-07-13 08:34:19 -070054 result = []
55 if not ips:
Jordan Haltermand326de12018-07-31 11:57:21 -070056 ips = [environ[v] for v in get_vars()]
Jordan Halterman00e92da2018-05-22 23:05:52 -070057 i = 1
Ray Milkeyff18b6e2017-07-13 08:34:19 -070058 for ip_string in ips:
59 address_tuple = ip_string.split(":")
60 if len(address_tuple) == 3:
Jordan Halterman19c123a2018-07-30 13:57:19 -070061 id = address_tuple[0]
62 ip = address_tuple[1]
63 port = int(address_tuple[2])
Ray Milkeyff18b6e2017-07-13 08:34:19 -070064 else:
Jordan Haltermand326de12018-07-31 11:57:21 -070065 id = 'atomix-{}'.format(i)
Jordan Halterman00e92da2018-05-22 23:05:52 -070066 i += 1
Jordan Halterman19c123a2018-07-30 13:57:19 -070067 ip = ip_string
68 port = default_port
Ray Milkeyff18b6e2017-07-13 08:34:19 -070069 result.append(node(id, ip, port))
70 return result
Brian O'Connor6e192432015-02-26 15:17:23 -080071
Jordan Halterman19c123a2018-07-30 13:57:19 -070072
Brian O'Connor6e192432015-02-26 15:17:23 -080073if __name__ == '__main__':
Jordan Halterman19c123a2018-07-30 13:57:19 -070074 parser = argparse.ArgumentParser(
75 description="Generate the partitions json file given a list of IPs or from environment variables.")
76 parser.add_argument(
Jordan Haltermand326de12018-07-31 11:57:21 -070077 'node', metavar='node_ip', type=str, nargs='?',
78 help='The node for which to generate the configuration')
79 parser.add_argument(
Jordan Halterman19c123a2018-07-30 13:57:19 -070080 'filename', metavar='filename', type=str, nargs='?',
81 help='File to write output to. If none is provided, output is written to stdout.')
82 parser.add_argument(
Jordan Haltermand326de12018-07-31 11:57:21 -070083 '--nodes', '-n', metavar='node_ip', type=str, nargs='+',
Jordan Halterman19c123a2018-07-30 13:57:19 -070084 help='IP Address(es) of the storage nodes. If no IPs are given, ' +
Jordan Haltermand326de12018-07-31 11:57:21 -070085 'will use the $OCC* or $OC* environment variables. NOTE: these arguments' +
Jordan Halterman19c123a2018-07-30 13:57:19 -070086 ' are only processed after the filename argument.')
Thiago Santos9eb23c12016-09-20 14:03:34 -030087
Jordan Halterman19c123a2018-07-30 13:57:19 -070088 args = parser.parse_args()
Jordan Haltermand326de12018-07-31 11:57:21 -070089 node = args.node
pierventre69db90c2022-05-06 23:06:20 +020090 print(node)
Jordan Halterman19c123a2018-07-30 13:57:19 -070091 filename = args.filename
Jordan Haltermand326de12018-07-31 11:57:21 -070092 nodes = get_nodes(args.nodes)
Thiago Santos9eb23c12016-09-20 14:03:34 -030093
Jordan Haltermand326de12018-07-31 11:57:21 -070094 data = {
95 'name': 'onos',
96 'node': {
97 'id': node,
98 'ip': node,
99 'port': 9876
100 },
101 'storage': nodes
102 }
Jordan Halterman19c123a2018-07-30 13:57:19 -0700103 output = json.dumps(data, indent=4)
Brian O'Connor6e192432015-02-26 15:17:23 -0800104
Jordan Halterman19c123a2018-07-30 13:57:19 -0700105 if filename:
106 with open(filename, 'w') as f:
107 f.write(output)
108 else:
pierventre69db90c2022-05-06 23:06:20 +0200109 print(output)