X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=juju-charms%2Flayers%2Fnetutils%2Freactive%2Flayer_netutils.py;fp=juju-charms%2Flayers%2Fnetutils%2Freactive%2Flayer_netutils.py;h=1fd4cb246c5df4f3903248be2a494cfe082ee9de;hb=c7a82d0be7c72183e6db9e0fb522b484f9c8df84;hp=0000000000000000000000000000000000000000;hpb=7a74772ace6ca83bd7795d3251960ba16c57f90d;p=osm%2Fdevops.git diff --git a/juju-charms/layers/netutils/reactive/layer_netutils.py b/juju-charms/layers/netutils/reactive/layer_netutils.py new file mode 100644 index 00000000..1fd4cb24 --- /dev/null +++ b/juju-charms/layers/netutils/reactive/layer_netutils.py @@ -0,0 +1,137 @@ +from charmhelpers.core.hookenv import ( + action_get, + 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, +) +import charms.sshproxy +from subprocess import CalledProcessError + + +@when_not('netutils.ready') +def ready(): + status_set('active', 'Ready!') + set_flag('netutils.ready') + + +@when('actions.dig') +def dig(): + err = '' + try: + nsserver = action_get('nsserver') + host = action_get('host') + nstype = action_get('type') + cmd = "dig" + + if nsserver: + cmd += " @{}".format(nsserver) + if host: + cmd += " {}".format(host) + else: + action_fail('Hostname required.') + if nstype: + cmd += " -t {}".format(nstype) + + result, err = charms.sshproxy._run(cmd) + except: + action_fail('dig command failed:' + err) + else: + action_set({'outout': result}) + finally: + remove_flag('actions.dig') + + +@when('actions.nmap') +def nmap(): + err = '' + try: + result, err = charms.sshproxy._run( + 'nmap {}'.format(action_get('destination')) + ) + except: + action_fail('nmap command failed:' + err) + else: + action_set({'outout': result}) + finally: + remove_flag('actions.nmap') + + +@when('actions.ping') +def ping(): + err = '' + try: + result, err = charms.sshproxy._run('ping -qc {} {}'.format( + action_get('count'), action_get('destination')) + ) + + except: + action_fail('ping command failed:' + err) + else: + # Here you can send results back from ping, if you had time to parse it + action_set({'output': result}) + finally: + remove_flag('actions.ping') + + +@when('actions.traceroute') +def traceroute(): + try: + result, err = charms.sshproxy._run( + 'traceroute -m {} {}'.format( + action_get('hops'), + action_get('destination') + ) + ) + except: + action_fail('traceroute command failed') + else: + # Here you can send results back from ping, if you had time to parse it + 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.")