Skip to content

Commit

Permalink
improved docker image lookup to shut down correct server
Browse files Browse the repository at this point in the history
  • Loading branch information
albertlatacz committed Jan 6, 2017
1 parent 8c47186 commit 304c924
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions bin/restart-after-tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@ function getTags() {
curl -s "https://registry.hub.docker.com/v2/repositories/${REPOSITORY}/tags/" | jq ".results[].name" > "${TAGS_FILE}"
}

function extractName() {
REPOSITORY=$1
PROJECT=${REPOSITORY##*/}
echo "${PROJECT/[-]/_}"
}

function restartContainer() {
REPOSITORY=$1
COMMAND="$2"
DOCKER_OPTIONS="$2"
NAME=`extractName ${REPOSITORY}`
echo "New version of ${REPOSITORY} available. Deploying..."
docker stop $(docker ps -q --filter ancestor=${REPOSITORY})
docker rm $(docker ps -q --filter status=exited --filter ancestor=${REPOSITORY})
docker rmi $(docker images -q --filter dangling=true --filter ancestor=${REPOSITORY})
docker stop $(docker ps -q --filter name=${NAME})
docker rm $(docker ps -q --filter status=exited --filter name=${NAME})
docker rmi $(docker images -q --filter dangling=true --filter name=${NAME})
docker pull ${REPOSITORY}
eval "${COMMAND} ${REPOSITORY}"
eval "docker run --restart=always -d --name=${NAME} ${DOCKER_OPTIONS} ${REPOSITORY}"
}

function checkForNewVersion() {
REPOSITORY=$1
COMMAND="$2"
DOCKER_OPTIONS="$2"
CURRENT_TAGS_FILE=`tagsFile ${REPOSITORY} "current"`
NEW_TAGS_FILE=`tagsFile ${REPOSITORY} "new"`
if [ -f ${CURRENT_TAGS_FILE} ];
Expand All @@ -36,19 +43,19 @@ function checkForNewVersion() {
if [ "${FILES_DIFF}" != "" ];
then
getTags ${REPOSITORY} ${CURRENT_TAGS_FILE}
restartContainer "${REPOSITORY}" "${COMMAND}"
restartContainer "${REPOSITORY}" "${DOCKER_OPTIONS}"
fi
else
getTags ${REPOSITORY} ${CURRENT_TAGS_FILE}
restartContainer "${REPOSITORY}" "${COMMAND}"
restartContainer "${REPOSITORY}" "${DOCKER_OPTIONS}"
fi
}


DOCKER_HUB_REPOSITORY=$1
COMMAND="$2"
DOCKER_OPTIONS="$2"

checkForNewVersion ${DOCKER_HUB_REPOSITORY} "${COMMAND}"
checkForNewVersion ${DOCKER_HUB_REPOSITORY} "${DOCKER_OPTIONS}"



Expand Down

0 comments on commit 304c924

Please sign in to comment.