diff --git a/netem b/netem deleted file mode 100644 index f628fcd1edd233955fcf8cabd88d8e3ea6295550..0000000000000000000000000000000000000000 --- a/netem +++ /dev/null @@ -1,43 +0,0 @@ -#! /usr/bin/env python - -import json -import subprocess - -def main(): - module = AnsibleModule(argument_spec=dict( - state=dict(required=True), - name=dict(required=True,type='str'), - device=dict(required=True,type='str'), - )) - - j = json.loads(module.params['state']) - name = module.params['name'] - device = module.params['device'] - changed = False - args = ["/sbin/ip", - "netns", "exec", name, - "tc", "qdisc", "change", "dev", device, "root", - "netem"] - if 'delay' in j: - args.append("delay") - args.append(j['delay']) - changed = True - if 'rate' in j: - args.append("rate") - args.append(j['rate']) - changed = True - if 'loss' in j: - args.append("loss") - args.append(j['loss']) - changed = True - if changed: - cmd = reduce( lambda x,y: "%s %s" % (x,y), args ) - sargs = map( lambda x: str(x), args ) - try: - subprocess.check_output(sargs,stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as e: - module.fail_json(msg=e.output) - module.exit_json(changed=changed,cmd=cmd) - -from ansible.module_utils.basic import * -main() diff --git a/netem.py b/netem.py new file mode 100755 index 0000000000000000000000000000000000000000..892745def40ecbde6760ae29b6b0fccc2f9b6478 --- /dev/null +++ b/netem.py @@ -0,0 +1,47 @@ +#! /usr/bin/env python + +import json +import subprocess +from argparse import ArgumentParser + +parser = ArgumentParser() +parser.add_argument('-n', '--namespace', + dest='namespace', + help='network namespace for the link to modify' + ) +parser.add_argument('-d', '--device', + dest='device', + help='device to modify' + ) +parser.add_argument('-B, '--bandwidth', + dest='bandwidth', + help='bandwidth to set' + ) +parser.add_argument('-D, '--delay', + dest='delay', + help='link latency' + ) +parser.add_argument('-L, '--loss', + dest='loss', + help='loss rate' + ) + +args = parser.parse_args() + +# Enable network emulation on the device. +cmd = ["sudo", "/sbin/ip", + "netns", "exec", args.namespace, + "tc", "qdisc", "change", "dev", args.device, "root", + "netem"] +if args.delay is not None: + cmd.append("delay") + cmd.append(str(args.delay)) +if args.bandwidth is not None: + cmd.append("rate") + cmd.append(str(args.bandwidth)) +if args.loss is not None: + cmd.append("loss") + cmd.append(str(args.loss)) + +subprocess.check_output(cmd,stderr=subprocess.STDOUT) +