blob: 05debf4562f5a05e26630d770dfe7af480e71c32 [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
Philip Joseph71d56bb2017-01-05 18:54:15 +053015from subprocess import (
16 Popen,
17 CalledProcessError,
18 PIPE,
19)
Adam Israel32e2fa52016-12-14 22:50:51 -050020
21
22cfg = config()
23
24
25@when('config.changed')
26def config_changed():
27 if all(k in cfg for k in ['mode']):
28 if cfg['mode'] in ['ping', 'pong']:
29 set_flag('pingpong.configured')
30 status_set('active', 'ready!')
31 return
32 status_set('blocked', 'Waiting for configuration')
33
34
35def is_ping():
36 if cfg['mode'] == 'ping':
37 return True
38 return False
39
40
41def is_pong():
42 return not is_ping()
43
44
45def get_port():
46 port = 18888
47 if is_pong():
48 port = 18889
49 return port
50
Philip Joseph0efe5842017-01-14 00:07:30 +053051
Adam Israel32e2fa52016-12-14 22:50:51 -050052@when('pingpong.configured')
53@when('actions.start')
54def start():
Adam Israel32e2fa52016-12-14 22:50:51 -050055 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +053056 # Bring up the eth1 interface.
57 # The selinux label on the file needs to be set correctly
58 cmd = "sudo /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
Adam Israel32e2fa52016-12-14 22:50:51 -050059 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +053060 except Exception as e:
61 err = "{}".format(e)
62 action_fail('command failed: {}, errors: {}'.format(err, e.output))
63 remove_flag('actions.start')
64 return
65
66 try:
67 cmd = "sudo /sbin/ifup eth1"
68 result, err = charms.sshproxy._run(cmd)
69 except Exception as e:
70 action_fail('command failed: {}, errors: {}'.format(e, e.output))
71 remove_flag('actions.start')
72 return
73
74 try:
75 cmd = "sudo /usr/bin/systemctl start {}". \
76 format(cfg['mode'])
77 result, err = charms.sshproxy._run(cmd)
78 except Exception as e:
79 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -050080 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +053081 action_set({'stdout': result,
82 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -050083 finally:
84 remove_flag('actions.start')
85
86
87@when('pingpong.configured')
88@when('actions.stop')
89def stop():
Adam Israel32e2fa52016-12-14 22:50:51 -050090 try:
91 # Enter the command to stop your service(s)
Philip Joseph71d56bb2017-01-05 18:54:15 +053092 cmd = "sudo /usr/bin/systemctl stop {}".format(cfg['mode'])
Adam Israel32e2fa52016-12-14 22:50:51 -050093 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +053094 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.stop')
101
102
103@when('pingpong.configured')
104@when('actions.restart')
105def restart():
Adam Israel32e2fa52016-12-14 22:50:51 -0500106 try:
107 # Enter the command to restart your service(s)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530108 cmd = "sudo /usr/bin/systemctl restart {}".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.restart')
117
118
119@when('pingpong.configured')
120@when('actions.set-server')
121def set_server():
Adam Israel32e2fa52016-12-14 22:50:51 -0500122 try:
123 # Get the target service info
124 target_ip = action_get('server-ip')
125 target_port = action_get('server-port')
126
Philip Joseph71d56bb2017-01-05 18:54:15 +0530127 data = '{{"ip" : "{}", "port" : {} }}'. \
128 format(target_ip, target_port)
Adam Israel32e2fa52016-12-14 22:50:51 -0500129
130 cmd = format_curl(
131 'POST',
132 '/server',
133 data,
134 )
135
Philip Josephefe9e062017-01-20 02:09:41 +0530136 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530137 except Exception as e:
138 action_fail('command failed: {}, errors: {}'.format(e, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500139 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530140 action_set({'stdout': result,
141 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500142 finally:
143 remove_flag('actions.set-server')
144
145
146@when('pingpong.configured')
147@when('actions.set-rate')
148def set_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500149 try:
150 if is_ping():
151 rate = action_get('rate')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530152 cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate))
Adam Israel32e2fa52016-12-14 22:50:51 -0500153
Philip Josephefe9e062017-01-20 02:09:41 +0530154 result, err = charms.sshproxy._run(cmd)
Philip Joseph71d56bb2017-01-05 18:54:15 +0530155 except Exception as e:
156 err = "{}".format(e)
157 action_fail('command failed: {}, errors: {}'.format(err, e.output))
Adam Israel32e2fa52016-12-14 22:50:51 -0500158 else:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530159 action_set({'stdout': result,
160 'errors': err})
Adam Israel32e2fa52016-12-14 22:50:51 -0500161 finally:
162 remove_flag('actions.set-rate')
163
164
165@when('pingpong.configured')
166@when('actions.get-rate')
167def get_rate():
Adam Israel32e2fa52016-12-14 22:50:51 -0500168 try:
169 if is_ping():
170 cmd = format_curl('GET', '/rate')
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.get-rate')
180
181
182@when('pingpong.configured')
183@when('actions.get-state')
184def get_state():
Adam Israel32e2fa52016-12-14 22:50:51 -0500185 try:
186 cmd = format_curl('GET', '/state')
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-state')
196
197
198@when('pingpong.configured')
199@when('actions.get-stats')
200def get_stats():
Adam Israel32e2fa52016-12-14 22:50:51 -0500201 try:
202 cmd = format_curl('GET', '/stats')
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-stats')
212
213
214@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530215@when('actions.start-traffic')
216def start_traffic():
Adam Israel32e2fa52016-12-14 22:50:51 -0500217 try:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530218 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}')
Adam Israel32e2fa52016-12-14 22:50:51 -0500219
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:
Philip Joseph71d56bb2017-01-05 18:54:15 +0530227 remove_flag('actions.start-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500228
229
230@when('pingpong.configured')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530231@when('actions.stop-traffic')
232def stop_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" : false}')
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.stop-traffic')
Adam Israel32e2fa52016-12-14 22:50:51 -0500244
245
246def format_curl(method, path, data=None):
247 """ A utility function to build the curl command line. """
248
249 # method must be GET or POST
250 if method not in ['GET', 'POST']:
251 # Throw exception
252 return None
253
254 # Get our service info
Philip Josephefe9e062017-01-20 02:09:41 +0530255 host = '127.0.0.1'
Adam Israel32e2fa52016-12-14 22:50:51 -0500256 port = get_port()
257 mode = cfg['mode']
258
259 cmd = ['curl',
260 # '-D', '/dev/stdout',
Philip Joseph71d56bb2017-01-05 18:54:15 +0530261 '-H', 'Accept: application/vnd.yang.data+xml',
262 '-H', 'Content-Type: application/vnd.yang.data+json',
Adam Israel32e2fa52016-12-14 22:50:51 -0500263 '-X', method]
264
265 if method == "POST" and data:
266 cmd.append('-d')
Philip Joseph71d56bb2017-01-05 18:54:15 +0530267 cmd.append('{}'.format(data))
Adam Israel32e2fa52016-12-14 22:50:51 -0500268
269 cmd.append(
270 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path)
271 )
272 return cmd