| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 1 | from charmhelpers.core.hookenv import ( |
| 2 | action_get, |
| 3 | action_fail, |
| 4 | action_set, |
| 5 | config, |
| 6 | status_set, |
| 7 | ) |
| 8 | |
| 9 | from charms.reactive import ( |
| 10 | remove_state as remove_flag, |
| 11 | set_state as set_flag, |
| 12 | when, |
| Adam Israel | 5a0f6e4 | 2017-10-18 10:39:40 -0400 | [diff] [blame] | 13 | when_not, |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 14 | ) |
| 15 | import charms.sshproxy |
| Adam Israel | 5a0f6e4 | 2017-10-18 10:39:40 -0400 | [diff] [blame] | 16 | # from subprocess import ( |
| 17 | # Popen, |
| 18 | # CalledProcessError, |
| 19 | # PIPE, |
| 20 | # ) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 21 | |
| 22 | |
| 23 | cfg = config() |
| 24 | |
| 25 | |
| Adam Israel | c1ec571 | 2017-10-19 19:26:00 -0400 | [diff] [blame] | 26 | @when_not('pingpong.configured') |
| 27 | def not_configured(): |
| 28 | """Check the current configuration. |
| Adam Israel | 1423eb6 | 2017-11-06 17:19:02 -0500 | [diff] [blame^] | 29 | |
| Adam Israel | c1ec571 | 2017-10-19 19:26:00 -0400 | [diff] [blame] | 30 | Check the current values in config to see if we have enough |
| Adam Israel | 1423eb6 | 2017-11-06 17:19:02 -0500 | [diff] [blame^] | 31 | information to continue. |
| 32 | """ |
| Adam Israel | c1ec571 | 2017-10-19 19:26:00 -0400 | [diff] [blame] | 33 | config_changed() |
| 34 | |
| 35 | |
| Adam Israel | 5a0f6e4 | 2017-10-18 10:39:40 -0400 | [diff] [blame] | 36 | @when('config.changed', 'sshproxy.configured') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 37 | def config_changed(): |
| Adam Israel | 5a0f6e4 | 2017-10-18 10:39:40 -0400 | [diff] [blame] | 38 | """Verify the configuration. |
| Adam Israel | 1423eb6 | 2017-11-06 17:19:02 -0500 | [diff] [blame^] | 39 | |
| Adam Israel | 5a0f6e4 | 2017-10-18 10:39:40 -0400 | [diff] [blame] | 40 | Verify that the charm has been configured |
| 41 | """ |
| Adam Israel | c1ec571 | 2017-10-19 19:26:00 -0400 | [diff] [blame] | 42 | status_set('maintenance', 'Verifying configuration data...') |
| Adam Israel | 5a0f6e4 | 2017-10-18 10:39:40 -0400 | [diff] [blame] | 43 | (validated, output) = charms.sshproxy.verify_ssh_credentials() |
| 44 | if not validated: |
| 45 | status_set('blocked', 'Unable to verify SSH credentials: {}'.format( |
| 46 | output |
| 47 | )) |
| Adam Israel | 1423eb6 | 2017-11-06 17:19:02 -0500 | [diff] [blame^] | 48 | return |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 49 | if all(k in cfg for k in ['mode']): |
| 50 | if cfg['mode'] in ['ping', 'pong']: |
| 51 | set_flag('pingpong.configured') |
| 52 | status_set('active', 'ready!') |
| 53 | return |
| 54 | status_set('blocked', 'Waiting for configuration') |
| 55 | |
| 56 | |
| Adam Israel | 5a0f6e4 | 2017-10-18 10:39:40 -0400 | [diff] [blame] | 57 | @when('config.changed') |
| 58 | @when_not('sshproxy.configured') |
| 59 | def invalid_credentials(): |
| 60 | status_set('blocked', 'Waiting for SSH credentials.') |
| 61 | pass |
| 62 | |
| 63 | |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 64 | def is_ping(): |
| 65 | if cfg['mode'] == 'ping': |
| 66 | return True |
| 67 | return False |
| 68 | |
| 69 | |
| 70 | def is_pong(): |
| 71 | return not is_ping() |
| 72 | |
| 73 | |
| 74 | def get_port(): |
| 75 | port = 18888 |
| 76 | if is_pong(): |
| 77 | port = 18889 |
| 78 | return port |
| 79 | |
| Philip Joseph | 0efe584 | 2017-01-14 00:07:30 +0530 | [diff] [blame] | 80 | |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 81 | @when('pingpong.configured') |
| 82 | @when('actions.start') |
| 83 | def start(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 84 | try: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 85 | # Bring up the eth1 interface. |
| 86 | # The selinux label on the file needs to be set correctly |
| Philip Joseph | 24a06f0 | 2017-04-05 21:31:12 +0530 | [diff] [blame] | 87 | cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1" |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 88 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 89 | except Exception as e: |
| 90 | err = "{}".format(e) |
| 91 | action_fail('command failed: {}, errors: {}'.format(err, e.output)) |
| 92 | remove_flag('actions.start') |
| 93 | return |
| 94 | |
| Adam Israel | 1f9ce57 | 2017-10-16 14:46:29 -0400 | [diff] [blame] | 95 | # Attempt to raise the non-mgmt interface, but ignore failures if |
| 96 | # the interface is already up. |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 97 | try: |
| Adam Israel | 1f9ce57 | 2017-10-16 14:46:29 -0400 | [diff] [blame] | 98 | cmd = "sudo timeout 30 /sbin/ifup eth1" |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 99 | result, err = charms.sshproxy._run(cmd) |
| 100 | except Exception as e: |
| Adam Israel | 1f9ce57 | 2017-10-16 14:46:29 -0400 | [diff] [blame] | 101 | pass |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 102 | |
| 103 | try: |
| Adam Israel | 1f9ce57 | 2017-10-16 14:46:29 -0400 | [diff] [blame] | 104 | cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \ |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 105 | format(cfg['mode']) |
| 106 | result, err = charms.sshproxy._run(cmd) |
| 107 | except Exception as e: |
| 108 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 109 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 110 | action_set({'stdout': result, |
| 111 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 112 | finally: |
| 113 | remove_flag('actions.start') |
| 114 | |
| 115 | |
| 116 | @when('pingpong.configured') |
| 117 | @when('actions.stop') |
| 118 | def stop(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 119 | try: |
| 120 | # Enter the command to stop your service(s) |
| Philip Joseph | 24a06f0 | 2017-04-05 21:31:12 +0530 | [diff] [blame] | 121 | cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg['mode']) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 122 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 123 | except Exception as e: |
| 124 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 125 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 126 | action_set({'stdout': result, |
| 127 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 128 | finally: |
| 129 | remove_flag('actions.stop') |
| 130 | |
| 131 | |
| 132 | @when('pingpong.configured') |
| 133 | @when('actions.restart') |
| 134 | def restart(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 135 | try: |
| 136 | # Enter the command to restart your service(s) |
| Philip Joseph | 24a06f0 | 2017-04-05 21:31:12 +0530 | [diff] [blame] | 137 | cmd = "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg['mode']) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 138 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 139 | except Exception as e: |
| 140 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 141 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 142 | action_set({'stdout': result, |
| 143 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 144 | finally: |
| 145 | remove_flag('actions.restart') |
| 146 | |
| 147 | |
| 148 | @when('pingpong.configured') |
| 149 | @when('actions.set-server') |
| 150 | def set_server(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 151 | try: |
| 152 | # Get the target service info |
| 153 | target_ip = action_get('server-ip') |
| 154 | target_port = action_get('server-port') |
| 155 | |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 156 | data = '{{"ip" : "{}", "port" : {} }}'. \ |
| 157 | format(target_ip, target_port) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 158 | |
| 159 | cmd = format_curl( |
| 160 | 'POST', |
| 161 | '/server', |
| 162 | data, |
| 163 | ) |
| 164 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 165 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 166 | except Exception as e: |
| 167 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 168 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 169 | action_set({'stdout': result, |
| 170 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 171 | finally: |
| 172 | remove_flag('actions.set-server') |
| 173 | |
| 174 | |
| 175 | @when('pingpong.configured') |
| 176 | @when('actions.set-rate') |
| 177 | def set_rate(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 178 | try: |
| 179 | if is_ping(): |
| 180 | rate = action_get('rate') |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 181 | cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 182 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 183 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 184 | except Exception as e: |
| 185 | err = "{}".format(e) |
| 186 | action_fail('command failed: {}, errors: {}'.format(err, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 187 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 188 | action_set({'stdout': result, |
| 189 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 190 | finally: |
| 191 | remove_flag('actions.set-rate') |
| 192 | |
| 193 | |
| 194 | @when('pingpong.configured') |
| 195 | @when('actions.get-rate') |
| 196 | def get_rate(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 197 | try: |
| 198 | if is_ping(): |
| 199 | cmd = format_curl('GET', '/rate') |
| 200 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 201 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 202 | except Exception as e: |
| 203 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 204 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 205 | action_set({'stdout': result, |
| 206 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 207 | finally: |
| 208 | remove_flag('actions.get-rate') |
| 209 | |
| 210 | |
| 211 | @when('pingpong.configured') |
| 212 | @when('actions.get-state') |
| 213 | def get_state(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 214 | try: |
| 215 | cmd = format_curl('GET', '/state') |
| 216 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 217 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 218 | except Exception as e: |
| 219 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 220 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 221 | action_set({'stdout': result, |
| 222 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 223 | finally: |
| 224 | remove_flag('actions.get-state') |
| 225 | |
| 226 | |
| 227 | @when('pingpong.configured') |
| 228 | @when('actions.get-stats') |
| 229 | def get_stats(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 230 | try: |
| 231 | cmd = format_curl('GET', '/stats') |
| 232 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 233 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 234 | except Exception as e: |
| 235 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 236 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 237 | action_set({'stdout': result, |
| 238 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 239 | finally: |
| 240 | remove_flag('actions.get-stats') |
| 241 | |
| 242 | |
| 243 | @when('pingpong.configured') |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 244 | @when('actions.start-traffic') |
| 245 | def start_traffic(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 246 | try: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 247 | cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 248 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 249 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 250 | except Exception as e: |
| 251 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 252 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 253 | action_set({'stdout': result, |
| 254 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 255 | finally: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 256 | remove_flag('actions.start-traffic') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 257 | |
| 258 | |
| 259 | @when('pingpong.configured') |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 260 | @when('actions.stop-traffic') |
| 261 | def stop_traffic(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 262 | try: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 263 | cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 264 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 265 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 266 | except Exception as e: |
| 267 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 268 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 269 | action_set({'stdout': result, |
| 270 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 271 | finally: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 272 | remove_flag('actions.stop-traffic') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 273 | |
| 274 | |
| 275 | def format_curl(method, path, data=None): |
| 276 | """ A utility function to build the curl command line. """ |
| 277 | |
| 278 | # method must be GET or POST |
| 279 | if method not in ['GET', 'POST']: |
| 280 | # Throw exception |
| 281 | return None |
| 282 | |
| 283 | # Get our service info |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 284 | host = '127.0.0.1' |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 285 | port = get_port() |
| 286 | mode = cfg['mode'] |
| 287 | |
| 288 | cmd = ['curl', |
| 289 | # '-D', '/dev/stdout', |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 290 | '-H', 'Accept: application/vnd.yang.data+xml', |
| 291 | '-H', 'Content-Type: application/vnd.yang.data+json', |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 292 | '-X', method] |
| 293 | |
| 294 | if method == "POST" and data: |
| 295 | cmd.append('-d') |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 296 | cmd.append('{}'.format(data)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 297 | |
| 298 | cmd.append( |
| 299 | 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path) |
| 300 | ) |
| 301 | return cmd |