blob: 73b5fd9128f522445d14c661cde6805503c6b812 [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,
13)
14import charms.sshproxy
15import json
Philip Joseph71d56bb2017-01-05 18:54:15 +053016from subprocess import (
17 Popen,
18 CalledProcessError,
19 PIPE,
20)
21import time
Adam Israel32e2fa52016-12-14 22:50:51 -050022
23
24cfg = config()
25
26
27@when('config.changed')
28def config_changed():
29 if all(k in cfg for k in ['mode']):
30 if cfg['mode'] in ['ping', 'pong']:
31 set_flag('pingpong.configured')
32 status_set('active', 'ready!')
33 return
34 status_set('blocked', 'Waiting for configuration')
35
36
37def is_ping():
38 if cfg['mode'] == 'ping':
39 return True
40 return False
41
42
43def is_pong():
44 return not is_ping()
45
46
47def get_port():
48 port = 18888
49 if is_pong():
50 port = 18889
51 return port
52
Philip Joseph71d56bb2017-01-05 18:54:15 +053053def run(cmd):
54 """ Run a command on the local machine. """
55 if isinstance(cmd, str):
56 cmd = cmd.split() if ' ' in cmd else [cmd]
57 p = Popen(cmd,
58 stdout=PIPE,
59 stderr=PIPE)
60 stdout, stderr = p.communicate()
61 retcode = p.poll()
62 if retcode > 0:
63 raise CalledProcessError(returncode=retcode,
64 cmd=cmd,
65 output=stderr.decode("utf-8").strip())
66 return (stdout.decode('utf-8').strip(), stderr.decode('utf-8').strip())
Adam Israel32e2fa52016-12-14 22:50:51 -050067
68@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
74 cmd = "sudo /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
82 try:
83 cmd = "sudo /sbin/ifup eth1"
84 result, err = charms.sshproxy._run(cmd)
85 except Exception as e:
86 action_fail('command failed: {}, errors: {}'.format(e, e.output))
87 remove_flag('actions.start')
88 return
89
90 try:
91 cmd = "sudo /usr/bin/systemctl start {}". \
92 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 Joseph71d56bb2017-01-05 18:54:15 +0530108 cmd = "sudo /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 Joseph71d56bb2017-01-05 18:54:15 +0530124 cmd = "sudo /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 Joseph71d56bb2017-01-05 18:54:15 +0530152 result, err = run(cmd)
153 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 Joseph71d56bb2017-01-05 18:54:15 +0530170 result, err = run(cmd)
171 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 Joseph71d56bb2017-01-05 18:54:15 +0530188 result, err = run(cmd)
189 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 Joseph71d56bb2017-01-05 18:54:15 +0530204 result, err = run(cmd)
205 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 Joseph71d56bb2017-01-05 18:54:15 +0530220 result, err = run(cmd)
221 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 Joseph71d56bb2017-01-05 18:54:15 +0530236 result, err = run(cmd)
237 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 Joseph71d56bb2017-01-05 18:54:15 +0530252 result, err = run(cmd)
253 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
271 host = cfg['ssh-hostname']
272 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