blob: 697e83c9191633ce825d2c19d64d3ee6135180f9 [file] [log] [blame]
Adam Israel33779282016-10-05 09:08:16 -07001from charmhelpers.core.hookenv import (
Adam Israel33779282016-10-05 09:08:16 -07002 status_set,
3 action_get,
4 action_set,
5 action_fail,
Adam Israel33779282016-10-05 09:08:16 -07006)
7
8from charms.reactive import (
9 when,
10 when_not,
11 set_state as set_flag,
12 remove_state as remove_flag,
13)
Adam Israel1e00fd12016-11-04 11:07:17 -040014import charms.sshproxy
Adam Israel33779282016-10-05 09:08:16 -070015
16
17@when_not('netutils.ready')
18def ready():
19 status_set('active', 'Ready!')
20 set_flag('netutils.ready')
21
Adam Israel1e00fd12016-11-04 11:07:17 -040022
Adam Israel27aa41d2016-10-05 16:27:19 -070023@when('actions.dig')
24def dig():
25 err = ''
26 try:
27 nsserver = action_get('nsserver')
28 host = action_get('host')
29 nstype = action_get('type')
30 cmd = "dig"
31
32 if nsserver:
33 cmd += " @{}".format(nsserver)
34 if host:
35 cmd += " {}".format(host)
36 else:
37 action_fail('Hostname required.')
38 if nstype:
39 cmd += " -t {}".format(nstype)
40
Adam Israel1e00fd12016-11-04 11:07:17 -040041 result, err = charms.sshproxy._run(cmd)
Adam Israel27aa41d2016-10-05 16:27:19 -070042 except:
43 action_fail('dig command failed:' + err)
44 else:
45 action_set({'outout': result})
46 finally:
47 remove_flag('actions.dig')
Adam Israel33779282016-10-05 09:08:16 -070048
Adam Israel1e00fd12016-11-04 11:07:17 -040049
Adam Israel33779282016-10-05 09:08:16 -070050@when('actions.nmap')
51def nmap():
52 err = ''
53 try:
Adam Israel1e00fd12016-11-04 11:07:17 -040054 result, err = charms.sshproxy._run(
55 'nmap {}'.format(action_get('destination'))
56 )
Adam Israel33779282016-10-05 09:08:16 -070057 except:
58 action_fail('nmap command failed:' + err)
59 else:
60 action_set({'outout': result})
61 finally:
62 remove_flag('actions.nmap')
63
64
65@when('actions.ping')
66def ping():
67 err = ''
68 try:
Adam Israel1e00fd12016-11-04 11:07:17 -040069 result, err = charms.sshproxy._run('ping -qc {} {}'.format(
Adam Israel33779282016-10-05 09:08:16 -070070 action_get('count'), action_get('destination'))
71 )
72
73 except:
74 action_fail('ping command failed:' + err)
75 else:
76 # Here you can send results back from ping, if you had time to parse it
77 action_set({'output': result})
78 finally:
79 remove_flag('actions.ping')
80
81
Adam Israel33779282016-10-05 09:08:16 -070082@when('actions.traceroute')
83def traceroute():
84 try:
Adam Israel1e00fd12016-11-04 11:07:17 -040085 result, err = charms.sshproxy._run(
86 'traceroute -m {} {}'.format(
87 action_get('hops'),
88 action_get('destination')
89 )
90 )
Adam Israel33779282016-10-05 09:08:16 -070091 except:
92 action_fail('traceroute command failed')
93 else:
94 # Here you can send results back from ping, if you had time to parse it
95 action_set({'output': result})
96 finally:
97 remove_flag('actions.traceroute')