c09647569e75b739d8637a872780086a21fc1498
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(heartbeat
=None, timeout
=120) #heartbeat=None, timeout=120
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"))
33 r
= self
.c
.compute_action_start(
34 args
.get("datacenter"),
38 args
.get("docker_command")
43 r
= self
.c
.compute_action_stop(
44 args
.get("datacenter"), args
.get("name"))
48 r
= self
.c
.compute_list(
49 args
.get("datacenter"))
52 # for each container add a line to the output table
58 if len(status
.get("network")) > 0:
59 eth0ip
= status
.get("network")[0].get("ip")
60 eth0status
= "up" if status
.get(
61 "network")[0].get("up") else "down"
62 table
.append([status
.get("datacenter"),
67 status
.get("state").get("Status")])
68 headers
= ["Datacenter",
74 print(tabulate(table
, headers
=headers
, tablefmt
="grid"))
76 def status(self
, args
):
77 r
= self
.c
.compute_status(
78 args
.get("datacenter"), args
.get("name"))
81 def profile(self
, args
):
83 if args
.get("network") is not None:
84 nw_list
= self
._parse
_network
(args
.get("network"))
86 params
= self
._create
_dict
(
88 command
=args
.get("docker_command"),
89 input=args
.get("input"),
90 output
=args
.get("output"))
92 for output
in self
.c
.compute_profile(
93 args
.get("datacenter"),
103 def _create_dict(self
, **kwargs
):
106 def _parse_network(self
, network_str
):
108 parse the options for all network interfaces of the vnf
109 :param network_str: (id=x,ip=x.x.x.x/x), ...
110 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
113 networks
= network_str
[1:-1].split('),(')
115 nw_dict
= dict(tuple(e
.split('=')) for e
in nw
.split(','))
116 nw_list
.append(nw_dict
)
122 parser
= argparse
.ArgumentParser(description
='son-emu compute')
125 choices
=['start', 'stop', 'list', 'status', 'profile'],
126 help="Action to be executed.")
128 "--datacenter", "-d", dest
="datacenter",
129 help="Data center to in which the compute instance should be executed")
131 "--name", "-n", dest
="name",
132 help="Name of compute instance e.g. 'vnf1'")
134 "--image","-i", dest
="image",
135 help="Name of container image to be used e.g. 'ubuntu:trusty'")
137 "--dcmd", "-c", dest
="docker_command",
138 help="Startup command of the container e.g. './start.sh'")
140 "--net", dest
="network",
141 help="Network properties of a compute instance e.g. \
142 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
144 "--input", "-in", dest
="input",
145 help="input interface of the vnf to profile")
147 "--output", "-out", dest
="output",
148 help="output interface of the vnf to profile")
152 args
= vars(parser
.parse_args(argv
))
154 c
.execute_command(args
)