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