blob: b5f775ff79ac2891f464ddcc8d0057cd147185a9 [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
stevenvanrossem5b376412016-05-04 15:34:49 +0200104 def _create_dict(self, **kwargs):
105 return kwargs
106
stevenvanrossem14c89052016-04-10 23:49:59 +0200107 def _parse_network(self, network_str):
108 '''
109 parse the options for all network interfaces of the vnf
110 :param network_str: (id=x,ip=x.x.x.x/x), ...
111 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
112 '''
113 nw_list = list()
114 networks = network_str[1:-1].split('),(')
115 for nw in networks:
116 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
117 nw_list.append(nw_dict)
118
119 return nw_list
120
121
peusterm58310762016-01-12 17:09:20 +0100122
123parser = argparse.ArgumentParser(description='son-emu compute')
peusterm58310762016-01-12 17:09:20 +0100124parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100125 "command",
stevenvanrosseme131bf52016-07-14 11:42:09 +0200126 choices=['start', 'stop', 'list', 'status'],
peustermd313dc12016-02-04 15:36:02 +0100127 help="Action to be executed.")
peusterm58310762016-01-12 17:09:20 +0100128parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100129 "--datacenter", "-d", dest="datacenter",
130 help="Data center to in which the compute instance should be executed")
peusterm7973f052016-01-29 14:38:05 +0100131parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100132 "--name", "-n", dest="name",
133 help="Name of compute instance e.g. 'vnf1'")
134parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100135 "--image","-i", dest="image",
peusterm0dc3ae02016-04-27 09:33:28 +0200136 help="Name of container image to be used e.g. 'ubuntu:trusty'")
peusterm45dce612016-01-29 15:34:38 +0100137parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100138 "--dcmd", "-c", dest="docker_command",
139 help="Startup command of the container e.g. './start.sh'")
140parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +0100141 "--net", dest="network",
stevenvanrossem49378152016-05-19 11:33:48 +0200142 help="Network properties of a compute instance e.g. \
143 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
peusterm58310762016-01-12 17:09:20 +0100144
145def main(argv):
146 args = vars(parser.parse_args(argv))
147 c = ZeroRpcClient()
148 c.execute_command(args)