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('config.changed', 'sshproxy.configured')
28 """Verify the configuration.
29 Verify that the charm has been configured
31 (validated
, output
) = charms
.sshproxy
.verify_ssh_credentials()
33 status_set('blocked', 'Unable to verify SSH credentials: {}'.format(
36 if all(k
in cfg
for k
in ['mode']):
37 if cfg
['mode'] in ['ping', 'pong']:
38 set_flag('pingpong.configured')
39 status_set('active', 'ready!')
41 status_set('blocked', 'Waiting for configuration')
44 @when('config.changed')
45 @when_not('sshproxy.configured')
46 def invalid_credentials():
47 status_set('blocked', 'Waiting for SSH credentials.')
52 if cfg
['mode'] == 'ping':
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 timeout 5 /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')
82 # Attempt to raise the non-mgmt interface, but ignore failures if
83 # the interface is already up.
85 cmd
= "sudo timeout 30 /sbin/ifup eth1"
86 result
, err
= charms
.sshproxy
._run
(cmd
)
87 except Exception as e
:
91 cmd
= "sudo timeout 30 /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 timeout 30 /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 timeout 30 /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
= charms
.sshproxy
._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
= charms
.sshproxy
._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
= charms
.sshproxy
._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
= charms
.sshproxy
._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
= charms
.sshproxy
._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
= charms
.sshproxy
._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
= charms
.sshproxy
._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
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
)