name docker interfaces according to start_compute option
[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()
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
34 r = self.c.compute_action_start(
35 args.get("datacenter"),
36 args.get("name"),
37 args.get("image"),
38 nw_list,
39 args.get("docker_command")
40 )
41 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):
49 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")
76
77 def status(self, args):
78 r = self.c.compute_status(
79 args.get("datacenter"), args.get("name"))
80 pp.pprint(r)
81
82 def profile(self, args):
83 nw_list = list()
84 if args.get("network") is not None:
85 nw_list = self._parse_network(args.get("network"))
86
87 params = self._create_dict(
88 network=nw_list,
89 command=args.get("docker_command"),
90 input=args.get("input"),
91 output=args.get("output"))
92
93 r = self.c.compute_profile(
94 args.get("datacenter"),
95 args.get("name"),
96 args.get("image"),
97 params
98 )
99 pp.pprint(r)
100
101 def _create_dict(self, **kwargs):
102 return kwargs
103
104 def _parse_network(self, network_str):
105 '''
106 parse the options for all network interfaces of the vnf
107 :param network_str: (id=x,ip=x.x.x.x/x), ...
108 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
109 '''
110 nw_list = list()
111 networks = network_str[1:-1].split('),(')
112 for nw in networks:
113 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
114 nw_list.append(nw_dict)
115
116 return nw_list
117
118
119
120 parser = argparse.ArgumentParser(description='son-emu compute')
121 parser.add_argument(
122 "command",
123 choices=['start', 'stop', 'list', 'status'],
124 help="Action to be executed.")
125 parser.add_argument(
126 "--datacenter", "-d", dest="datacenter",
127 help="Data center to in which the compute instance should be executed")
128 parser.add_argument(
129 "--name", "-n", dest="name",
130 help="Name of compute instance e.g. 'vnf1'")
131 parser.add_argument(
132 "--image","-i", dest="image",
133 help="Name of container image to be used e.g. 'ubuntu:trusty'")
134 parser.add_argument(
135 "--dcmd", "-c", dest="docker_command",
136 help="Startup command of the container e.g. './start.sh'")
137 parser.add_argument(
138 "--net", dest="network",
139 help="Network properties of compute instance e.g. \
140 '10.0.0.123/8' or '10.0.0.123/8,11.0.0.123/24' for multiple interfaces.")
141 parser.add_argument(
142 "--input", "-in", dest="input",
143 help="input interface of the vnf to profile")
144 parser.add_argument(
145 "--output", "-out", dest="output",
146 help="output interface of the vnf to profile")
147
148
149 def main(argv):
150 args = vars(parser.parse_args(argv))
151 c = ZeroRpcClient()
152 c.execute_command(args)