forked from gandelman-a/ec2-resource-agents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticip
281 lines (249 loc) · 7.67 KB
/
elasticip
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
#
OCF_ROOT=/usr/lib/ocf
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
metadata() {
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="elasticip">
<version>1.0</version>
<longdesc lang="en">
Resource agnet for managing an EC2 Elastic IP. It relies on Tim Kay's perl aws client
available @ http://timkay.com/aws/.
</longdesc>
<shortdesc lang="en">Manage an EC2 elastic ip</shortdesc>
<parameters>
<parameter name="address" required="1">
<longdesc lang="en">
EC2 elastic IP address
</longdesc>
<shortdesc lang="en">ec2 ip</shortdesc>
<content type="string" default="" />
</parameter>
<parameter name="api_tools" required="1">
<longdesc lang="en">
API tools to use.
</longdesc>
<shortdesc lang="en">api tools</shortdesc>
<content type="string" default="aws" />
</parameter>
<parameter name="credentials" required="0">
<longdesc lang="en">
Location of file containing appropriate credentials.
</longdesc>
<shortdesc lang="en">credentials</shortdesc>
<content type="string" default="/root/.cred.txt" />
</parameter>
<parameter name="iptype" required="0">
<longdesc lang="en">
type of ip (vpc|classic).
</longdesc>
<shortdesc lang="en">iptype</shortdesc>
<content type="string" default="classic" />
</parameter>
</parameters>
<actions>
<action name="start" timeout="180" />
<action name="stop" timeout="180" />
<action name="notify" timeout="180" />
<action name="monitor" depth="0" timeout="30" interval="30" />
<action name="validate-all" timeout="5" />
<action name="meta-data" timeout="5" />
</actions>
</resource-agent>
END
}
debug=0
API_TIMEOUT=20
MAX_RETRY=10
AWS_COMMAND="aws"
AWS_TIMEOUT=60
debugger() {
[[ $debug != 0 ]] && ocf_log info $1
return $OCF_SUCCESS
}
ec2ip_metadata() {
EC2_INSTANCE_ID=$(ec2metadata --instance-id)
EC2_PUBLIC_IP=$(ec2metadata --public-ipv4)
}
ec2ip_load_credentials() {
debugger "load_credentials:"
local missing_cred
[[ ! -e $OCF_RESKEY_credentials ]] && ocf_log error "EC2: ERROR: Credentials file not found at $OCF_RESKEY_credentials" \
exit $OCF_ERR_INSTALLED
case $OCF_RESKEY_api_tools in
"ec2-api-tools")
. $OCF_RESKEY_credentials
[[ -z $EC2_PRIVATE_KEY ]] || \
[[ -z $EC2_CERT ]] || \
[[ -z $EC2_KEY ]] || \
[[ -z $EC2_KEYID ]] && missing_cred=1
export EC2_PRIVATE_KEY
export EC2_CERT
export EC2_KEY
export EC2_KEYID
debugger "- Loaded ec2-api-tools credentials"
debugger "-- EC2_PRIVATE_KEY = $EC2_PRIVATE_KEY"
debugger "-- EC2_CERT = $EC2_CERT"
debugger "-- EC2_KEY = $EC2_KEY"
debugger "-- EC2_KEYID= $EC2_KEYID"
;;
"aws") # AWS credentials get loaded via file, not environment
[[ $OCF_RESKEY_credentials != "/root/.awssecret" ]] && missing_cred=1
debugger "- Found aws secrets @ $OCF_RESKEY_credentials"
;;
"euca-2ools")
. $OCF_RESKEY_credentials
[[ -z $EC2_ACCESS_KEY ]] || \
[[ -z $EC2_SECRET_KEY ]] && missing_cred=1
# TODO, load this from file OR set default
export EC2_ACCESS_KEY
export EC2_SECRET_KEY
export EC2_URL="https://ec2.amazonaws.com"
debugger "- Loaded euca-2ools credentials"
debugger "-- EC2_ACCESS_KEY = $EC2_ACCESS_KEY"
debugger "-- EC2_SECRET_KEY = $EC2_SECRET_KEY"
;;
esac
[[ $missing_cred -eq 1 ]] && \
ocf_log error "EC2 ERROR: Missing credentials for $OCF_RESKEY_api_tools in $OCF_RESKEY_credentials" && \
exit $OCF_ERR_INSTALLED
return 0
}
ec2ip_validate() {
debugger "validate"
[[ -z $OCF_RESKEY_address ]] && ocf_log error "EC2 ERROR: address param not set $OCF_RESKEY_ADDRESS!" && exit $OCF_ERR_CONFIGURED
case $OCF_RESKEY_api_tools in
"ec2-api-tools")
ASSOCIATE_CMD="ec2-associate-address"
DISASSOCIATE_CMD="ec2-disassociate-address"
EC2_COMMANDS="$ASSOCIATE_CMD $DISASSOCIATE_CMD"
;;
"aws")
ASSOCIATE_CMD="aws associate-address"
DISASSOCIATE_CMD="aws disassociate-address"
EC2_COMMANDS="aws"
;;
"euca-2ools")
ASSOCIATE_CMD="euca-associate-address"
DISASSOCIATE_CMD="euca-disassociate-address"
EC2_COMMANDS="euca-associate-address euca-disassociate-address"
;;
*) ocf_log error "EC2 ERROR: Invalid api tools flavor: $OCF_RESKEY_api_tools" && exit $OCF_ERR_CONFIGURED
;;
esac
ec2ip_load_credentials
EC2_COMMANDS="$EC2_COMMANDS ec2metadata"
for i in $EC2_COMMANDS ; do
debugger "- Locating command $i: "
[[ ! -x $(which $i) ]] && ocf_log error "ERROR: Command $i not found/exectuable" && exit $OCF_ERR_INSTALLED
debugger "found"
done
}
ec2ip_monitor() {
debugger "monitor"
ec2ip_metadata
[[ $EC2_PUBLIC_IP == $OCF_RESKEY_address ]] && debugger "Running" && return $OCF_SUCCESS
debugger "Not running"
return $OCF_NOT_RUNNING
}
###### GENERIC
ec2ip_disassociate() {
debugger "ec2ip_disassociate: $DISASSOCIATE_CMD"
if [ "$OCF_RESKEY_iptype" == "vpc" ]; then
ec2ip_metadata
case $OCF_RESKEY_api_tools in
"euca-2ools")
l_cols=( `euca-describe-addresses --show-empty-fields --filter instance-id=$EC2_INSTANCE_ID $OCF_RESKEY_address` );
IP_eipassoc=${l_cols[5]}
;;
esac
$DISASSOCIATE_CMD --association-id $IP_eipassoc
else
$DISASSOCIATE_CMD $OCF_RESKEY_address
fi
if [ $? != 0 ]; then
debugger "- failed"
return $OCF_ERR_GENERIC
fi
debugger "- success"
return $OCF_SUCCESS
}
ec2ip_associate() {
debugger "ec2ip_associate: $ASSOCIATE_CMD"
if [ "$OCF_RESKEY_iptype" == "vpc" ]; then
ec2ip_metadata
case $OCF_RESKEY_api_tools in
"euca-2ools")
l_cols=( `euca-describe-addresses --show-empty-fields $OCF_RESKEY_address` );
IP_eipalloc=${l_cols[4]}
;;
esac
$ASSOCIATE_CMD -i $EC2_INSTANCE_ID --allocation-id $IP_eipalloc
else
$ASSOCIATE_CMD -i $EC2_INSTANCE_ID $OCF_RESKEY_address
fi
if [ $? != 0 ]; then
debugger "- failed, rc: $?"
return $OCF_ERR_GENERIC
fi
debugger "-success"
return $OCF_SUCCESS
}
ec2ip_stop() {
ocf_log info "EC2: Bringing down elastic ip $OCF_RESKEY_address"
local i
ec2ip_monitor
[[ $? == $OCF_NOT_RUNNING ]] && ocf_log info "EC2: Address $OCF_RESKEY_address already down" && return $OCF_SUCCESS
ocf_log info "EC2: Sending request to AWS via $OCF_RESKEY_api_tools"
ec2ip_disassociate
[[ $? != $OCF_SUCCESS ]] && return $OCF_ERR_GENERIC
i=0
ocf_log info "EC2: API request sent, waiting for IP to go down."
debugger "Stop loop"
while [[ $i -le $AWS_TIMEOUT ]] ; do
ec2ip_monitor
if [ $? == $OCF_NOT_RUNNING ]; then
ocf_log info "EC2: Successfully brought down $OCF_RESKEY_address"
return $OCF_SUCCESS
fi
sleep 1
i=$[$i+1]
done
ocf_log error "EC2: ERROR timeout reached ($AWS_TIMEOUT) while waiting for IP to get released."
return $OCF_ERR_GENERIC
}
ec2ip_start() {
local i
ocf_log info "EC2: Starting elastic ip $OCF_RESKEY_address"
ec2ip_monitor
[[ $? == $OCF_SUCCESS ]] && ocf_log info "EC2: $OCF_RESKEY_address already started" && return $OCF_SUCCESS
ocf_log info "EC2: Sending request to AWS via $OCF_RESKEY_api_tools"
ec2ip_associate
[[ $? != 0 ]] && echo "ERROR: Received $? from 'aws'" && return $OCF_ERR_GENERIC
i=0
ocf_log info "EC2: API request sent, waiting for IP."
while [[ $i -le $AWS_TIMEOUT ]] ; do
ec2ip_monitor
[[ $? == $OCF_SUCCESS ]] && return $?
sleep 1
i=$[$i+1]
done
ocf_log error "EC2: ERROR timeout reached ($AWS_TIMEOUT sec) while waiting for IP"
return $OCF_ERR_GENERIC
}
case $__OCF_ACTION in
meta-data) metadata
exit $OCF_SUCCESS;;
monitor)
ec2ip_monitor;;
stop)
ec2ip_validate && ec2ip_stop;;
validate-all) ec2ip_validate;;
start)
ec2ip_validate && ec2ip_start;;
*) exit $OCF_ERR_UNIMPLEMENTED;;
esac
exit $?