| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 1 | """ |
| 2 | son-emu compute CLI |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 3 | (c) 2016 by Manuel Peuster <manuel.peuster@upb.de> |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 4 | """ |
| 5 | |
| 6 | import argparse |
| 7 | import pprint |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 8 | from tabulate import tabulate |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 9 | import zerorpc |
| 10 | |
| 11 | |
| 12 | pp = pprint.PrettyPrinter(indent=4) |
| 13 | |
| 14 | |
| 15 | class ZeroRpcClient(object): |
| 16 | |
| 17 | def __init__(self): |
| 18 | self.c = zerorpc.Client() |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 19 | self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 20 | self.cmds = {} |
| 21 | |
| 22 | def execute_command(self, args): |
| 23 | if getattr(self, args["command"]) is not None: |
| 24 | # call the local method with the same name as the command arg |
| 25 | getattr(self, args["command"])(args) |
| 26 | else: |
| 27 | print "Command not implemented." |
| 28 | |
| 29 | def start(self, args): |
| peusterm | 7f8e840 | 2016-02-28 18:38:10 +0100 | [diff] [blame] | 30 | nw_list = list() |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 31 | if args.get("network") is not None: |
| stevenvanrossem | 14c8905 | 2016-04-10 23:49:59 +0200 | [diff] [blame] | 32 | nw_list = self._parse_network(args.get("network")) |
| 33 | |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 34 | r = self.c.compute_action_start( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 35 | args.get("datacenter"), |
| 36 | args.get("name"), |
| 37 | args.get("image"), |
| stevenvanrossem | 8fbf978 | 2016-02-17 11:40:23 +0100 | [diff] [blame] | 38 | args.get("docker_command"), |
| peusterm | 7f8e840 | 2016-02-28 18:38:10 +0100 | [diff] [blame] | 39 | nw_list) |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 40 | pp.pprint(r) |
| 41 | |
| 42 | def stop(self, args): |
| 43 | r = self.c.compute_action_stop( |
| 44 | args.get("datacenter"), args.get("name")) |
| 45 | pp.pprint(r) |
| 46 | |
| 47 | def list(self, args): |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 48 | r = self.c.compute_list( |
| 49 | args.get("datacenter")) |
| 50 | table = [] |
| 51 | for c in r: |
| 52 | # for each container add a line to the output table |
| 53 | if len(c) > 1: |
| 54 | name = c[0] |
| 55 | status = c[1] |
| 56 | eth0ip = None |
| 57 | eth0status = "down" |
| 58 | if len(status.get("network")) > 0: |
| 59 | eth0ip = status.get("network")[0][1] |
| 60 | eth0status = "up" if status.get( |
| 61 | "network")[0][3] else "down" |
| 62 | table.append([status.get("datacenter"), |
| 63 | name, |
| 64 | status.get("image"), |
| 65 | eth0ip, |
| 66 | eth0status, |
| 67 | status.get("state").get("Status")]) |
| 68 | headers = ["Datacenter", |
| 69 | "Container", |
| 70 | "Image", |
| 71 | "eth0 IP", |
| 72 | "eth0 status", |
| 73 | "Status"] |
| 74 | print tabulate(table, headers=headers, tablefmt="grid") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 75 | |
| 76 | def status(self, args): |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 77 | r = self.c.compute_status( |
| 78 | args.get("datacenter"), args.get("name")) |
| 79 | pp.pprint(r) |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 80 | |
| stevenvanrossem | 14c8905 | 2016-04-10 23:49:59 +0200 | [diff] [blame] | 81 | def _parse_network(self, network_str): |
| 82 | ''' |
| 83 | parse the options for all network interfaces of the vnf |
| 84 | :param network_str: (id=x,ip=x.x.x.x/x), ... |
| 85 | :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...] |
| 86 | ''' |
| 87 | nw_list = list() |
| 88 | networks = network_str[1:-1].split('),(') |
| 89 | for nw in networks: |
| 90 | nw_dict = dict(tuple(e.split('=')) for e in nw.split(',')) |
| 91 | nw_list.append(nw_dict) |
| 92 | |
| 93 | return nw_list |
| 94 | |
| 95 | |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 96 | |
| 97 | parser = argparse.ArgumentParser(description='son-emu compute') |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 98 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 99 | "command", |
| peusterm | d313dc1 | 2016-02-04 15:36:02 +0100 | [diff] [blame] | 100 | choices=['start', 'stop', 'list', 'status'], |
| 101 | help="Action to be executed.") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 102 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 103 | "--datacenter", "-d", dest="datacenter", |
| 104 | help="Data center to in which the compute instance should be executed") |
| peusterm | 7973f05 | 2016-01-29 14:38:05 +0100 | [diff] [blame] | 105 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 106 | "--name", "-n", dest="name", |
| 107 | help="Name of compute instance e.g. 'vnf1'") |
| 108 | parser.add_argument( |
| stevenvanrossem | 8fbf978 | 2016-02-17 11:40:23 +0100 | [diff] [blame] | 109 | "--image","-i", dest="image", |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 110 | help="Name of container image to be used e.g. 'ubuntu'") |
| 111 | parser.add_argument( |
| stevenvanrossem | 8fbf978 | 2016-02-17 11:40:23 +0100 | [diff] [blame] | 112 | "--dcmd", "-c", dest="docker_command", |
| 113 | help="Startup command of the container e.g. './start.sh'") |
| 114 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 115 | "--net", dest="network", |
| peusterm | 7f8e840 | 2016-02-28 18:38:10 +0100 | [diff] [blame] | 116 | help="Network properties of compute instance e.g. \ |
| 117 | '10.0.0.123/8' or '10.0.0.123/8,11.0.0.123/24' for multiple interfaces.") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 118 | |
| 119 | |
| 120 | def main(argv): |
| 121 | args = vars(parser.parse_args(argv)) |
| 122 | c = ZeroRpcClient() |
| 123 | c.execute_command(args) |