added image argument to CLI
[osm/vim-emu.git] / emuvim / cli / compute.py
1 """
2 son-emu compute 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 start(self, args):
30 r = self.c.compute_action_start(
31 args.get("datacenter"), args.get("name"), args.get("image"))
32 pp.pprint(r)
33
34 def stop(self, args):
35 r = self.c.compute_action_stop(
36 args.get("datacenter"), args.get("name"))
37 pp.pprint(r)
38
39 def list(self, args):
40 r = self.c.compute_list(
41 args.get("datacenter"))
42 table = []
43 for c in r:
44 # for each container add a line to the output table
45 if len(c) > 1:
46 name = c[0]
47 status = c[1]
48 eth0ip = None
49 eth0status = "down"
50 if len(status.get("network")) > 0:
51 eth0ip = status.get("network")[0][1]
52 eth0status = "up" if status.get(
53 "network")[0][3] else "down"
54 table.append([status.get("datacenter"),
55 name,
56 status.get("image"),
57 eth0ip,
58 eth0status,
59 status.get("state").get("Status")])
60 headers = ["Datacenter",
61 "Container",
62 "Image",
63 "eth0 IP",
64 "eth0 status",
65 "Status"]
66 print tabulate(table, headers=headers, tablefmt="grid")
67
68 def status(self, args):
69 r = self.c.compute_status(
70 args.get("datacenter"), args.get("name"))
71 pp.pprint(r)
72
73
74 parser = argparse.ArgumentParser(description='son-emu compute')
75 parser.add_argument("command", help="Action to be executed.")
76 parser.add_argument(
77 "--datacenter", "-d", dest="datacenter", help="Data center.")
78 parser.add_argument(
79 "--name", "-n", dest="name", help="Compute name.")
80 parser.add_argument(
81 "--image", "-i", dest="image", help="Name of container image to be used.")
82 # TODO: IP, image, etc. pp.
83
84
85 def main(argv):
86 args = vars(parser.parse_args(argv))
87 c = ZeroRpcClient()
88 c.execute_command(args)