| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# |
| 4 |
# Manage Elastic IP with Pacemaker |
| 5 |
# |
| 6 |
# |
| 7 |
# Copyright 2016-2018 guessi <[email protected]> |
| 8 |
# |
| 9 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 |
# you may not use this file except in compliance with the License. |
| 11 |
# You may obtain a copy of the License at |
| 12 |
# |
| 13 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
# |
| 15 |
# Unless required by applicable law or agreed to in writing, software |
| 16 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
# See the License for the specific language governing permissions and |
| 19 |
# limitations under the License. |
| 20 |
# |
| 21 |
# |
| 22 |
|
| 23 |
# |
| 24 |
# Prerequisites: |
| 25 |
# |
| 26 |
# - preconfigured AWS CLI running environment (AccessKey, SecretAccessKey, etc.) or |
| 27 |
# (AWSRole) Setup up relevant AWS Policies to allow agent related functions to be executed. |
| 28 |
# - a reserved secondary private IP address for EC2 instances high availability |
| 29 |
# - IAM user role with the following permissions: |
| 30 |
# * DescribeInstances |
| 31 |
# * AssociateAddress |
| 32 |
# * DescribeAddresses |
| 33 |
# * DisassociateAddress |
| 34 |
# |
| 35 |
|
| 36 |
####################################################################### |
| 37 |
# Initialization: |
| 38 |
|
| 39 |
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat} |
| 40 |
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs |
| 41 |
. ${OCF_FUNCTIONS_DIR}/aws.sh |
| 42 |
|
| 43 |
####################################################################### |
| 44 |
|
| 45 |
# |
| 46 |
# Defaults |
| 47 |
# |
| 48 |
OCF_RESKEY_awscli_default="/usr/bin/aws" |
| 49 |
OCF_RESKEY_auth_type_default="key" |
| 50 |
OCF_RESKEY_profile_default="default" |
| 51 |
OCF_RESKEY_region_default="" |
| 52 |
OCF_RESKEY_api_delay_default="3" |
| 53 |
|
| 54 |
: ${OCF_RESKEY_awscli=${OCF_RESKEY_awscli_default}} |
| 55 |
: ${OCF_RESKEY_auth_type=${OCF_RESKEY_auth_type_default}} |
| 56 |
: ${OCF_RESKEY_profile=${OCF_RESKEY_profile_default}} |
| 57 |
: ${OCF_RESKEY_region=${OCF_RESKEY_region_default}} |
| 58 |
: ${OCF_RESKEY_api_delay=${OCF_RESKEY_api_delay_default}} |
| 59 |
|
| 60 |
meta_data() { |
| 61 |
cat <<END |
| 62 |
<?xml version="1.0"?> |
| 63 |
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> |
| 64 |
<resource-agent name="awseip" version="1.0"> |
| 65 |
<version>1.0</version> |
| 66 |
|
| 67 |
<longdesc lang="en"> |
| 68 |
Resource Agent for Amazon AWS Elastic IP Addresses. |
| 69 |
|
| 70 |
It manages AWS Elastic IP Addresses with awscli. |
| 71 |
|
| 72 |
Credentials needs to be setup by running "aws configure", or by using AWS Policies. |
| 73 |
|
| 74 |
See https://aws.amazon.com/cli/ for more information about awscli. |
| 75 |
</longdesc> |
| 76 |
<shortdesc lang="en">Amazon AWS Elastic IP Address Resource Agent</shortdesc> |
| 77 |
|
| 78 |
<parameters> |
| 79 |
|
| 80 |
<parameter name="awscli" unique="0"> |
| 81 |
<longdesc lang="en"> |
| 82 |
command line tools for aws services |
| 83 |
</longdesc> |
| 84 |
<shortdesc lang="en">aws cli tools</shortdesc> |
| 85 |
<content type="string" default="${OCF_RESKEY_awscli_default}" /> |
| 86 |
</parameter> |
| 87 |
|
| 88 |
<parameter name="auth_type"> |
| 89 |
<longdesc lang="en"> |
| 90 |
Authentication type "key" for AccessKey and SecretAccessKey set via "aws configure", |
| 91 |
or "role" to use AWS Policies. |
| 92 |
</longdesc> |
| 93 |
<shortdesc lang="en">Authentication type</shortdesc> |
| 94 |
<content type="string" default="${OCF_RESKEY_auth_type_default}" /> |
| 95 |
</parameter> |
| 96 |
|
| 97 |
<parameter name="profile"> |
| 98 |
<longdesc lang="en"> |
| 99 |
Valid AWS CLI profile name (see ~/.aws/config and 'aws configure') |
| 100 |
</longdesc> |
| 101 |
<shortdesc lang="en">profile name</shortdesc> |
| 102 |
<content type="string" default="${OCF_RESKEY_profile_default}" /> |
| 103 |
</parameter> |
| 104 |
|
| 105 |
<parameter name="elastic_ip" unique="1" required="1"> |
| 106 |
<longdesc lang="en"> |
| 107 |
reserved elastic ip for ec2 instance |
| 108 |
</longdesc> |
| 109 |
<shortdesc lang="en">reserved elastic ip for ec2 instance</shortdesc> |
| 110 |
<content type="string" default="" /> |
| 111 |
</parameter> |
| 112 |
|
| 113 |
<parameter name="allocation_id" unique="1" required="1"> |
| 114 |
<longdesc lang="en"> |
| 115 |
reserved allocation id for ec2 instance |
| 116 |
</longdesc> |
| 117 |
<shortdesc lang="en">reserved allocation id for ec2 instance</shortdesc> |
| 118 |
<content type="string" default="" /> |
| 119 |
</parameter> |
| 120 |
|
| 121 |
<parameter name="private_ip_address" unique="1" required="0"> |
| 122 |
<longdesc lang="en"> |
| 123 |
predefined private ip address for ec2 instance |
| 124 |
</longdesc> |
| 125 |
<shortdesc lang="en">predefined private ip address for ec2 instance</shortdesc> |
| 126 |
<content type="string" default="" /> |
| 127 |
</parameter> |
| 128 |
|
| 129 |
<parameter name="region" required="0"> |
| 130 |
<longdesc lang="en"> |
| 131 |
Region for AWS resource (required for role-based authentication) |
| 132 |
</longdesc> |
| 133 |
<shortdesc lang="en">Region</shortdesc> |
| 134 |
<content type="string" default="${OCF_RESKEY_region_default}" /> |
| 135 |
</parameter> |
| 136 |
|
| 137 |
<parameter name="api_delay" unique="0"> |
| 138 |
<longdesc lang="en"> |
| 139 |
a short delay between API calls, to avoid sending API too quick |
| 140 |
</longdesc> |
| 141 |
<shortdesc lang="en">a short delay between API calls</shortdesc> |
| 142 |
<content type="integer" default="${OCF_RESKEY_api_delay_default}" /> |
| 143 |
</parameter> |
| 144 |
|
| 145 |
<parameter name="curl_retries" unique="0"> |
| 146 |
<longdesc lang="en"> |
| 147 |
curl retries before failing |
| 148 |
</longdesc> |
| 149 |
<shortdesc lang="en">curl retries</shortdesc> |
| 150 |
<content type="integer" default="${OCF_RESKEY_curl_retries_default}" /> |
| 151 |
</parameter> |
| 152 |
|
| 153 |
<parameter name="curl_sleep" unique="0"> |
| 154 |
<longdesc lang="en"> |
| 155 |
curl sleep between tries |
| 156 |
</longdesc> |
| 157 |
<shortdesc lang="en">curl sleep</shortdesc> |
| 158 |
<content type="integer" default="${OCF_RESKEY_curl_sleep_default}" /> |
| 159 |
</parameter> |
| 160 |
|
| 161 |
</parameters> |
| 162 |
|
| 163 |
<actions> |
| 164 |
<action name="start" timeout="30s" /> |
| 165 |
<action name="stop" timeout="30s" /> |
| 166 |
<action name="monitor" timeout="30s" interval="20s" depth="0" /> |
| 167 |
<action name="migrate_to" timeout="30s" /> |
| 168 |
<action name="migrate_from" timeout="30s" /> |
| 169 |
<action name="meta-data" timeout="5s" /> |
| 170 |
<action name="validate" timeout="10s" /> |
| 171 |
<action name="validate-all" timeout="10s" /> |
| 172 |
</actions> |
| 173 |
</resource-agent> |
| 174 |
END |
| 175 |
} |
| 176 |
|
| 177 |
####################################################################### |
| 178 |
|
| 179 |
awseip_usage() { |
| 180 |
cat <<END |
| 181 |
usage: $0 {start|stop|monitor|migrate_to|migrate_from|validate|validate-all|meta-data} |
| 182 |
|
| 183 |
Expects to have a fully populated OCF RA-compliant environment set. |
| 184 |
END |
| 185 |
} |
| 186 |
|
| 187 |
awseip_start() { |
| 188 |
awseip_monitor && return $OCF_SUCCESS |
| 189 |
|
| 190 |
if [ -n "${PRIVATE_IP_ADDRESS}" ]; then |
| 191 |
NETWORK_INTERFACES_MACS=$(curl_retry "$OCF_RESKEY_curl_retries" "$OCF_RESKEY_curl_sleep" "-s -H 'X-aws-ec2-metadata-token: $TOKEN'" "http://169.254.169.254/latest/meta-data/network/interfaces/macs/") |
| 192 |
for MAC in ${NETWORK_INTERFACES_MACS}; do |
| 193 |
curl_retry "$OCF_RESKEY_curl_retries" "$OCF_RESKEY_curl_sleep" "-s -H 'X-aws-ec2-metadata-token: $TOKEN'" "http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC%/*}/local-ipv4s" | |
| 194 |
grep -q "^${PRIVATE_IP_ADDRESS}$" |
| 195 |
if [ $? -eq 0 ]; then |
| 196 |
NETWORK_ID=$(curl_retry "$OCF_RESKEY_curl_retries" "$OCF_RESKEY_curl_sleep" "-s -H 'X-aws-ec2-metadata-token: $TOKEN'" "http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC%/*}/interface-id") |
| 197 |
fi |
| 198 |
done |
| 199 |
if [ -z "$NETWORK_ID" ]; then |
| 200 |
ocf_exit_reason "Could not find network interface for private_ip_address: $PRIVATE_IP_ADDRESS" |
| 201 |
exit $OCF_ERR_GENERIC |
| 202 |
fi |
| 203 |
$AWSCLI_CMD ec2 associate-address \ |
| 204 |
--network-interface-id ${NETWORK_ID} \ |
| 205 |
--allocation-id ${ALLOCATION_ID} \ |
| 206 |
--private-ip-address ${PRIVATE_IP_ADDRESS} |
| 207 |
RET=$? |
| 208 |
else |
| 209 |
$AWSCLI_CMD ec2 associate-address \ |
| 210 |
--instance-id ${INSTANCE_ID} \ |
| 211 |
--allocation-id ${ALLOCATION_ID} |
| 212 |
RET=$? |
| 213 |
fi |
| 214 |
|
| 215 |
# delay to avoid sending request too fast |
| 216 |
sleep ${OCF_RESKEY_api_delay} |
| 217 |
|
| 218 |
if [ $RET -ne 0 ]; then |
| 219 |
return $OCF_NOT_RUNNING |
| 220 |
fi |
| 221 |
|
| 222 |
ocf_log info "elastic_ip has been successfully brought up (${ELASTIC_IP})" |
| 223 |
return $OCF_SUCCESS |
| 224 |
} |
| 225 |
|
| 226 |
awseip_stop() { |
| 227 |
awseip_monitor || return $OCF_SUCCESS |
| 228 |
|
| 229 |
ASSOCIATION_ID=$($AWSCLI_CMD --output json ec2 describe-addresses \ |
| 230 |
--allocation-id ${ALLOCATION_ID} | grep -m 1 "AssociationId" | awk -F'"' '{print$4}') |
| 231 |
|
| 232 |
if [ -z "${ASSOCIATION_ID}" ]; then |
| 233 |
ocf_log info "ASSOCIATION_ID not found while stopping AWS Elastic IP" |
| 234 |
return $OCF_NOT_RUNNING |
| 235 |
fi |
| 236 |
|
| 237 |
$AWSCLI_CMD ec2 disassociate-address --association-id ${ASSOCIATION_ID} |
| 238 |
RET=$? |
| 239 |
|
| 240 |
# delay to avoid sending request too fast |
| 241 |
sleep ${OCF_RESKEY_api_delay} |
| 242 |
|
| 243 |
if [ $RET -ne 0 ]; then |
| 244 |
return $OCF_NOT_RUNNING |
| 245 |
fi |
| 246 |
|
| 247 |
ocf_log info "elastic_ip has been successfully brought down (${ELASTIC_IP})" |
| 248 |
return $OCF_SUCCESS |
| 249 |
} |
| 250 |
|
| 251 |
awseip_monitor() { |
| 252 |
$AWSCLI_CMD ec2 describe-instances --instance-id "${INSTANCE_ID}" | grep -q "${ELASTIC_IP}" |
| 253 |
RET=$? |
| 254 |
|
| 255 |
if [ $RET -ne 0 ]; then |
| 256 |
return $OCF_NOT_RUNNING |
| 257 |
fi |
| 258 |
return $OCF_SUCCESS |
| 259 |
} |
| 260 |
|
| 261 |
awseip_validate() { |
| 262 |
check_binary "${OCF_RESKEY_awscli}" |
| 263 |
|
| 264 |
if [ "x${OCF_RESKEY_auth_type}" = "xkey" ] && [ -z "$OCF_RESKEY_profile" ]; then |
| 265 |
ocf_exit_reason "profile parameter not set" |
| 266 |
return $OCF_ERR_CONFIGURED |
| 267 |
fi |
| 268 |
|
| 269 |
if [ -z "${INSTANCE_ID}" ]; then |
| 270 |
ocf_exit_reason "instance_id not found. Is this a EC2 instance?" |
| 271 |
return $OCF_ERR_GENERIC |
| 272 |
fi |
| 273 |
|
| 274 |
return $OCF_SUCCESS |
| 275 |
} |
| 276 |
|
| 277 |
case $__OCF_ACTION in |
| 278 |
meta-data) |
| 279 |
meta_data |
| 280 |
exit $OCF_SUCCESS |
| 281 |
;; |
| 282 |
usage|help) |
| 283 |
awseip_usage |
| 284 |
exit $OCF_SUCCESS |
| 285 |
;; |
| 286 |
esac |
| 287 |
|
| 288 |
AWSCLI_CMD="${OCF_RESKEY_awscli}" |
| 289 |
if [ "x${OCF_RESKEY_auth_type}" = "xkey" ]; then |
| 290 |
AWSCLI_CMD="$AWSCLI_CMD --profile ${OCF_RESKEY_profile}" |
| 291 |
elif [ "x${OCF_RESKEY_auth_type}" = "xrole" ]; then |
| 292 |
if [ -z "${OCF_RESKEY_region}" ]; then |
| 293 |
ocf_exit_reason "region needs to be set when using role-based authentication" |
| 294 |
exit $OCF_ERR_CONFIGURED |
| 295 |
fi |
| 296 |
else |
| 297 |
ocf_exit_reason "Incorrect auth_type: ${OCF_RESKEY_auth_type}" |
| 298 |
exit $OCF_ERR_CONFIGURED |
| 299 |
fi |
| 300 |
if [ -n "${OCF_RESKEY_region}" ]; then |
| 301 |
AWSCLI_CMD="$AWSCLI_CMD --region ${OCF_RESKEY_region}" |
| 302 |
fi |
| 303 |
ELASTIC_IP="${OCF_RESKEY_elastic_ip}" |
| 304 |
ALLOCATION_ID="${OCF_RESKEY_allocation_id}" |
| 305 |
PRIVATE_IP_ADDRESS="${OCF_RESKEY_private_ip_address}" |
| 306 |
TOKEN=$(get_token) |
| 307 |
[ $? -ne 0 ] && exit $OCF_ERR_GENERIC |
| 308 |
INSTANCE_ID=$(get_instance_id) |
| 309 |
[ $? -ne 0 ] && exit $OCF_ERR_GENERIC |
| 310 |
|
| 311 |
case $__OCF_ACTION in |
| 312 |
start) |
| 313 |
awseip_validate |
| 314 |
awseip_start |
| 315 |
;; |
| 316 |
stop) |
| 317 |
awseip_stop |
| 318 |
;; |
| 319 |
monitor) |
| 320 |
awseip_monitor |
| 321 |
;; |
| 322 |
migrate_to) |
| 323 |
ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrate_target}." |
| 324 |
awseip_stop |
| 325 |
;; |
| 326 |
migrate_from) |
| 327 |
ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} from ${OCF_RESKEY_CRM_meta_migrate_source}." |
| 328 |
awseip_start |
| 329 |
;; |
| 330 |
reload) |
| 331 |
ocf_log info "Reloading ${OCF_RESOURCE_INSTANCE} ..." |
| 332 |
;; |
| 333 |
validate|validate-all) |
| 334 |
awseip_validate |
| 335 |
;; |
| 336 |
*) |
| 337 |
awseip_usage |
| 338 |
exit $OCF_ERR_UNIMPLEMENTED |
| 339 |
;; |
| 340 |
esac |
| 341 |
|
| 342 |
rc=$? |
| 343 |
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc" |
| 344 |
exit $rc |