Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 3 | usage: onos-gen-config [-h] [-s PARTITION_SIZE] [-n NUM_PARTITIONS] |
| 4 | [filename] [node_ip [node_ip ...]] |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 5 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 6 | Generate the partitions json file given a list of IPs or from the $OCC* |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 7 | environment variables. |
| 8 | |
| 9 | positional 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 Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 14 | variables. NOTE: these arguments are only processed |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 15 | after the filename argument. |
| 16 | |
| 17 | optional 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 Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 26 | """ |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 27 | |
| 28 | from os import environ |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 29 | import argparse |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 30 | import re |
| 31 | import json |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 32 | |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 33 | convert = lambda text: int(text) if text.isdigit() else text.lower() |
| 34 | alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 35 | |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 36 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 37 | def 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 | |
| 46 | def 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 Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 49 | result = [] |
| 50 | if not ips: |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 51 | ips = [environ[v] for v in get_vars(type)] |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 52 | i = 1 |
Ray Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 53 | for ip_string in ips: |
| 54 | address_tuple = ip_string.split(":") |
| 55 | if len(address_tuple) == 3: |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 56 | id = address_tuple[0] |
| 57 | ip = address_tuple[1] |
| 58 | port = int(address_tuple[2]) |
Ray Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 59 | else: |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 60 | id = '{}-{}'.format(prefixes[type], i) |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 61 | i += 1 |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 62 | ip = ip_string |
| 63 | port = default_port |
Ray Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 64 | result.append(node(id, ip, port)) |
| 65 | return result |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 66 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 67 | |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 68 | if __name__ == '__main__': |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 69 | 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 Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 84 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 85 | 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 Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 89 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 90 | 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'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 101 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 102 | if filename: |
| 103 | with open(filename, 'w') as f: |
| 104 | f.write(output) |
| 105 | else: |
| 106 | print output |