| peusterm | e7c9e86 | 2016-02-04 16:10:19 +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 | e7c9e86 | 2016-02-04 16:10:19 +0100 | [diff] [blame] | 27 | """ |
| 28 | |
| 29 | import argparse |
| 30 | import pprint |
| 31 | from tabulate import tabulate |
| 32 | import zerorpc |
| 33 | |
| 34 | |
| 35 | pp = pprint.PrettyPrinter(indent=4) |
| 36 | |
| 37 | |
| 38 | class ZeroRpcClient(object): |
| 39 | |
| 40 | def __init__(self): |
| 41 | self.c = zerorpc.Client() |
| 42 | self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later |
| 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 | e7c9e86 | 2016-02-04 16:10:19 +0100 | [diff] [blame] | 51 | |
| 52 | def list(self, args): |
| 53 | r = self.c.datacenter_list() |
| 54 | table = [] |
| 55 | for d in r: |
| 56 | # for each dc add a line to the output table |
| 57 | if len(d) > 0: |
| 58 | table.append([d.get("label"), |
| 59 | d.get("internalname"), |
| 60 | d.get("switch"), |
| 61 | d.get("n_running_containers"), |
| 62 | len(d.get("metadata"))]) |
| 63 | headers = ["Label", |
| 64 | "Internal Name", |
| 65 | "Switch", |
| 66 | "# Containers", |
| 67 | "# Metadata Items"] |
| stevenvanrossem | 4937815 | 2016-05-19 11:33:48 +0200 | [diff] [blame] | 68 | print(tabulate(table, headers=headers, tablefmt="grid")) |
| peusterm | e7c9e86 | 2016-02-04 16:10:19 +0100 | [diff] [blame] | 69 | |
| 70 | def status(self, args): |
| 71 | r = self.c.datacenter_status( |
| 72 | args.get("datacenter")) |
| 73 | pp.pprint(r) |
| 74 | |
| 75 | |
| 76 | parser = argparse.ArgumentParser(description='son-emu datacenter') |
| 77 | parser.add_argument( |
| 78 | "command", |
| 79 | choices=['list', 'status'], |
| 80 | help="Action to be executed.") |
| 81 | parser.add_argument( |
| 82 | "--datacenter", "-d", dest="datacenter", |
| 83 | help="Data center to which the command should be applied.") |
| 84 | |
| 85 | |
| 86 | def main(argv): |
| 87 | args = vars(parser.parse_args(argv)) |
| 88 | c = ZeroRpcClient() |
| 89 | c.execute_command(args) |