Merge pull request #131 from stevenvanrossem/master
[osm/vim-emu.git] / src / emuvim / cli / compute.py
1 """
2 son-emu compute CLI
3 (c) 2016 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5
6 import argparse
7 import pprint
8 from tabulate import tabulate
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(heartbeat=None, timeout=120) #heartbeat=None, timeout=120
19 self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
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):
30 nw_list = list()
31 if args.get("network") is not None:
32 nw_list = self._parse_network(args.get("network"))
33 r = self.c.compute_action_start(
34 args.get("datacenter"),
35 args.get("name"),
36 args.get("image"),
37 nw_list,
38 args.get("docker_command")
39 )
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):
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].get("ip")
60 eth0status = "up" if status.get(
61 "network")[0].get("up") 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"))
75
76 def status(self, args):
77 r = self.c.compute_status(
78 args.get("datacenter"), args.get("name"))
79 pp.pprint(r)
80
81 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"))
85
86 params = self._create_dict(
87 network=nw_list,
88 command=args.get("docker_command"),
89 image=args.get("image"),
90 input=args.get("input"),
91 output=args.get("output"))
92
93 for output in self.c.compute_profile(
94 args.get("datacenter"),
95 args.get("name"),
96 params):
97 print(output + '\n')
98
99 #pp.pprint(r)
100 #print(r)
101
102 def _create_dict(self, **kwargs):
103 return kwargs
104
105 def _parse_network(self, network_str):
106 '''
107 parse the options for all network interfaces of the vnf
108 :param network_str: (id=x,ip=x.x.x.x/x), ...
109 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
110 '''
111 nw_list = list()
112 networks = network_str[1:-1].split('),(')
113 for nw in networks:
114 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
115 nw_list.append(nw_dict)
116
117 return nw_list
118
119
120
121 parser = argparse.ArgumentParser(description='son-emu compute')
122 parser.add_argument(
123 "command",
124 choices=['start', 'stop', 'list', 'status', 'profile'],
125 help="Action to be executed.")
126 parser.add_argument(
127 "--datacenter", "-d", dest="datacenter",
128 help="Data center to in which the compute instance should be executed")
129 parser.add_argument(
130 "--name", "-n", dest="name",
131 help="Name of compute instance e.g. 'vnf1'")
132 parser.add_argument(
133 "--image","-i", dest="image",
134 help="Name of container image to be used e.g. 'ubuntu:trusty'")
135 parser.add_argument(
136 "--dcmd", "-c", dest="docker_command",
137 help="Startup command of the container e.g. './start.sh'")
138 parser.add_argument(
139 "--net", dest="network",
140 help="Network properties of a compute instance e.g. \
141 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
142 parser.add_argument(
143 "--input", "-in", dest="input",
144 help="input interface of the vnf to profile")
145 parser.add_argument(
146 "--output", "-out", dest="output",
147 help="output interface of the vnf to profile")
148
149
150 def main(argv):
151 args = vars(parser.parse_args(argv))
152 c = ZeroRpcClient()
153 c.execute_command(args)