timeout 360 rancher-compose -f docker-compose.yml -p $APPLICATION_NAME up -d --force-upgrade
error_code=$?
if [ "$error_code" -eq "0" ]; then
echo "SUCCESS: Confirming Upgrade"
rancher-compose -p $APPLICATION_NAME up -d --upgrade --confirm-upgrade
else
echo "FAILURE: Rolling back"
PROJECT_JSON=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"https://rancher.example.com/v1/projects/{$ENV_ID}/environments?name=${APPLICATION_NAME}&sort=id"`
# Gets the id for the project from Rancher API
PROJECT_ID=`echo "$PROJECT_JSON" | jq -r '.data[].id' `
# Gets the links to the services (containers)
SERVICES_LINK=`echo "$PROJECT_JSON" | jq -r '.data[].links.services' `
# Grabs all the services for the project that are stuck in an upgrading state
SERVICES_TO_CANCEL=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"${SERVICES_LINK}?state=upgrading" \
| jq -r '.data[] | select(.state == "upgrading") | .actions.cancelupgrade'`
# Loop through the services that need to be cancelled, and cancel them
for i in $(echo "${SERVICES_TO_CANCEL}"); do
SERVICE=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{}' \
"${i}" | jq -r '.id'`
echo "Sent stop upgrade to $SERVICE"
done
# Checks the Rancher API to determine whether the services have been cancelled or not
CONDITIONAL="curl -s --ssl -u \"${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}\" \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
\"${SERVICES_LINK}\" \
| jq -r '.data[] | select(.state == \"canceling-upgrade\")'"
# When all the services have been cancelled, we stop checking if they've been cancelled
while [ -n "`eval $CONDITIONAL`" ]; do
echo "Waiting for canceling-upgrade to finish..."
sleep 5
done
# Get all the services that need to be rolled back
SERVICES_TO_ROLLBACK=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"${SERVICES_LINK}" \
| jq -r '.data[] | select(.state == "canceled-upgrade") | .actions.rollback'`
# Roll them back
for i in $(echo "${SERVICES_TO_ROLLBACK}"); do
SERVICE=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{}' \
"${i}" | jq -r '.id'`
echo "Rolled back $SERVICE"
done
fi