1 from charmhelpers
.core
.hookenv
import (
10 from charms
.reactive
import (
11 remove_state
as remove_flag
,
12 set_state
as set_flag
,
16 import charms
.sshproxy
17 from subprocess
import CalledProcessError
20 @when_not('netutils.ready')
22 status_set('active', 'Ready!')
23 set_flag('netutils.ready')
30 nsserver
= action_get('nsserver')
31 host
= action_get('host')
32 nstype
= action_get('type')
36 cmd
+= " @{}".format(nsserver
)
38 cmd
+= " {}".format(host
)
40 action_fail('Hostname required.')
42 cmd
+= " -t {}".format(nstype
)
44 result
, err
= charms
.sshproxy
._run
(cmd
)
46 action_fail('dig command failed:' + err
)
48 action_set({'outout': result
})
50 remove_flag('actions.dig')
57 result
, err
= charms
.sshproxy
._run
(
58 'nmap {}'.format(action_get('destination'))
61 action_fail('nmap command failed:' + err
)
63 action_set({'outout': result
})
65 remove_flag('actions.nmap')
72 result
, err
= charms
.sshproxy
._run
('ping -qc {} {}'.format(
73 action_get('count'), action_get('destination'))
77 action_fail('ping command failed:' + err
)
79 # Here you can send results back from ping, if you had time to parse it
80 action_set({'output': result
})
82 remove_flag('actions.ping')
85 @when('actions.traceroute')
88 result
, err
= charms
.sshproxy
._run
(
89 'traceroute -m {} {}'.format(
91 action_get('destination')
95 action_fail('traceroute command failed')
97 # Here you can send results back from ping, if you had time to parse it
98 action_set({'output': result
})
100 remove_flag('actions.traceroute')
103 @when('actions.iperf3')
107 # TODO: read all the flags via action_get and build the
108 # proper command line to run iperf3
109 host
= action_get('host')
111 cmd
= 'iperf3 -c {} --json'.format(host
)
112 result
, err
= charms
.sshproxy
._run
(cmd
)
113 except CalledProcessError
as e
:
114 action_fail('iperf3 command failed:' + e
.output
)
116 action_set({'outout': result
})
118 remove_flag('actions.iperf3')
121 @when('config.changed')
122 def config_changed():
123 """ Handle configuration changes """
125 if cfg
.changed('iperf3'):
127 # start iperf in server + daemon mode
130 cmd
= "killall iperf3"
132 charms
.sshproxy
._run
(cmd
)
133 log("iperf3 stopped.")
134 except CalledProcessError
:
135 log("iperf3 not running.")
137 log("iperf3 started.")