From 5831076971df3aeec3ecbd3d972557709ab27a4f Mon Sep 17 00:00:00 2001 From: peusterm Date: Tue, 12 Jan 2016 17:09:20 +0100 Subject: [PATCH] added a first, early prototype of the emulator CLI --- README.md | 10 ++++--- emuvim/cli/__main__.py | 40 ---------------------------- emuvim/cli/compute.py | 56 ++++++++++++++++++++++++++++++++++++++++ emuvim/cli/network.py | 8 ++++++ emuvim/cli/son-emu-cli | 28 ++++++++++++++++++++ emuvim/dcemulator/net.py | 4 +-- 6 files changed, 100 insertions(+), 46 deletions(-) delete mode 100644 emuvim/cli/__main__.py create mode 100644 emuvim/cli/compute.py create mode 100644 emuvim/cli/network.py create mode 100755 emuvim/cli/son-emu-cli diff --git a/README.md b/README.md index 873a936..92c3655 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,7 @@ ### TODO * DCemulator - * correctly start and connect new compute resources at runtime - * remove and disconnect compute resources at runtime - * do IP management for new containers - * list active compute resources + * ... * Cloud-like reference API with CLI for demonstrations * Write CLI client * Start compute @@ -50,3 +47,8 @@ * Define API endpoints in topology * call startAPI from topology definition and start it in a own thread * make it possible to start different API endpoints for different DCs +* DCemulator + * correctly start and connect new compute resources at runtime + * remove and disconnect compute resources at runtime + * do IP management for new containers + * list active compute resources diff --git a/emuvim/cli/__main__.py b/emuvim/cli/__main__.py deleted file mode 100644 index 756ff89..0000000 --- a/emuvim/cli/__main__.py +++ /dev/null @@ -1,40 +0,0 @@ -""" - For now only a dummy client. Connects to the zerorpc interface of the - emulator and performs some actions (start/stop/list). - - We will provide a full CLI here later on which looks like: - - cli compute start dc1 my_name flavor_a - cli network create dc1 11.0.0.0/24 -""" -import time -import pprint -import zerorpc - - -def main(): - pp = pprint.PrettyPrinter(indent=4) - print "Example CLI client" - # create connection to remote Mininet instance - c = zerorpc.Client() - c.connect("tcp://127.0.0.1:4242") - - # do some API tests - print c.compute_action_start("dc2", "d1") - print c.compute_action_start("dc2", "d2") - - time.sleep(1) - print c.compute_list("dc2") - - time.sleep(1) - pp.pprint(c.compute_status("dc2", "d1")) - pp.pprint(c.compute_status("dc2", "d2")) - - time.sleep(5) - - print c.compute_action_stop("dc2", "d1") - print c.compute_action_stop("dc2", "d2") - - -if __name__ == '__main__': - main() diff --git a/emuvim/cli/compute.py b/emuvim/cli/compute.py new file mode 100644 index 0000000..b75b043 --- /dev/null +++ b/emuvim/cli/compute.py @@ -0,0 +1,56 @@ +""" +son-emu compute CLI +""" + +import argparse +import pprint +import zerorpc + + +pp = pprint.PrettyPrinter(indent=4) + + +class ZeroRpcClient(object): + + def __init__(self): + self.c = zerorpc.Client() + self.c.connect("tcp://127.0.0.1:4242") # yes, hard coded for now. we'll change this later + self.cmds = {} + + def execute_command(self, args): + if getattr(self, args["command"]) is not None: + # call the local method with the same name as the command arg + getattr(self, args["command"])(args) + else: + print "Command not implemented." + + def start(self, args): + r = self.c.compute_action_start( + args.get("datacenter"), args.get("name")) + pp.pprint(r) + + def stop(self, args): + r = self.c.compute_action_stop( + args.get("datacenter"), args.get("name")) + pp.pprint(r) + + def list(self, args): + print "TODO: Not implemented" + + def status(self, args): + print "TODO: Not implemented" + + +parser = argparse.ArgumentParser(description='son-emu compute') +parser.add_argument("command", help="Action to be executed.") +parser.add_argument( + "--datacenter", "-d", dest="datacenter", help="Data center.") +parser.add_argument( + "--name", "-n", dest="name", help="Compute name.") +# TODO: IP, image, etc. pp. + + +def main(argv): + args = vars(parser.parse_args(argv)) + c = ZeroRpcClient() + c.execute_command(args) diff --git a/emuvim/cli/network.py b/emuvim/cli/network.py new file mode 100644 index 0000000..0c50dda --- /dev/null +++ b/emuvim/cli/network.py @@ -0,0 +1,8 @@ +""" +son-emu network CLI +""" + + +def main(argv): + print "This is the son-emu network CLI." + print "Arguments: %s" % str(argv) diff --git a/emuvim/cli/son-emu-cli b/emuvim/cli/son-emu-cli new file mode 100755 index 0000000..57ae053 --- /dev/null +++ b/emuvim/cli/son-emu-cli @@ -0,0 +1,28 @@ +#!/usr/bin/python +""" + Simple CLI client to interact with a running emulator. + + The CLI offers different tools, e.g., compute, network, ... + Each of these tools is implemented as an independent Python + module. + + cli compute start dc1 my_name flavor_a + cli network create dc1 11.0.0.0/24 +""" + +import sys +import compute +import network + + +def main(): + if len(sys.argv) < 2: + print "Usage: son-emu-cli " + exit(0) + if sys.argv[1] == "compute": + compute.main(sys.argv[2:]) + elif sys.argv[1] == "network": + network.main(sys.argv[2:]) + +if __name__ == '__main__': + main() diff --git a/emuvim/dcemulator/net.py b/emuvim/dcemulator/net.py index f5e58d1..1762e0b 100644 --- a/emuvim/dcemulator/net.py +++ b/emuvim/dcemulator/net.py @@ -4,7 +4,7 @@ Distributed Cloud Emulator (dcemulator) """ import logging -from mininet.net import Mininet +from mininet.net import Dockernet from mininet.node import Controller, OVSKernelSwitch, Switch, Docker, Host from mininet.cli import CLI from mininet.log import setLogLevel, info @@ -28,7 +28,7 @@ class DCNetwork(object): # create a Mininet/Dockernet network setLogLevel('info') # set Mininet loglevel - self.mnet = Mininet(controller=Controller, switch=OVSKernelSwitch) + self.mnet = Dockernet(controller=Controller, switch=OVSKernelSwitch) self.mnet.addController('c0') def addDatacenter(self, name): -- 2.25.1