Add additional configuration check
[osm/devops.git] / juju-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 Check the current values in config to see if we have enough
30 information to continue."""
31 config_changed()
32
33
34 @when('config.changed', 'sshproxy.configured')
35 def config_changed():
36 """Verify the configuration.
37 Verify that the charm has been configured
38 """
39 status_set('maintenance', 'Verifying configuration data...')
40 (validated, output) = charms.sshproxy.verify_ssh_credentials()
41 if not validated:
42 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
43 output
44 ))
45 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
53 @when('config.changed')
54 @when_not('sshproxy.configured')
55 def invalid_credentials():
56 status_set('blocked', 'Waiting for SSH credentials.')
57 pass
58
59
60 def is_ping():
61 if cfg['mode'] == 'ping':
62 return True
63 return False
64
65
66 def is_pong():
67 return not is_ping()
68
69
70 def get_port():
71 port = 18888
72 if is_pong():
73 port = 18889
74 return port
75
76
77 @when('pingpong.configured')
78 @when('actions.start')
79 def start():
80 try:
81 # Bring up the eth1 interface.
82 # The selinux label on the file needs to be set correctly
83 cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
84 result, err = charms.sshproxy._run(cmd)
85 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
91 # Attempt to raise the non-mgmt interface, but ignore failures if
92 # the interface is already up.
93 try:
94 cmd = "sudo timeout 30 /sbin/ifup eth1"
95 result, err = charms.sshproxy._run(cmd)
96 except Exception as e:
97 pass
98
99 try:
100 cmd = "sudo timeout 30 /usr/bin/systemctl start {}". \
101 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))
105 else:
106 action_set({'stdout': result,
107 'errors': err})
108 finally:
109 remove_flag('actions.start')
110
111
112 @when('pingpong.configured')
113 @when('actions.stop')
114 def stop():
115 try:
116 # Enter the command to stop your service(s)
117 cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg['mode'])
118 result, err = charms.sshproxy._run(cmd)
119 except Exception as e:
120 action_fail('command failed: {}, errors: {}'.format(e, e.output))
121 else:
122 action_set({'stdout': result,
123 'errors': err})
124 finally:
125 remove_flag('actions.stop')
126
127
128 @when('pingpong.configured')
129 @when('actions.restart')
130 def restart():
131 try:
132 # Enter the command to restart your service(s)
133 cmd = "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg['mode'])
134 result, err = charms.sshproxy._run(cmd)
135 except Exception as e:
136 action_fail('command failed: {}, errors: {}'.format(e, e.output))
137 else:
138 action_set({'stdout': result,
139 'errors': err})
140 finally:
141 remove_flag('actions.restart')
142
143
144 @when('pingpong.configured')
145 @when('actions.set-server')
146 def set_server():
147 try:
148 # Get the target service info
149 target_ip = action_get('server-ip')
150 target_port = action_get('server-port')
151
152 data = '{{"ip" : "{}", "port" : {} }}'. \
153 format(target_ip, target_port)
154
155 cmd = format_curl(
156 'POST',
157 '/server',
158 data,
159 )
160
161 result, err = charms.sshproxy._run(cmd)
162 except Exception as e:
163 action_fail('command failed: {}, errors: {}'.format(e, e.output))
164 else:
165 action_set({'stdout': result,
166 'errors': err})
167 finally:
168 remove_flag('actions.set-server')
169
170
171 @when('pingpong.configured')
172 @when('actions.set-rate')
173 def set_rate():
174 try:
175 if is_ping():
176 rate = action_get('rate')
177 cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate))
178
179 result, err = charms.sshproxy._run(cmd)
180 except Exception as e:
181 err = "{}".format(e)
182 action_fail('command failed: {}, errors: {}'.format(err, e.output))
183 else:
184 action_set({'stdout': result,
185 'errors': err})
186 finally:
187 remove_flag('actions.set-rate')
188
189
190 @when('pingpong.configured')
191 @when('actions.get-rate')
192 def get_rate():
193 try:
194 if is_ping():
195 cmd = format_curl('GET', '/rate')
196
197 result, err = charms.sshproxy._run(cmd)
198 except Exception as e:
199 action_fail('command failed: {}, errors: {}'.format(e, e.output))
200 else:
201 action_set({'stdout': result,
202 'errors': err})
203 finally:
204 remove_flag('actions.get-rate')
205
206
207 @when('pingpong.configured')
208 @when('actions.get-state')
209 def get_state():
210 try:
211 cmd = format_curl('GET', '/state')
212
213 result, err = charms.sshproxy._run(cmd)
214 except Exception as e:
215 action_fail('command failed: {}, errors: {}'.format(e, e.output))
216 else:
217 action_set({'stdout': result,
218 'errors': err})
219 finally:
220 remove_flag('actions.get-state')
221
222
223 @when('pingpong.configured')
224 @when('actions.get-stats')
225 def get_stats():
226 try:
227 cmd = format_curl('GET', '/stats')
228
229 result, err = charms.sshproxy._run(cmd)
230 except Exception as e:
231 action_fail('command failed: {}, errors: {}'.format(e, e.output))
232 else:
233 action_set({'stdout': result,
234 'errors': err})
235 finally:
236 remove_flag('actions.get-stats')
237
238
239 @when('pingpong.configured')
240 @when('actions.start-traffic')
241 def start_traffic():
242 try:
243 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}')
244
245 result, err = charms.sshproxy._run(cmd)
246 except Exception as e:
247 action_fail('command failed: {}, errors: {}'.format(e, e.output))
248 else:
249 action_set({'stdout': result,
250 'errors': err})
251 finally:
252 remove_flag('actions.start-traffic')
253
254
255 @when('pingpong.configured')
256 @when('actions.stop-traffic')
257 def stop_traffic():
258 try:
259 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}')
260
261 result, err = charms.sshproxy._run(cmd)
262 except Exception as e:
263 action_fail('command failed: {}, errors: {}'.format(e, e.output))
264 else:
265 action_set({'stdout': result,
266 'errors': err})
267 finally:
268 remove_flag('actions.stop-traffic')
269
270
271 def 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
280 host = '127.0.0.1'
281 port = get_port()
282 mode = cfg['mode']
283
284 cmd = ['curl',
285 # '-D', '/dev/stdout',
286 '-H', 'Accept: application/vnd.yang.data+xml',
287 '-H', 'Content-Type: application/vnd.yang.data+json',
288 '-X', method]
289
290 if method == "POST" and data:
291 cmd.append('-d')
292 cmd.append('{}'.format(data))
293
294 cmd.append(
295 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path)
296 )
297 return cmd