From d12766ec8e4b0ad013975d76111a7da1eab0833b Mon Sep 17 00:00:00 2001 From: Theo Date: Thu, 6 Apr 2017 14:30:52 -0700 Subject: [PATCH] Expose parameters in manifest in the ENV of commands being executed on hosts --- .../scripts/mininet/multi_switch_mininet.py | 20 ++++++++++++++----- examples/customtopo.p4app/p4app.json | 4 ++-- examples/multi_iface.p4app/p4app.json | 8 ++++---- examples/multiswitch.p4app/p4app.json | 4 ++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/docker/scripts/mininet/multi_switch_mininet.py b/docker/scripts/mininet/multi_switch_mininet.py index 40fda7c..150df4f 100755 --- a/docker/scripts/mininet/multi_switch_mininet.py +++ b/docker/scripts/mininet/multi_switch_mininet.py @@ -59,6 +59,8 @@ next_thrift_port = args.thrift_port +def run_command(command): + return os.WEXITSTATUS(os.system(command)) def configureP4Switch(**switch_args): class ConfiguredP4Switch(P4Switch): @@ -79,9 +81,11 @@ def main(): conf = manifest['targets'][args.target] params = conf['parameters'] if 'parameters' in conf else {} + os.environ.update(dict(map(lambda (k,v): (k, str(v)), params.iteritems()))) + def formatParams(s): for param in params: - s = s.replace('%'+param+'%', str(params[param])) + s = s.replace('{'+param+'}', str(params[param])) return s AppTopo = apptopo.AppTopo @@ -100,6 +104,7 @@ def formatParams(s): if not os.path.isdir(args.log_dir): if os.path.exists(args.log_dir): raise Exception('Log dir exists and is not a dir') os.mkdir(args.log_dir) + os.environ['P4APP_LOGDIR'] = args.log_dir links = [l[:2] for l in conf['links']] @@ -157,7 +162,6 @@ def formatParams(s): def formatCmd(cmd): cmd = formatParams(cmd) - cmd = cmd.replace('%log_dir%', args.log_dir) for h in net.hosts: cmd = cmd.replace(h.name, h.defaultIntf().updateIP()) return cmd @@ -172,6 +176,8 @@ def _wait_for_exit(p, host): stdout_files[host_name].flush() stdout_files[host_name].close() + print '\n'.join(map(lambda (k,v): "%s: %s"%(k,v), params.iteritems())) + '\n' + for host_name in sorted(conf['hosts'].keys()): host = conf['hosts'][host_name] if 'cmd' not in host: continue @@ -181,7 +187,7 @@ def _wait_for_exit(p, host): stdout_files[h.name] = open(stdout_filename, 'w') cmd = formatCmd(host['cmd']) print h.name, cmd - p = h.popen(cmd, stdout=stdout_files[h.name]) + p = h.popen(cmd, stdout=stdout_files[h.name], shell=True, preexec_fn=os.setpgrp) if 'startup_sleep' in host: sleep(host['startup_sleep']) if 'wait' in host and host['wait']: @@ -193,13 +199,17 @@ def _wait_for_exit(p, host): if 'wait' in conf['hosts'][host_name] and conf['hosts'][host_name]['wait']: _wait_for_exit(p, host_name) + for p, host_name in host_procs: if 'wait' in conf['hosts'][host_name] and conf['hosts'][host_name]['wait']: continue if p.returncode is None: - p.send_signal(signal.SIGINT) + run_command('pkill -INT -P %d' % p.pid) sleep(0.2) - if p.returncode is None: p.kill() + rc = run_command('pkill -0 -P %d' % p.pid) # check if it's still running + if rc == 0: # the process group is still running, send TERM + sleep(1) # give it a little more time to exit gracefully + run_command('pkill -TERM -P %d' % p.pid) _wait_for_exit(p, host_name) if 'after' in conf and 'cmd' in conf['after']: diff --git a/examples/customtopo.p4app/p4app.json b/examples/customtopo.p4app/p4app.json index 68a8a41..fd53457 100644 --- a/examples/customtopo.p4app/p4app.json +++ b/examples/customtopo.p4app/p4app.json @@ -9,12 +9,12 @@ "links": [["h1", "s1"], ["s1", "s2"], ["s2", "h2", 50]], "hosts": { "h1": { - "cmd": "python echo_server.py %port%", + "cmd": "python echo_server.py $port", "startup_sleep": 0.2, "wait": false }, "h2": { - "cmd": "python echo_client.py h1 %port% %echo_msg%", + "cmd": "python echo_client.py h1 $port $echo_msg", "wait": true } }, diff --git a/examples/multi_iface.p4app/p4app.json b/examples/multi_iface.p4app/p4app.json index 21d8973..0ee6073 100644 --- a/examples/multi_iface.p4app/p4app.json +++ b/examples/multi_iface.p4app/p4app.json @@ -4,20 +4,20 @@ "targets": { "multiswitch": { "auto-control-plane": true, - "links": [["h1", "s1"], ["h2", "s2"], ["h3", "s1"], ["h3", "s2", "%mylatency%ms"]], + "links": [["h1", "s1"], ["h2", "s2"], ["h3", "s1"], ["h3", "s2", "{mylatency}ms"]], "hosts": { "h1": { - "cmd": "python echo_server.py %port%", + "cmd": "python echo_server.py $port", "startup_sleep": 0.2, "wait": false }, "h2": { - "cmd": "python echo_server.py %port%", + "cmd": "python echo_server.py $port", "startup_sleep": 0.2, "wait": false }, "h3": { - "cmd": "python echo_client.py %echo_msg% h1 %port% h2 %port%", + "cmd": "python echo_client.py $echo_msg h1 $port h2 $port", "wait": true } }, diff --git a/examples/multiswitch.p4app/p4app.json b/examples/multiswitch.p4app/p4app.json index ddc38f4..cdef195 100644 --- a/examples/multiswitch.p4app/p4app.json +++ b/examples/multiswitch.p4app/p4app.json @@ -7,12 +7,12 @@ "links": [["h1", "s1"], ["s1", "s2"], ["s2", "h2", 50]], "hosts": { "h1": { - "cmd": "python echo_server.py %port%", + "cmd": "python echo_server.py $port", "startup_sleep": 0.2, "wait": false }, "h2": { - "cmd": "python echo_client.py h1 %port% %echo_msg%", + "cmd": "python echo_client.py h1 $port $echo_msg", "wait": true } },