1 from charmhelpers
.core
.hookenv
import (
10 from charms
.reactive
import (
13 set_state
as set_flag
,
14 remove_state
as remove_flag
,
20 @when_not('netutils.ready')
22 status_set('active', 'Ready!')
23 set_flag('netutils.ready')
30 result
, err
= _run('nmap {}'.format(action_get('destination')))
32 action_fail('nmap command failed:' + err
)
34 action_set({'outout': result
})
36 remove_flag('actions.nmap')
43 result
, err
= _run('ping -qc {} {}'.format(
44 action_get('count'), action_get('destination'))
48 action_fail('ping command failed:' + err
)
50 # Here you can send results back from ping, if you had time to parse it
51 action_set({'output': result
})
53 remove_flag('actions.ping')
57 @when('actions.traceroute')
60 result
, err
= _run('traceroute -m {} {}'.format(action_get('hops'), action_get('destination')))
62 action_fail('traceroute command failed')
64 # Here you can send results back from ping, if you had time to parse it
65 action_set({'output': result
})
67 remove_flag('actions.traceroute')
71 def _run(cmd
, env
=None):
72 if isinstance(cmd
, str):
73 cmd
= cmd
.split() if ' ' in cmd
else [cmd
]
76 p
= subprocess
.Popen(cmd
,
78 stdout
=subprocess
.PIPE
,
79 stderr
=subprocess
.PIPE
)
80 stdout
, stderr
= p
.communicate()
83 raise subprocess
.CalledProcessError(returncode
=retcode
,
85 output
=stderr
.decode("utf-8").strip())
86 return (stdout
.decode('utf-8'), stderr
.decode('utf-8'))