e88bd4326a2c182385d4cee78c0d2bd95ba203ee
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
21 @when('config.changed')
23 if all(k
in cfg
for k
in ['mode']):
24 if cfg
['mode'] in ['ping', 'pong']:
25 set_flag('pingpong.configured')
26 status_set('active', 'ready!')
28 status_set('blocked', 'Waiting for configuration')
32 if cfg
['mode'] == 'ping':
48 @when('pingpong.configured')
49 @when('actions.start')
53 cmd
= "service {} start".format(cfg
['mode'])
54 result
, err
= charms
.sshproxy
._run
(cmd
)
56 action_fail('command failed:' + err
)
58 action_set({'outout': result
})
60 remove_flag('actions.start')
63 @when('pingpong.configured')
68 # Enter the command to stop your service(s)
69 cmd
= "service {} stop".format(cfg
['mode'])
70 result
, err
= charms
.sshproxy
._run
(cmd
)
72 action_fail('command failed:' + err
)
74 action_set({'outout': result
})
76 remove_flag('actions.stop')
79 @when('pingpong.configured')
80 @when('actions.restart')
84 # Enter the command to restart your service(s)
85 cmd
= "service {} restart".format(cfg
['mode'])
86 result
, err
= charms
.sshproxy
._run
(cmd
)
88 action_fail('command failed:' + err
)
90 action_set({'outout': result
})
92 remove_flag('actions.restart')
95 @when('pingpong.configured')
96 @when('actions.set-server')
100 # Get the target service info
101 target_ip
= action_get('server-ip')
102 target_port
= action_get('server-port')
104 data
= json
.dumps({'ip': target_ip
, 'port': target_port
})
112 result
, err
= charms
.sshproxy
._run
(cmd
)
113 except Exception as err
:
114 print("error: {0}".format(err
))
115 action_fail('command failed:' + err
)
117 action_set({'outout': result
})
119 remove_flag('actions.set-server')
122 @when('pingpong.configured')
123 @when('actions.set-rate')
128 rate
= action_get('rate')
129 cmd
= format_curl('POST', '/rate', '{"rate": {}}'.format(rate
))
131 result
, err
= charms
.sshproxy
._run
(cmd
)
133 action_fail('command failed:' + err
)
135 action_set({'outout': result
})
137 remove_flag('actions.set-rate')
140 @when('pingpong.configured')
141 @when('actions.get-rate')
146 cmd
= format_curl('GET', '/rate')
148 result
, err
= charms
.sshproxy
._run
(cmd
)
150 action_fail('command failed:' + err
)
152 action_set({'outout': result
})
154 remove_flag('actions.get-rate')
157 @when('pingpong.configured')
158 @when('actions.get-state')
162 cmd
= format_curl('GET', '/state')
164 result
, err
= charms
.sshproxy
._run
(cmd
)
166 action_fail('command failed:' + err
)
168 action_set({'outout': result
})
170 remove_flag('actions.get-state')
173 @when('pingpong.configured')
174 @when('actions.get-stats')
178 cmd
= format_curl('GET', '/stats')
180 result
, err
= charms
.sshproxy
._run
(cmd
)
182 action_fail('command failed:' + err
)
184 action_set({'outout': result
})
186 remove_flag('actions.get-stats')
189 @when('pingpong.configured')
190 @when('actions.start-ping')
194 cmd
= format_curl('POST', '/adminstatus/state', '{"enable":true}')
196 result
, err
= charms
.sshproxy
._run
(cmd
)
198 action_fail('command failed:' + err
)
200 action_set({'outout': result
})
202 remove_flag('actions.start-ping')
205 @when('pingpong.configured')
206 @when('actions.stop-ping')
210 cmd
= format_curl('POST', '/adminstatus/state', '{"enable":false}')
212 result
, err
= charms
.sshproxy
._run
(cmd
)
214 action_fail('command failed:' + err
)
216 action_set({'outout': result
})
218 remove_flag('actions.stop-ping')
221 def format_curl(method
, path
, data
=None):
222 """ A utility function to build the curl command line. """
224 # method must be GET or POST
225 if method
not in ['GET', 'POST']:
229 # Get our service info
230 host
= cfg
['ssh-hostname']
235 # '-D', '/dev/stdout',
236 '-H', '"Accept: application/vnd.yang.data+xml"',
237 '-H', '"Content-Type: application/vnd.yang.data+json"',
240 if method
== "POST" and data
:
242 cmd
.append("'{}'".format(data
))
245 'http://{}:{}/api/v1/{}{}'.format(host
, port
, mode
, path
)