fixed wait-for-new-auto-replicas-and-terminate-upgrade-replicas.sh script

This commit is contained in:
Axel Uhl
2021-07-24 12:06:57 +09:00
parent dca89990de
commit e1c43bf286
3 changed files with 8 additions and 7 deletions
@@ -38,7 +38,7 @@ for REGION in $( cat `dirname $0`/regions.txt ); do
echo "-------------------------------------------------------"
TARGET_GROUP_ARN=$( aws elbv2 describe-target-groups --names ${TARGET_GROUP_NAME} | jq -r '.TargetGroups[].TargetGroupArn' )
HEALTHY_TARGETS_IN_REGION=$( aws elbv2 describe-target-health --target-group-arn ${TARGET_GROUP_ARN} | jq '.TargetHealthDescriptions | map(select(.TargetHealth.State == "healthy")) | length' )
echo "Found ${HEALTHY_TARGETS_IN_REGION} healthy targets in target group ${TARGET_GROUP_NAME} in region."
echo "Found ${HEALTHY_TARGETS_IN_REGION} healthy targets in target group ${TARGET_GROUP_NAME} in region ${REGION}."
if [ ${HEALTHY_TARGETS_IN_REGION} = 0 ]; then
echo "Launching at least one replica."
HEALTHY_TARGETS_IN_REGION=1
@@ -141,6 +141,7 @@ if [ "${EXIT_CODE}" != "0" ]; then
echo "Launching replicas in the regions failed with exit code ${EXIT_CODE}"
exit ${EXIT_CODE}
fi
wait
read -p "Press ENTER to terminate all ${INSTANCE_NAME_TO_TERMINATE} instances"
echo " * Terminating all instances named ${INSTANCE_NAME_TO_TERMINATE} to force auto-scaling group to launch and register upgraded ones"
for REGION in $( cat `dirname $0`/regions.txt ); do
@@ -159,4 +160,5 @@ for REGION in $( cat `dirname $0`/regions.txt ); do
echo " * Waiting for new auto-replicas to become available in region ${REGION}, then terminating upgrade replicas..."
`dirname $0`/wait-for-new-auto-replicas-and-terminate-upgrade-replicas.sh -g ${REGION} &
done
wait
echo " * DONE"
@@ -26,23 +26,22 @@ do
exit 4;;
esac
done
if [ -n "${REGION}" ]; then
export AWS_DEFAULT_REGION=${REGION}
fi
export AWS_DEFAULT_REGION=${REGION}
TARGET_GROUP_ARN=$( aws elbv2 describe-target-groups --names ${TARGET_GROUP_NAME} | jq -r '.TargetGroups[].TargetGroupArn' )
echo "Found target group with name ${TARGET_GROUP_NAME} and ARN ${TARGET_GROUP_ARN}"
UPGRADE_REPLICA_IDS=$( aws ec2 describe-instances --filters Name=tag:Name,Values="${UPGRADE_REPLICA_NAME}" | jq -r '.Reservations[].Instances[].InstanceId' )
NUMBER_OF_UPGRADE_REPLICAS=$( echo ${UPGRADE_REPLICA_IDS} | wc )
NUMBER_OF_UPGRADE_REPLICAS=$( echo ${UPGRADE_REPLICA_IDS} | wc -w )
echo "Found ${NUMBER_OF_UPGRADE_REPLICAS} upgrade replicas. Waiting until we see this many auto-replicas named ${AUTO_REPLICA_NAME}..."
TOTAL_NUMBER_OF_TOTAL_REPLICAS_TO_WAIT_FOR=$(( 2 * ${NUMBER_OF_UPGRADE_REPLICAS} ))
NUMBER_OF_TOTAL_HEALTHY_REPLICAS=$( aws --region eu-west-2 elbv2 describe-target-health --target-group-arn ${TARGET_GROUP_ARN} | jq '.TargetHealthDescriptions | map(select(.TargetHealth.State=="healthy")) | length' )
NUMBER_OF_TOTAL_HEALTHY_REPLICAS=$( aws elbv2 describe-target-health --target-group-arn ${TARGET_GROUP_ARN} | jq '.TargetHealthDescriptions | map(select(.TargetHealth.State=="healthy")) | length' )
while [ ${NUMBER_OF_TOTAL_HEALTHY_REPLICAS} -lt ${TOTAL_NUMBER_OF_TOTAL_REPLICAS_TO_WAIT_FOR} ]; do
echo "Found ${NUMBER_OF_TOTAL_HEALTHY_REPLICAS} healthy replicas so far; waiting until we see ${TOTAL_NUMBER_OF_TOTAL_REPLICAS_TO_WAIT_FOR}..."
NUMBER_OF_TOTAL_HEALTHY_REPLICAS=$( aws elbv2 describe-target-health --target-group-arn ${TARGET_GROUP_ARN} | jq '.TargetHealthDescriptions | map(select(.TargetHealth.State=="healthy")) | length' )
sleep 10
done
echo "Found a total of ${TOTAL_NUMBER_OF_TOTAL_REPLICAS_TO_WAIT_FOR} healthy replicas in target group ${TARGET_GROUP_NAME}. Terminating ${UPGRADE_REPLICA_NAME} replicas..."
for UPGRADE_REPLICA_ID in ${UPGRADE_REPLICA_IDS}; do
echo "Terminating instance with ID ${UPGRADE_REPLICA_ID}..."
aws ec2 terminate-instance --instance-ids ${UPGRADE_REPLICA_ID}
aws ec2 terminate-instances --instance-ids ${UPGRADE_REPLICA_ID}
done