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