blob: 870eea155896df951cbe0f3be6206c9f6f7d947e [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
33
34pp = pprint.PrettyPrinter(indent=4)
35
hadik3r237d3f52016-06-27 17:57:49 +020036
hadik3ra9dd9012016-08-09 10:51:13 +020037class RestApiClient():
hadik3r237d3f52016-06-27 17:57:49 +020038 def __init__(self):
39 self.cmds = {}
40
41 def execute_command(self, args):
42 if getattr(self, args["command"]) is not None:
43 # call the local method with the same name as the command arg
44 getattr(self, args["command"])(args)
45 else:
46 print("Command not implemented.")
47
48 def start(self, args):
49
hadik3ra9dd9012016-08-09 10:51:13 +020050 req = {'image': args.get("image"),
51 'command': args.get("docker_command"),
52 'network': args.get("network")}
hadik3r237d3f52016-06-27 17:57:49 +020053
hadik3ra9dd9012016-08-09 10:51:13 +020054 response = put("%s/restapi/compute/%s/%s" %
hadik3r237d3f52016-06-27 17:57:49 +020055 (args.get("endpoint"),
56 args.get("datacenter"),
57 args.get("name")),
hadik3ra9dd9012016-08-09 10:51:13 +020058 json=req)
stevenvanrossemff6b4042016-07-14 20:51:37 +020059
stevenvanrossem73efd192016-06-29 01:44:07 +020060 pp.pprint(response.json())
61
hadik3r237d3f52016-06-27 17:57:49 +020062 def stop(self, args):
63
hadik3ra9dd9012016-08-09 10:51:13 +020064 response = delete("%s/restapi/compute/%s/%s" %
65 (args.get("endpoint"),
66 args.get("datacenter"),
67 args.get("name")))
stevenvanrossem73efd192016-06-29 01:44:07 +020068 pp.pprint(response.json())
hadik3r237d3f52016-06-27 17:57:49 +020069
hadik3ra9dd9012016-08-09 10:51:13 +020070 def list(self, args):
hadik3r237d3f52016-06-27 17:57:49 +020071
hadik3ra9dd9012016-08-09 10:51:13 +020072 list = get('%s/restapi/compute/%s' % (args.get("endpoint"), args.get('datacenter'))).json()
hadik3r237d3f52016-06-27 17:57:49 +020073
74 table = []
75 for c in list:
76 # for each container add a line to the output table
77 if len(c) > 1:
78 name = c[0]
79 status = c[1]
peustermfa63aa92016-08-19 08:49:39 +020080 eth0ip = status.get("docker_network", "-")
stevenvanrossem566779d2016-11-07 06:33:44 +010081 netw_list = [netw_dict['intf_name'] for netw_dict in status.get("network")]
82 dc_if_list = [netw_dict['dc_portname'] for netw_dict in status.get("network")]
hadik3r237d3f52016-06-27 17:57:49 +020083 table.append([status.get("datacenter"),
84 name,
85 status.get("image"),
stevenvanrossem566779d2016-11-07 06:33:44 +010086 ','.join(netw_list),
87 ','.join(dc_if_list)])
88 #status.get("state").get("Status")]
hadik3r237d3f52016-06-27 17:57:49 +020089
90 headers = ["Datacenter",
91 "Container",
92 "Image",
stevenvanrossem566779d2016-11-07 06:33:44 +010093 "Interface list",
94 "Datacenter interfaces"]
hadik3r237d3f52016-06-27 17:57:49 +020095 print(tabulate(table, headers=headers, tablefmt="grid"))
96
hadik3ra9dd9012016-08-09 10:51:13 +020097 def status(self, args):
hadik3r237d3f52016-06-27 17:57:49 +020098
99 list = get("%s/restapi/compute/%s/%s" %
100 (args.get("endpoint"),
101 args.get("datacenter"),
102 args.get("name"))).json()
hadik3ra9dd9012016-08-09 10:51:13 +0200103
hadik3r237d3f52016-06-27 17:57:49 +0200104 pp.pprint(list)
105
106
peustermfa63aa92016-08-19 08:49:39 +0200107parser = argparse.ArgumentParser(description="""son-emu compute
108
109 Examples:
110 - son-emu-cli compute start -d dc2 -n client -i sonatanfv/sonata-iperf3-vnf
111 - son-emu-cli list
112 - son-emu-cli compute status -d dc2 -n client
113 """, formatter_class=argparse.RawTextHelpFormatter)
hadik3r237d3f52016-06-27 17:57:49 +0200114parser.add_argument(
115 "command",
hadik3rbe81adb2016-06-27 19:03:36 +0200116 choices=['start', 'stop', 'list', 'status'],
hadik3r237d3f52016-06-27 17:57:49 +0200117 help="Action to be executed.")
118parser.add_argument(
119 "--datacenter", "-d", dest="datacenter",
120 help="Data center to which the command should be applied.")
121parser.add_argument(
122 "--name", "-n", dest="name",
123 help="Name of compute instance e.g. 'vnf1'.")
124parser.add_argument(
hadik3ra9dd9012016-08-09 10:51:13 +0200125 "--image", "-i", dest="image",
hadik3r237d3f52016-06-27 17:57:49 +0200126 help="Name of container image to be used e.g. 'ubuntu:trusty'")
127parser.add_argument(
128 "--dcmd", "-c", dest="docker_command",
129 help="Startup command of the container e.g. './start.sh'")
130parser.add_argument(
131 "--net", dest="network",
132 help="Network properties of a compute instance e.g. \
133 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
134parser.add_argument(
hadik3r237d3f52016-06-27 17:57:49 +0200135 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +0200136 default="http://127.0.0.1:5001",
hadik3r237d3f52016-06-27 17:57:49 +0200137 help="UUID of the plugin to be manipulated.")
138
hadik3ra9dd9012016-08-09 10:51:13 +0200139
hadik3r237d3f52016-06-27 17:57:49 +0200140def main(argv):
141 args = vars(parser.parse_args(argv))
142 c = RestApiClient()
hadik3ra9dd9012016-08-09 10:51:13 +0200143 c.execute_command(args)