| 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 | action_get, |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 3 | action_fail, |
| Adam Israel | f7d9a2b | 2016-11-10 11:20:46 -0500 | [diff] [blame^] | 4 | action_set, |
| 5 | config, |
| 6 | log, |
| 7 | status_set, |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | from charms.reactive import ( |
| Adam Israel | f7d9a2b | 2016-11-10 11:20:46 -0500 | [diff] [blame^] | 11 | remove_state as remove_flag, |
| 12 | set_state as set_flag, |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 13 | when, |
| 14 | when_not, |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 15 | ) |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 16 | import charms.sshproxy |
| Adam Israel | f7d9a2b | 2016-11-10 11:20:46 -0500 | [diff] [blame^] | 17 | from subprocess import CalledProcessError |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | @when_not('netutils.ready') |
| 21 | def ready(): |
| 22 | status_set('active', 'Ready!') |
| 23 | set_flag('netutils.ready') |
| 24 | |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 25 | |
| Adam Israel | 27aa41d | 2016-10-05 16:27:19 -0700 | [diff] [blame] | 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 | |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 44 | result, err = charms.sshproxy._run(cmd) |
| Adam Israel | 27aa41d | 2016-10-05 16:27:19 -0700 | [diff] [blame] | 45 | except: |
| 46 | action_fail('dig command failed:' + err) |
| 47 | else: |
| 48 | action_set({'outout': result}) |
| 49 | finally: |
| 50 | remove_flag('actions.dig') |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 51 | |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 52 | |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 53 | @when('actions.nmap') |
| 54 | def nmap(): |
| 55 | err = '' |
| 56 | try: |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 57 | result, err = charms.sshproxy._run( |
| 58 | 'nmap {}'.format(action_get('destination')) |
| 59 | ) |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 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: |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 72 | result, err = charms.sshproxy._run('ping -qc {} {}'.format( |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 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 | |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 85 | @when('actions.traceroute') |
| 86 | def traceroute(): |
| 87 | try: |
| Adam Israel | 1e00fd1 | 2016-11-04 11:07:17 -0400 | [diff] [blame] | 88 | result, err = charms.sshproxy._run( |
| 89 | 'traceroute -m {} {}'.format( |
| 90 | action_get('hops'), |
| 91 | action_get('destination') |
| 92 | ) |
| 93 | ) |
| Adam Israel | 3377928 | 2016-10-05 09:08:16 -0700 | [diff] [blame] | 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') |
| Adam Israel | f7d9a2b | 2016-11-10 11:20:46 -0500 | [diff] [blame^] | 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.") |