| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 1 | from charmhelpers.core.hookenv import ( |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 2 | status_set, |
| 3 | action_get, |
| 4 | action_set, |
| 5 | action_fail, |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 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 | ) |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 14 | import charms.sshproxy |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 15 | |
| 16 | |
| 17 | @when_not('netutils.ready') |
| 18 | def ready(): |
| 19 | status_set('active', 'Ready!') |
| 20 | set_flag('netutils.ready') |
| 21 | |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 22 | |
| Adam Israel | 27aa41d | 2016-10-05 16:27:19 -0700 | [diff] [blame] | 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 | |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 41 | result, err = charms.sshproxy._run(cmd) |
| Adam Israel | 27aa41d | 2016-10-05 16:27:19 -0700 | [diff] [blame] | 42 | except: |
| 43 | action_fail('dig command failed:' + err) |
| 44 | else: |
| 45 | action_set({'outout': result}) |
| 46 | finally: |
| 47 | remove_flag('actions.dig') |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 48 | |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 49 | |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 50 | @when('actions.nmap') |
| 51 | def nmap(): |
| 52 | err = '' |
| 53 | try: |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 54 | result, err = charms.sshproxy._run( |
| 55 | 'nmap {}'.format(action_get('destination')) |
| 56 | ) |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 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: |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 69 | result, err = charms.sshproxy._run('ping -qc {} {}'.format( |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 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 | |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 82 | @when('actions.traceroute') |
| 83 | def traceroute(): |
| 84 | try: |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 85 | result, err = charms.sshproxy._run( |
| 86 | 'traceroute -m {} {}'.format( |
| 87 | action_get('hops'), |
| 88 | action_get('destination') |
| 89 | ) |
| 90 | ) |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 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') |