1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 from requests
import get
, put
, delete
27 from tabulate
import tabulate
30 from subprocess
import Popen
32 pp
= pprint
.PrettyPrinter(indent
=4)
35 class RestApiClient():
39 def execute_command(self
, args
):
40 if getattr(self
, args
["command"]) is not None:
41 # call the local method with the same name as the command arg
42 getattr(self
, args
["command"])(args
)
44 print("Command not implemented.")
46 def start(self
, args
):
48 req
= {'image': args
.get("image"),
49 'command': args
.get("docker_command"),
50 'network': args
.get("network")}
52 response
= put("%s/restapi/compute/%s/%s" %
53 (args
.get("endpoint"),
54 args
.get("datacenter"),
58 pp
.pprint(response
.json())
62 response
= delete("%s/restapi/compute/%s/%s" %
63 (args
.get("endpoint"),
64 args
.get("datacenter"),
66 pp
.pprint(response
.json())
70 list = get('%s/restapi/compute/%s' %
71 (args
.get("endpoint"), args
.get('datacenter'))).json()
75 # for each container add a line to the output table
79 # eth0ip = status.get("docker_network", "-")
80 netw_list
= [netw_dict
['intf_name']
81 for netw_dict
in status
.get("network")]
82 dc_if_list
= [netw_dict
['dc_portname']
83 for netw_dict
in status
.get("network")]
84 table
.append([status
.get("datacenter"),
88 ','.join(dc_if_list
)])
89 # status.get("state").get("Status")]
91 headers
= ["Datacenter",
95 "Datacenter interfaces"]
96 print(tabulate(table
, headers
=headers
, tablefmt
="grid"))
98 def status(self
, args
):
100 list = get("%s/restapi/compute/%s/%s" %
101 (args
.get("endpoint"),
102 args
.get("datacenter"),
103 args
.get("name"))).json()
107 def xterm(self
, args
):
108 vnf_names
= args
.get("vnf_names")
109 for vnf_name
in vnf_names
:
110 Popen(['xterm', '-xrm', 'XTerm.vt100.allowTitleOps: false', '-T', vnf_name
,
111 '-e', "docker exec -it mn.{0} /bin/bash".format(vnf_name
)])
114 parser
= argparse
.ArgumentParser(description
="""son-emu-cli compute
117 - son-emu-cli compute start -d dc2 -n client -i sonatanfv/sonata-iperf3-vnf
119 - son-emu-cli compute status -d dc2 -n client
120 """, formatter_class
=argparse
.RawTextHelpFormatter
)
123 choices
=['start', 'stop', 'list', 'status', 'xterm'],
124 help="Action to be executed.")
128 help="vnf names to open an xterm for")
130 "--datacenter", "-d", dest
="datacenter",
131 help="Data center to which the command should be applied.")
133 "--name", "-n", dest
="name",
134 help="Name of compute instance e.g. 'vnf1'.")
136 "--image", "-i", dest
="image",
137 help="Name of container image to be used e.g. 'ubuntu:trusty'")
139 "--dcmd", "-c", dest
="docker_command",
140 help="Startup command of the container e.g. './start.sh'")
142 "--net", dest
="network",
143 help="Network properties of a compute instance e.g. \
144 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
146 "--endpoint", "-e", dest
="endpoint",
147 default
="http://127.0.0.1:5001",
148 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
152 args
= vars(parser
.parse_args(argv
))
154 c
.execute_command(args
)