73b5fd9128f522445d14c661cde6805503c6b812
1 from charmhelpers
.core
.hookenv
import (
9 from charms
.reactive
import (
10 remove_state
as remove_flag
,
11 set_state
as set_flag
,
14 import charms
.sshproxy
16 from subprocess
import (
27 @when('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!')
34 status_set('blocked', 'Waiting for configuration')
38 if cfg
['mode'] == 'ping':
54 """ Run a command on the local machine. """
55 if isinstance(cmd
, str):
56 cmd
= cmd
.split() if ' ' in cmd
else [cmd
]
60 stdout
, stderr
= p
.communicate()
63 raise CalledProcessError(returncode
=retcode
,
65 output
=stderr
.decode("utf-8").strip())
66 return (stdout
.decode('utf-8').strip(), stderr
.decode('utf-8').strip())
68 @when('pingpong.configured')
69 @when('actions.start')
72 # 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"
75 result
, err
= charms
.sshproxy
._run
(cmd
)
76 except Exception as e
:
78 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
79 remove_flag('actions.start')
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')
91 cmd
= "sudo /usr/bin/systemctl start {}". \
93 result
, err
= charms
.sshproxy
._run
(cmd
)
94 except Exception as e
:
95 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
97 action_set({'stdout': result
,
100 remove_flag('actions.start')
103 @when('pingpong.configured')
104 @when('actions.stop')
107 # Enter the command to stop your service(s)
108 cmd
= "sudo /usr/bin/systemctl stop {}".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
))
113 action_set({'stdout': result
,
116 remove_flag('actions.stop')
119 @when('pingpong.configured')
120 @when('actions.restart')
123 # Enter the command to restart your service(s)
124 cmd
= "sudo /usr/bin/systemctl restart {}".format(cfg
['mode'])
125 result
, err
= charms
.sshproxy
._run
(cmd
)
126 except Exception as e
:
127 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
129 action_set({'stdout': result
,
132 remove_flag('actions.restart')
135 @when('pingpong.configured')
136 @when('actions.set-server')
139 # Get the target service info
140 target_ip
= action_get('server-ip')
141 target_port
= action_get('server-port')
143 data
= '{{"ip" : "{}", "port" : {} }}'. \
144 format(target_ip
, target_port
)
152 result
, err
= run(cmd
)
153 except Exception as e
:
154 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
156 action_set({'stdout': result
,
159 remove_flag('actions.set-server')
162 @when('pingpong.configured')
163 @when('actions.set-rate')
167 rate
= action_get('rate')
168 cmd
= format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate
))
170 result
, err
= run(cmd
)
171 except Exception as e
:
173 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
175 action_set({'stdout': result
,
178 remove_flag('actions.set-rate')
181 @when('pingpong.configured')
182 @when('actions.get-rate')
186 cmd
= format_curl('GET', '/rate')
188 result
, err
= run(cmd
)
189 except Exception as e
:
190 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
192 action_set({'stdout': result
,
195 remove_flag('actions.get-rate')
198 @when('pingpong.configured')
199 @when('actions.get-state')
202 cmd
= format_curl('GET', '/state')
204 result
, err
= run(cmd
)
205 except Exception as e
:
206 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
208 action_set({'stdout': result
,
211 remove_flag('actions.get-state')
214 @when('pingpong.configured')
215 @when('actions.get-stats')
218 cmd
= format_curl('GET', '/stats')
220 result
, err
= run(cmd
)
221 except Exception as e
:
222 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
224 action_set({'stdout': result
,
227 remove_flag('actions.get-stats')
230 @when('pingpong.configured')
231 @when('actions.start-traffic')
234 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : true}')
236 result
, err
= run(cmd
)
237 except Exception as e
:
238 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
240 action_set({'stdout': result
,
243 remove_flag('actions.start-traffic')
246 @when('pingpong.configured')
247 @when('actions.stop-traffic')
250 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : false}')
252 result
, err
= run(cmd
)
253 except Exception as e
:
254 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
256 action_set({'stdout': result
,
259 remove_flag('actions.stop-traffic')
262 def format_curl(method
, path
, data
=None):
263 """ A utility function to build the curl command line. """
265 # method must be GET or POST
266 if method
not in ['GET', 'POST']:
270 # Get our service info
271 host
= cfg
['ssh-hostname']
276 # '-D', '/dev/stdout',
277 '-H', 'Accept: application/vnd.yang.data+xml',
278 '-H', 'Content-Type: application/vnd.yang.data+json',
281 if method
== "POST" and data
:
283 cmd
.append('{}'.format(data
))
286 'http://{}:{}/api/v1/{}{}'.format(host
, port
, mode
, path
)