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
44 status_set('maintenance', 'Verifying configuration data...')
46 (validated
, output
) = charms
.sshproxy
.verify_ssh_credentials()
48 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
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!')
58 status_set('blocked', 'Waiting for configuration')
60 except Exception as err
:
61 status_set('blocked', 'Waiting for valid configuration ({})'.format(err
))
64 @when('config.changed')
65 @when_not('sshproxy.configured')
66 def invalid_credentials():
67 status_set('blocked', 'Waiting for SSH credentials.')
72 if cfg
['mode'] == 'ping':
88 @when('pingpong.configured')
89 @when('actions.start')
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
:
98 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
99 remove_flag('actions.start')
102 # Attempt to raise the non-mgmt interface, but ignore failures if
103 # the interface is already up.
105 cmd
= "sudo timeout 30 /sbin/ifup eth1"
106 result
, err
= charms
.sshproxy
._run
(cmd
)
107 except Exception as e
:
111 cmd
= "sudo timeout 30 /usr/bin/systemctl start {}". \
113 result
, err
= charms
.sshproxy
._run
(cmd
)
114 except Exception as e
:
115 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
117 action_set({'stdout': result
,
120 remove_flag('actions.start')
123 @when('pingpong.configured')
124 @when('actions.stop')
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
))
133 action_set({'stdout': result
,
136 remove_flag('actions.stop')
139 @when('pingpong.configured')
140 @when('actions.restart')
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
))
149 action_set({'stdout': result
,
152 remove_flag('actions.restart')
155 @when('pingpong.configured')
156 @when('actions.set-server')
159 # Get the target service info
160 target_ip
= action_get('server-ip')
161 target_port
= action_get('server-port')
163 data
= '{{"ip" : "{}", "port" : {} }}'. \
164 format(target_ip
, target_port
)
172 result
, err
= charms
.sshproxy
._run
(cmd
)
173 except Exception as e
:
174 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
176 action_set({'stdout': result
,
179 remove_flag('actions.set-server')
182 @when('pingpong.configured')
183 @when('actions.set-rate')
187 rate
= action_get('rate')
188 cmd
= format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate
))
190 result
, err
= charms
.sshproxy
._run
(cmd
)
191 except Exception as e
:
193 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
195 action_set({'stdout': result
,
198 remove_flag('actions.set-rate')
201 @when('pingpong.configured')
202 @when('actions.get-rate')
206 cmd
= format_curl('GET', '/rate')
208 result
, err
= charms
.sshproxy
._run
(cmd
)
209 except Exception as e
:
210 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
212 action_set({'stdout': result
,
215 remove_flag('actions.get-rate')
218 @when('pingpong.configured')
219 @when('actions.get-state')
222 cmd
= format_curl('GET', '/state')
224 result
, err
= charms
.sshproxy
._run
(cmd
)
225 except Exception as e
:
226 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
228 action_set({'stdout': result
,
231 remove_flag('actions.get-state')
234 @when('pingpong.configured')
235 @when('actions.get-stats')
238 cmd
= format_curl('GET', '/stats')
240 result
, err
= charms
.sshproxy
._run
(cmd
)
241 except Exception as e
:
242 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
244 action_set({'stdout': result
,
247 remove_flag('actions.get-stats')
250 @when('pingpong.configured')
251 @when('actions.start-traffic')
254 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : true}')
256 result
, err
= charms
.sshproxy
._run
(cmd
)
257 except Exception as e
:
258 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
260 action_set({'stdout': result
,
263 remove_flag('actions.start-traffic')
266 @when('pingpong.configured')
267 @when('actions.stop-traffic')
270 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : false}')
272 result
, err
= charms
.sshproxy
._run
(cmd
)
273 except Exception as e
:
274 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
276 action_set({'stdout': result
,
279 remove_flag('actions.stop-traffic')
282 def format_curl(method
, path
, data
=None):
283 """ A utility function to build the curl command line. """
285 # method must be GET or POST
286 if method
not in ['GET', 'POST']:
290 # Get our service info
296 # '-D', '/dev/stdout',
297 '-H', 'Accept: application/vnd.yang.data+xml',
298 '-H', 'Content-Type: application/vnd.yang.data+json',
301 if method
== "POST" and data
:
303 cmd
.append('{}'.format(data
))
306 'http://{}:{}/api/v1/{}{}'.format(host
, port
, mode
, path
)