1 from requests
import get
,put
2 from tabulate
import tabulate
7 pp
= pprint
.PrettyPrinter(indent
=4)
14 def execute_command(self
, args
):
15 if getattr(self
, args
["command"]) is not None:
16 # call the local method with the same name as the command arg
17 getattr(self
, args
["command"])(args
)
19 print("Command not implemented.")
21 def start(self
, args
):
24 if args
.get("network") is not None:
25 nw_list
= self
._parse
_network
(args
.get("network"))
26 req
= {'image':args
.get("image"),
27 'command':args
.get("docker_command"),
30 response
= put("%s/restapi/compute/%s/%s/start" %
31 (args
.get("endpoint"),
32 args
.get("datacenter"),
34 json
= json
.dumps(req
))
35 pp
.pprint(response
.json())
39 response
= get("%s/restapi/compute/%s/%s/stop" %
40 (args
.get("endpoint"),
41 args
.get("datacenter"),
43 pp
.pprint(response
.json())
47 list = get('%s/restapi/compute/%s' % (args
.get("endpoint"),args
.get('datacenter'))).json()
51 # for each container add a line to the output table
57 if len(status
.get("network")) > 0:
58 eth0ip
= status
.get("network")[0].get("ip")
59 eth0status
= "up" if status
.get(
60 "network")[0].get("up") else "down"
61 table
.append([status
.get("datacenter"),
66 status
.get("state").get("Status")])
68 headers
= ["Datacenter",
74 print(tabulate(table
, headers
=headers
, tablefmt
="grid"))
76 def status(self
,args
):
78 list = get("%s/restapi/compute/%s/%s" %
79 (args
.get("endpoint"),
80 args
.get("datacenter"),
81 args
.get("name"))).json()
86 def _parse_network(self
, network_str
):
88 parse the options for all network interfaces of the vnf
89 :param network_str: (id=x,ip=x.x.x.x/x), ...
90 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
93 networks
= network_str
[1:-1].split('),(')
95 nw_dict
= dict(tuple(e
.split('=')) for e
in nw
.split(','))
96 nw_list
.append(nw_dict
)
101 parser
= argparse
.ArgumentParser(description
='son-emu datacenter')
104 choices
=['start', 'stop', 'list', 'status'],
105 help="Action to be executed.")
107 "--datacenter", "-d", dest
="datacenter",
108 help="Data center to which the command should be applied.")
110 "--name", "-n", dest
="name",
111 help="Name of compute instance e.g. 'vnf1'.")
113 "--image","-i", dest
="image",
114 help="Name of container image to be used e.g. 'ubuntu:trusty'")
116 "--dcmd", "-c", dest
="docker_command",
117 help="Startup command of the container e.g. './start.sh'")
119 "--net", dest
="network",
120 help="Network properties of a compute instance e.g. \
121 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
123 "--endpoint", "-e", dest
="endpoint",
124 default
="http://127.0.0.1:5000",
125 help="UUID of the plugin to be manipulated.")
128 args
= vars(parser
.parse_args(argv
))
130 c
.execute_command(args
)