Add init loop in prometheus sidecar container
[osm/devops.git] / charms / layers / pingpong / reactive / pingpong.py
1 from charmhelpers.core.hookenv import (
2 action_get,
3 action_fail,
4 action_set,
5 config,
6 status_set,
7 )
8
9 from charms.reactive import (
10 remove_state as remove_flag,
11 set_state as set_flag,
12 when,
13 when_not,
14 )
15 import charms.sshproxy
16 # from subprocess import (
17 # Popen,
18 # CalledProcessError,
19 # PIPE,
20 # )
21
22
23 cfg = config()
24
25
26 @when_not('pingpong.configured')
27 def not_configured():
28 """Check the current configuration.
29
30 Check the current values in config to see if we have enough
31 information to continue.
32 """
33 config_changed()
34
35
36 @when('config.changed', 'sshproxy.configured')
37 def config_changed():
38 """Verify the configuration.
39
40 Verify that the charm has been configured
41 """
42
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 ))
51 return
52
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))
62
63
64 @when('config.changed')
65 @when_not('sshproxy.configured')
66 def invalid_credentials():
67 status_set('blocked', 'Waiting for SSH credentials.')
68 pass
69
70
71 def is_ping():
72 if cfg['mode'] == 'ping':
73 return True
74 return False
75
76
77 def is_pong():
78 return not is_ping()
79
80
81 def get_port():
82 port = 18888
83 if is_pong():
84 port = 18889
85 return port
86
87
88 @when('pingpong.configured')
89 @when('actions.start')
90 def start():
91 try:
92 # Bring up the eth1 interface.
93 # The selinux label on the file needs to be set correctly
94 cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
95 result, err = charms.sshproxy._run(cmd)
96 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
102 # Attempt to raise the non-mgmt interface, but ignore failures if
103 # the interface is already up.
104 try:
105 cmd = "sudo timeout 30 /sbin/ifup eth1"
106 result, err = charms.sshproxy._run(cmd)
107 except Exception as e:
108 pass
109
110 try:
111 cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \
112 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))
116 else:
117 action_set({'stdout': result,
118 'errors': err})
119 finally:
120 remove_flag('actions.start')
121
122
123 @when('pingpong.configured')
124 @when('actions.stop')
125 def stop():
126 try:
127 # Enter the command to stop your service(s)
128 cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg['mode'])
129 result, err = charms.sshproxy._run(cmd)
130 except Exception as e:
131 action_fail('command failed: {}, errors: {}'.format(e, e.output))
132 else:
133 action_set({'stdout': result,
134 'errors': err})
135 finally:
136 remove_flag('actions.stop')
137
138
139 @when('pingpong.configured')
140 @when('actions.restart')
141 def restart():
142 try:
143 # Enter the command to restart your service(s)
144 cmd = "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg['mode'])
145 result, err = charms.sshproxy._run(cmd)
146 except Exception as e:
147 action_fail('command failed: {}, errors: {}'.format(e, e.output))
148 else:
149 action_set({'stdout': result,
150 'errors': err})
151 finally:
152 remove_flag('actions.restart')
153
154
155 @when('pingpong.configured')
156 @when('actions.set-server')
157 def set_server():
158 try:
159 # Get the target service info
160 target_ip = action_get('server-ip')
161 target_port = action_get('server-port')
162
163 data = '{{"ip" : "{}", "port" : {} }}'. \
164 format(target_ip, target_port)
165
166 cmd = format_curl(
167 'POST',
168 '/server',
169 data,
170 )
171
172 result, err = charms.sshproxy._run(cmd)
173 except Exception as e:
174 action_fail('command failed: {}, errors: {}'.format(e, e.output))
175 else:
176 action_set({'stdout': result,
177 'errors': err})
178 finally:
179 remove_flag('actions.set-server')
180
181
182 @when('pingpong.configured')
183 @when('actions.set-rate')
184 def set_rate():
185 try:
186 if is_ping():
187 rate = action_get('rate')
188 cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate))
189
190 result, err = charms.sshproxy._run(cmd)
191 except Exception as e:
192 err = "{}".format(e)
193 action_fail('command failed: {}, errors: {}'.format(err, e.output))
194 else:
195 action_set({'stdout': result,
196 'errors': err})
197 finally:
198 remove_flag('actions.set-rate')
199
200
201 @when('pingpong.configured')
202 @when('actions.get-rate')
203 def get_rate():
204 try:
205 if is_ping():
206 cmd = format_curl('GET', '/rate')
207
208 result, err = charms.sshproxy._run(cmd)
209 except Exception as e:
210 action_fail('command failed: {}, errors: {}'.format(e, e.output))
211 else:
212 action_set({'stdout': result,
213 'errors': err})
214 finally:
215 remove_flag('actions.get-rate')
216
217
218 @when('pingpong.configured')
219 @when('actions.get-state')
220 def get_state():
221 try:
222 cmd = format_curl('GET', '/state')
223
224 result, err = charms.sshproxy._run(cmd)
225 except Exception as e:
226 action_fail('command failed: {}, errors: {}'.format(e, e.output))
227 else:
228 action_set({'stdout': result,
229 'errors': err})
230 finally:
231 remove_flag('actions.get-state')
232
233
234 @when('pingpong.configured')
235 @when('actions.get-stats')
236 def get_stats():
237 try:
238 cmd = format_curl('GET', '/stats')
239
240 result, err = charms.sshproxy._run(cmd)
241 except Exception as e:
242 action_fail('command failed: {}, errors: {}'.format(e, e.output))
243 else:
244 action_set({'stdout': result,
245 'errors': err})
246 finally:
247 remove_flag('actions.get-stats')
248
249
250 @when('pingpong.configured')
251 @when('actions.start-traffic')
252 def start_traffic():
253 try:
254 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}')
255
256 result, err = charms.sshproxy._run(cmd)
257 except Exception as e:
258 action_fail('command failed: {}, errors: {}'.format(e, e.output))
259 else:
260 action_set({'stdout': result,
261 'errors': err})
262 finally:
263 remove_flag('actions.start-traffic')
264
265
266 @when('pingpong.configured')
267 @when('actions.stop-traffic')
268 def stop_traffic():
269 try:
270 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}')
271
272 result, err = charms.sshproxy._run(cmd)
273 except Exception as e:
274 action_fail('command failed: {}, errors: {}'.format(e, e.output))
275 else:
276 action_set({'stdout': result,
277 'errors': err})
278 finally:
279 remove_flag('actions.stop-traffic')
280
281
282 def 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
291 host = '127.0.0.1'
292 port = get_port()
293 mode = cfg['mode']
294
295 cmd = ['curl',
296 # '-D', '/dev/stdout',
297 '-H', 'Accept: application/vnd.yang.data+xml',
298 '-H', 'Content-Type: application/vnd.yang.data+json',
299 '-X', method]
300
301 if method == "POST" and data:
302 cmd.append('-d')
303 cmd.append('{}'.format(data))
304
305 cmd.append(
306 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path)
307 )
308 return cmd