blob: ae18076cb5915148ff0a70a5951289bc6ba957d1 [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 Israelc1ec5712017-10-19 19:26:00 -040042 status_set('maintenance', 'Verifying configuration data...')
Adam Israel5a0f6e42017-10-18 10:39:40 -040043 (validated, output) = charms.sshproxy.verify_ssh_credentials()
44 if not validated:
45 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
46 output
47 ))
Adam Israel1423eb62017-11-06 17:19:02 -050048 return
Adam Israel32e2fa52016-12-14 22:50:51 -050049 if all(k in cfg for k in ['mode']):
50 if cfg['mode'] in ['ping', 'pong']:
51 set_flag('pingpong.configured')
52 status_set('active', 'ready!')
53 return
54 status_set('blocked', 'Waiting for configuration')
55
56
Adam Israel5a0f6e42017-10-18 10:39:40 -040057@when('config.changed')
58@when_not('sshproxy.configured')
59def invalid_credentials():
60 status_set('blocked', 'Waiting for SSH credentials.')
61 pass
62
63
Adam Israel32e2fa52016-12-14 22:50:51 -050064def is_ping():
65 if cfg['mode'] == 'ping':
66 return True
67 return False
68
69
70def is_pong():
71 return not is_ping()
72
73
74def get_port():
75 port = 18888
76 if is_pong():
77 port = 18889
78 return port
79
Philip Joseph0efe5842017-01-14 00:07:30 +053080
Adam Israel32e2fa52016-12-14 22:50:51 -050081@when('pingpong.configured')
82@when('actions.start')
83def start():
Adam Israel32e2fa52016-12-14 22:50:51 -050084 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +053085 # Bring up the eth1 interface.
86 # The selinux label on the file needs to be set correctly
Philip Joseph24a06f02017-04-05 21:31:12 +053087 cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
Adam Israel32e2fa52016-12-14 22:50:51 -050088 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +053089 except Exception as e:
90 err = "{}".format(e)
91 action_fail('command failed: {}, errors: {}'.format(err, e.output))
92 remove_flag('actions.start')
93 return
94
Adam Israel1f9ce572017-10-16 14:46:29 -040095 # Attempt to raise the non-mgmt interface, but ignore failures if
96 # the interface is already up.
Philip Joseph71d56bb2017-01-05 18:54:15 +053097 try:
Adam Israel1f9ce572017-10-16 14:46:29 -040098 cmd = "sudo timeout 30 /sbin/ifup eth1"
Philip Joseph71d56bb2017-01-05 18:54:15 +053099 result, err = charms.sshproxy._run(cmd)
100 except Exception as e:
Adam Israel1f9ce572017-10-16 14:46:29 -0400101 pass
Philip Joseph71d56bb2017-01-05 18:54:15 +0530102
103 try:
Adam Israel1f9ce572017-10-16 14:46:29 -0400104 cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \
Philip Joseph71d56bb2017-01-05 18:54:15 +0530105 format(cfg['mode'])
106 result, err = charms.sshproxy._run(cmd)
107 except Exception as e:
108 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500109 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530110 action_set({'stdout': result,
111 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500112 finally:
113 remove_flag('actions.start')
114
115
116@when('pingpong.configured')
117@when('actions.stop')
118def stop():
Adam Israel32e2fa52016-12-14 22:50:51 -0500119 try:
120 # Enter the command to stop your service(s)
Philip Joseph24a06f02017-04-05 21:31:12 +0530121 cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -0500122 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530123 except Exception as e:
124 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500125 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530126 action_set({'stdout': result,
127 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500128 finally:
129 remove_flag('actions.stop')
130
131
132@when('pingpong.configured')
133@when('actions.restart')
134def restart():
Adam Israel32e2fa52016-12-14 22:50:51 -0500135 try:
136 # Enter the command to restart your service(s)
Philip Joseph24a06f02017-04-05 21:31:12 +0530137 cmd = "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -0500138 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530139 except Exception as e:
140 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500141 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530142 action_set({'stdout': result,
143 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500144 finally:
145 remove_flag('actions.restart')
146
147
148@when('pingpong.configured')
149@when('actions.set-server')
150def set_server():
Adam Israel32e2fa52016-12-14 22:50:51 -0500151 try:
152 # Get the target service info
153 target_ip = action_get('server-ip')
154 target_port = action_get('server-port')
155
Philip Joseph71d56bb2017-01-05 18:54:15 +0530156 data = '{{"ip" : "{}", "port" : {} }}'. \
157 format(target_ip, target_port)
Adam Israel32e2fa52016-12-14 22:50:51 -0500158
159 cmd = format_curl(
160 'POST',
161 '/server',
162 data,
163 )
164
Philip Josephefe9e062017-01-20 02:09:41 +0530165 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530166 except Exception as e:
167 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500168 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530169 action_set({'stdout': result,
170 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500171 finally:
172 remove_flag('actions.set-server')
173
174
175@when('pingpong.configured')
176@when('actions.set-rate')
177def set_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500178 try:
179 if is_ping():
180 rate = action_get('rate')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530181 cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate))
Adam Israel32e2fa52016-12-14 22:50:51 -0500182
Philip Josephefe9e062017-01-20 02:09:41 +0530183 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530184 except Exception as e:
185 err = "{}".format(e)
186 action_fail('command failed: {}, errors: {}'.format(err, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500187 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530188 action_set({'stdout': result,
189 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500190 finally:
191 remove_flag('actions.set-rate')
192
193
194@when('pingpong.configured')
195@when('actions.get-rate')
196def get_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500197 try:
198 if is_ping():
199 cmd = format_curl('GET', '/rate')
200
Philip Josephefe9e062017-01-20 02:09:41 +0530201 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530202 except Exception as e:
203 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500204 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530205 action_set({'stdout': result,
206 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500207 finally:
208 remove_flag('actions.get-rate')
209
210
211@when('pingpong.configured')
212@when('actions.get-state')
213def get_state():
Adam Israel32e2fa52016-12-14 22:50:51 -0500214 try:
215 cmd = format_curl('GET', '/state')
216
Philip Josephefe9e062017-01-20 02:09:41 +0530217 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530218 except Exception as e:
219 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500220 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530221 action_set({'stdout': result,
222 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500223 finally:
224 remove_flag('actions.get-state')
225
226
227@when('pingpong.configured')
228@when('actions.get-stats')
229def get_stats():
Adam Israel32e2fa52016-12-14 22:50:51 -0500230 try:
231 cmd = format_curl('GET', '/stats')
232
Philip Josephefe9e062017-01-20 02:09:41 +0530233 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530234 except Exception as e:
235 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500236 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530237 action_set({'stdout': result,
238 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500239 finally:
240 remove_flag('actions.get-stats')
241
242
243@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530244@when('actions.start-traffic')
245def start_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500246 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530247 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500248
Philip Josephefe9e062017-01-20 02:09:41 +0530249 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530250 except Exception as e:
251 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500252 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530253 action_set({'stdout': result,
254 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500255 finally:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530256 remove_flag('actions.start-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500257
258
259@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530260@when('actions.stop-traffic')
261def stop_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500262 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530263 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500264
Philip Josephefe9e062017-01-20 02:09:41 +0530265 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530266 except Exception as e:
267 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500268 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530269 action_set({'stdout': result,
270 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500271 finally:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530272 remove_flag('actions.stop-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500273
274
275def format_curl(method, path, data=None):
276 """ A utility function to build the curl command line. """
277
278 # method must be GET or POST
279 if method not in ['GET', 'POST']:
280 # Throw exception
281 return None
282
283 # Get our service info
Philip Josephefe9e062017-01-20 02:09:41 +0530284 host = '127.0.0.1'
Adam Israel32e2fa52016-12-14 22:50:51 -0500285 port = get_port()
286 mode = cfg['mode']
287
288 cmd = ['curl',
289 # '-D', '/dev/stdout',
Philip Joseph71d56bb2017-01-05 18:54:15 +0530290 '-H', 'Accept: application/vnd.yang.data+xml',
291 '-H', 'Content-Type: application/vnd.yang.data+json',
Adam Israel32e2fa52016-12-14 22:50:51 -0500292 '-X', method]
293
294 if method == "POST" and data:
295 cmd.append('-d')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530296 cmd.append('{}'.format(data))
Adam Israel32e2fa52016-12-14 22:50:51 -0500297
298 cmd.append(
299 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path)
300 )
301 return cmd