ae18076cb5915148ff0a70a5951289bc6ba957d1
1 from charmhelpers
.core
.hookenv
import (
9 from charms
.reactive
import (
10 remove_state
as remove_flag
,
11 set_state
as set_flag
,
15 import charms
.sshproxy
16 # from subprocess import (
26 @when_not('pingpong.configured')
28 """Check the current configuration.
30 Check the current values in config to see if we have enough
31 information to continue.
36 @when('config.changed', 'sshproxy.configured')
38 """Verify the configuration.
40 Verify that the charm has been configured
42 status_set('maintenance', 'Verifying configuration data...')
43 (validated
, output
) = charms
.sshproxy
.verify_ssh_credentials()
45 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
49 if all(k
in cfg
for k
in ['mode']):
50 if cfg
['mode'] in ['ping', 'pong']:
51 set_flag('pingpong.configured')
52 status_set('active', 'ready!')
54 status_set('blocked', 'Waiting for configuration')
57 @when('config.changed')
58 @when_not('sshproxy.configured')
59 def invalid_credentials():
60 status_set('blocked', 'Waiting for SSH credentials.')
65 if cfg
['mode'] == 'ping':
81 @when('pingpong.configured')
82 @when('actions.start')
85 # Bring up the eth1 interface.
86 # The selinux label on the file needs to be set correctly
87 cmd
= "sudo timeout 5 /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
88 result
, err
= charms
.sshproxy
._run
(cmd
)
89 except Exception as e
:
91 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
92 remove_flag('actions.start')
95 # Attempt to raise the non-mgmt interface, but ignore failures if
96 # the interface is already up.
98 cmd
= "sudo timeout 30 /sbin/ifup eth1"
99 result
, err
= charms
.sshproxy
._run
(cmd
)
100 except Exception as e
:
104 cmd
= "sudo timeout 30 /usr/bin/systemctl start {}". \
106 result
, err
= charms
.sshproxy
._run
(cmd
)
107 except Exception as e
:
108 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
110 action_set({'stdout': result
,
113 remove_flag('actions.start')
116 @when('pingpong.configured')
117 @when('actions.stop')
120 # Enter the command to stop your service(s)
121 cmd
= "sudo timeout 30 /usr/bin/systemctl stop {}".format(cfg
['mode'])
122 result
, err
= charms
.sshproxy
._run
(cmd
)
123 except Exception as e
:
124 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
126 action_set({'stdout': result
,
129 remove_flag('actions.stop')
132 @when('pingpong.configured')
133 @when('actions.restart')
136 # Enter the command to restart your service(s)
137 cmd
= "sudo timeout 30 /usr/bin/systemctl restart {}".format(cfg
['mode'])
138 result
, err
= charms
.sshproxy
._run
(cmd
)
139 except Exception as e
:
140 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
142 action_set({'stdout': result
,
145 remove_flag('actions.restart')
148 @when('pingpong.configured')
149 @when('actions.set-server')
152 # Get the target service info
153 target_ip
= action_get('server-ip')
154 target_port
= action_get('server-port')
156 data
= '{{"ip" : "{}", "port" : {} }}'. \
157 format(target_ip
, target_port
)
165 result
, err
= charms
.sshproxy
._run
(cmd
)
166 except Exception as e
:
167 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
169 action_set({'stdout': result
,
172 remove_flag('actions.set-server')
175 @when('pingpong.configured')
176 @when('actions.set-rate')
180 rate
= action_get('rate')
181 cmd
= format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate
))
183 result
, err
= charms
.sshproxy
._run
(cmd
)
184 except Exception as e
:
186 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
188 action_set({'stdout': result
,
191 remove_flag('actions.set-rate')
194 @when('pingpong.configured')
195 @when('actions.get-rate')
199 cmd
= format_curl('GET', '/rate')
201 result
, err
= charms
.sshproxy
._run
(cmd
)
202 except Exception as e
:
203 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
205 action_set({'stdout': result
,
208 remove_flag('actions.get-rate')
211 @when('pingpong.configured')
212 @when('actions.get-state')
215 cmd
= format_curl('GET', '/state')
217 result
, err
= charms
.sshproxy
._run
(cmd
)
218 except Exception as e
:
219 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
221 action_set({'stdout': result
,
224 remove_flag('actions.get-state')
227 @when('pingpong.configured')
228 @when('actions.get-stats')
231 cmd
= format_curl('GET', '/stats')
233 result
, err
= charms
.sshproxy
._run
(cmd
)
234 except Exception as e
:
235 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
237 action_set({'stdout': result
,
240 remove_flag('actions.get-stats')
243 @when('pingpong.configured')
244 @when('actions.start-traffic')
247 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : true}')
249 result
, err
= charms
.sshproxy
._run
(cmd
)
250 except Exception as e
:
251 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
253 action_set({'stdout': result
,
256 remove_flag('actions.start-traffic')
259 @when('pingpong.configured')
260 @when('actions.stop-traffic')
263 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : false}')
265 result
, err
= charms
.sshproxy
._run
(cmd
)
266 except Exception as e
:
267 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
269 action_set({'stdout': result
,
272 remove_flag('actions.stop-traffic')
275 def format_curl(method
, path
, data
=None):
276 """ A utility function to build the curl command line. """
278 # method must be GET or POST
279 if method
not in ['GET', 'POST']:
283 # Get our service info
289 # '-D', '/dev/stdout',
290 '-H', 'Accept: application/vnd.yang.data+xml',
291 '-H', 'Content-Type: application/vnd.yang.data+json',
294 if method
== "POST" and data
:
296 cmd
.append('{}'.format(data
))
299 'http://{}:{}/api/v1/{}{}'.format(host
, port
, mode
, path
)