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