Debian Codegraph @

heartbeat/aws-vpc-move-ip

raw symbols SCIP
1 #!/bin/sh
2 #
3 #
4 # OCF resource agent to move an IP address within a VPC in the AWS
5 #
6 # Copyright (c) 2017 Markus Guertler (SUSE)
7 # Based on code of Adam Gandelman (GitHub ec2-resource-agents/elasticip)
8 # All Rights Reserved.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of version 2 of the GNU General Public License as
12 # published by the Free Software Foundation.
13 #
14 # This program is distributed in the hope that it would be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 #
18 # Further, this software is distributed without any warranty that it is
19 # free of the rightful claim of any third person regarding infringement
20 # or the like. Any license provided herein, whether implied or
21 # otherwise, applies only to this software file. Patent licenses, if
22 # any, provided herein do not apply to combinations of this program with
23 # other software, or any other product whatsoever.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write the Free Software Foundation,
27 # Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
28 #
29
30
31 #######################################################################
32 # Initialization:
33
34 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
35 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
36 . ${OCF_FUNCTIONS_DIR}/aws.sh
37
38 # Defaults
39 OCF_RESKEY_awscli_default="/usr/bin/aws"
40 OCF_RESKEY_auth_type_default="key"
41 OCF_RESKEY_profile_default="default"
42 OCF_RESKEY_region_default=""
43 OCF_RESKEY_ip_default=""
44 OCF_RESKEY_address_default=""
45 OCF_RESKEY_routing_table_default=""
46 OCF_RESKEY_routing_table_role_default=""
47 OCF_RESKEY_interface_default="eth0"
48 OCF_RESKEY_iflabel_default=""
49 OCF_RESKEY_monapi_default="false"
50 OCF_RESKEY_lookup_type_default="InstanceId"
51 OCF_RESKEY_awscli_timeout_default=""
52
53 : ${OCF_RESKEY_awscli=${OCF_RESKEY_awscli_default}}
54 : ${OCF_RESKEY_auth_type=${OCF_RESKEY_auth_type_default}}
55 : ${OCF_RESKEY_profile=${OCF_RESKEY_profile_default}}
56 : ${OCF_RESKEY_region=${OCF_RESKEY_region_default}}
57 : ${OCF_RESKEY_ip=${OCF_RESKEY_ip_default}}
58 : ${OCF_RESKEY_address=${OCF_RESKEY_address_default}}
59 : ${OCF_RESKEY_routing_table=${OCF_RESKEY_routing_table_default}}
60 : ${OCF_RESKEY_routing_table_role=${OCF_RESKEY_routing_table_role_default}}
61 : ${OCF_RESKEY_interface=${OCF_RESKEY_interface_default}}
62 : ${OCF_RESKEY_iflabel=${OCF_RESKEY_iflabel_default}}
63 : ${OCF_RESKEY_monapi=${OCF_RESKEY_monapi_default}}
64 : ${OCF_RESKEY_lookup_type=${OCF_RESKEY_lookup_type_default}}
65 : ${OCF_RESKEY_awscli_timeout=${OCF_RESKEY_awscli_timeout_default}}
66 #######################################################################
67
68
69 USAGE="usage: $0 {start|stop|status|meta-data}";
70 ###############################################################################
71
72
73 ###############################################################################
74 #
75 # Functions
76 #
77 ###############################################################################
78
79
80 metadata() {
81 cat <<END
82 <?xml version="1.0"?>
83 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
84 <resource-agent name="aws-vpc-move-ip" version="2.0">
85 <version>1.0</version>
86 <longdesc lang="en">
87 Resource Agent to move IP addresses within a VPC of the Amazon Webservices EC2
88 by changing an entry in an specific routing table
89
90 Credentials needs to be setup by running "aws configure", or by using AWS Policies.
91
92 See https://aws.amazon.com/cli/ for more information about awscli.
93 </longdesc>
94 <shortdesc lang="en">Move IP within a VPC of the AWS EC2</shortdesc>
95
96 <parameters>
97 <parameter name="awscli">
98 <longdesc lang="en">
99 Path to command line tools for AWS
100 </longdesc>
101 <shortdesc lang="en">Path to AWS CLI tools</shortdesc>
102 <content type="string" default="${OCF_RESKEY_awscli_default}" />
103 </parameter>
104
105 <parameter name="auth_type">
106 <longdesc lang="en">
107 Authentication type "key" for AccessKey and SecretAccessKey set via "aws configure",
108 or "role" to use AWS Policies.
109 </longdesc>
110 <shortdesc lang="en">Authentication type</shortdesc>
111 <content type="string" default="${OCF_RESKEY_auth_type_default}" />
112 </parameter>
113
114 <parameter name="profile">
115 <longdesc lang="en">
116 Valid AWS CLI profile name (see ~/.aws/config and 'aws configure')
117 </longdesc>
118 <shortdesc lang="en">profile name</shortdesc>
119 <content type="string" default="${OCF_RESKEY_profile_default}" />
120 </parameter>
121
122 <parameter name="region">
123 <longdesc lang="en">
124 Valid AWS region name (e.g., 'us-west-2')
125 </longdesc>
126 <shortdesc lang="en">region name</shortdesc>
127 <content type="string" default="${OCF_RESKEY_region_default}" />
128 </parameter>
129
130 <parameter name="ip" required="1">
131 <longdesc lang="en">
132 VPC private IP address
133 </longdesc>
134 <shortdesc lang="en">VPC private IP</shortdesc>
135 <content type="string" default="${OCF_RESKEY_ip_default}" />
136 </parameter>
137
138 <parameter name="address">
139 <longdesc lang="en">
140 Deprecated IP address param. Use the ip param instead.
141 </longdesc>
142 <shortdesc lang="en">Deprecated VPC private IP Address</shortdesc>
143 <content type="string" default="${OCF_RESKEY_address_default}" />
144 </parameter>
145
146 <parameter name="routing_table" required="1">
147 <longdesc lang="en">
148 Name of the routing table(s), where the route for the IP address should be changed. If declaring multiple routing tables they should be separated by comma. Example: rtb-XXXXXXXX,rtb-YYYYYYYYY
149 </longdesc>
150 <shortdesc lang="en">routing table name(s)</shortdesc>
151 <content type="string" default="${OCF_RESKEY_routing_table_default}" />
152 </parameter>
153
154 <parameter name="routing_table_role" required="0">
155 <longdesc lang="en">
156 Role to use to query/update the route table
157 </longdesc>
158 <shortdesc lang="en">route table query/update role</shortdesc>
159 <content type="string" default="${OCF_RESKEY_routing_table_role_default}" />
160 </parameter>
161
162 <parameter name="interface" required="0">
163 <longdesc lang="en">
164 Name of the network interface, i.e. eth0
165 </longdesc>
166 <shortdesc lang="en">network interface name</shortdesc>
167 <content type="string" default="${OCF_RESKEY_interface_default}" />
168 </parameter>
169
170 <parameter name="iflabel">
171 <longdesc lang="en">
172 You can specify an additional label for your IP address here.
173 This label is appended to your interface name.
174
175 The kernel allows alphanumeric labels up to a maximum length of 15
176 characters including the interface name and colon (e.g. eth0:foobar1234)
177 </longdesc>
178 <shortdesc lang="en">Interface label</shortdesc>
179 <content type="string" default="${OCF_RESKEY_iflabel_default}"/>
180 </parameter>
181
182 <parameter name="monapi">
183 <longdesc lang="en">
184 Enable enhanced monitoring using AWS API calls to check route table entry
185 </longdesc>
186 <shortdesc lang="en">Enhanced Monitoring</shortdesc>
187 <content type="boolean" default="${OCF_RESKEY_monapi_default}" />
188 </parameter>
189
190 <parameter name="lookup_type" required="0">
191 <longdesc lang="en">
192 Name of resource type to lookup in route table.
193 "InstanceId" : EC2 instance ID. (default)
194 "NetworkInterfaceId" : ENI ID. (useful in shared VPC setups).
195 </longdesc>
196 <shortdesc lang="en">lookup type for route table resource</shortdesc>
197 <content type="string" default="${OCF_RESKEY_lookup_type_default}" />
198 </parameter>
199
200 <parameter name="curl_retries" unique="0">
201 <longdesc lang="en">
202 curl retries before failing
203 </longdesc>
204 <shortdesc lang="en">curl retries</shortdesc>
205 <content type="integer" default="${OCF_RESKEY_curl_retries_default}" />
206 </parameter>
207
208 <parameter name="curl_sleep" unique="0">
209 <longdesc lang="en">
210 curl sleep between tries
211 </longdesc>
212 <shortdesc lang="en">curl sleep</shortdesc>
213 <content type="integer" default="${OCF_RESKEY_curl_sleep_default}" />
214 </parameter>
215
216 <parameter name="awscli_timeout" unique="0">
217 <longdesc lang="en">
218 awscli cli-connect-timeout value
219 </longdesc>
220 <shortdesc lang="en">awscli cli-connect-timeout</shortdesc>
221 <content type="integer" default="${OCF_RESKEY_awscli_timeout_default}" />
222 </parameter>
223
224 </parameters>
225
226 <actions>
227 <action name="start" timeout="180s" />
228 <action name="stop" timeout="180s" />
229 <action name="monitor" depth="0" timeout="30s" interval="60s" />
230 <action name="validate-all" timeout="5s" />
231 <action name="meta-data" timeout="5s" />
232 </actions>
233 </resource-agent>
234 END
235 }
236
237
238 execute_cmd_as_role(){
239 cmd=$1
240 role=$2
241 output="$($AWSCLI_CMD sts assume-role --role-arn $role --role-session-name AWSCLI-RouteTableUpdate --output=text)"
242 export AWS_ACCESS_KEY_ID="$(echo $output | awk -F" " '$4=="CREDENTIALS" {print $5}')"
243 export AWS_SECRET_ACCESS_KEY="$(echo $output | awk -F" " '$4=="CREDENTIALS" {print $7}')"
244 export AWS_SESSION_TOKEN="$(echo $output | awk -F" " '$4=="CREDENTIALS" {print $8}')"
245
246 #Execute command
247 ocf_log debug "Assumed Role ${role}"
248 ocf_log debug "$($OCF_RESKEY_awscli sts get-caller-identity)"
249 ocf_log debug "executing command: $cmd"
250 response="$($cmd)"
251 unset output AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
252 echo $response
253 }
254
255 ec2ip_set_address_param_compat(){
256 # Include backward compatibility for the deprecated address parameter
257 if [ -z "$OCF_RESKEY_ip" ] && [ -n "$OCF_RESKEY_address" ]; then
258 OCF_RESKEY_ip="$OCF_RESKEY_address"
259 fi
260 }
261
262 ec2ip_validate() {
263 for cmd in "$OCF_RESKEY_awscli" ip curl; do
264 check_binary "$cmd"
265 done
266
267 if [ "x${OCF_RESKEY_auth_type}" = "xkey" ] && [ -z "$OCF_RESKEY_profile" ]; then
268 ocf_exit_reason "profile parameter not set"
269 return $OCF_ERR_CONFIGURED
270 fi
271
272 if [ -n "$OCF_RESKEY_iflabel" ]; then
273 label=${OCF_RESKEY_interface}:${OFC_RESKEY_iflabel}
274 if [ ${#label} -gt 15 ]; then
275 ocf_exit_reason "Interface label [$label] exceeds maximum character limit of 15"
276 exit $OCF_ERR_CONFIGURED
277 fi
278 fi
279
280 TOKEN=$(get_token)
281 [ $? -ne 0 ] && exit $OCF_ERR_GENERIC
282 EC2_INSTANCE_ID=$(get_instance_id)
283 [ $? -ne 0 ] && exit $OCF_ERR_GENERIC
284
285 if [ -z "${EC2_INSTANCE_ID}" ]; then
286 ocf_exit_reason "Instance ID not found. Is this a EC2 instance?"
287 return $OCF_ERR_GENERIC
288 fi
289
290 return $OCF_SUCCESS
291 }
292
293 ec2ip_monitor() {
294 MON_RES=""
295 if [ "${OCF_RESKEY_lookup_type}" = "NetworkInterfaceId" ]; then
296 EC2_ID="$(ec2ip_get_instance_eni)"
297 RESOURCE_TYPE="interface"
298 else
299 EC2_ID="$EC2_INSTANCE_ID"
300 RESOURCE_TYPE="instance"
301 fi
302
303 if ocf_is_true ${OCF_RESKEY_monapi} || [ "$__OCF_ACTION" = "start" ] || ocf_is_probe; then
304 for rtb in $(echo $OCF_RESKEY_routing_table | sed -e 's/,/ /g'); do
305 ocf_log info "monitor: check routing table (API call) - $rtb"
306 if [ -z "${OCF_RESKEY_routing_table_role}" ]; then
307 cmd="$AWSCLI_CMD --output text ec2 describe-route-tables --route-table-ids $rtb --query RouteTables[*].Routes[?DestinationCidrBlock=='$OCF_RESKEY_ip/32'].$OCF_RESKEY_lookup_type"
308 ocf_log debug "executing command: $cmd"
309 ROUTE_TO_INSTANCE="$($cmd)"
310 else
311 cmd="$OCF_RESKEY_awscli $region_opt --output text ec2 describe-route-tables --route-table-ids $rtb --query RouteTables[*].Routes[?DestinationCidrBlock=='$OCF_RESKEY_ip/32'].$OCF_RESKEY_lookup_type"
312 ROUTE_TO_INSTANCE="$(execute_cmd_as_role "$cmd" $OCF_RESKEY_routing_table_role)"
313 fi
314 ocf_log debug "Overlay IP is currently routed to ${ROUTE_TO_INSTANCE}"
315 if [ -z "$ROUTE_TO_INSTANCE" ]; then
316 ROUTE_TO_INSTANCE="<unknown>"
317 fi
318
319 if [ "$EC2_ID" != "$ROUTE_TO_INSTANCE" ]; then
320 ocf_log warn "not routed to this $RESOURCE_TYPE ($EC2_ID) but to $RESOURCE_TYPE $ROUTE_TO_INSTANCE on $rtb"
321 MON_RES="$MON_RES $rtb"
322 fi
323 sleep 1
324 done
325
326 if [ ! -z "$MON_RES" ]; then
327 return $OCF_NOT_RUNNING
328 fi
329
330 else
331 ocf_log debug "monitor: Enhanced Monitoring disabled - omitting API call"
332 fi
333
334 cmd="ip addr show dev $OCF_RESKEY_interface to $OCF_RESKEY_ip up"
335 ocf_log debug "executing command: $cmd"
336 RESULT=$($cmd | grep "$OCF_RESKEY_ip")
337 if [ -z "$RESULT" ]; then
338 if [ "$__OCF_ACTION" = "monitor" ] && ! ocf_is_probe; then
339 level="error"
340 else
341 level="info"
342 fi
343
344 ocf_log "$level" "IP $OCF_RESKEY_ip not assigned to interface $OCF_RESKEY_interface"
345 return $OCF_NOT_RUNNING
346 fi
347
348 ocf_log debug "route in VPC and address assigned"
349 return $OCF_SUCCESS
350 }
351
352
353 ec2ip_drop() {
354 cmd="ip addr delete ${OCF_RESKEY_ip}/32 dev $OCF_RESKEY_interface"
355 ocf_log debug "executing command: $cmd"
356 output=$($cmd 2>&1)
357 rc=$?
358
359 if [ "$rc" -gt 0 ]; then
360 if [ "$__OCF_ACTION" = "start" ]; then
361 # expected to fail during start
362 level="debug"
363 else
364 level="warn"
365 fi
366
367 ocf_log "$level" "command failed, rc $rc"
368 ocf_log "$level" "output/error: $output"
369 return $OCF_ERR_GENERIC
370 else
371 ocf_log debug "output/error: $output"
372 fi
373
374 # delete remaining route-entries if any
375 ip route show to exact ${OCF_RESKEY_ip}/32 dev $OCF_RESKEY_interface | xargs -r ip route delete
376 ip route show table local to exact ${OCF_RESKEY_ip}/32 dev $OCF_RESKEY_interface | xargs -r ip route delete
377
378 return $OCF_SUCCESS
379 }
380
381 ec2ip_get_instance_eni() {
382 MAC_ADDR=$(get_interface_mac)
383 ocf_log debug "MAC address associated with interface ${OCF_RESKEY_interface}: ${MAC_ADDR}"
384
385 cmd="curl_retry \"$OCF_RESKEY_curl_retries\" \"$OCF_RESKEY_curl_sleep\" \"--show-error -s -H 'X-aws-ec2-metadata-token: $TOKEN'\" \"http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC_ADDR}/interface-id\""
386 EC2_NETWORK_INTERFACE_ID="$(eval $cmd)"
387 [ $? -ne 0 ] && exit $OCF_ERR_GENERIC
388 ocf_log debug "network interface id associated MAC address ${MAC_ADDR}: ${EC2_NETWORK_INTERFACE_ID}"
389 echo $EC2_NETWORK_INTERFACE_ID
390 }
391
392 ec2ip_get_and_configure() {
393 EC2_NETWORK_INTERFACE_ID="$(ec2ip_get_instance_eni)"
394 for rtb in $(echo $OCF_RESKEY_routing_table | sed -e 's/,/ /g'); do
395 if [ -z "${OCF_RESKEY_routing_table_role}" ]; then
396 cmd="$AWSCLI_CMD --output text ec2 replace-route --route-table-id $rtb --destination-cidr-block ${OCF_RESKEY_ip}/32 --network-interface-id $EC2_NETWORK_INTERFACE_ID"
397 ocf_log debug "executing command: $cmd"
398 $cmd
399 else
400 cmd="$OCF_RESKEY_awscli $region_opt --output text ec2 replace-route --route-table-id $rtb --destination-cidr-block ${OCF_RESKEY_ip}/32 --network-interface-id $EC2_NETWORK_INTERFACE_ID"
401 update_response="$(execute_cmd_as_role "$cmd" $OCF_RESKEY_routing_table_role)"
402 fi
403 rc=$?
404 if [ "$rc" != 0 ]; then
405 ocf_log warn "command failed, rc: $rc"
406 return $OCF_ERR_GENERIC
407 fi
408 sleep 1
409 done
410
411 # Reconfigure the local ip address
412 ec2ip_drop
413
414 extra_opts=""
415 if [ -n "$OCF_RESKEY_iflabel" ]; then
416 extra_opts="$extra_opts label $OCF_RESKEY_interface:$OCF_RESKEY_iflabel"
417 fi
418
419 cmd="ip addr add ${OCF_RESKEY_ip}/32 dev $OCF_RESKEY_interface $extra_opts"
420 ocf_log debug "executing command: $cmd"
421 $cmd
422 rc=$?
423 if [ $rc != 0 ]; then
424 ocf_log warn "command failed, rc: $rc"
425 return $OCF_ERR_GENERIC
426 fi
427
428 return $OCF_SUCCESS
429 }
430
431 ec2ip_stop() {
432 ocf_log info "EC2: Bringing down IP address $OCF_RESKEY_ip"
433
434 ec2ip_monitor
435 if [ $? = $OCF_NOT_RUNNING ]; then
436 ocf_log info "EC2: Address $OCF_RESKEY_ip already down"
437 return $OCF_SUCCESS
438 fi
439
440 ec2ip_drop
441 if [ $? != $OCF_SUCCESS ]; then
442 return $OCF_ERR_GENERIC
443 fi
444
445 ec2ip_monitor
446 if [ $? != $OCF_NOT_RUNNING ]; then
447 ocf_log error "EC2: Couldn't bring down IP address $OCF_RESKEY_ip on interface $OCF_RESKEY_interface."
448 return $OCF_ERR_GENERIC
449 fi
450
451 ocf_log info "EC2: Successfully brought down $OCF_RESKEY_ip"
452 return $OCF_SUCCESS
453 }
454
455 ec2ip_start() {
456 ocf_log info "EC2: Moving IP address $OCF_RESKEY_ip to this host by adjusting routing table $OCF_RESKEY_routing_table"
457
458 ec2ip_monitor
459 if [ $? = $OCF_SUCCESS ]; then
460 ocf_log info "EC2: $OCF_RESKEY_ip already started"
461 return $OCF_SUCCESS
462 fi
463
464 ocf_log info "EC2: Adjusting routing table and locally configuring IP address"
465 ec2ip_get_and_configure
466 rc=$?
467 if [ $rc != $OCF_SUCCESS ]; then
468 ocf_log error "Received $rc from 'aws'"
469 return $OCF_ERR_GENERIC
470 fi
471
472 ec2ip_monitor
473 if [ $? != $OCF_SUCCESS ]; then
474 ocf_log error "EC2: IP address couldn't be configured on this host (IP: $OCF_RESKEY_ip, Interface: $OCF_RESKEY_interface)"
475 return $OCF_ERR_GENERIC
476 fi
477
478 return $OCF_SUCCESS
479 }
480
481 ###############################################################################
482 #
483 # MAIN
484 #
485 ###############################################################################
486
487 case $__OCF_ACTION in
488 meta-data)
489 metadata
490 exit $OCF_SUCCESS
491 ;;
492 usage|help)
493 echo $USAGE
494 exit $OCF_SUCCESS
495 ;;
496 esac
497
498 if ! ocf_is_root; then
499 ocf_log err "You must be root for $__OCF_ACTION operation."
500 exit $OCF_ERR_PERM
501 fi
502
503 if [ -n "${OCF_RESKEY_awscli_timeout}" ]; then
504 AWSCLI_CMD="${OCF_RESKEY_awscli} --cli-connect-timeout ${OCF_RESKEY_awscli_timeout}"
505 else
506 AWSCLI_CMD="${OCF_RESKEY_awscli}"
507 fi
508 if [ "x${OCF_RESKEY_auth_type}" = "xkey" ]; then
509 AWSCLI_CMD="$AWSCLI_CMD --profile ${OCF_RESKEY_profile}"
510 elif [ "x${OCF_RESKEY_auth_type}" = "xrole" ]; then
511 if [ -z "${OCF_RESKEY_region}" ]; then
512 ocf_exit_reason "region needs to be set when using role-based authentication"
513 exit $OCF_ERR_CONFIGURED
514 fi
515 else
516 ocf_exit_reason "Incorrect auth_type: ${OCF_RESKEY_auth_type}"
517 exit $OCF_ERR_CONFIGURED
518 fi
519 if [ -n "${OCF_RESKEY_region}" ]; then
520 AWSCLI_CMD="$AWSCLI_CMD --region ${OCF_RESKEY_region}"
521 fi
522
523 ec2ip_set_address_param_compat
524
525 ec2ip_validate
526
527 case $__OCF_ACTION in
528 start)
529 ec2ip_start;;
530 stop)
531 ec2ip_stop;;
532 monitor)
533 ec2ip_monitor;;
534 validate-all)
535 exit $?;;
536 *)
537 echo $USAGE
538 exit $OCF_ERR_UNIMPLEMENTED
539 ;;
540 esac