| 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 |
| 16 | |
| 17 | |
| 18 | cfg = config() |
| 19 | |
| 20 | |
| 21 | @when('config.changed') |
| 22 | def config_changed(): |
| 23 | if all(k in cfg for k in ['mode']): |
| 24 | if cfg['mode'] in ['ping', 'pong']: |
| 25 | set_flag('pingpong.configured') |
| 26 | status_set('active', 'ready!') |
| 27 | return |
| 28 | status_set('blocked', 'Waiting for configuration') |
| 29 | |
| 30 | |
| 31 | def is_ping(): |
| 32 | if cfg['mode'] == 'ping': |
| 33 | return True |
| 34 | return False |
| 35 | |
| 36 | |
| 37 | def is_pong(): |
| 38 | return not is_ping() |
| 39 | |
| 40 | |
| 41 | def get_port(): |
| 42 | port = 18888 |
| 43 | if is_pong(): |
| 44 | port = 18889 |
| 45 | return port |
| 46 | |
| 47 | |
| 48 | @when('pingpong.configured') |
| 49 | @when('actions.start') |
| 50 | def start(): |
| 51 | err = '' |
| 52 | try: |
| 53 | cmd = "service {} start".format(cfg['mode']) |
| 54 | result, err = charms.sshproxy._run(cmd) |
| 55 | except: |
| 56 | action_fail('command failed:' + err) |
| 57 | else: |
| 58 | action_set({'outout': result}) |
| 59 | finally: |
| 60 | remove_flag('actions.start') |
| 61 | |
| 62 | |
| 63 | @when('pingpong.configured') |
| 64 | @when('actions.stop') |
| 65 | def stop(): |
| 66 | err = '' |
| 67 | try: |
| 68 | # Enter the command to stop your service(s) |
| 69 | cmd = "service {} stop".format(cfg['mode']) |
| 70 | result, err = charms.sshproxy._run(cmd) |
| 71 | except: |
| 72 | action_fail('command failed:' + err) |
| 73 | else: |
| 74 | action_set({'outout': result}) |
| 75 | finally: |
| 76 | remove_flag('actions.stop') |
| 77 | |
| 78 | |
| 79 | @when('pingpong.configured') |
| 80 | @when('actions.restart') |
| 81 | def restart(): |
| 82 | err = '' |
| 83 | try: |
| 84 | # Enter the command to restart your service(s) |
| 85 | cmd = "service {} restart".format(cfg['mode']) |
| 86 | result, err = charms.sshproxy._run(cmd) |
| 87 | except: |
| 88 | action_fail('command failed:' + err) |
| 89 | else: |
| 90 | action_set({'outout': result}) |
| 91 | finally: |
| 92 | remove_flag('actions.restart') |
| 93 | |
| 94 | |
| 95 | @when('pingpong.configured') |
| 96 | @when('actions.set-server') |
| 97 | def set_server(): |
| 98 | err = '' |
| 99 | try: |
| 100 | # Get the target service info |
| 101 | target_ip = action_get('server-ip') |
| 102 | target_port = action_get('server-port') |
| 103 | |
| 104 | data = json.dumps({'ip': target_ip, 'port': target_port}) |
| 105 | |
| 106 | cmd = format_curl( |
| 107 | 'POST', |
| 108 | '/server', |
| 109 | data, |
| 110 | ) |
| 111 | |
| 112 | result, err = charms.sshproxy._run(cmd) |
| 113 | except Exception as err: |
| 114 | print("error: {0}".format(err)) |
| 115 | action_fail('command failed:' + err) |
| 116 | else: |
| 117 | action_set({'outout': result}) |
| 118 | finally: |
| 119 | remove_flag('actions.set-server') |
| 120 | |
| 121 | |
| 122 | @when('pingpong.configured') |
| 123 | @when('actions.set-rate') |
| 124 | def set_rate(): |
| 125 | err = '' |
| 126 | try: |
| 127 | if is_ping(): |
| 128 | rate = action_get('rate') |
| 129 | cmd = format_curl('POST', '/rate', '{"rate": {}}'.format(rate)) |
| 130 | |
| 131 | result, err = charms.sshproxy._run(cmd) |
| 132 | except: |
| 133 | action_fail('command failed:' + err) |
| 134 | else: |
| 135 | action_set({'outout': result}) |
| 136 | finally: |
| 137 | remove_flag('actions.set-rate') |
| 138 | |
| 139 | |
| 140 | @when('pingpong.configured') |
| 141 | @when('actions.get-rate') |
| 142 | def get_rate(): |
| 143 | err = '' |
| 144 | try: |
| 145 | if is_ping(): |
| 146 | cmd = format_curl('GET', '/rate') |
| 147 | |
| 148 | result, err = charms.sshproxy._run(cmd) |
| 149 | except: |
| 150 | action_fail('command failed:' + err) |
| 151 | else: |
| 152 | action_set({'outout': result}) |
| 153 | finally: |
| 154 | remove_flag('actions.get-rate') |
| 155 | |
| 156 | |
| 157 | @when('pingpong.configured') |
| 158 | @when('actions.get-state') |
| 159 | def get_state(): |
| 160 | err = '' |
| 161 | try: |
| 162 | cmd = format_curl('GET', '/state') |
| 163 | |
| 164 | result, err = charms.sshproxy._run(cmd) |
| 165 | except: |
| 166 | action_fail('command failed:' + err) |
| 167 | else: |
| 168 | action_set({'outout': result}) |
| 169 | finally: |
| 170 | remove_flag('actions.get-state') |
| 171 | |
| 172 | |
| 173 | @when('pingpong.configured') |
| 174 | @when('actions.get-stats') |
| 175 | def get_stats(): |
| 176 | err = '' |
| 177 | try: |
| 178 | cmd = format_curl('GET', '/stats') |
| 179 | |
| 180 | result, err = charms.sshproxy._run(cmd) |
| 181 | except: |
| 182 | action_fail('command failed:' + err) |
| 183 | else: |
| 184 | action_set({'outout': result}) |
| 185 | finally: |
| 186 | remove_flag('actions.get-stats') |
| 187 | |
| 188 | |
| 189 | @when('pingpong.configured') |
| 190 | @when('actions.start-ping') |
| 191 | def start_ping(): |
| 192 | err = '' |
| 193 | try: |
| 194 | cmd = format_curl('POST', '/adminstatus/state', '{"enable":true}') |
| 195 | |
| 196 | result, err = charms.sshproxy._run(cmd) |
| 197 | except: |
| 198 | action_fail('command failed:' + err) |
| 199 | else: |
| 200 | action_set({'outout': result}) |
| 201 | finally: |
| 202 | remove_flag('actions.start-ping') |
| 203 | |
| 204 | |
| 205 | @when('pingpong.configured') |
| 206 | @when('actions.stop-ping') |
| 207 | def stop_ping(): |
| 208 | err = '' |
| 209 | try: |
| 210 | cmd = format_curl('POST', '/adminstatus/state', '{"enable":false}') |
| 211 | |
| 212 | result, err = charms.sshproxy._run(cmd) |
| 213 | except: |
| 214 | action_fail('command failed:' + err) |
| 215 | else: |
| 216 | action_set({'outout': result}) |
| 217 | finally: |
| 218 | remove_flag('actions.stop-ping') |
| 219 | |
| 220 | |
| 221 | def format_curl(method, path, data=None): |
| 222 | """ A utility function to build the curl command line. """ |
| 223 | |
| 224 | # method must be GET or POST |
| 225 | if method not in ['GET', 'POST']: |
| 226 | # Throw exception |
| 227 | return None |
| 228 | |
| 229 | # Get our service info |
| 230 | host = cfg['ssh-hostname'] |
| 231 | port = get_port() |
| 232 | mode = cfg['mode'] |
| 233 | |
| 234 | cmd = ['curl', |
| 235 | # '-D', '/dev/stdout', |
| 236 | '-H', '"Accept: application/vnd.yang.data+xml"', |
| 237 | '-H', '"Content-Type: application/vnd.yang.data+json"', |
| 238 | '-X', method] |
| 239 | |
| 240 | if method == "POST" and data: |
| 241 | cmd.append('-d') |
| 242 | cmd.append("'{}'".format(data)) |
| 243 | |
| 244 | cmd.append( |
| 245 | 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path) |
| 246 | ) |
| 247 | return cmd |