Last active
February 22, 2018 00:50
-
-
Save aarongorka/c3a8cae970d5510c78aa009d5197d43b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
while true; do | |
# API call to GoCD to retrieve list of agents + status | |
OUTPUT="$(curl -s "${GOCD_URL}/api/agents" -u "${GOCD_USERNAME}:${GOCD_PASSWORD}" -H 'Accept: application/vnd.go.cd.v4+json')" | |
# Extract the status of the the current agent from the returned JSON using jq | |
CUR_STATE="$(echo "${OUTPUT}" | jq -r "._embedded.agents[] | select(.hostname == \"${HOSTNAME}\") | .build_state")" | |
if [[ "${CUR_STATE}" == "Building" ]] && ! [[ "${CUR_STATE}" == "${PREV_STATE}" ]]; then # if the agent's state has changed from something to Building, protect the instance | |
aws autoscaling set-instance-protection --region "${EC2_REGION}" --instance-ids "${INSTANCE_ID}" --auto-scaling-group-name "${ASG_NAME}" --protected-from-scale-in & | |
elif [[ "${PREV_STATE}" == "Building" ]] && ! [[ "${CUR_STATE}" == "${PREV_STATE}" ]]; then # if the agent's state has changed from Building to something else, unprotect it | |
aws autoscaling set-instance-protection --region "${EC2_REGION}" --instance-ids "${INSTANCE_ID}" --auto-scaling-group-name "${ASG_NAME}" --no-protected-from-scale-in & | |
fi | |
sleep 5s | |
PREV_STATE="${CUR_STATE}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment