blob: 2bcbc143254dfc57faf21912a58746d770e2af0f [file] [log] [blame]
Adam Israel32e2fa52016-12-14 22:50:51 -05001from charmhelpers.core.hookenv import (
2 action_get,
3 action_fail,
4 action_set,
5 config,
6 status_set,
7)
8
9from charms.reactive import (
10 remove_state as remove_flag,
11 set_state as set_flag,
12 when,
Adam Israel5a0f6e42017-10-18 10:39:40 -040013 when_not,
Adam Israel32e2fa52016-12-14 22:50:51 -050014)
15import charms.sshproxy
Adam Israel5a0f6e42017-10-18 10:39:40 -040016# from subprocess import (
17# Popen,
18# CalledProcessError,
19# PIPE,
20# )
Adam Israel32e2fa52016-12-14 22:50:51 -050021
22
23cfg = config()
24
25
Adam Israel5a0f6e42017-10-18 10:39:40 -040026@when('config.changed', 'sshproxy.configured')
Adam Israel32e2fa52016-12-14 22:50:51 -050027def config_changed():
Adam Israel5a0f6e42017-10-18 10:39:40 -040028 """Verify the configuration.
29 Verify that the charm has been configured
30 """
31 (validated, output) = charms.sshproxy.verify_ssh_credentials()
32 if not validated:
33 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
34 output
35 ))
Adam Israel32e2fa52016-12-14 22:50:51 -050036 if all(k in cfg for k in ['mode']):
37 if cfg['mode'] in ['ping', 'pong']:
38 set_flag('pingpong.configured')
39 status_set('active', 'ready!')
40 return
41 status_set('blocked', 'Waiting for configuration')
42
43
Adam Israel5a0f6e42017-10-18 10:39:40 -040044@when('config.changed')
45@when_not('sshproxy.configured')
46def invalid_credentials():
47 status_set('blocked', 'Waiting for SSH credentials.')
48 pass
49
50
Adam Israel32e2fa52016-12-14 22:50:51 -050051def is_ping():
52 if cfg['mode'] == 'ping':
53 return True
54 return False
55
56
57def is_pong():
58 return not is_ping()
59
60
61def get_port():
62 port = 18888
63 if is_pong():
64 port = 18889
65 return port
66
Philip Joseph0efe5842017-01-14 00:07:30 +053067
Adam Israel32e2fa52016-12-14 22:50:51 -050068@when('pingpong.configured')
69@when('actions.start')
70def start():
Adam Israel32e2fa52016-12-14 22:50:51 -050071 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +053072 # Bring up the eth1 interface.
73 # The selinux label on the file needs to be set correctly
Philip Joseph24a06f02017-04-05 21:31:12 +053074 cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
Adam Israel32e2fa52016-12-14 22:50:51 -050075 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +053076 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
Adam Israel1f9ce572017-10-16 14:46:29 -040082 # Attempt to raise the non-mgmt interface, but ignore failures if
83 # the interface is already up.
Philip Joseph71d56bb2017-01-05 18:54:15 +053084 try:
Adam Israel1f9ce572017-10-16 14:46:29 -040085 cmd = "sudo timeout 30 /sbin/ifup eth1"
Philip Joseph71d56bb2017-01-05 18:54:15 +053086 result, err = charms.sshproxy._run(cmd)
87 except Exception as e:
Adam Israel1f9ce572017-10-16 14:46:29 -040088 pass
Philip Joseph71d56bb2017-01-05 18:54:15 +053089
90 try:
Adam Israel1f9ce572017-10-16 14:46:29 -040091 cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \
Philip Joseph71d56bb2017-01-05 18:54:15 +053092 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 Israel32e2fa52016-12-14 22:50:51 -050096 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +053097 action_set({'stdout': result,
98 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -050099 finally:
100 remove_flag('actions.start')
101
102
103@when('pingpong.configured')
104@when('actions.stop')
105def stop():
Adam Israel32e2fa52016-12-14 22:50:51 -0500106 try:
107 # Enter the command to stop your service(s)
Philip Joseph24a06f02017-04-05 21:31:12 +0530108 cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -0500109 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530110 except Exception as e:
111 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500112 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530113 action_set({'stdout': result,
114 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500115 finally:
116 remove_flag('actions.stop')
117
118
119@when('pingpong.configured')
120@when('actions.restart')
121def restart():
Adam Israel32e2fa52016-12-14 22:50:51 -0500122 try:
123 # Enter the command to restart your service(s)
Philip Joseph24a06f02017-04-05 21:31:12 +0530124 cmd = "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -0500125 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530126 except Exception as e:
127 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500128 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530129 action_set({'stdout': result,
130 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500131 finally:
132 remove_flag('actions.restart')
133
134
135@when('pingpong.configured')
136@when('actions.set-server')
137def set_server():
Adam Israel32e2fa52016-12-14 22:50:51 -0500138 try:
139 # Get the target service info
140 target_ip = action_get('server-ip')
141 target_port = action_get('server-port')
142
Philip Joseph71d56bb2017-01-05 18:54:15 +0530143 data = '{{"ip" : "{}", "port" : {} }}'. \
144 format(target_ip, target_port)
Adam Israel32e2fa52016-12-14 22:50:51 -0500145
146 cmd = format_curl(
147 'POST',
148 '/server',
149 data,
150 )
151
Philip Josephefe9e062017-01-20 02:09:41 +0530152 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530153 except Exception as e:
154 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500155 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530156 action_set({'stdout': result,
157 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500158 finally:
159 remove_flag('actions.set-server')
160
161
162@when('pingpong.configured')
163@when('actions.set-rate')
164def set_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500165 try:
166 if is_ping():
167 rate = action_get('rate')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530168 cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate))
Adam Israel32e2fa52016-12-14 22:50:51 -0500169
Philip Josephefe9e062017-01-20 02:09:41 +0530170 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530171 except Exception as e:
172 err = "{}".format(e)
173 action_fail('command failed: {}, errors: {}'.format(err, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500174 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530175 action_set({'stdout': result,
176 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500177 finally:
178 remove_flag('actions.set-rate')
179
180
181@when('pingpong.configured')
182@when('actions.get-rate')
183def get_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500184 try:
185 if is_ping():
186 cmd = format_curl('GET', '/rate')
187
Philip Josephefe9e062017-01-20 02:09:41 +0530188 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530189 except Exception as e:
190 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500191 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530192 action_set({'stdout': result,
193 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500194 finally:
195 remove_flag('actions.get-rate')
196
197
198@when('pingpong.configured')
199@when('actions.get-state')
200def get_state():
Adam Israel32e2fa52016-12-14 22:50:51 -0500201 try:
202 cmd = format_curl('GET', '/state')
203
Philip Josephefe9e062017-01-20 02:09:41 +0530204 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530205 except Exception as e:
206 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500207 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530208 action_set({'stdout': result,
209 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500210 finally:
211 remove_flag('actions.get-state')
212
213
214@when('pingpong.configured')
215@when('actions.get-stats')
216def get_stats():
Adam Israel32e2fa52016-12-14 22:50:51 -0500217 try:
218 cmd = format_curl('GET', '/stats')
219
Philip Josephefe9e062017-01-20 02:09:41 +0530220 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530221 except Exception as e:
222 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500223 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530224 action_set({'stdout': result,
225 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500226 finally:
227 remove_flag('actions.get-stats')
228
229
230@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530231@when('actions.start-traffic')
232def start_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500233 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530234 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500235
Philip Josephefe9e062017-01-20 02:09:41 +0530236 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530237 except Exception as e:
238 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500239 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530240 action_set({'stdout': result,
241 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500242 finally:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530243 remove_flag('actions.start-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500244
245
246@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530247@when('actions.stop-traffic')
248def stop_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500249 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530250 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500251
Philip Josephefe9e062017-01-20 02:09:41 +0530252 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530253 except Exception as e:
254 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500255 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530256 action_set({'stdout': result,
257 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500258 finally:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530259 remove_flag('actions.stop-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500260
261
262def 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
Philip Josephefe9e062017-01-20 02:09:41 +0530271 host = '127.0.0.1'
Adam Israel32e2fa52016-12-14 22:50:51 -0500272 port = get_port()
273 mode = cfg['mode']
274
275 cmd = ['curl',
276 # '-D', '/dev/stdout',
Philip Joseph71d56bb2017-01-05 18:54:15 +0530277 '-H', 'Accept: application/vnd.yang.data+xml',
278 '-H', 'Content-Type: application/vnd.yang.data+json',
Adam Israel32e2fa52016-12-14 22:50:51 -0500279 '-X', method]
280
281 if method == "POST" and data:
282 cmd.append('-d')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530283 cmd.append('{}'.format(data))
Adam Israel32e2fa52016-12-14 22:50:51 -0500284
285 cmd.append(
286 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path)
287 )
288 return cmd