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')
29 nsserver
= action_get('nsserver')
30 host
= action_get('host')
31 nstype
= action_get('type')
35 cmd
+= " @{}".format(nsserver
)
37 cmd
+= " {}".format(host
)
39 action_fail('Hostname required.')
41 cmd
+= " -t {}".format(nstype
)
43 result
, err
= _run(cmd
)
45 action_fail('dig command failed:' + err
)
47 action_set({'outout': result
})
49 remove_flag('actions.dig')
55 result
, err
= _run('nmap {}'.format(action_get('destination')))
57 action_fail('nmap command failed:' + err
)
59 action_set({'outout': result
})
61 remove_flag('actions.nmap')
68 result
, err
= _run('ping -qc {} {}'.format(
69 action_get('count'), action_get('destination'))
73 action_fail('ping command failed:' + err
)
75 # Here you can send results back from ping, if you had time to parse it
76 action_set({'output': result
})
78 remove_flag('actions.ping')
82 @when('actions.traceroute')
85 result
, err
= _run('traceroute -m {} {}'.format(action_get('hops'), action_get('destination')))
87 action_fail('traceroute command failed')
89 # Here you can send results back from ping, if you had time to parse it
90 action_set({'output': result
})
92 remove_flag('actions.traceroute')
96 def _run(cmd
, env
=None):
97 if isinstance(cmd
, str):
98 cmd
= cmd
.split() if ' ' in cmd
else [cmd
]
101 p
= subprocess
.Popen(cmd
,
103 stdout
=subprocess
.PIPE
,
104 stderr
=subprocess
.PIPE
)
105 stdout
, stderr
= p
.communicate()
108 raise subprocess
.CalledProcessError(returncode
=retcode
,
110 output
=stderr
.decode("utf-8").strip())
111 return (stdout
.decode('utf-8'), stderr
.decode('utf-8'))