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 network
= {"ip": args
.get("network")}
33 r
= self
.c
.compute_action_start(
34 args
.get("datacenter"),
37 args
.get("docker_command"),
42 r
= self
.c
.compute_action_stop(
43 args
.get("datacenter"), args
.get("name"))
47 r
= self
.c
.compute_list(
48 args
.get("datacenter"))
51 # for each container add a line to the output table
57 if len(status
.get("network")) > 0:
58 eth0ip
= status
.get("network")[0][1]
59 eth0status
= "up" if status
.get(
60 "network")[0][3] else "down"
61 table
.append([status
.get("datacenter"),
66 status
.get("state").get("Status")])
67 headers
= ["Datacenter",
73 print tabulate(table
, headers
=headers
, tablefmt
="grid")
75 def status(self
, args
):
76 r
= self
.c
.compute_status(
77 args
.get("datacenter"), args
.get("name"))
81 parser
= argparse
.ArgumentParser(description
='son-emu compute')
84 choices
=['start', 'stop', 'list', 'status'],
85 help="Action to be executed.")
87 "--datacenter", "-d", dest
="datacenter",
88 help="Data center to in which the compute instance should be executed")
90 "--name", "-n", dest
="name",
91 help="Name of compute instance e.g. 'vnf1'")
93 "--image","-i", dest
="image",
94 help="Name of container image to be used e.g. 'ubuntu'")
96 "--dcmd", "-c", dest
="docker_command",
97 help="Startup command of the container e.g. './start.sh'")
99 "--net", dest
="network",
100 help="Network properties of compute instance e.g. '10.0.0.123/8'")
104 args
= vars(parser
.parse_args(argv
))
106 c
.execute_command(args
)