Skip to content

Commit

Permalink
Expose parameters in manifest in the ENV of commands being executed o…
Browse files Browse the repository at this point in the history
…n hosts
  • Loading branch information
Theo authored and sethfowler committed Apr 10, 2017
1 parent cf69fa8 commit d12766e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
20 changes: 15 additions & 5 deletions docker/scripts/mininet/multi_switch_mininet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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']]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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']:
Expand All @@ -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']:
Expand Down
4 changes: 2 additions & 2 deletions examples/customtopo.p4app/p4app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand Down
8 changes: 4 additions & 4 deletions examples/multi_iface.p4app/p4app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand Down
4 changes: 2 additions & 2 deletions examples/multiswitch.p4app/p4app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand Down

0 comments on commit d12766e

Please sign in to comment.