Merge "cirros test code changes for VCD"
[osm/devops.git] / juju-charms / layers / netutils / reactive / layer_netutils.py
1 from charmhelpers.core.hookenv import (
2 action_get,
3 action_fail,
4 action_set,
5 config,
6 log,
7 status_set,
8 )
9
10 from charms.reactive import (
11 remove_state as remove_flag,
12 set_state as set_flag,
13 when,
14 when_not,
15 )
16 import charms.sshproxy
17 from subprocess import CalledProcessError
18
19
20 @when_not('netutils.ready')
21 def ready():
22 status_set('active', 'Ready!')
23 set_flag('netutils.ready')
24
25
26 @when('actions.dig')
27 def dig():
28 err = ''
29 try:
30 nsserver = action_get('nsserver')
31 host = action_get('host')
32 nstype = action_get('type')
33 cmd = "dig"
34
35 if nsserver:
36 cmd += " @{}".format(nsserver)
37 if host:
38 cmd += " {}".format(host)
39 else:
40 action_fail('Hostname required.')
41 if nstype:
42 cmd += " -t {}".format(nstype)
43
44 result, err = charms.sshproxy._run(cmd)
45 except:
46 action_fail('dig command failed:' + err)
47 else:
48 action_set({'outout': result})
49 finally:
50 remove_flag('actions.dig')
51
52
53 @when('actions.nmap')
54 def nmap():
55 err = ''
56 try:
57 result, err = charms.sshproxy._run(
58 'nmap {}'.format(action_get('destination'))
59 )
60 except:
61 action_fail('nmap command failed:' + err)
62 else:
63 action_set({'outout': result})
64 finally:
65 remove_flag('actions.nmap')
66
67
68 @when('actions.ping')
69 def ping():
70 err = ''
71 try:
72 result, err = charms.sshproxy._run('ping -qc {} {}'.format(
73 action_get('count'), action_get('destination'))
74 )
75
76 except:
77 action_fail('ping command failed:' + err)
78 else:
79 # Here you can send results back from ping, if you had time to parse it
80 action_set({'output': result})
81 finally:
82 remove_flag('actions.ping')
83
84
85 @when('actions.traceroute')
86 def traceroute():
87 try:
88 result, err = charms.sshproxy._run(
89 'traceroute -m {} {}'.format(
90 action_get('hops'),
91 action_get('destination')
92 )
93 )
94 except:
95 action_fail('traceroute command failed')
96 else:
97 # Here you can send results back from ping, if you had time to parse it
98 action_set({'output': result})
99 finally:
100 remove_flag('actions.traceroute')
101
102
103 @when('actions.iperf3')
104 def iperf3():
105 err = ''
106 try:
107 # TODO: read all the flags via action_get and build the
108 # proper command line to run iperf3
109 host = action_get('host')
110
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)
115 else:
116 action_set({'outout': result})
117 finally:
118 remove_flag('actions.iperf3')
119
120
121 @when('config.changed')
122 def config_changed():
123 """ Handle configuration changes """
124 cfg = config()
125 if cfg.changed('iperf3'):
126 if cfg['iperf3']:
127 # start iperf in server + daemon mode
128 cmd = "iperf3 -s -D"
129 else:
130 cmd = "killall iperf3"
131 try:
132 charms.sshproxy._run(cmd)
133 log("iperf3 stopped.")
134 except CalledProcessError:
135 log("iperf3 not running.")
136 else:
137 log("iperf3 started.")