blob: 25ac2477598197d101ca7aef9ac8848f140e0967 [file] [log] [blame]
peusterm58310762016-01-12 17:09:20 +01001"""
peusterm79ef6ae2016-07-08 13:53:57 +02002Copyright (c) 2015 SONATA-NFV and Paderborn University
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
peusterm58310762016-01-12 17:09:20 +010027"""
28
29import argparse
30import pprint
peusterm2ec74e12016-01-13 11:17:53 +010031from tabulate import tabulate
peusterm58310762016-01-12 17:09:20 +010032import zerorpc
33
34
35pp = pprint.PrettyPrinter(indent=4)
36
37
38class ZeroRpcClient(object):
39
40 def __init__(self):
stevenvanrossem461941c2016-05-10 11:41:29 +020041 self.c = zerorpc.Client(heartbeat=None, timeout=120) #heartbeat=None, timeout=120
peusterm2ec74e12016-01-13 11:17:53 +010042 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 +010043 self.cmds = {}
44
45 def execute_command(self, args):
46 if getattr(self, args["command"]) is not None:
47 # call the local method with the same name as the command arg
48 getattr(self, args["command"])(args)
49 else:
stevenvanrossem49378152016-05-19 11:33:48 +020050 print("Command not implemented.")
peusterm58310762016-01-12 17:09:20 +010051
52 def start(self, args):
peusterm7f8e8402016-02-28 18:38:10 +010053 nw_list = list()
peusterm45dce612016-01-29 15:34:38 +010054 if args.get("network") is not None:
stevenvanrossem14c89052016-04-10 23:49:59 +020055 nw_list = self._parse_network(args.get("network"))
peusterm58310762016-01-12 17:09:20 +010056 r = self.c.compute_action_start(
peusterm45dce612016-01-29 15:34:38 +010057 args.get("datacenter"),
58 args.get("name"),
59 args.get("image"),
stevenvanrossem994245b2016-05-04 12:36:57 +020060 nw_list,
61 args.get("docker_command")
stevenvanrossem6b1d9b92016-05-02 13:10:40 +020062 )
peusterm58310762016-01-12 17:09:20 +010063 pp.pprint(r)
64
65 def stop(self, args):
66 r = self.c.compute_action_stop(
67 args.get("datacenter"), args.get("name"))
68 pp.pprint(r)
69
70 def list(self, args):
peusterm2ec74e12016-01-13 11:17:53 +010071 r = self.c.compute_list(
72 args.get("datacenter"))
73 table = []
74 for c in r:
75 # for each container add a line to the output table
76 if len(c) > 1:
77 name = c[0]
78 status = c[1]
79 eth0ip = None
80 eth0status = "down"
81 if len(status.get("network")) > 0:
peusterma3ddeba2016-05-13 14:40:02 +020082 eth0ip = status.get("network")[0].get("ip")
peusterm2ec74e12016-01-13 11:17:53 +010083 eth0status = "up" if status.get(
peusterma3ddeba2016-05-13 14:40:02 +020084 "network")[0].get("up") else "down"
peusterm2ec74e12016-01-13 11:17:53 +010085 table.append([status.get("datacenter"),
86 name,
87 status.get("image"),
88 eth0ip,
89 eth0status,
90 status.get("state").get("Status")])
91 headers = ["Datacenter",
92 "Container",
93 "Image",
94 "eth0 IP",
95 "eth0 status",
96 "Status"]
stevenvanrossem49378152016-05-19 11:33:48 +020097 print(tabulate(table, headers=headers, tablefmt="grid"))
peusterm58310762016-01-12 17:09:20 +010098
99 def status(self, args):
peusterm2ec74e12016-01-13 11:17:53 +0100100 r = self.c.compute_status(
101 args.get("datacenter"), args.get("name"))
102 pp.pprint(r)
peusterm58310762016-01-12 17:09:20 +0100103
stevenvanrossem994245b2016-05-04 12:36:57 +0200104 def profile(self, args):
105 nw_list = list()
106 if args.get("network") is not None:
107 nw_list = self._parse_network(args.get("network"))
stevenvanrossem5b376412016-05-04 15:34:49 +0200108
109 params = self._create_dict(
110 network=nw_list,
111 command=args.get("docker_command"),
stevenvanrossembb084ef2016-05-20 09:27:01 +0200112 image=args.get("image"),
stevenvanrossem5b376412016-05-04 15:34:49 +0200113 input=args.get("input"),
114 output=args.get("output"))
115
stevenvanrossem461941c2016-05-10 11:41:29 +0200116 for output in self.c.compute_profile(
stevenvanrossem994245b2016-05-04 12:36:57 +0200117 args.get("datacenter"),
118 args.get("name"),
stevenvanrossembb084ef2016-05-20 09:27:01 +0200119 params):
stevenvanrossem461941c2016-05-10 11:41:29 +0200120 print(output + '\n')
121
122 #pp.pprint(r)
123 #print(r)
stevenvanrossem994245b2016-05-04 12:36:57 +0200124
stevenvanrossem5b376412016-05-04 15:34:49 +0200125 def _create_dict(self, **kwargs):
126 return kwargs
127
stevenvanrossem14c89052016-04-10 23:49:59 +0200128 def _parse_network(self, network_str):
129 '''
130 parse the options for all network interfaces of the vnf
131 :param network_str: (id=x,ip=x.x.x.x/x), ...
132 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
133 '''
134 nw_list = list()
135 networks = network_str[1:-1].split('),(')
136 for nw in networks:
137 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
138 nw_list.append(nw_dict)
139
140 return nw_list
141
142
peusterm58310762016-01-12 17:09:20 +0100143
144parser = argparse.ArgumentParser(description='son-emu compute')
peusterm58310762016-01-12 17:09:20 +0100145parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100146 "command",
stevenvanrossemedcbeeb2016-05-04 17:28:08 +0200147 choices=['start', 'stop', 'list', 'status', 'profile'],
peustermd313dc12016-02-04 15:36:02 +0100148 help="Action to be executed.")
peusterm58310762016-01-12 17:09:20 +0100149parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100150 "--datacenter", "-d", dest="datacenter",
151 help="Data center to in which the compute instance should be executed")
peusterm7973f052016-01-29 14:38:05 +0100152parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100153 "--name", "-n", dest="name",
154 help="Name of compute instance e.g. 'vnf1'")
155parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100156 "--image","-i", dest="image",
peusterm0dc3ae02016-04-27 09:33:28 +0200157 help="Name of container image to be used e.g. 'ubuntu:trusty'")
peusterm45dce612016-01-29 15:34:38 +0100158parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100159 "--dcmd", "-c", dest="docker_command",
160 help="Startup command of the container e.g. './start.sh'")
161parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100162 "--net", dest="network",
stevenvanrossem49378152016-05-19 11:33:48 +0200163 help="Network properties of a compute instance e.g. \
164 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
stevenvanrossem994245b2016-05-04 12:36:57 +0200165parser.add_argument(
166 "--input", "-in", dest="input",
167 help="input interface of the vnf to profile")
168parser.add_argument(
169 "--output", "-out", dest="output",
170 help="output interface of the vnf to profile")
peusterm58310762016-01-12 17:09:20 +0100171
172
173def main(argv):
174 args = vars(parser.parse_args(argv))
175 c = ZeroRpcClient()
176 c.execute_command(args)