642391775a1b533797343e1616a043e3959d53d6
[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 pp.pprint('nwlist1: {0}'.format(nw_list))
35
36 r = self.c.compute_action_start(
37 args.get("datacenter"),
38 args.get("name"),
39 args.get("image"),
40 nw_list,
41 args.get("docker_command")
42 )
43 pp.pprint(r)
44
45 def stop(self, args):
46 r = self.c.compute_action_stop(
47 args.get("datacenter"), args.get("name"))
48 pp.pprint(r)
49
50 def list(self, args):
51 r = self.c.compute_list(
52 args.get("datacenter"))
53 table = []
54 for c in r:
55 # for each container add a line to the output table
56 if len(c) > 1:
57 name = c[0]
58 status = c[1]
59 eth0ip = None
60 eth0status = "down"
61 if len(status.get("network")) > 0:
62 eth0ip = status.get("network")[0][1]
63 eth0status = "up" if status.get(
64 "network")[0][3] else "down"
65 table.append([status.get("datacenter"),
66 name,
67 status.get("image"),
68 eth0ip,
69 eth0status,
70 status.get("state").get("Status")])
71 headers = ["Datacenter",
72 "Container",
73 "Image",
74 "eth0 IP",
75 "eth0 status",
76 "Status"]
77 print tabulate(table, headers=headers, tablefmt="grid")
78
79 def status(self, args):
80 r = self.c.compute_status(
81 args.get("datacenter"), args.get("name"))
82 pp.pprint(r)
83
84 def profile(self, args):
85 nw_list = list()
86 if args.get("network") is not None:
87 nw_list = self._parse_network(args.get("network"))
88 logging.info('nwlist: {0}'.format(nw_list))
89 r = self.c.compute_profile(
90 args.get("datacenter"),
91 args.get("name"),
92 args.get("image"),
93 network=nw_list,
94 command=args.get("docker_command"),
95 input=args.get("input"),
96 output=args.get("output")
97 )
98 pp.pprint(r)
99
100 def _parse_network(self, network_str):
101 '''
102 parse the options for all network interfaces of the vnf
103 :param network_str: (id=x,ip=x.x.x.x/x), ...
104 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
105 '''
106 nw_list = list()
107 networks = network_str[1:-1].split('),(')
108 for nw in networks:
109 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
110 nw_list.append(nw_dict)
111
112 return nw_list
113
114
115
116 parser = argparse.ArgumentParser(description='son-emu compute')
117 parser.add_argument(
118 "command",
119 choices=['start', 'stop', 'list', 'status'],
120 help="Action to be executed.")
121 parser.add_argument(
122 "--datacenter", "-d", dest="datacenter",
123 help="Data center to in which the compute instance should be executed")
124 parser.add_argument(
125 "--name", "-n", dest="name",
126 help="Name of compute instance e.g. 'vnf1'")
127 parser.add_argument(
128 "--image","-i", dest="image",
129 help="Name of container image to be used e.g. 'ubuntu:trusty'")
130 parser.add_argument(
131 "--dcmd", "-c", dest="docker_command",
132 help="Startup command of the container e.g. './start.sh'")
133 parser.add_argument(
134 "--net", dest="network",
135 help="Network properties of compute instance e.g. \
136 '10.0.0.123/8' or '10.0.0.123/8,11.0.0.123/24' for multiple interfaces.")
137 parser.add_argument(
138 "--input", "-in", dest="input",
139 help="input interface of the vnf to profile")
140 parser.add_argument(
141 "--output", "-out", dest="output",
142 help="output interface of the vnf to profile")
143
144
145 def main(argv):
146 args = vars(parser.parse_args(argv))
147 c = ZeroRpcClient()
148 c.execute_command(args)