c3850fc9e10869ba8eb58bd51ca07edc0a71fff0
[osm/vim-emu.git] / emuvim / cli / datacenter.py
1 """
2 son-emu datacenter CLI
3 (c) 2016 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5
6 import argparse
7 import pprint
8 from tabulate import tabulate
9 import zerorpc
10
11
12 pp = pprint.PrettyPrinter(indent=4)
13
14
15 class ZeroRpcClient(object):
16
17 def __init__(self):
18 self.c = zerorpc.Client()
19 self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
20 self.cmds = {}
21
22 def execute_command(self, args):
23 if getattr(self, args["command"]) is not None:
24 # call the local method with the same name as the command arg
25 getattr(self, args["command"])(args)
26 else:
27 print "Command not implemented."
28
29 def list(self, args):
30 r = self.c.datacenter_list()
31 table = []
32 for d in r:
33 # for each dc add a line to the output table
34 if len(d) > 0:
35 table.append([d.get("label"),
36 d.get("internalname"),
37 d.get("switch"),
38 d.get("n_running_containers"),
39 len(d.get("metadata"))])
40 headers = ["Label",
41 "Internal Name",
42 "Switch",
43 "# Containers",
44 "# Metadata Items"]
45 print tabulate(table, headers=headers, tablefmt="grid")
46
47 def status(self, args):
48 r = self.c.datacenter_status(
49 args.get("datacenter"))
50 pp.pprint(r)
51
52
53 parser = argparse.ArgumentParser(description='son-emu datacenter')
54 parser.add_argument(
55 "command",
56 choices=['list', 'status'],
57 help="Action to be executed.")
58 parser.add_argument(
59 "--datacenter", "-d", dest="datacenter",
60 help="Data center to which the command should be applied.")
61
62
63 def main(argv):
64 args = vars(parser.parse_args(argv))
65 c = ZeroRpcClient()
66 c.execute_command(args)