Skip to content

Instantly share code, notes, and snippets.

@saada
Last active February 22, 2017 23:10
Show Gist options
  • Save saada/d4aec413eccdee61abe864e3d2a544d1 to your computer and use it in GitHub Desktop.
Save saada/d4aec413eccdee61abe864e3d2a544d1 to your computer and use it in GitHub Desktop.
Redeploy stack in Docker Cloud synchronously. Great for redeploying in Jenkins and other build systems and provides relevant output.
#!/bin/bash
#############################################################
# This script depends on docker.
# simply run ./docker-cloud-sync-redeploy.sh <stack name>
#############################################################
set -e
STATE_RUNNING='Running'
STATE_NOT_RUNNING='Not running'
STATE_STARTING='Starting'
STATE_REDEPLOYING='Redeploying'
docker-cloud() {
docker run -v ~/.docker:/root/.docker:ro --rm dockercloud/cli "$@"
}
state() {
docker-cloud stack inspect $1 | grep -q "\"state\": \"$2\""
}
# get passed stack
if [ -z "$1" ]; then
echo 'error: no stack name passed'
echo "run the command as: ./docker-cloud-sync-redeploy.sh <stack name>"
exit 1
fi
dc_env="$1"
# redeploy stack
echo "redeploying $dc_env ..."
docker-cloud stack redeploy $dc_env
# wait until deployment is complete
echo 'Waiting for deployment to finish...'
while state $dc_env "$STATE_REDEPLOYING" || state $dc_env "$STATE_STARTING"; do
docker-cloud stack inspect $dc_env | grep state
sleep 1
done;
# show relevant output depending on results
if state $dc_env "$STATE_RUNNING"; then
echo "Deployment is successful! :)"
exit 0
else
echo "Failed to deploy image"
docker-cloud stack inspect $dc_env
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment