| peusterm | 79ef6ae | 2016-07-08 13:53:57 +0200 | [diff] [blame] | 1 | """ |
| 2 | Copyright (c) 2015 SONATA-NFV and Paderborn University |
| 3 | ALL RIGHTS RESERVED. |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | |
| 17 | Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION] |
| 18 | nor the names of its contributors may be used to endorse or promote |
| 19 | products derived from this software without specific prior written |
| 20 | permission. |
| 21 | |
| 22 | This work has been performed in the framework of the SONATA project, |
| 23 | funded by the European Commission under Grant number 671517 through |
| 24 | the Horizon 2020 and 5G-PPP programmes. The authors would like to |
| 25 | acknowledge the contributions of their colleagues of the SONATA |
| 26 | partner consortium (www.sonata-nfv.eu). |
| 27 | """ |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 28 | from requests import get, put, delete |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 29 | from tabulate import tabulate |
| 30 | import pprint |
| 31 | import argparse |
| 32 | import json |
| 33 | |
| 34 | pp = pprint.PrettyPrinter(indent=4) |
| 35 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 36 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 37 | class RestApiClient(): |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 38 | 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 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 50 | req = {'image': args.get("image"), |
| 51 | 'command': args.get("docker_command"), |
| 52 | 'network': args.get("network")} |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 53 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 54 | response = put("%s/restapi/compute/%s/%s" % |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 55 | (args.get("endpoint"), |
| 56 | args.get("datacenter"), |
| 57 | args.get("name")), |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 58 | json=req) |
| stevenvanrossem | ff6b404 | 2016-07-14 20:51:37 +0200 | [diff] [blame] | 59 | |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 60 | pp.pprint(response.json()) |
| 61 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 62 | def stop(self, args): |
| 63 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 64 | response = delete("%s/restapi/compute/%s/%s" % |
| 65 | (args.get("endpoint"), |
| 66 | args.get("datacenter"), |
| 67 | args.get("name"))) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 68 | pp.pprint(response.json()) |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 69 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 70 | def list(self, args): |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 71 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 72 | list = get('%s/restapi/compute/%s' % (args.get("endpoint"), args.get('datacenter'))).json() |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 73 | |
| 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] |
| peusterm | fa63aa9 | 2016-08-19 08:49:39 +0200 | [diff] [blame] | 80 | eth0ip = status.get("docker_network", "-") |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 81 | table.append([status.get("datacenter"), |
| 82 | name, |
| 83 | status.get("image"), |
| 84 | eth0ip, |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 85 | status.get("state").get("Status")]) |
| 86 | |
| 87 | headers = ["Datacenter", |
| 88 | "Container", |
| 89 | "Image", |
| peusterm | fa63aa9 | 2016-08-19 08:49:39 +0200 | [diff] [blame] | 90 | "docker0", |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 91 | "Status"] |
| 92 | print(tabulate(table, headers=headers, tablefmt="grid")) |
| 93 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 94 | def status(self, args): |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 95 | |
| 96 | list = get("%s/restapi/compute/%s/%s" % |
| 97 | (args.get("endpoint"), |
| 98 | args.get("datacenter"), |
| 99 | args.get("name"))).json() |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 100 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 101 | pp.pprint(list) |
| 102 | |
| 103 | |
| peusterm | fa63aa9 | 2016-08-19 08:49:39 +0200 | [diff] [blame] | 104 | parser = argparse.ArgumentParser(description="""son-emu compute |
| 105 | |
| 106 | Examples: |
| 107 | - son-emu-cli compute start -d dc2 -n client -i sonatanfv/sonata-iperf3-vnf |
| 108 | - son-emu-cli list |
| 109 | - son-emu-cli compute status -d dc2 -n client |
| 110 | """, formatter_class=argparse.RawTextHelpFormatter) |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 111 | parser.add_argument( |
| 112 | "command", |
| hadik3r | be81adb | 2016-06-27 19:03:36 +0200 | [diff] [blame] | 113 | choices=['start', 'stop', 'list', 'status'], |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 114 | help="Action to be executed.") |
| 115 | parser.add_argument( |
| 116 | "--datacenter", "-d", dest="datacenter", |
| 117 | help="Data center to which the command should be applied.") |
| 118 | parser.add_argument( |
| 119 | "--name", "-n", dest="name", |
| 120 | help="Name of compute instance e.g. 'vnf1'.") |
| 121 | parser.add_argument( |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 122 | "--image", "-i", dest="image", |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 123 | help="Name of container image to be used e.g. 'ubuntu:trusty'") |
| 124 | parser.add_argument( |
| 125 | "--dcmd", "-c", dest="docker_command", |
| 126 | help="Startup command of the container e.g. './start.sh'") |
| 127 | parser.add_argument( |
| 128 | "--net", dest="network", |
| 129 | help="Network properties of a compute instance e.g. \ |
| 130 | '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.") |
| 131 | parser.add_argument( |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 132 | "--endpoint", "-e", dest="endpoint", |
| peusterm | 0a336cc | 2016-07-04 09:15:47 +0200 | [diff] [blame] | 133 | default="http://127.0.0.1:5001", |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 134 | help="UUID of the plugin to be manipulated.") |
| 135 | |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 136 | |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 137 | def main(argv): |
| 138 | args = vars(parser.parse_args(argv)) |
| 139 | c = RestApiClient() |
| hadik3r | a9dd901 | 2016-08-09 10:51:13 +0200 | [diff] [blame] | 140 | c.execute_command(args) |