| 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 | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 30 | network = {} |
| 31 | if args.get("network") is not None: |
| 32 | network = {"ip": args.get("network")} |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 33 | r = self.c.compute_action_start( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 34 | args.get("datacenter"), |
| 35 | args.get("name"), |
| 36 | args.get("image"), |
| 37 | network) |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 38 | pp.pprint(r) |
| 39 | |
| 40 | def stop(self, args): |
| 41 | r = self.c.compute_action_stop( |
| 42 | args.get("datacenter"), args.get("name")) |
| 43 | pp.pprint(r) |
| 44 | |
| 45 | def list(self, args): |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 46 | r = self.c.compute_list( |
| 47 | args.get("datacenter")) |
| 48 | table = [] |
| 49 | for c in r: |
| 50 | # for each container add a line to the output table |
| 51 | if len(c) > 1: |
| 52 | name = c[0] |
| 53 | status = c[1] |
| 54 | eth0ip = None |
| 55 | eth0status = "down" |
| 56 | if len(status.get("network")) > 0: |
| 57 | eth0ip = status.get("network")[0][1] |
| 58 | eth0status = "up" if status.get( |
| 59 | "network")[0][3] else "down" |
| 60 | table.append([status.get("datacenter"), |
| 61 | name, |
| 62 | status.get("image"), |
| 63 | eth0ip, |
| 64 | eth0status, |
| 65 | status.get("state").get("Status")]) |
| 66 | headers = ["Datacenter", |
| 67 | "Container", |
| 68 | "Image", |
| 69 | "eth0 IP", |
| 70 | "eth0 status", |
| 71 | "Status"] |
| 72 | print tabulate(table, headers=headers, tablefmt="grid") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 73 | |
| 74 | def status(self, args): |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 75 | r = self.c.compute_status( |
| 76 | args.get("datacenter"), args.get("name")) |
| 77 | pp.pprint(r) |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 78 | |
| 79 | |
| 80 | parser = argparse.ArgumentParser(description='son-emu compute') |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 81 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 82 | "command", |
| peusterm | d313dc1 | 2016-02-04 15:36:02 +0100 | [diff] [blame^] | 83 | choices=['start', 'stop', 'list', 'status'], |
| 84 | help="Action to be executed.") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 85 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 86 | "--datacenter", "-d", dest="datacenter", |
| 87 | help="Data center to in which the compute instance should be executed") |
| peusterm | 7973f05 | 2016-01-29 14:38:05 +0100 | [diff] [blame] | 88 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 89 | "--name", "-n", dest="name", |
| 90 | help="Name of compute instance e.g. 'vnf1'") |
| 91 | parser.add_argument( |
| 92 | "--image", dest="image", |
| 93 | help="Name of container image to be used e.g. 'ubuntu'") |
| 94 | parser.add_argument( |
| 95 | "--net", dest="network", |
| 96 | help="Network properties of compute instance e.g. '10.0.0.123/8'") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 97 | |
| 98 | |
| 99 | def main(argv): |
| 100 | args = vars(parser.parse_args(argv)) |
| 101 | c = ZeroRpcClient() |
| 102 | c.execute_command(args) |