Skip to content

Instantly share code, notes, and snippets.

@andyburke
Created August 29, 2015 03:32
Show Gist options
  • Save andyburke/3766ae9d425b04dd1d37 to your computer and use it in GitHub Desktop.
Save andyburke/3766ae9d425b04dd1d37 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo "Updating server: ${HOSTNAME}"
echo ""
# pull latest image
docker pull float/auth:${CODE_BRANCH}
# get image id
NEW_IMAGE_ID=$(docker images | grep -E "^float\/auth.*?${CODE_BRANCH}" | awk -e '{print $3}')
NEW_CONTAINER_NAME="auth_${NEW_IMAGE_ID}"
# get running container name
RUNNING_CONTAINER_NAME=$(docker ps | grep -E "auth_.*?$" | awk -e '{print $NF}')
if [ "${RUNNING_CONTAINER_NAME}" == "${NEW_CONTAINER_NAME}" ]
then
echo "WARNING: Attempt to deploy already-running verion: ${NEW_IMAGE_ID}"
exit 1
fi
echo "Updating to image id: ${NEW_IMAGE_ID}"
echo " New container name: ${NEW_CONTAINER_NAME}"
# create new container
docker run -d \
--name ${NEW_CONTAINER_NAME} \
--volumes-from=data \
--link rethinkdb_proxy:rethinkdb \
--restart=always \
-P \
-e "EPICENTER_HTTPS_CERT=/data/auth/certs/server.crt" \
-e "EPICENTER_HTTPS_KEY=/data/auth/certs/server.key" \
-e "EPICENTER_HTTPS_REDIRECT=1" \
float/auth:${CODE_BRANCH}
if [ "${RUNNING_CONTAINER_NAME}" != "" ]
then
RUNNING_CONTAINER_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${RUNNING_CONTAINER_NAME})
fi
# get new container mapped ports
NEW_CONTAINER_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${NEW_CONTAINER_NAME})
echo " Networking info:"
echo " New container ip: ${NEW_CONTAINER_IP}"
if [ "${RUNNING_CONTAINER_NAME}" != "" ]
then
echo " Running container ip: ${RUNNING_CONTAINER_IP}"
fi
#HTTPS_PORT=$(docker port ${NEW_CONTAINER_NAME} 4443/tcp | sed 's/.*:\([0-9]*\)/\1/')
#HTTP_PORT=$(docker port ${NEW_CONTAINER_NAME} 8000/tcp | sed 's/.*:\([0-9]*\)/\1/')
# test container
echo " Waiting for new container to be ready..."
until $(curl -ks https://${NEW_CONTAINER_IP}:4443/__epicenter | grep -q '"ready":true'); do
printf '.'
sleep 1
done
# point iptables to new container
echo " Switching networking to new container..."
iptables -t nat -A DOCKER ! -i docker0 -p tcp --dport 443 -j DNAT --to-destination ${NEW_CONTAINER_IP}:4443
iptables -t nat -A DOCKER ! -i docker0 -p tcp --dport 80 -j DNAT --to-destination ${NEW_CONTAINER_IP}:8000
# shut down old container
if [ "${RUNNING_CONTAINER_NAME}" != "" ]
then
echo " Removing previous container networking forwards..."
iptables -t nat -D DOCKER ! -i docker0 -p tcp --dport 443 -j DNAT --to-destination ${RUNNING_CONTAINER_IP}:4443
iptables -t nat -D DOCKER ! -i docker0 -p tcp --dport 80 -j DNAT --to-destination ${RUNNING_CONTAINER_IP}:8000
# wait for connections to drop on old container
echo " Waiting for old container to be finished serving requests..."
# single active connection is the request for status itself (vs. 0 active)
until $(curl -ks https://${RUNNING_CONTAINER_IP}:4443/__epicenter | grep -q '"active":1,'); do
printf '.'
sleep 1
done
# stop old container
echo " Stopping old container: ${RUNNING_CONTAINER_NAME}"
docker stop ${RUNNING_CONTAINER_NAME}
# remove old container
echo " Removing old container: ${RUNNING_CONTAINER_NAME}"
docker rm ${RUNNING_CONTAINER_NAME}
fi
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment