Fix error raising interface
[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 )
14 import charms.sshproxy
15 from subprocess import (
16 Popen,
17 CalledProcessError,
18 PIPE,
19 )
20
21
22 cfg = config()
23
24
25 @when('config.changed')
26 def 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
35 def is_ping():
36 if cfg['mode'] == 'ping':
37 return True
38 return False
39
40
41 def is_pong():
42 return not is_ping()
43
44
45 def get_port():
46 port = 18888
47 if is_pong():
48 port = 18889
49 return port
50
51
52 @when('pingpong.configured')
53 @when('actions.start')
54 def start():
55 try:
56 # Bring up the eth1 interface.
57 # The selinux label on the file needs to be set correctly
58 cmd = "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
59 result, err = charms.sshproxy._run(cmd)
60 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 # Attempt to raise the non-mgmt interface, but ignore failures if
67 # the interface is already up.
68 try:
69 cmd = "sudo timeout 30 /sbin/ifup eth1"
70 result, err = charms.sshproxy._run(cmd)
71 except Exception as e:
72 pass
73
74 try:
75 cmd = "sudo timeout 30 /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))
80 else:
81 action_set({'stdout': result,
82 'errors': err})
83 finally:
84 remove_flag('actions.start')
85
86
87 @when('pingpong.configured')
88 @when('actions.stop')
89 def stop():
90 try:
91 # Enter the command to stop your service(s)
92 cmd = "sudo timeout 30 /usr/bin/systemctl stop {}".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))
96 else:
97 action_set({'stdout': result,
98 'errors': err})
99 finally:
100 remove_flag('actions.stop')
101
102
103 @when('pingpong.configured')
104 @when('actions.restart')
105 def restart():
106 try:
107 # Enter the command to restart your service(s)
108 cmd = "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg['mode'])
109 result, err = charms.sshproxy._run(cmd)
110 except Exception as e:
111 action_fail('command failed: {}, errors: {}'.format(e, e.output))
112 else:
113 action_set({'stdout': result,
114 'errors': err})
115 finally:
116 remove_flag('actions.restart')
117
118
119 @when('pingpong.configured')
120 @when('actions.set-server')
121 def set_server():
122 try:
123 # Get the target service info
124 target_ip = action_get('server-ip')
125 target_port = action_get('server-port')
126
127 data = '{{"ip" : "{}", "port" : {} }}'. \
128 format(target_ip, target_port)
129
130 cmd = format_curl(
131 'POST',
132 '/server',
133 data,
134 )
135
136 result, err = charms.sshproxy._run(cmd)
137 except Exception as e:
138 action_fail('command failed: {}, errors: {}'.format(e, e.output))
139 else:
140 action_set({'stdout': result,
141 'errors': err})
142 finally:
143 remove_flag('actions.set-server')
144
145
146 @when('pingpong.configured')
147 @when('actions.set-rate')
148 def set_rate():
149 try:
150 if is_ping():
151 rate = action_get('rate')
152 cmd = format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate))
153
154 result, err = charms.sshproxy._run(cmd)
155 except Exception as e:
156 err = "{}".format(e)
157 action_fail('command failed: {}, errors: {}'.format(err, e.output))
158 else:
159 action_set({'stdout': result,
160 'errors': err})
161 finally:
162 remove_flag('actions.set-rate')
163
164
165 @when('pingpong.configured')
166 @when('actions.get-rate')
167 def get_rate():
168 try:
169 if is_ping():
170 cmd = format_curl('GET', '/rate')
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.get-rate')
180
181
182 @when('pingpong.configured')
183 @when('actions.get-state')
184 def get_state():
185 try:
186 cmd = format_curl('GET', '/state')
187
188 result, err = charms.sshproxy._run(cmd)
189 except Exception as e:
190 action_fail('command failed: {}, errors: {}'.format(e, e.output))
191 else:
192 action_set({'stdout': result,
193 'errors': err})
194 finally:
195 remove_flag('actions.get-state')
196
197
198 @when('pingpong.configured')
199 @when('actions.get-stats')
200 def get_stats():
201 try:
202 cmd = format_curl('GET', '/stats')
203
204 result, err = charms.sshproxy._run(cmd)
205 except Exception as e:
206 action_fail('command failed: {}, errors: {}'.format(e, e.output))
207 else:
208 action_set({'stdout': result,
209 'errors': err})
210 finally:
211 remove_flag('actions.get-stats')
212
213
214 @when('pingpong.configured')
215 @when('actions.start-traffic')
216 def start_traffic():
217 try:
218 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : true}')
219
220 result, err = charms.sshproxy._run(cmd)
221 except Exception as e:
222 action_fail('command failed: {}, errors: {}'.format(e, e.output))
223 else:
224 action_set({'stdout': result,
225 'errors': err})
226 finally:
227 remove_flag('actions.start-traffic')
228
229
230 @when('pingpong.configured')
231 @when('actions.stop-traffic')
232 def stop_traffic():
233 try:
234 cmd = format_curl('POST', '/adminstatus/state', '{"enable" : false}')
235
236 result, err = charms.sshproxy._run(cmd)
237 except Exception as e:
238 action_fail('command failed: {}, errors: {}'.format(e, e.output))
239 else:
240 action_set({'stdout': result,
241 'errors': err})
242 finally:
243 remove_flag('actions.stop-traffic')
244
245
246 def 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
255 host = '127.0.0.1'
256 port = get_port()
257 mode = cfg['mode']
258
259 cmd = ['curl',
260 # '-D', '/dev/stdout',
261 '-H', 'Accept: application/vnd.yang.data+xml',
262 '-H', 'Content-Type: application/vnd.yang.data+json',
263 '-X', method]
264
265 if method == "POST" and data:
266 cmd.append('-d')
267 cmd.append('{}'.format(data))
268
269 cmd.append(
270 'http://{}:{}/api/v1/{}{}'.format(host, port, mode, path)
271 )
272 return cmd