blob: 15fea9194c180623e0b2128f68ea3bfd59175401 [file] [log] [blame]
peusterm58310762016-01-12 17:09:20 +01001"""
2son-emu compute CLI
peusterm2ec74e12016-01-13 11:17:53 +01003(c) 2016 by Manuel Peuster <manuel.peuster@upb.de>
peusterm58310762016-01-12 17:09:20 +01004"""
5
6import argparse
7import pprint
peusterm2ec74e12016-01-13 11:17:53 +01008from tabulate import tabulate
peusterm58310762016-01-12 17:09:20 +01009import zerorpc
10
11
12pp = pprint.PrettyPrinter(indent=4)
13
14
15class ZeroRpcClient(object):
16
17 def __init__(self):
18 self.c = zerorpc.Client()
peusterm2ec74e12016-01-13 11:17:53 +010019 self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
peusterm58310762016-01-12 17:09:20 +010020 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):
peusterm45dce612016-01-29 15:34:38 +010030 network = {}
31 if args.get("network") is not None:
32 network = {"ip": args.get("network")}
peusterm58310762016-01-12 17:09:20 +010033 r = self.c.compute_action_start(
peusterm45dce612016-01-29 15:34:38 +010034 args.get("datacenter"),
35 args.get("name"),
36 args.get("image"),
stevenvanrossem8fbf9782016-02-17 11:40:23 +010037 args.get("docker_command"),
peusterm45dce612016-01-29 15:34:38 +010038 network)
peusterm58310762016-01-12 17:09:20 +010039 pp.pprint(r)
40
41 def stop(self, args):
42 r = self.c.compute_action_stop(
43 args.get("datacenter"), args.get("name"))
44 pp.pprint(r)
45
46 def list(self, args):
peusterm2ec74e12016-01-13 11:17:53 +010047 r = self.c.compute_list(
48 args.get("datacenter"))
49 table = []
50 for c in r:
51 # for each container add a line to the output table
52 if len(c) > 1:
53 name = c[0]
54 status = c[1]
55 eth0ip = None
56 eth0status = "down"
57 if len(status.get("network")) > 0:
58 eth0ip = status.get("network")[0][1]
59 eth0status = "up" if status.get(
60 "network")[0][3] else "down"
61 table.append([status.get("datacenter"),
62 name,
63 status.get("image"),
64 eth0ip,
65 eth0status,
66 status.get("state").get("Status")])
67 headers = ["Datacenter",
68 "Container",
69 "Image",
70 "eth0 IP",
71 "eth0 status",
72 "Status"]
73 print tabulate(table, headers=headers, tablefmt="grid")
peusterm58310762016-01-12 17:09:20 +010074
75 def status(self, args):
peusterm2ec74e12016-01-13 11:17:53 +010076 r = self.c.compute_status(
77 args.get("datacenter"), args.get("name"))
78 pp.pprint(r)
peusterm58310762016-01-12 17:09:20 +010079
80
81parser = argparse.ArgumentParser(description='son-emu compute')
peusterm58310762016-01-12 17:09:20 +010082parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +010083 "command",
peustermd313dc12016-02-04 15:36:02 +010084 choices=['start', 'stop', 'list', 'status'],
85 help="Action to be executed.")
peusterm58310762016-01-12 17:09:20 +010086parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +010087 "--datacenter", "-d", dest="datacenter",
88 help="Data center to in which the compute instance should be executed")
peusterm7973f052016-01-29 14:38:05 +010089parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +010090 "--name", "-n", dest="name",
91 help="Name of compute instance e.g. 'vnf1'")
92parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +010093 "--image","-i", dest="image",
peusterm45dce612016-01-29 15:34:38 +010094 help="Name of container image to be used e.g. 'ubuntu'")
95parser.add_argument(
stevenvanrossem8fbf9782016-02-17 11:40:23 +010096 "--dcmd", "-c", dest="docker_command",
97 help="Startup command of the container e.g. './start.sh'")
98parser.add_argument(
peusterm45dce612016-01-29 15:34:38 +010099 "--net", dest="network",
100 help="Network properties of compute instance e.g. '10.0.0.123/8'")
peusterm58310762016-01-12 17:09:20 +0100101
102
103def main(argv):
104 args = vars(parser.parse_args(argv))
105 c = ZeroRpcClient()
106 c.execute_command(args)