blob: 7eedc84a1a6d652cd5e199448e2da2be1f86d0ec [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 Israelc1ec5712017-10-19 19:26:00 -040026@when_not('pingpong.configured')
27def not_configured():
28 """Check the current configuration.
Adam Israel1423eb62017-11-06 17:19:02 -050029
Adam Israelc1ec5712017-10-19 19:26:00 -040030 Check the current values in config to see if we have enough
Adam Israel1423eb62017-11-06 17:19:02 -050031 information to continue.
32 """
Adam Israelc1ec5712017-10-19 19:26:00 -040033 config_changed()
34
35
Adam Israel5a0f6e42017-10-18 10:39:40 -040036@when('config.changed', 'sshproxy.configured')
Adam Israel32e2fa52016-12-14 22:50:51 -050037def config_changed():
Adam Israel5a0f6e42017-10-18 10:39:40 -040038 """Verify the configuration.
Adam Israel1423eb62017-11-06 17:19:02 -050039
Adam Israel5a0f6e42017-10-18 10:39:40 -040040 Verify that the charm has been configured
41 """
Adam Israel15474012017-11-10 18:17:05 -050042
43 try:
44 status_set('maintenance', 'Verifying configuration data...')
45
46 (validated, output) = charms.sshproxy.verify_ssh_credentials()
47 if not validated:
48 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
49 output
50 ))
Adam Israel32e2fa52016-12-14 22:50:51 -050051 return
Adam Israel15474012017-11-10 18:17:05 -050052
53 if all(k in cfg for k in ['mode']):
54 if cfg['mode'] in ['ping', 'pong']:
55 set_flag('pingpong.configured')
56 status_set('active', 'ready!')
57 return
58 status_set('blocked', 'Waiting for configuration')
59
60 except Exception as err:
61 status_set('blocked', 'Waiting for valid configuration ({})'.format(err))
Adam Israel32e2fa52016-12-14 22:50:51 -050062
63
Adam Israel5a0f6e42017-10-18 10:39:40 -040064@when('config.changed')
65@when_not('sshproxy.configured')
66def invalid_credentials():
67 status_set('blocked', 'Waiting for SSH credentials.')
68 pass
69
70
Adam Israel32e2fa52016-12-14 22:50:51 -050071def is_ping():
72 if cfg['mode'] == 'ping':
73 return True
74 return False
75
76
77def is_pong():
78 return not is_ping()
79
80
81def get_port():
82 port = 18888
83 if is_pong():
84 port = 18889
85 return port
86
Philip Joseph0efe5842017-01-14 00:07:30 +053087
Adam Israel32e2fa52016-12-14 22:50:51 -050088@when('pingpong.configured')
89@when('actions.start')
90def start():
Adam Israel32e2fa52016-12-14 22:50:51 -050091 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +053092 # Bring up the eth1 interface.
93 # The selinux label on the file needs to be set correctly
Philip Joseph24a06f02017-04-05 21:31:12 +053094 cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
Adam Israel32e2fa52016-12-14 22:50:51 -050095 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +053096 except Exception as e:
97 err = "{}".format(e)
98 action_fail('command failed: {}, errors: {}'.format(err, e.output))
99 remove_flag('actions.start')
100 return
101
Adam Israel1f9ce572017-10-16 14:46:29 -0400102 # Attempt to raise the non-mgmt interface, but ignore failures if
103 # the interface is already up.
Philip Joseph71d56bb2017-01-05 18:54:15 +0530104 try:
Adam Israel1f9ce572017-10-16 14:46:29 -0400105 cmd = "sudo timeout 30 /sbin/ifup eth1"
Philip Joseph71d56bb2017-01-05 18:54:15 +0530106 result, err = charms.sshproxy._run(cmd)
107 except Exception as e:
Adam Israel1f9ce572017-10-16 14:46:29 -0400108 pass
Philip Joseph71d56bb2017-01-05 18:54:15 +0530109
110 try:
Adam Israel1f9ce572017-10-16 14:46:29 -0400111 cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \
Philip Joseph71d56bb2017-01-05 18:54:15 +0530112 format(cfg['mode'])
113 result, err = charms.sshproxy._run(cmd)
114 except Exception as e:
115 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500116 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530117 action_set({'stdout': result,
118 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500119 finally:
120 remove_flag('actions.start')
121
122
123@when('pingpong.configured')
124@when('actions.stop')
125def stop():
Adam Israel32e2fa52016-12-14 22:50:51 -0500126 try:
127 # Enter the command to stop your service(s)
Philip Joseph24a06f02017-04-05 21:31:12 +0530128 cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -0500129 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530130 except Exception as e:
131 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500132 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530133 action_set({'stdout': result,
134 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500135 finally:
136 remove_flag('actions.stop')
137
138
139@when('pingpong.configured')
140@when('actions.restart')
141def restart():
Adam Israel32e2fa52016-12-14 22:50:51 -0500142 try:
143 # Enter the command to restart your service(s)
Philip Joseph24a06f02017-04-05 21:31:12 +0530144 cmd = "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -0500145 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530146 except Exception as e:
147 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500148 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530149 action_set({'stdout': result,
150 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500151 finally:
152 remove_flag('actions.restart')
153
154
155@when('pingpong.configured')
156@when('actions.set-server')
157def set_server():
Adam Israel32e2fa52016-12-14 22:50:51 -0500158 try:
159 # Get the target service info
160 target_ip = action_get('server-ip')
161 target_port = action_get('server-port')
162
Philip Joseph71d56bb2017-01-05 18:54:15 +0530163 data = '{{"ip" : "{}", "port" : {} }}'. \
164 format(target_ip, target_port)
Adam Israel32e2fa52016-12-14 22:50:51 -0500165
166 cmd = format_curl(
167 'POST',
168 '/server',
169 data,
170 )
171
Philip Josephefe9e062017-01-20 02:09:41 +0530172 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530173 except Exception as e:
174 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500175 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530176 action_set({'stdout': result,
177 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500178 finally:
179 remove_flag('actions.set-server')
180
181
182@when('pingpong.configured')
183@when('actions.set-rate')
184def set_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500185 try:
186 if is_ping():
187 rate = action_get('rate')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530188 cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate))
Adam Israel32e2fa52016-12-14 22:50:51 -0500189
Philip Josephefe9e062017-01-20 02:09:41 +0530190 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530191 except Exception as e:
192 err = "{}".format(e)
193 action_fail('command failed: {}, errors: {}'.format(err, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500194 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530195 action_set({'stdout': result,
196 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500197 finally:
198 remove_flag('actions.set-rate')
199
200
201@when('pingpong.configured')
202@when('actions.get-rate')
203def get_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500204 try:
205 if is_ping():
206 cmd = format_curl('GET', '/rate')
207
Philip Josephefe9e062017-01-20 02:09:41 +0530208 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530209 except Exception as e:
210 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500211 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530212 action_set({'stdout': result,
213 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500214 finally:
215 remove_flag('actions.get-rate')
216
217
218@when('pingpong.configured')
219@when('actions.get-state')
220def get_state():
Adam Israel32e2fa52016-12-14 22:50:51 -0500221 try:
222 cmd = format_curl('GET', '/state')
223
Philip Josephefe9e062017-01-20 02:09:41 +0530224 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530225 except Exception as e:
226 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500227 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530228 action_set({'stdout': result,
229 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500230 finally:
231 remove_flag('actions.get-state')
232
233
234@when('pingpong.configured')
235@when('actions.get-stats')
236def get_stats():
Adam Israel32e2fa52016-12-14 22:50:51 -0500237 try:
238 cmd = format_curl('GET', '/stats')
239
Philip Josephefe9e062017-01-20 02:09:41 +0530240 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530241 except Exception as e:
242 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500243 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530244 action_set({'stdout': result,
245 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500246 finally:
247 remove_flag('actions.get-stats')
248
249
250@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530251@when('actions.start-traffic')
252def start_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500253 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530254 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500255
Philip Josephefe9e062017-01-20 02:09:41 +0530256 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530257 except Exception as e:
258 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500259 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530260 action_set({'stdout': result,
261 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500262 finally:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530263 remove_flag('actions.start-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500264
265
266@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530267@when('actions.stop-traffic')
268def stop_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500269 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530270 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500271
Philip Josephefe9e062017-01-20 02:09:41 +0530272 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530273 except Exception as e:
274 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500275 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530276 action_set({'stdout': result,
277 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500278 finally:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530279 remove_flag('actions.stop-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500280
281
282def format_curl(method, path, data=None):
283 """ A utility function to build the curl command line. """
284
285 # method must be GET or POST
286 if method not in ['GET', 'POST']:
287 # Throw exception
288 return None
289
290 # Get our service info
Philip Josephefe9e062017-01-20 02:09:41 +0530291 host = '127.0.0.1'
Adam Israel32e2fa52016-12-14 22:50:51 -0500292 port = get_port()
293 mode = cfg['mode']
294
295 cmd = ['curl',
296 # '-D', '/dev/stdout',
Philip Joseph71d56bb2017-01-05 18:54:15 +0530297 '-H', 'Accept: application/vnd.yang.data+xml',
298 '-H', 'Content-Type: application/vnd.yang.data+json',
Adam Israel32e2fa52016-12-14 22:50:51 -0500299 '-X', method]
300
301 if method == "POST" and data:
302 cmd.append('-d')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530303 cmd.append('{}'.format(data))
Adam Israel32e2fa52016-12-14 22:50:51 -0500304
305 cmd.append(
306 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path)
307 )
308 return cmd