| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 1 | """ |
| peusterm | 79ef6ae | 2016-07-08 13:53:57 +0200 | [diff] [blame] | 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). |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 27 | """ |
| 28 | |
| 29 | import argparse |
| 30 | import pprint |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 31 | from tabulate import tabulate |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 32 | import zerorpc |
| 33 | |
| 34 | |
| 35 | pp = pprint.PrettyPrinter(indent=4) |
| 36 | |
| 37 | |
| 38 | class ZeroRpcClient(object): |
| 39 | |
| 40 | def __init__(self): |
| stevenvanrossem | 461941c | 2016-05-10 11:41:29 +0200 | [diff] [blame] | 41 | self.c = zerorpc.Client(heartbeat=None, timeout=120) #heartbeat=None, timeout=120 |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 42 | self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 43 | self.cmds = {} |
| 44 | |
| 45 | def execute_command(self, args): |
| 46 | if getattr(self, args["command"]) is not None: |
| 47 | # call the local method with the same name as the command arg |
| 48 | getattr(self, args["command"])(args) |
| 49 | else: |
| stevenvanrossem | 4937815 | 2016-05-19 11:33:48 +0200 | [diff] [blame] | 50 | print("Command not implemented.") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 51 | |
| 52 | def start(self, args): |
| peusterm | 7f8e840 | 2016-02-28 18:38:10 +0100 | [diff] [blame] | 53 | nw_list = list() |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 54 | if args.get("network") is not None: |
| stevenvanrossem | 14c8905 | 2016-04-10 23:49:59 +0200 | [diff] [blame] | 55 | nw_list = self._parse_network(args.get("network")) |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 56 | r = self.c.compute_action_start( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 57 | args.get("datacenter"), |
| 58 | args.get("name"), |
| 59 | args.get("image"), |
| stevenvanrossem | 994245b | 2016-05-04 12:36:57 +0200 | [diff] [blame] | 60 | nw_list, |
| 61 | args.get("docker_command") |
| stevenvanrossem | 6b1d9b9 | 2016-05-02 13:10:40 +0200 | [diff] [blame] | 62 | ) |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 63 | pp.pprint(r) |
| 64 | |
| 65 | def stop(self, args): |
| 66 | r = self.c.compute_action_stop( |
| 67 | args.get("datacenter"), args.get("name")) |
| 68 | pp.pprint(r) |
| 69 | |
| 70 | def list(self, args): |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 71 | r = self.c.compute_list( |
| 72 | args.get("datacenter")) |
| 73 | table = [] |
| 74 | for c in r: |
| 75 | # for each container add a line to the output table |
| 76 | if len(c) > 1: |
| 77 | name = c[0] |
| 78 | status = c[1] |
| 79 | eth0ip = None |
| 80 | eth0status = "down" |
| 81 | if len(status.get("network")) > 0: |
| peusterm | a3ddeba | 2016-05-13 14:40:02 +0200 | [diff] [blame] | 82 | eth0ip = status.get("network")[0].get("ip") |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 83 | eth0status = "up" if status.get( |
| peusterm | a3ddeba | 2016-05-13 14:40:02 +0200 | [diff] [blame] | 84 | "network")[0].get("up") else "down" |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 85 | table.append([status.get("datacenter"), |
| 86 | name, |
| 87 | status.get("image"), |
| 88 | eth0ip, |
| 89 | eth0status, |
| 90 | status.get("state").get("Status")]) |
| 91 | headers = ["Datacenter", |
| 92 | "Container", |
| 93 | "Image", |
| 94 | "eth0 IP", |
| 95 | "eth0 status", |
| 96 | "Status"] |
| stevenvanrossem | 4937815 | 2016-05-19 11:33:48 +0200 | [diff] [blame] | 97 | print(tabulate(table, headers=headers, tablefmt="grid")) |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 98 | |
| 99 | def status(self, args): |
| peusterm | 2ec74e1 | 2016-01-13 11:17:53 +0100 | [diff] [blame] | 100 | r = self.c.compute_status( |
| 101 | args.get("datacenter"), args.get("name")) |
| 102 | pp.pprint(r) |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 103 | |
| stevenvanrossem | 5b37641 | 2016-05-04 15:34:49 +0200 | [diff] [blame] | 104 | def _create_dict(self, **kwargs): |
| 105 | return kwargs |
| 106 | |
| stevenvanrossem | 14c8905 | 2016-04-10 23:49:59 +0200 | [diff] [blame] | 107 | def _parse_network(self, network_str): |
| 108 | ''' |
| 109 | parse the options for all network interfaces of the vnf |
| 110 | :param network_str: (id=x,ip=x.x.x.x/x), ... |
| 111 | :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...] |
| 112 | ''' |
| 113 | nw_list = list() |
| 114 | networks = network_str[1:-1].split('),(') |
| 115 | for nw in networks: |
| 116 | nw_dict = dict(tuple(e.split('=')) for e in nw.split(',')) |
| 117 | nw_list.append(nw_dict) |
| 118 | |
| 119 | return nw_list |
| 120 | |
| 121 | |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 122 | |
| 123 | parser = argparse.ArgumentParser(description='son-emu compute') |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 124 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 125 | "command", |
| stevenvanrossem | e131bf5 | 2016-07-14 11:42:09 +0200 | [diff] [blame^] | 126 | choices=['start', 'stop', 'list', 'status'], |
| peusterm | d313dc1 | 2016-02-04 15:36:02 +0100 | [diff] [blame] | 127 | help="Action to be executed.") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 128 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 129 | "--datacenter", "-d", dest="datacenter", |
| 130 | help="Data center to in which the compute instance should be executed") |
| peusterm | 7973f05 | 2016-01-29 14:38:05 +0100 | [diff] [blame] | 131 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 132 | "--name", "-n", dest="name", |
| 133 | help="Name of compute instance e.g. 'vnf1'") |
| 134 | parser.add_argument( |
| stevenvanrossem | 8fbf978 | 2016-02-17 11:40:23 +0100 | [diff] [blame] | 135 | "--image","-i", dest="image", |
| peusterm | 0dc3ae0 | 2016-04-27 09:33:28 +0200 | [diff] [blame] | 136 | help="Name of container image to be used e.g. 'ubuntu:trusty'") |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 137 | parser.add_argument( |
| stevenvanrossem | 8fbf978 | 2016-02-17 11:40:23 +0100 | [diff] [blame] | 138 | "--dcmd", "-c", dest="docker_command", |
| 139 | help="Startup command of the container e.g. './start.sh'") |
| 140 | parser.add_argument( |
| peusterm | 45dce61 | 2016-01-29 15:34:38 +0100 | [diff] [blame] | 141 | "--net", dest="network", |
| stevenvanrossem | 4937815 | 2016-05-19 11:33:48 +0200 | [diff] [blame] | 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.") |
| peusterm | 5831076 | 2016-01-12 17:09:20 +0100 | [diff] [blame] | 144 | |
| 145 | def main(argv): |
| 146 | args = vars(parser.parse_args(argv)) |
| 147 | c = ZeroRpcClient() |
| 148 | c.execute_command(args) |