| 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 |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 15 | from subprocess import ( |
| 16 | Popen, |
| 17 | CalledProcessError, |
| 18 | PIPE, |
| 19 | ) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 20 | |
| 21 | |
| 22 | cfg = config() |
| 23 | |
| 24 | |
| 25 | @when('config.changed') |
| 26 | def config_changed(): |
| 27 | if all(k in cfg for k in ['mode']): |
| 28 | if cfg['mode'] in ['ping', 'pong']: |
| 29 | set_flag('pingpong.configured') |
| 30 | status_set('active', 'ready!') |
| 31 | return |
| 32 | status_set('blocked', 'Waiting for configuration') |
| 33 | |
| 34 | |
| 35 | def is_ping(): |
| 36 | if cfg['mode'] == 'ping': |
| 37 | return True |
| 38 | return False |
| 39 | |
| 40 | |
| 41 | def is_pong(): |
| 42 | return not is_ping() |
| 43 | |
| 44 | |
| 45 | def get_port(): |
| 46 | port = 18888 |
| 47 | if is_pong(): |
| 48 | port = 18889 |
| 49 | return port |
| 50 | |
| Philip Joseph | 0efe584 | 2017-01-14 00:07:30 +0530 | [diff] [blame] | 51 | |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 52 | @when('pingpong.configured') |
| 53 | @when('actions.start') |
| 54 | def start(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 55 | try: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 56 | # Bring up the eth1 interface. |
| 57 | # The selinux label on the file needs to be set correctly |
| 58 | cmd = "sudo /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1" |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 59 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 60 | except Exception as e: |
| 61 | err = "{}".format(e) |
| 62 | action_fail('command failed: {}, errors: {}'.format(err, e.output)) |
| 63 | remove_flag('actions.start') |
| 64 | return |
| 65 | |
| 66 | try: |
| 67 | cmd = "sudo /sbin/ifup eth1" |
| 68 | result, err = charms.sshproxy._run(cmd) |
| 69 | except Exception as e: |
| 70 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| 71 | remove_flag('actions.start') |
| 72 | return |
| 73 | |
| 74 | try: |
| 75 | cmd = "sudo /usr/bin/systemctl start {}". \ |
| 76 | format(cfg['mode']) |
| 77 | result, err = charms.sshproxy._run(cmd) |
| 78 | except Exception as e: |
| 79 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 80 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 81 | action_set({'stdout': result, |
| 82 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 83 | finally: |
| 84 | remove_flag('actions.start') |
| 85 | |
| 86 | |
| 87 | @when('pingpong.configured') |
| 88 | @when('actions.stop') |
| 89 | def stop(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 90 | try: |
| 91 | # Enter the command to stop your service(s) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 92 | cmd = "sudo /usr/bin/systemctl stop {}".format(cfg['mode']) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 93 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 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.stop') |
| 101 | |
| 102 | |
| 103 | @when('pingpong.configured') |
| 104 | @when('actions.restart') |
| 105 | def restart(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 106 | try: |
| 107 | # Enter the command to restart your service(s) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 108 | cmd = "sudo /usr/bin/systemctl restart {}".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.restart') |
| 117 | |
| 118 | |
| 119 | @when('pingpong.configured') |
| 120 | @when('actions.set-server') |
| 121 | def set_server(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 122 | try: |
| 123 | # Get the target service info |
| 124 | target_ip = action_get('server-ip') |
| 125 | target_port = action_get('server-port') |
| 126 | |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 127 | data = '{{"ip" : "{}", "port" : {} }}'. \ |
| 128 | format(target_ip, target_port) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 129 | |
| 130 | cmd = format_curl( |
| 131 | 'POST', |
| 132 | '/server', |
| 133 | data, |
| 134 | ) |
| 135 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 136 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 137 | except Exception as e: |
| 138 | action_fail('command failed: {}, errors: {}'.format(e, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 139 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 140 | action_set({'stdout': result, |
| 141 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 142 | finally: |
| 143 | remove_flag('actions.set-server') |
| 144 | |
| 145 | |
| 146 | @when('pingpong.configured') |
| 147 | @when('actions.set-rate') |
| 148 | def set_rate(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 149 | try: |
| 150 | if is_ping(): |
| 151 | rate = action_get('rate') |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 152 | cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 153 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 154 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 155 | except Exception as e: |
| 156 | err = "{}".format(e) |
| 157 | action_fail('command failed: {}, errors: {}'.format(err, e.output)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 158 | else: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 159 | action_set({'stdout': result, |
| 160 | 'errors': err}) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 161 | finally: |
| 162 | remove_flag('actions.set-rate') |
| 163 | |
| 164 | |
| 165 | @when('pingpong.configured') |
| 166 | @when('actions.get-rate') |
| 167 | def get_rate(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 168 | try: |
| 169 | if is_ping(): |
| 170 | cmd = format_curl('GET', '/rate') |
| 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.get-rate') |
| 180 | |
| 181 | |
| 182 | @when('pingpong.configured') |
| 183 | @when('actions.get-state') |
| 184 | def get_state(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 185 | try: |
| 186 | cmd = format_curl('GET', '/state') |
| 187 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 188 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 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-state') |
| 196 | |
| 197 | |
| 198 | @when('pingpong.configured') |
| 199 | @when('actions.get-stats') |
| 200 | def get_stats(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 201 | try: |
| 202 | cmd = format_curl('GET', '/stats') |
| 203 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 204 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 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-stats') |
| 212 | |
| 213 | |
| 214 | @when('pingpong.configured') |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 215 | @when('actions.start-traffic') |
| 216 | def start_traffic(): |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 217 | try: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 218 | cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 219 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 220 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 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: |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 227 | remove_flag('actions.start-traffic') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 228 | |
| 229 | |
| 230 | @when('pingpong.configured') |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 231 | @when('actions.stop-traffic') |
| 232 | def stop_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" : false}') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 235 | |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 236 | result, err = charms.sshproxy._run(cmd) |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 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.stop-traffic') |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 244 | |
| 245 | |
| 246 | def format_curl(method, path, data=None): |
| 247 | """ A utility function to build the curl command line. """ |
| 248 | |
| 249 | # method must be GET or POST |
| 250 | if method not in ['GET', 'POST']: |
| 251 | # Throw exception |
| 252 | return None |
| 253 | |
| 254 | # Get our service info |
| Philip Joseph | efe9e06 | 2017-01-20 02:09:41 +0530 | [diff] [blame] | 255 | host = '127.0.0.1' |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 256 | port = get_port() |
| 257 | mode = cfg['mode'] |
| 258 | |
| 259 | cmd = ['curl', |
| 260 | # '-D', '/dev/stdout', |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 261 | '-H', 'Accept: application/vnd.yang.data+xml', |
| 262 | '-H', 'Content-Type: application/vnd.yang.data+json', |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 263 | '-X', method] |
| 264 | |
| 265 | if method == "POST" and data: |
| 266 | cmd.append('-d') |
| Philip Joseph | 71d56bb | 2017-01-05 18:54:15 +0530 | [diff] [blame] | 267 | cmd.append('{}'.format(data)) |
| Adam Israel | 32e2fa5 | 2016-12-14 22:50:51 -0500 | [diff] [blame] | 268 | |
| 269 | cmd.append( |
| 270 | 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path) |
| 271 | ) |
| 272 | return cmd |