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.
29 Check the current values in config to see if we have enough
30 information to continue."""
34 @when('config.changed', 'sshproxy.configured')
36 """Verify the configuration.
37 Verify that the charm has been configured
39 status_set('maintenance', 'Verifying configuration data...')
40 (validated
, output
) = charms
.sshproxy
.verify_ssh_credentials()
42 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
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!')
50 status_set('blocked', 'Waiting for configuration')
53 @when('config.changed')
54 @when_not('sshproxy.configured')
55 def invalid_credentials():
56 status_set('blocked', 'Waiting for SSH credentials.')
61 if cfg
['mode'] == 'ping':
77 @when('pingpong.configured')
78 @when('actions.start')
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
:
87 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
88 remove_flag('actions.start')
91 # Attempt to raise the non-mgmt interface, but ignore failures if
92 # the interface is already up.
94 cmd
= "sudo timeout 30 /sbin/ifup eth1"
95 result
, err
= charms
.sshproxy
._run
(cmd
)
96 except Exception as e
:
100 cmd
= "sudo timeout 30 /usr/bin/systemctl start {}". \
102 result
, err
= charms
.sshproxy
._run
(cmd
)
103 except Exception as e
:
104 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
106 action_set({'stdout': result
,
109 remove_flag('actions.start')
112 @when('pingpong.configured')
113 @when('actions.stop')
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
))
122 action_set({'stdout': result
,
125 remove_flag('actions.stop')
128 @when('pingpong.configured')
129 @when('actions.restart')
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
))
138 action_set({'stdout': result
,
141 remove_flag('actions.restart')
144 @when('pingpong.configured')
145 @when('actions.set-server')
148 # Get the target service info
149 target_ip
= action_get('server-ip')
150 target_port
= action_get('server-port')
152 data
= '{{"ip" : "{}", "port" : {} }}'. \
153 format(target_ip
, target_port
)
161 result
, err
= charms
.sshproxy
._run
(cmd
)
162 except Exception as e
:
163 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
165 action_set({'stdout': result
,
168 remove_flag('actions.set-server')
171 @when('pingpong.configured')
172 @when('actions.set-rate')
176 rate
= action_get('rate')
177 cmd
= format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate
))
179 result
, err
= charms
.sshproxy
._run
(cmd
)
180 except Exception as e
:
182 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
184 action_set({'stdout': result
,
187 remove_flag('actions.set-rate')
190 @when('pingpong.configured')
191 @when('actions.get-rate')
195 cmd
= format_curl('GET', '/rate')
197 result
, err
= charms
.sshproxy
._run
(cmd
)
198 except Exception as e
:
199 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
201 action_set({'stdout': result
,
204 remove_flag('actions.get-rate')
207 @when('pingpong.configured')
208 @when('actions.get-state')
211 cmd
= format_curl('GET', '/state')
213 result
, err
= charms
.sshproxy
._run
(cmd
)
214 except Exception as e
:
215 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
217 action_set({'stdout': result
,
220 remove_flag('actions.get-state')
223 @when('pingpong.configured')
224 @when('actions.get-stats')
227 cmd
= format_curl('GET', '/stats')
229 result
, err
= charms
.sshproxy
._run
(cmd
)
230 except Exception as e
:
231 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
233 action_set({'stdout': result
,
236 remove_flag('actions.get-stats')
239 @when('pingpong.configured')
240 @when('actions.start-traffic')
243 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : true}')
245 result
, err
= charms
.sshproxy
._run
(cmd
)
246 except Exception as e
:
247 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
249 action_set({'stdout': result
,
252 remove_flag('actions.start-traffic')
255 @when('pingpong.configured')
256 @when('actions.stop-traffic')
259 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : false}')
261 result
, err
= charms
.sshproxy
._run
(cmd
)
262 except Exception as e
:
263 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
265 action_set({'stdout': result
,
268 remove_flag('actions.stop-traffic')
271 def format_curl(method
, path
, data
=None):
272 """ A utility function to build the curl command line. """
274 # method must be GET or POST
275 if method
not in ['GET', 'POST']:
279 # Get our service info
285 # '-D', '/dev/stdout',
286 '-H', 'Accept: application/vnd.yang.data+xml',
287 '-H', 'Content-Type: application/vnd.yang.data+json',
290 if method
== "POST" and data
:
292 cmd
.append('{}'.format(data
))
295 'http://{}:{}/api/v1/{}{}'.format(host
, port
, mode
, path
)