Add initial support for iperf3, both in client and server modes.
[osm/devops.git] / layers / netutils / reactive / layer_netutils.py
index 697e83c..1fd4cb2 100644 (file)
@@ -1,17 +1,20 @@
 from charmhelpers.core.hookenv import (
-    status_set,
     action_get,
-    action_set,
     action_fail,
+    action_set,
+    config,
+    log,
+    status_set,
 )
 
 from charms.reactive import (
+    remove_state as remove_flag,
+    set_state as set_flag,
     when,
     when_not,
-    set_state as set_flag,
-    remove_state as remove_flag,
 )
 import charms.sshproxy
+from subprocess import CalledProcessError
 
 
 @when_not('netutils.ready')
@@ -95,3 +98,40 @@ def traceroute():
         action_set({'output': result})
     finally:
         remove_flag('actions.traceroute')
+
+
+@when('actions.iperf3')
+def iperf3():
+    err = ''
+    try:
+        # TODO: read all the flags via action_get and build the
+        # proper command line to run iperf3
+        host = action_get('host')
+
+        cmd = 'iperf3 -c {} --json'.format(host)
+        result, err = charms.sshproxy._run(cmd)
+    except CalledProcessError as e:
+        action_fail('iperf3 command failed:' + e.output)
+    else:
+        action_set({'outout': result})
+    finally:
+        remove_flag('actions.iperf3')
+
+
+@when('config.changed')
+def config_changed():
+    """ Handle configuration changes """
+    cfg = config()
+    if cfg.changed('iperf3'):
+        if cfg['iperf3']:
+            # start iperf in server + daemon mode
+            cmd = "iperf3 -s -D"
+        else:
+            cmd = "killall iperf3"
+        try:
+            charms.sshproxy._run(cmd)
+            log("iperf3 stopped.")
+        except CalledProcessError:
+            log("iperf3 not running.")
+        else:
+            log("iperf3 started.")