blob: f2fdc62e8d20c6f890cf5a8f1322f9aa8253349e [file] [log] [blame]
peusterm58310762016-01-12 17:09:20 +01001"""
2son-emu compute CLI
peusterm2ec74e12016-01-13 11:17:53 +01003(c) 2016 by Manuel Peuster <manuel.peuster@upb.de>
peusterm58310762016-01-12 17:09:20 +01004"""
5
6import argparse
7import pprint
peusterm2ec74e12016-01-13 11:17:53 +01008from tabulate import tabulate
peusterm58310762016-01-12 17:09:20 +01009import zerorpc
10
11
12pp = pprint.PrettyPrinter(indent=4)
13
14
15class ZeroRpcClient(object):
16
17 def __init__(self):
18 self.c = zerorpc.Client()
peusterm2ec74e12016-01-13 11:17:53 +010019 self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
peusterm58310762016-01-12 17:09:20 +010020 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):
peusterm7f8e8402016-02-28 18:38:10 +010030 nw_list = list()
peusterm45dce612016-01-29 15:34:38 +010031 if args.get("network") is not None:
stevenvanrossem14c89052016-04-10 23:49:59 +020032 nw_list = self._parse_network(args.get("network"))
33
peusterm58310762016-01-12 17:09:20 +010034 r = self.c.compute_action_start(
peusterm45dce612016-01-29 15:34:38 +010035 args.get("datacenter"),
36 args.get("name"),
37 args.get("image"),
stevenvanrossem6b1d9b92016-05-02 13:10:40 +020038 network=nw_list,
39 command=args.get("docker_command")
40 )
peusterm58310762016-01-12 17:09:20 +010041 pp.pprint(r)
42
43 def stop(self, args):
44 r = self.c.compute_action_stop(
45 args.get("datacenter"), args.get("name"))
46 pp.pprint(r)
47
48 def list(self, args):
peusterm2ec74e12016-01-13 11:17:53 +010049 r = self.c.compute_list(
50 args.get("datacenter"))
51 table = []
52 for c in r:
53 # for each container add a line to the output table
54 if len(c) > 1:
55 name = c[0]
56 status = c[1]
57 eth0ip = None
58 eth0status = "down"
59 if len(status.get("network")) > 0:
60 eth0ip = status.get("network")[0][1]
61 eth0status = "up" if status.get(
62 "network")[0][3] else "down"
63 table.append([status.get("datacenter"),
64 name,
65 status.get("image"),
66 eth0ip,
67 eth0status,
68 status.get("state").get("Status")])
69 headers = ["Datacenter",
70 "Container",
71 "Image",
72 "eth0 IP",
73 "eth0 status",
74 "Status"]
75 print tabulate(table, headers=headers, tablefmt="grid")
peusterm58310762016-01-12 17:09:20 +010076
77 def status(self, args):
peusterm2ec74e12016-01-13 11:17:53 +010078 r = self.c.compute_status(
79 args.get("datacenter"), args.get("name"))
80 pp.pprint(r)
peusterm58310762016-01-12 17:09:20 +010081
stevenvanrossem14c89052016-04-10 23:49:59 +020082 def _parse_network(self, network_str):
83 '''
84 parse the options for all network interfaces of the vnf
85 :param network_str: (id=x,ip=x.x.x.x/x), ...
86 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
87 '''
88 nw_list = list()
89 networks = network_str[1:-1].split('),(')
90 for nw in networks:
91 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
92 nw_list.append(nw_dict)
93
94 return nw_list
95
96
peusterm58310762016-01-12 17:09:20 +010097
98parser = argparse.ArgumentParser(description='son-emu compute')
peusterm58310762016-01-12 17:09:20 +010099parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100100 "command",
peustermd313dc12016-02-04 15:36:02 +0100101 choices=['start', 'stop', 'list', 'status'],
102 help="Action to be executed.")
peusterm58310762016-01-12 17:09:20 +0100103parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100104 "--datacenter", "-d", dest="datacenter",
105 help="Data center to in which the compute instance should be executed")
peusterm7973f052016-01-29 14:38:05 +0100106parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100107 "--name", "-n", dest="name",
108 help="Name of compute instance e.g. 'vnf1'")
109parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100110 "--image","-i", dest="image",
peusterm0dc3ae02016-04-27 09:33:28 +0200111 help="Name of container image to be used e.g. 'ubuntu:trusty'")
peusterm45dce612016-01-29 15:34:38 +0100112parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100113 "--dcmd", "-c", dest="docker_command",
114 help="Startup command of the container e.g. './start.sh'")
115parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100116 "--net", dest="network",
peusterm7f8e8402016-02-28 18:38:10 +0100117 help="Network properties of compute instance e.g. \
118 '10.0.0.123/8' or '10.0.0.123/8,11.0.0.123/24' for multiple interfaces.")
peusterm58310762016-01-12 17:09:20 +0100119
120
121def main(argv):
122 args = vars(parser.parse_args(argv))
123 c = ZeroRpcClient()
124 c.execute_command(args)