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 | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 37 | def get_vars_by_type(type): |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 38 | vars = [] |
| 39 | for var in environ: |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 40 | if re.match(r"{}[0-9]+".format(type), var): |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 41 | vars.append(var) |
| 42 | return sorted(vars, key=alphanum_key) |
| 43 | |
| 44 | |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 45 | def 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 | |
| 52 | def get_nodes(ips=None, default_port=5679): |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 53 | node = lambda id, ip, port: {'id': id, 'ip': ip, 'port': port} |
Ray Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 54 | result = [] |
| 55 | if not ips: |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 56 | ips = [environ[v] for v in get_vars()] |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 57 | i = 1 |
Ray Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 58 | for ip_string in ips: |
| 59 | address_tuple = ip_string.split(":") |
| 60 | if len(address_tuple) == 3: |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 61 | id = address_tuple[0] |
| 62 | ip = address_tuple[1] |
| 63 | port = int(address_tuple[2]) |
Ray Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 64 | else: |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 65 | id = 'atomix-{}'.format(i) |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 66 | i += 1 |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 67 | ip = ip_string |
| 68 | port = default_port |
Ray Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 69 | result.append(node(id, ip, port)) |
| 70 | return result |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 71 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 72 | |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 73 | if __name__ == '__main__': |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 74 | parser = argparse.ArgumentParser( |
| 75 | description="Generate the partitions json file given a list of IPs or from environment variables.") |
| 76 | parser.add_argument( |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 77 | 'node', metavar='node_ip', type=str, nargs='?', |
| 78 | help='The node for which to generate the configuration') |
| 79 | parser.add_argument( |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 80 | '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 Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 83 | '--nodes', '-n', metavar='node_ip', type=str, nargs='+', |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 84 | help='IP Address(es) of the storage nodes. If no IPs are given, ' + |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 85 | 'will use the $OCC* or $OC* environment variables. NOTE: these arguments' + |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 86 | ' are only processed after the filename argument.') |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 87 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 88 | args = parser.parse_args() |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 89 | node = args.node |
| 90 | print node |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 91 | filename = args.filename |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 92 | nodes = get_nodes(args.nodes) |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 93 | |
Jordan Halterman | d326de1 | 2018-07-31 11:57:21 -0700 | [diff] [blame] | 94 | data = { |
| 95 | 'name': 'onos', |
| 96 | 'node': { |
| 97 | 'id': node, |
| 98 | 'ip': node, |
| 99 | 'port': 9876 |
| 100 | }, |
| 101 | 'storage': nodes |
| 102 | } |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 103 | output = json.dumps(data, indent=4) |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 104 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 105 | if filename: |
| 106 | with open(filename, 'w') as f: |
| 107 | f.write(output) |
| 108 | else: |
| 109 | print output |