f2fdc62e8d20c6f890cf5a8f1322f9aa8253349e
3 (c) 2016 by Manuel Peuster <manuel.peuster@upb.de>
8 from tabulate
import tabulate
12 pp
= pprint
.PrettyPrinter(indent
=4)
15 class ZeroRpcClient(object):
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
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
)
27 print "Command not implemented."
29 def start(self
, args
):
31 if args
.get("network") is not None:
32 nw_list
= self
._parse
_network
(args
.get("network"))
34 r
= self
.c
.compute_action_start(
35 args
.get("datacenter"),
39 command
=args
.get("docker_command")
44 r
= self
.c
.compute_action_stop(
45 args
.get("datacenter"), args
.get("name"))
49 r
= self
.c
.compute_list(
50 args
.get("datacenter"))
53 # for each container add a line to the output table
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"),
68 status
.get("state").get("Status")])
69 headers
= ["Datacenter",
75 print tabulate(table
, headers
=headers
, tablefmt
="grid")
77 def status(self
, args
):
78 r
= self
.c
.compute_status(
79 args
.get("datacenter"), args
.get("name"))
82 def _parse_network(self
, network_str
):
84 parse the options for all network interfaces of the vnf
85 :param network_str: (id=x,ip=x.x.x.x/x), ...
86 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
89 networks
= network_str
[1:-1].split('),(')
91 nw_dict
= dict(tuple(e
.split('=')) for e
in nw
.split(','))
92 nw_list
.append(nw_dict
)
98 parser
= argparse
.ArgumentParser(description
='son-emu compute')
101 choices
=['start', 'stop', 'list', 'status'],
102 help="Action to be executed.")
104 "--datacenter", "-d", dest
="datacenter",
105 help="Data center to in which the compute instance should be executed")
107 "--name", "-n", dest
="name",
108 help="Name of compute instance e.g. 'vnf1'")
110 "--image","-i", dest
="image",
111 help="Name of container image to be used e.g. 'ubuntu:trusty'")
113 "--dcmd", "-c", dest
="docker_command",
114 help="Startup command of the container e.g. './start.sh'")
116 "--net", dest
="network",
117 help="Network properties of compute instance e.g. \
118 '10.0.0.123/8' or '10.0.0.123/8,11.0.0.123/24' for multiple interfaces.")
122 args
= vars(parser
.parse_args(argv
))
124 c
.execute_command(args
)