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 responce
= put("%s/restapi/compute/%s/%s/start" %
31 (args
.get("endpoint"),
32 args
.get("datacenter"),
34 json
= json
.dumps(req
))
35 pp
.pprint(responce
.json())
38 responce
= get("%s/restapi/compute/%s/%s/stop" %
39 (args
.get("endpoint"),
40 args
.get("datacenter"),
42 pp
.pprint(responce
.json())
46 list = get('%s/restapi/compute/%s' % (args
.get("endpoint"),args
.get('datacenter'))).json()
50 # for each container add a line to the output table
56 if len(status
.get("network")) > 0:
57 eth0ip
= status
.get("network")[0].get("ip")
58 eth0status
= "up" if status
.get(
59 "network")[0].get("up") else "down"
60 table
.append([status
.get("datacenter"),
65 status
.get("state").get("Status")])
67 headers
= ["Datacenter",
73 print(tabulate(table
, headers
=headers
, tablefmt
="grid"))
75 def status(self
,args
):
77 list = get("%s/restapi/compute/%s/%s" %
78 (args
.get("endpoint"),
79 args
.get("datacenter"),
80 args
.get("name"))).json()
85 def _parse_network(self
, network_str
):
87 parse the options for all network interfaces of the vnf
88 :param network_str: (id=x,ip=x.x.x.x/x), ...
89 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
92 networks
= network_str
[1:-1].split('),(')
94 nw_dict
= dict(tuple(e
.split('=')) for e
in nw
.split(','))
95 nw_list
.append(nw_dict
)
100 parser
= argparse
.ArgumentParser(description
='son-emu datacenter')
103 choices
=['start', 'stop', 'list', 'status', 'profile'],
104 help="Action to be executed.")
106 "--datacenter", "-d", dest
="datacenter",
107 help="Data center to which the command should be applied.")
109 "--name", "-n", dest
="name",
110 help="Name of compute instance e.g. 'vnf1'.")
112 "--image","-i", dest
="image",
113 help="Name of container image to be used e.g. 'ubuntu:trusty'")
115 "--dcmd", "-c", dest
="docker_command",
116 help="Startup command of the container e.g. './start.sh'")
118 "--net", dest
="network",
119 help="Network properties of a compute instance e.g. \
120 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
122 "--input", "-in", dest
="input",
123 help="input interface of the vnf to profile")
125 "--output", "-out", dest
="output",
126 help="output interface of the vnf to profile")
128 "--endpoint", "-e", dest
="endpoint",
129 default
="http://127.0.0.1:5000",
130 help="UUID of the plugin to be manipulated.")
133 args
= vars(parser
.parse_args(argv
))
135 c
.execute_command(args
)