blob: d01dfed65d6003fcb15a4491a1591883b2774043 [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):
stevenvanrossem461941c2016-05-10 11:41:29 +020018 self.c = zerorpc.Client(heartbeat=None, timeout=120) #heartbeat=None, timeout=120
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"))
peusterm58310762016-01-12 17:09:20 +010033 r = self.c.compute_action_start(
peusterm45dce612016-01-29 15:34:38 +010034 args.get("datacenter"),
35 args.get("name"),
36 args.get("image"),
stevenvanrossem994245b2016-05-04 12:36:57 +020037 nw_list,
38 args.get("docker_command")
stevenvanrossem6b1d9b92016-05-02 13:10:40 +020039 )
peusterm58310762016-01-12 17:09:20 +010040 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):
peusterm2ec74e12016-01-13 11:17:53 +010048 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")
peusterm58310762016-01-12 17:09:20 +010075
76 def status(self, args):
peusterm2ec74e12016-01-13 11:17:53 +010077 r = self.c.compute_status(
78 args.get("datacenter"), args.get("name"))
79 pp.pprint(r)
peusterm58310762016-01-12 17:09:20 +010080
stevenvanrossem994245b2016-05-04 12:36:57 +020081 def profile(self, args):
82 nw_list = list()
83 if args.get("network") is not None:
84 nw_list = self._parse_network(args.get("network"))
stevenvanrossem5b376412016-05-04 15:34:49 +020085
86 params = self._create_dict(
87 network=nw_list,
88 command=args.get("docker_command"),
89 input=args.get("input"),
90 output=args.get("output"))
91
stevenvanrossem461941c2016-05-10 11:41:29 +020092 for output in self.c.compute_profile(
stevenvanrossem994245b2016-05-04 12:36:57 +020093 args.get("datacenter"),
94 args.get("name"),
95 args.get("image"),
stevenvanrossem5b376412016-05-04 15:34:49 +020096 params
stevenvanrossem461941c2016-05-10 11:41:29 +020097 ):
98 print(output + '\n')
99
100 #pp.pprint(r)
101 #print(r)
stevenvanrossem994245b2016-05-04 12:36:57 +0200102
stevenvanrossem5b376412016-05-04 15:34:49 +0200103 def _create_dict(self, **kwargs):
104 return kwargs
105
stevenvanrossem14c89052016-04-10 23:49:59 +0200106 def _parse_network(self, network_str):
107 '''
108 parse the options for all network interfaces of the vnf
109 :param network_str: (id=x,ip=x.x.x.x/x), ...
110 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
111 '''
112 nw_list = list()
113 networks = network_str[1:-1].split('),(')
114 for nw in networks:
115 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
116 nw_list.append(nw_dict)
117
118 return nw_list
119
120
peusterm58310762016-01-12 17:09:20 +0100121
122parser = argparse.ArgumentParser(description='son-emu compute')
peusterm58310762016-01-12 17:09:20 +0100123parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100124 "command",
stevenvanrossemedcbeeb2016-05-04 17:28:08 +0200125 choices=['start', 'stop', 'list', 'status', 'profile'],
peustermd313dc12016-02-04 15:36:02 +0100126 help="Action to be executed.")
peusterm58310762016-01-12 17:09:20 +0100127parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100128 "--datacenter", "-d", dest="datacenter",
129 help="Data center to in which the compute instance should be executed")
peusterm7973f052016-01-29 14:38:05 +0100130parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100131 "--name", "-n", dest="name",
132 help="Name of compute instance e.g. 'vnf1'")
133parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100134 "--image","-i", dest="image",
peusterm0dc3ae02016-04-27 09:33:28 +0200135 help="Name of container image to be used e.g. 'ubuntu:trusty'")
peusterm45dce612016-01-29 15:34:38 +0100136parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100137 "--dcmd", "-c", dest="docker_command",
138 help="Startup command of the container e.g. './start.sh'")
139parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100140 "--net", dest="network",
peusterm7f8e8402016-02-28 18:38:10 +0100141 help="Network properties of compute instance e.g. \
142 '10.0.0.123/8' or '10.0.0.123/8,11.0.0.123/24' for multiple interfaces.")
stevenvanrossem994245b2016-05-04 12:36:57 +0200143parser.add_argument(
144 "--input", "-in", dest="input",
145 help="input interface of the vnf to profile")
146parser.add_argument(
147 "--output", "-out", dest="output",
148 help="output interface of the vnf to profile")
peusterm58310762016-01-12 17:09:20 +0100149
150
151def main(argv):
152 args = vars(parser.parse_args(argv))
153 c = ZeroRpcClient()
154 c.execute_command(args)