blob: 701d9e2118b4e467fadc79b559a873fb8a9ec199 [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.
29 Check the current values in config to see if we have enough
30 information to continue."""
31 config_changed()
32
33
Adam Israel5a0f6e42017-10-18 10:39:40 -040034@when('config.changed', 'sshproxy.configured')
Adam Israel32e2fa52016-12-14 22:50:51 -050035def config_changed():
Adam Israel5a0f6e42017-10-18 10:39:40 -040036 """Verify the configuration.
37 Verify that the charm has been configured
38 """
Adam Israelc1ec5712017-10-19 19:26:00 -040039 status_set('maintenance', 'Verifying configuration data...')
Adam Israel5a0f6e42017-10-18 10:39:40 -040040 (validated, output) = charms.sshproxy.verify_ssh_credentials()
41 if not validated:
42 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
43 output
44 ))
Adam Israel32e2fa52016-12-14 22:50:51 -050045 if all(k in cfg for k in ['mode']):
46 if cfg['mode'] in ['ping', 'pong']:
47 set_flag('pingpong.configured')
48 status_set('active', 'ready!')
49 return
50 status_set('blocked', 'Waiting for configuration')
51
52
Adam Israel5a0f6e42017-10-18 10:39:40 -040053@when('config.changed')
54@when_not('sshproxy.configured')
55def invalid_credentials():
56 status_set('blocked', 'Waiting for SSH credentials.')
57 pass
58
59
Adam Israel32e2fa52016-12-14 22:50:51 -050060def is_ping():
61 if cfg['mode'] == 'ping':
62 return True
63 return False
64
65
66def is_pong():
67 return not is_ping()
68
69
70def get_port():
71 port = 18888
72 if is_pong():
73 port = 18889
74 return port
75
Philip Joseph0efe5842017-01-14 00:07:30 +053076
Adam Israel32e2fa52016-12-14 22:50:51 -050077@when('pingpong.configured')
78@when('actions.start')
79def start():
Adam Israel32e2fa52016-12-14 22:50:51 -050080 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +053081 # Bring up the eth1 interface.
82 # The selinux label on the file needs to be set correctly
Philip Joseph24a06f02017-04-05 21:31:12 +053083 cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
Adam Israel32e2fa52016-12-14 22:50:51 -050084 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +053085 except Exception as e:
86 err = "{}".format(e)
87 action_fail('command failed: {}, errors: {}'.format(err, e.output))
88 remove_flag('actions.start')
89 return
90
Adam Israel1f9ce572017-10-16 14:46:29 -040091 # Attempt to raise the non-mgmt interface, but ignore failures if
92 # the interface is already up.
Philip Joseph71d56bb2017-01-05 18:54:15 +053093 try:
Adam Israel1f9ce572017-10-16 14:46:29 -040094 cmd = "sudo timeout 30 /sbin/ifup eth1"
Philip Joseph71d56bb2017-01-05 18:54:15 +053095 result, err = charms.sshproxy._run(cmd)
96 except Exception as e:
Adam Israel1f9ce572017-10-16 14:46:29 -040097 pass
Philip Joseph71d56bb2017-01-05 18:54:15 +053098
99 try:
Adam Israel1f9ce572017-10-16 14:46:29 -0400100 cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \
Philip Joseph71d56bb2017-01-05 18:54:15 +0530101 format(cfg['mode'])
102 result, err = charms.sshproxy._run(cmd)
103 except Exception as e:
104 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500105 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530106 action_set({'stdout': result,
107 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500108 finally:
109 remove_flag('actions.start')
110
111
112@when('pingpong.configured')
113@when('actions.stop')
114def stop():
Adam Israel32e2fa52016-12-14 22:50:51 -0500115 try:
116 # Enter the command to stop your service(s)
Philip Joseph24a06f02017-04-05 21:31:12 +0530117 cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -0500118 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530119 except Exception as e:
120 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500121 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530122 action_set({'stdout': result,
123 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500124 finally:
125 remove_flag('actions.stop')
126
127
128@when('pingpong.configured')
129@when('actions.restart')
130def restart():
Adam Israel32e2fa52016-12-14 22:50:51 -0500131 try:
132 # Enter the command to restart your service(s)
Philip Joseph24a06f02017-04-05 21:31:12 +0530133 cmd = "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -0500134 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530135 except Exception as e:
136 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500137 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530138 action_set({'stdout': result,
139 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500140 finally:
141 remove_flag('actions.restart')
142
143
144@when('pingpong.configured')
145@when('actions.set-server')
146def set_server():
Adam Israel32e2fa52016-12-14 22:50:51 -0500147 try:
148 # Get the target service info
149 target_ip = action_get('server-ip')
150 target_port = action_get('server-port')
151
Philip Joseph71d56bb2017-01-05 18:54:15 +0530152 data = '{{"ip" : "{}", "port" : {} }}'. \
153 format(target_ip, target_port)
Adam Israel32e2fa52016-12-14 22:50:51 -0500154
155 cmd = format_curl(
156 'POST',
157 '/server',
158 data,
159 )
160
Philip Josephefe9e062017-01-20 02:09:41 +0530161 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530162 except Exception as e:
163 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500164 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530165 action_set({'stdout': result,
166 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500167 finally:
168 remove_flag('actions.set-server')
169
170
171@when('pingpong.configured')
172@when('actions.set-rate')
173def set_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500174 try:
175 if is_ping():
176 rate = action_get('rate')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530177 cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate))
Adam Israel32e2fa52016-12-14 22:50:51 -0500178
Philip Josephefe9e062017-01-20 02:09:41 +0530179 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530180 except Exception as e:
181 err = "{}".format(e)
182 action_fail('command failed: {}, errors: {}'.format(err, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500183 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530184 action_set({'stdout': result,
185 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500186 finally:
187 remove_flag('actions.set-rate')
188
189
190@when('pingpong.configured')
191@when('actions.get-rate')
192def get_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500193 try:
194 if is_ping():
195 cmd = format_curl('GET', '/rate')
196
Philip Josephefe9e062017-01-20 02:09:41 +0530197 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530198 except Exception as e:
199 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500200 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530201 action_set({'stdout': result,
202 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500203 finally:
204 remove_flag('actions.get-rate')
205
206
207@when('pingpong.configured')
208@when('actions.get-state')
209def get_state():
Adam Israel32e2fa52016-12-14 22:50:51 -0500210 try:
211 cmd = format_curl('GET', '/state')
212
Philip Josephefe9e062017-01-20 02:09:41 +0530213 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530214 except Exception as e:
215 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500216 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530217 action_set({'stdout': result,
218 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500219 finally:
220 remove_flag('actions.get-state')
221
222
223@when('pingpong.configured')
224@when('actions.get-stats')
225def get_stats():
Adam Israel32e2fa52016-12-14 22:50:51 -0500226 try:
227 cmd = format_curl('GET', '/stats')
228
Philip Josephefe9e062017-01-20 02:09:41 +0530229 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530230 except Exception as e:
231 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500232 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530233 action_set({'stdout': result,
234 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500235 finally:
236 remove_flag('actions.get-stats')
237
238
239@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530240@when('actions.start-traffic')
241def start_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500242 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530243 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500244
Philip Josephefe9e062017-01-20 02:09:41 +0530245 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530246 except Exception as e:
247 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500248 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530249 action_set({'stdout': result,
250 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500251 finally:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530252 remove_flag('actions.start-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500253
254
255@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530256@when('actions.stop-traffic')
257def stop_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500258 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530259 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500260
Philip Josephefe9e062017-01-20 02:09:41 +0530261 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530262 except Exception as e:
263 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500264 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530265 action_set({'stdout': result,
266 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500267 finally:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530268 remove_flag('actions.stop-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500269
270
271def format_curl(method, path, data=None):
272 """ A utility function to build the curl command line. """
273
274 # method must be GET or POST
275 if method not in ['GET', 'POST']:
276 # Throw exception
277 return None
278
279 # Get our service info
Philip Josephefe9e062017-01-20 02:09:41 +0530280 host = '127.0.0.1'
Adam Israel32e2fa52016-12-14 22:50:51 -0500281 port = get_port()
282 mode = cfg['mode']
283
284 cmd = ['curl',
285 # '-D', '/dev/stdout',
Philip Joseph71d56bb2017-01-05 18:54:15 +0530286 '-H', 'Accept: application/vnd.yang.data+xml',
287 '-H', 'Content-Type: application/vnd.yang.data+json',
Adam Israel32e2fa52016-12-14 22:50:51 -0500288 '-X', method]
289
290 if method == "POST" and data:
291 cmd.append('-d')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530292 cmd.append('{}'.format(data))
Adam Israel32e2fa52016-12-14 22:50:51 -0500293
294 cmd.append(
295 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path)
296 )
297 return cmd