Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 3 | usage: onos-gen-partitions [-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 | |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 6 | Generate the partitions json file given a list of IPs or from the $OC* |
| 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 |
| 14 | variables. NOTE: these arguemnts are only processed |
| 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 | from collections import deque |
| 30 | import argparse |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 31 | import re |
| 32 | import json |
Thomas Vachuska | b56b917 | 2015-11-24 11:24:23 -0800 | [diff] [blame] | 33 | import hashlib |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 34 | |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 35 | convert = lambda text: int(text) if text.isdigit() else text.lower() |
| 36 | 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] | 37 | |
| 38 | def get_OC_vars(): |
| 39 | vars = [] |
| 40 | for var in environ: |
| 41 | if re.match(r"OC[0-9]+", var): |
| 42 | vars.append(var) |
| 43 | return sorted(vars, key=alphanum_key) |
| 44 | |
Brian O'Connor | fb8a275 | 2016-05-16 16:48:23 -0700 | [diff] [blame] | 45 | def get_nodes(ips=None, port=9876): |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 46 | node = lambda k: { 'id': k, 'ip': k, 'port': port } |
Brian O'Connor | fb8a275 | 2016-05-16 16:48:23 -0700 | [diff] [blame] | 47 | if not ips: |
| 48 | ips = [ environ[v] for v in get_OC_vars() ] |
| 49 | return [ node(v) for v in ips ] |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 50 | |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 51 | def generate_partitions(nodes, k, n): |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 52 | l = deque(nodes) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 53 | perms = [] |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 54 | for i in range(1, n+1): |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 55 | part = { |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 56 | 'id': i, |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 57 | 'members': list(l)[:k] |
| 58 | } |
| 59 | perms.append(part) |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 60 | l.rotate(-1) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 61 | return perms |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 62 | |
| 63 | if __name__ == '__main__': |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 64 | 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 |
| 85 | nodes = get_nodes(args.nodes) |
| 86 | num_partitions = args.num_partitions |
| 87 | if not num_partitions: |
| 88 | num_partitions = len(nodes) |
| 89 | |
| 90 | partitions = generate_partitions([v.get('id') for v in nodes], partition_size, num_partitions) |
Brian O'Connor | fb8a275 | 2016-05-16 16:48:23 -0700 | [diff] [blame] | 91 | m = hashlib.sha256() |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 92 | for node in nodes: |
Brian O'Connor | fb8a275 | 2016-05-16 16:48:23 -0700 | [diff] [blame] | 93 | m.update(node['ip']) |
| 94 | name = int(m.hexdigest()[:8], base=16) # 32-bit int based on SHA256 digest |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 95 | data = { |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 96 | 'name': name, |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 97 | 'nodes': nodes, |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 98 | 'partitions': partitions |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 99 | } |
| 100 | output = json.dumps(data, indent=4) |
| 101 | |
Thiago Santos | 9eb23c1 | 2016-09-20 14:03:34 -0300 | [diff] [blame] | 102 | if filename: |
Brian O'Connor | 6e19243 | 2015-02-26 15:17:23 -0800 | [diff] [blame] | 103 | with open(filename, 'w') as f: |
| 104 | f.write(output) |
| 105 | else: |
| 106 | print output |