From 66ab304df70f7d99088d846bc24f60eece08e2e0 Mon Sep 17 00:00:00 2001
From: Mike Marsh <mmarsh@cs.umd.edu>
Date: Sat, 10 Feb 2018 13:09:28 -0500
Subject: [PATCH] putting together testbed

---
 example.yml      |  6 ++++--
 start_testbed.py | 43 ++++++++++++++++++++++++++++++++++++++++++
 stop_testbed.py  | 32 +++++++++++++++++++++++++++++++
 tear_down.py     | 49 ------------------------------------------------
 4 files changed, 79 insertions(+), 51 deletions(-)
 create mode 100755 start_testbed.py
 create mode 100755 stop_testbed.py
 delete mode 100755 tear_down.py

diff --git a/example.yml b/example.yml
index 22ffd7f..213c7e0 100644
--- a/example.yml
+++ b/example.yml
@@ -1,13 +1,15 @@
 ---
 # Modify these to suit your needs.
 image: "gizmonic.cs.umd.edu:8080/baseline"
-image_cmd: "tail -f /etc/issue"
+image_cmd: "/usr/bin/tail -f /etc/issue"
 
 # Here's how you define the network you want.
 # The topology can be "mesh" or "star". The
 # latter will designate the first node as the
-# center of the star.
+# center of the star. Containers will be named
+# <name>0 through <name><num-nodes - 1>.
 network:
+  name: test
   num_nodes: 4
   topology: mesh
   subnet_addr: 186.192.0.0
diff --git a/start_testbed.py b/start_testbed.py
new file mode 100755
index 0000000..4cd5295
--- /dev/null
+++ b/start_testbed.py
@@ -0,0 +1,43 @@
+#! /usr/bin/env python
+
+import subprocess
+import yaml
+from argparse import ArgumentParser
+
+parser = ArgumentParser()
+parser.add_argument('-f', '--file',
+                    dest='file',
+                    default='example.yml',
+                    help='YML file containing the testbed configuration'
+                    )
+
+args = parser.parse_args()
+
+with open(args.file) as cfile:
+    conf = yaml.load(cfile)
+
+retval = subprocess.call(['docker','pull',conf['image']])
+
+def make_mesh():
+    pass
+
+def make_star():
+    pass
+
+ntwk = conf['network']
+
+def start_node(name,i):
+    ctr_name = '{name}{i}'.format(name=name,i=i)
+    command = ['docker',
+               'run',
+               '-d',
+               '--name',
+               ctr_name,
+               conf['image']
+              ]
+    command.extend(conf['image_cmd'].split())
+    retval = subprocess.call(command)
+
+for i in range(ntwk['num_nodes']):
+    start_node(ntwk['name'],i)
+
diff --git a/stop_testbed.py b/stop_testbed.py
new file mode 100755
index 0000000..2d3556d
--- /dev/null
+++ b/stop_testbed.py
@@ -0,0 +1,32 @@
+#! /usr/bin/env python
+
+import subprocess
+import yaml
+from argparse import ArgumentParser
+
+parser = ArgumentParser()
+parser.add_argument('-f', '--file',
+                    dest='file',
+                    default='example.yml',
+                    help='YML file containing the testbed configuration'
+                    )
+
+args = parser.parse_args()
+
+with open(args.file) as cfile:
+    conf = yaml.load(cfile)
+
+retval = subprocess.call(['docker','pull',conf['image']])
+
+ntwk = conf['network']
+
+def stop_node(name,i):
+    ctr_name = '{name}{i}'.format(name=name,i=i)
+    command = ['docker', 'kill', ctr_name ]
+    retval = subprocess.call(command)
+    command = ['docker', 'rm', ctr_name ]
+    retval = subprocess.call(command)
+
+for i in range(ntwk['num_nodes']):
+    stop_node(ntwk['name'],i)
+
diff --git a/tear_down.py b/tear_down.py
deleted file mode 100755
index e536125..0000000
--- a/tear_down.py
+++ /dev/null
@@ -1,49 +0,0 @@
-#! /usr/bin/env python
-
-import json
-import docker.client
-import time
-import os
-
-d = docker.Client()
-fd = open("manifest.json")
-j = json.load(fd)
-
-def extract_name(ctr):
-    if 'Name' not in ctr: return None
-    slashed_name = ctr['Name']
-    return slashed_name.replace("/","")
-
-def remove(name):
-    if name is None: return
-    try:
-        d.kill(name)
-    except:
-        None
-    try:
-        d.remove_container(name)
-    except:
-        None
-    os.system("sudo ip netns delete %s" % name)
-
-for k in j:
-    subnet = j[k]
-    # First, kill the bridges.  We do this because otherwise any tcpdumps we leave
-    # running will not terminate properly.
-    if 'gateways' in subnet:
-        gateways = subnet['gateways']
-        for g in gateways:
-            if 'bridge' in g:
-                gname = g['name']
-                gdev = g['bridge']
-                os.system("sudo ip netns exec {} ip link delete {} type bridge".format(gname,gdev))
-    if 'containers' in subnet:
-        containers = subnet['containers']
-        if 'gateways' in containers:
-            gateways = containers['gateways']
-            for g in gateways:
-                remove(extract_name(g))
-        if 'nodes' in containers:
-            nodes = containers['nodes']
-            for n in nodes:
-                remove(extract_name(n))
-- 
GitLab