blob: 9e5e0abb462cd425e223034d16447acae4ad9ec7 [file] [log] [blame]
peusterm79ef6ae2016-07-08 13:53:57 +02001"""
2Copyright (c) 2015 SONATA-NFV and Paderborn University
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
27"""
hadik3ra9dd9012016-08-09 10:51:13 +020028from requests import get, put, delete
hadik3r237d3f52016-06-27 17:57:49 +020029from tabulate import tabulate
30import pprint
31import argparse
32import json
stevenvanrossem4e184a72016-11-08 08:47:20 +010033from subprocess import Popen
hadik3r237d3f52016-06-27 17:57:49 +020034
35pp = pprint.PrettyPrinter(indent=4)
36
hadik3r237d3f52016-06-27 17:57:49 +020037
hadik3ra9dd9012016-08-09 10:51:13 +020038class RestApiClient():
hadik3r237d3f52016-06-27 17:57:49 +020039 def __init__(self):
40 self.cmds = {}
41
42 def execute_command(self, args):
43 if getattr(self, args["command"]) is not None:
44 # call the local method with the same name as the command arg
45 getattr(self, args["command"])(args)
46 else:
47 print("Command not implemented.")
48
49 def start(self, args):
50
hadik3ra9dd9012016-08-09 10:51:13 +020051 req = {'image': args.get("image"),
52 'command': args.get("docker_command"),
53 'network': args.get("network")}
hadik3r237d3f52016-06-27 17:57:49 +020054
hadik3ra9dd9012016-08-09 10:51:13 +020055 response = put("%s/restapi/compute/%s/%s" %
hadik3r237d3f52016-06-27 17:57:49 +020056 (args.get("endpoint"),
57 args.get("datacenter"),
58 args.get("name")),
hadik3ra9dd9012016-08-09 10:51:13 +020059 json=req)
stevenvanrossemff6b4042016-07-14 20:51:37 +020060
stevenvanrossem73efd192016-06-29 01:44:07 +020061 pp.pprint(response.json())
62
hadik3r237d3f52016-06-27 17:57:49 +020063 def stop(self, args):
64
hadik3ra9dd9012016-08-09 10:51:13 +020065 response = delete("%s/restapi/compute/%s/%s" %
66 (args.get("endpoint"),
67 args.get("datacenter"),
68 args.get("name")))
stevenvanrossem73efd192016-06-29 01:44:07 +020069 pp.pprint(response.json())
hadik3r237d3f52016-06-27 17:57:49 +020070
hadik3ra9dd9012016-08-09 10:51:13 +020071 def list(self, args):
hadik3r237d3f52016-06-27 17:57:49 +020072
hadik3ra9dd9012016-08-09 10:51:13 +020073 list = get('%s/restapi/compute/%s' % (args.get("endpoint"), args.get('datacenter'))).json()
hadik3r237d3f52016-06-27 17:57:49 +020074
75 table = []
76 for c in list:
77 # for each container add a line to the output table
78 if len(c) > 1:
79 name = c[0]
80 status = c[1]
stevenvanrossem4e184a72016-11-08 08:47:20 +010081 #eth0ip = status.get("docker_network", "-")
stevenvanrossem566779d2016-11-07 06:33:44 +010082 netw_list = [netw_dict['intf_name'] for netw_dict in status.get("network")]
83 dc_if_list = [netw_dict['dc_portname'] for netw_dict in status.get("network")]
hadik3r237d3f52016-06-27 17:57:49 +020084 table.append([status.get("datacenter"),
85 name,
86 status.get("image"),
stevenvanrossem566779d2016-11-07 06:33:44 +010087 ','.join(netw_list),
88 ','.join(dc_if_list)])
89 #status.get("state").get("Status")]
hadik3r237d3f52016-06-27 17:57:49 +020090
91 headers = ["Datacenter",
92 "Container",
93 "Image",
stevenvanrossem566779d2016-11-07 06:33:44 +010094 "Interface list",
95 "Datacenter interfaces"]
hadik3r237d3f52016-06-27 17:57:49 +020096 print(tabulate(table, headers=headers, tablefmt="grid"))
97
hadik3ra9dd9012016-08-09 10:51:13 +020098 def status(self, args):
hadik3r237d3f52016-06-27 17:57:49 +020099
100 list = get("%s/restapi/compute/%s/%s" %
101 (args.get("endpoint"),
102 args.get("datacenter"),
103 args.get("name"))).json()
hadik3ra9dd9012016-08-09 10:51:13 +0200104
hadik3r237d3f52016-06-27 17:57:49 +0200105 pp.pprint(list)
106
stevenvanrossem4e184a72016-11-08 08:47:20 +0100107 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)])
hadik3r237d3f52016-06-27 17:57:49 +0200112
peustermfa63aa92016-08-19 08:49:39 +0200113parser = argparse.ArgumentParser(description="""son-emu compute
114
115 Examples:
116 - son-emu-cli compute start -d dc2 -n client -i sonatanfv/sonata-iperf3-vnf
117 - son-emu-cli list
118 - son-emu-cli compute status -d dc2 -n client
119 """, formatter_class=argparse.RawTextHelpFormatter)
hadik3r237d3f52016-06-27 17:57:49 +0200120parser.add_argument(
121 "command",
stevenvanrossem4e184a72016-11-08 08:47:20 +0100122 choices=['start', 'stop', 'list', 'status', 'xterm'],
hadik3r237d3f52016-06-27 17:57:49 +0200123 help="Action to be executed.")
124parser.add_argument(
stevenvanrossem4e184a72016-11-08 08:47:20 +0100125 "vnf_names",
126 nargs='*',
127 help="vnf names to open an xterm for")
128parser.add_argument(
hadik3r237d3f52016-06-27 17:57:49 +0200129 "--datacenter", "-d", dest="datacenter",
130 help="Data center to which the command should be applied.")
131parser.add_argument(
132 "--name", "-n", dest="name",
133 help="Name of compute instance e.g. 'vnf1'.")
134parser.add_argument(
hadik3ra9dd9012016-08-09 10:51:13 +0200135 "--image", "-i", dest="image",
hadik3r237d3f52016-06-27 17:57:49 +0200136 help="Name of container image to be used e.g. 'ubuntu:trusty'")
137parser.add_argument(
138 "--dcmd", "-c", dest="docker_command",
139 help="Startup command of the container e.g. './start.sh'")
140parser.add_argument(
141 "--net", dest="network",
142 help="Network properties of a compute instance e.g. \
143 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
144parser.add_argument(
hadik3r237d3f52016-06-27 17:57:49 +0200145 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +0200146 default="http://127.0.0.1:5001",
hadik3r237d3f52016-06-27 17:57:49 +0200147 help="UUID of the plugin to be manipulated.")
148
hadik3ra9dd9012016-08-09 10:51:13 +0200149
hadik3r237d3f52016-06-27 17:57:49 +0200150def main(argv):
151 args = vars(parser.parse_args(argv))
152 c = RestApiClient()
hadik3ra9dd9012016-08-09 10:51:13 +0200153 c.execute_command(args)