697e83c9191633ce825d2c19d64d3ee6135180f9
[osm/devops.git] / layer_netutils.py
1 from charmhelpers.core.hookenv import (
2 status_set,
3 action_get,
4 action_set,
5 action_fail,
6 )
7
8 from charms.reactive import (
9 when,
10 when_not,
11 set_state as set_flag,
12 remove_state as remove_flag,
13 )
14 import charms.sshproxy
15
16
17 @when_not('netutils.ready')
18 def ready():
19 status_set('active', 'Ready!')
20 set_flag('netutils.ready')
21
22
23 @when('actions.dig')
24 def 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
41 result, err = charms.sshproxy._run(cmd)
42 except:
43 action_fail('dig command failed:' + err)
44 else:
45 action_set({'outout': result})
46 finally:
47 remove_flag('actions.dig')
48
49
50 @when('actions.nmap')
51 def nmap():
52 err = ''
53 try:
54 result, err = charms.sshproxy._run(
55 'nmap {}'.format(action_get('destination'))
56 )
57 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')
66 def ping():
67 err = ''
68 try:
69 result, err = charms.sshproxy._run('ping -qc {} {}'.format(
70 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
82 @when('actions.traceroute')
83 def traceroute():
84 try:
85 result, err = charms.sshproxy._run(
86 'traceroute -m {} {}'.format(
87 action_get('hops'),
88 action_get('destination')
89 )
90 )
91 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')