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
15 from subprocess
import (
25 @when('config.changed')
27 if all(k
in cfg
for k
in ['mode']):
28 if cfg
['mode'] in ['ping', 'pong']:
29 set_flag('pingpong.configured')
30 status_set('active', 'ready!')
32 status_set('blocked', 'Waiting for configuration')
36 if cfg
['mode'] == 'ping':
52 @when('pingpong.configured')
53 @when('actions.start')
56 # Bring up the eth1 interface.
57 # The selinux label on the file needs to be set correctly
58 cmd
= "sudo /sbin/restorecon -v /etc/sysconfig/network-scripts/ifcfg-eth1"
59 result
, err
= charms
.sshproxy
._run
(cmd
)
60 except Exception as e
:
62 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
63 remove_flag('actions.start')
67 cmd
= "sudo /sbin/ifup eth1"
68 result
, err
= charms
.sshproxy
._run
(cmd
)
69 except Exception as e
:
70 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
71 remove_flag('actions.start')
75 cmd
= "sudo /usr/bin/systemctl start {}". \
77 result
, err
= charms
.sshproxy
._run
(cmd
)
78 except Exception as e
:
79 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
81 action_set({'stdout': result
,
84 remove_flag('actions.start')
87 @when('pingpong.configured')
91 # Enter the command to stop your service(s)
92 cmd
= "sudo /usr/bin/systemctl stop {}".format(cfg
['mode'])
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.stop')
103 @when('pingpong.configured')
104 @when('actions.restart')
107 # Enter the command to restart your service(s)
108 cmd
= "sudo /usr/bin/systemctl restart {}".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.restart')
119 @when('pingpong.configured')
120 @when('actions.set-server')
123 # Get the target service info
124 target_ip
= action_get('server-ip')
125 target_port
= action_get('server-port')
127 data
= '{{"ip" : "{}", "port" : {} }}'. \
128 format(target_ip
, target_port
)
136 result
, err
= charms
.sshproxy
._run
(cmd
)
137 except Exception as e
:
138 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
140 action_set({'stdout': result
,
143 remove_flag('actions.set-server')
146 @when('pingpong.configured')
147 @when('actions.set-rate')
151 rate
= action_get('rate')
152 cmd
= format_curl('POST', '/rate', '{{"rate" : {}}}'.format(rate
))
154 result
, err
= charms
.sshproxy
._run
(cmd
)
155 except Exception as e
:
157 action_fail('command failed: {}, errors: {}'.format(err
, e
.output
))
159 action_set({'stdout': result
,
162 remove_flag('actions.set-rate')
165 @when('pingpong.configured')
166 @when('actions.get-rate')
170 cmd
= format_curl('GET', '/rate')
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.get-rate')
182 @when('pingpong.configured')
183 @when('actions.get-state')
186 cmd
= format_curl('GET', '/state')
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-state')
198 @when('pingpong.configured')
199 @when('actions.get-stats')
202 cmd
= format_curl('GET', '/stats')
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-stats')
214 @when('pingpong.configured')
215 @when('actions.start-traffic')
218 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : true}')
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.start-traffic')
230 @when('pingpong.configured')
231 @when('actions.stop-traffic')
234 cmd
= format_curl('POST', '/adminstatus/state', '{"enable" : false}')
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.stop-traffic')
246 def format_curl(method
, path
, data
=None):
247 """ A utility function to build the curl command line. """
249 # method must be GET or POST
250 if method
not in ['GET', 'POST']:
254 # Get our service info
260 # '-D', '/dev/stdout',
261 '-H', 'Accept: application/vnd.yang.data+xml',
262 '-H', 'Content-Type: application/vnd.yang.data+json',
265 if method
== "POST" and data
:
267 cmd
.append('{}'.format(data
))
270 'http://{}:{}/api/v1/{}{}'.format(host
, port
, mode
, path
)