Integrate sshproxy layer so netutil actions can be run against a remote host
Fix lint errors
Signed-off-by: Adam Israel <adam.israel@canonical.com>
diff --git a/layers/netutils/layer.yaml b/layers/netutils/layer.yaml
index c1bf2cf..e13c81a 100644
--- a/layers/netutils/layer.yaml
+++ b/layers/netutils/layer.yaml
@@ -1,7 +1,8 @@
repo: git@github.com:AdamIsrael/layer-netutils.git
-includes: ['layer:basic'] # if you use any interfaces, add them here
+includes: ['layer:basic', 'layer:sshproxy']
options:
basic:
packages:
- traceroute
- nmap
+ - iperf3
diff --git a/layers/netutils/reactive/layer_netutils.py b/layers/netutils/reactive/layer_netutils.py
index 7043f19..697e83c 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 @@
set_state as set_flag,
remove_state as remove_flag,
)
-
-import subprocess
+import charms.sshproxy
@when_not('netutils.ready')
@@ -22,6 +19,7 @@
status_set('active', 'Ready!')
set_flag('netutils.ready')
+
@when('actions.dig')
def dig():
err = ''
@@ -40,7 +38,7 @@
if nstype:
cmd += " -t {}".format(nstype)
- result, err = _run(cmd)
+ result, err = charms.sshproxy._run(cmd)
except:
action_fail('dig command failed:' + err)
else:
@@ -48,11 +46,14 @@
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:
@@ -65,7 +66,7 @@
def ping():
err = ''
try:
- result, err = _run('ping -qc {} {}'.format(
+ result, err = charms.sshproxy._run('ping -qc {} {}'.format(
action_get('count'), action_get('destination'))
)
@@ -78,11 +79,15 @@
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:
@@ -90,22 +95,3 @@
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'))