X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=layers%2Fnetutils%2Freactive%2Flayer_netutils.py;h=697e83c9191633ce825d2c19d64d3ee6135180f9;hb=1e00fd1645b22d74c0b563e275e2433b937a3ee6;hp=8f8db268acbed7ab3ddcf43a00d676d13f08f931;hpb=3377928d267019b66a219da3bba007618256d2d4;p=osm%2Fdevops.git diff --git a/layers/netutils/reactive/layer_netutils.py b/layers/netutils/reactive/layer_netutils.py index 8f8db268..697e83c9 100644 --- a/layers/netutils/reactive/layer_netutils.py +++ b/layers/netutils/reactive/layer_netutils.py @@ -1,10 +1,8 @@ from charmhelpers.core.hookenv import ( - config, status_set, action_get, action_set, action_fail, - log, ) from charms.reactive import ( @@ -13,8 +11,7 @@ from charms.reactive import ( set_state as set_flag, remove_state as remove_flag, ) - -import subprocess +import charms.sshproxy @when_not('netutils.ready') @@ -23,11 +20,40 @@ def 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 = _run('nmap {}'.format(action_get('destination'))) + result, err = charms.sshproxy._run( + 'nmap {}'.format(action_get('destination')) + ) except: action_fail('nmap command failed:' + err) else: @@ -40,7 +66,7 @@ def nmap(): def ping(): err = '' try: - result, err = _run('ping -qc {} {}'.format( + result, err = charms.sshproxy._run('ping -qc {} {}'.format( action_get('count'), action_get('destination')) ) @@ -53,11 +79,15 @@ def ping(): remove_flag('actions.ping') - @when('actions.traceroute') def traceroute(): try: - result, err = _run('traceroute -m {} {}'.format(action_get('hops'), action_get('destination'))) + result, err = charms.sshproxy._run( + 'traceroute -m {} {}'.format( + action_get('hops'), + action_get('destination') + ) + ) except: action_fail('traceroute command failed') else: @@ -65,22 +95,3 @@ def traceroute(): action_set({'output': result}) finally: remove_flag('actions.traceroute') - - - -def _run(cmd, env=None): - if isinstance(cmd, str): - cmd = cmd.split() if ' ' in cmd else [cmd] - - log(cmd) - p = subprocess.Popen(cmd, - env=env, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - stdout, stderr = p.communicate() - retcode = p.poll() - if retcode > 0: - raise subprocess.CalledProcessError(returncode=retcode, - cmd=cmd, - output=stderr.decode("utf-8").strip()) - return (stdout.decode('utf-8'), stderr.decode('utf-8'))