-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatingIP
executable file
·247 lines (215 loc) · 5.76 KB
/
FloatingIP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/sh
#
# Resource Agent for managing hetzner cloud floating ip resources.
#
# License: MIT License (MIT)
# (c) 2018-2018 Sven Speckmaier
#
# Thank you to Tuomo Soini and Lars Marowsky-Brée for writing the IPaddr2
# ressource agent which served as reference and inspiration for this
#
#######################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
: OCF_RESKEY_ip=
: OCF_RESKEY_api_token=
FLOATING_IP=${OCF_RESKEY_ip}
API_TOKEN=${OCF_RESKEY_api_token}
#######################################################################
#######################################################################
# Helper:
ip_init() {
SERVER_ID="$(server_id)"
FLOATING_IP_ID="$(ip_to_id)"
}
server_id() {
if ( curl -f -s -H "Authorization: Bearer $API_TOKEN" \
"https://api.hetzner.cloud/v1/servers?name=$(hostname)" | \
python3 -c "import sys, json;
jsonData = json.load(sys.stdin)
servers = jsonData['servers']
if len(servers) < 1:
sys.exit(1)
print(servers[0]['id'])
sys.exit(0)
" ) ; then
return 0
fi
return 1
}
ip_to_id() {
if ( curl -f -s -H "Authorization: Bearer $API_TOKEN" \
https://api.hetzner.cloud/v1/floating_ips | \
python3 -c "import sys, json, socket;
jsonData = json.load(sys.stdin)
floatingIps = jsonData['floating_ips']
for floatingIp in floatingIps:
if floatingIp['ip'] == '${FLOATING_IP}':
print(floatingIp['id'])
sys.exit(0)
sys.exit(1)
" ) ; then
return 0
fi
return 1
}
ip_served() {
if ( curl -f -s -H "Authorization: Bearer $API_TOKEN" \
https://api.hetzner.cloud/v1/floating_ips | \
python3 -c "import sys, json, socket;
jsonData = json.load(sys.stdin)
expectedServerId = ${SERVER_ID}
floatingIps = jsonData['floating_ips']
for floatingIp in floatingIps:
if floatingIp['ip'] == '${FLOATING_IP}':
if floatingIp['server'] == expectedServerId:
print('ok')
sys.exit(0)
else:
print('no')
sys.exit(1)
print('not found')
sys.exit(2)
" ) ; then
return 0
fi
return 1
}
ip_validate() {
check_binary curl
check_binary python3
if [ -z "$SERVER_ID" ] ; then
echo "hostname $(hostname) not found in cloud api"
exit $OCF_ERR_CONFIGURED
fi
if [ -z "$FLOATING_IP_ID" ] ; then
echo "floating_ip $FLOATING_IP not found in cloud api"
exit $OCF_ERR_CONFIGURED
fi
}
#######################################################################
#######################################################################
# Actions:
ip_usage() {
cat <<END
usage: $0 {start|stop|status|monitor|validate-all|meta-data}
Expects to have a fully populated OCF RA-compliant environment set.
END
}
ip_start() {
while ! ( curl -f -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
-d '{"server":'$SERVER_ID'}' \
"https://api.hetzner.cloud/v1/floating_ips/${FLOATING_IP_ID}/actions/assign" \
> /dev/null ) ; do
sleep 1s
done
exit $OCF_SUCCESS
}
ip_stop() {
if ! ip_served ; then
exit $OCF_SUCCESS
fi
while ! ocf_run -q curl -f -s -X POST -H "Content-Type: application/json" -H \
"Authorization: Bearer $API_TOKEN" \
"https://api.hetzner.cloud/v1/floating_ips/${FLOATING_IP_ID}/actions/unassign"
do sleep 1s
done
exit $OCF_SUCCESS
}
ip_monitor() {
local ip_status="$(ip_served)"
case "${ip_status}" in
"ok")
exit $OCF_SUCCESS
;;
"no")
exit $OCF_NOT_RUNNING
;;
"not found"|*)
exit $OCF_ERR_GENERIC
;;
esac
}
meta_data() {
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="FloatingIP" version="0.1">
<version>0.1</version>
<longdesc lang="en">
Manage a hetzner cloud floating ip as cloud ressource.
Only routing the ip to this server is taken care of. Pair up with a IPaddr2
resource to properly activate the floating ip.
WARNING: This resource assumes that the hostname on the machine is the same as
its name in the Cloud Api
</longdesc>
<shortdesc lang="en">Manage a hetzner cloud floating ip as cloud ressource.</shortdesc>
<parameters>
<parameter name="ip" unique="1" required="1">
<longdesc lang="en">
The ip address to route to this host
e.g. 66.77.88.99
</longdesc>
<shortdesc lang="en">ip address</shortdesc>
<content type="string"/>
</parameter>
<parameter name="api_token" unique="0" required="1">
<longdesc lang="en">
The api token to access the hetzner cloud api. Created in Access/Api-Tokens under
https://console.hetzner.cloud</longdesc>
<shortdesc lang="en">Token for hetzner cloud api</shortdesc>
<content type="string" default="false"/>
</parameter>
</parameters>
<actions>
<action name="start" timeout="20" />
<action name="stop" timeout="20" />
<action name="monitor" timeout="20"
interval="10" depth="0" />
<action name="reload" timeout="20" />
<action name="migrate_to" timeout="20" />
<action name="migrate_from" timeout="20" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="20" />
</actions>
</resource-agent>
END
}
#######################################################################
#######################################################################
# Entrypoint:
case $__OCF_ACTION in
meta-data) meta_data
exit $OCF_SUCCESS
;;
usage|help) ip_usage
exit $OCF_SUCCESS
;;
esac
ip_init
ip_validate
case $__OCF_ACTION in
start) ip_start
;;
stop) ip_stop
;;
status) ip_status=`ip_served`
if [ $ip_status = "ok" ]; then
echo "running"
exit $OCF_SUCCESS
else
echo "stopped"
exit $OCF_NOT_RUNNING
fi
;;
monitor) ip_monitor
;;
validate-all) ;;
*) ip_usage
exit $OCF_ERR_UNIMPLEMENTED
;;
esac
#######################################################################