Skip to content

Commit 304c924

Browse files
committed
improved docker image lookup to shut down correct server
1 parent 8c47186 commit 304c924

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

bin/restart-after-tag.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,27 @@ function getTags() {
1212
curl -s "https://registry.hub.docker.com/v2/repositories/${REPOSITORY}/tags/" | jq ".results[].name" > "${TAGS_FILE}"
1313
}
1414

15+
function extractName() {
16+
REPOSITORY=$1
17+
PROJECT=${REPOSITORY##*/}
18+
echo "${PROJECT/[-]/_}"
19+
}
20+
1521
function restartContainer() {
1622
REPOSITORY=$1
17-
COMMAND="$2"
23+
DOCKER_OPTIONS="$2"
24+
NAME=`extractName ${REPOSITORY}`
1825
echo "New version of ${REPOSITORY} available. Deploying..."
19-
docker stop $(docker ps -q --filter ancestor=${REPOSITORY})
20-
docker rm $(docker ps -q --filter status=exited --filter ancestor=${REPOSITORY})
21-
docker rmi $(docker images -q --filter dangling=true --filter ancestor=${REPOSITORY})
26+
docker stop $(docker ps -q --filter name=${NAME})
27+
docker rm $(docker ps -q --filter status=exited --filter name=${NAME})
28+
docker rmi $(docker images -q --filter dangling=true --filter name=${NAME})
2229
docker pull ${REPOSITORY}
23-
eval "${COMMAND} ${REPOSITORY}"
30+
eval "docker run --restart=always -d --name=${NAME} ${DOCKER_OPTIONS} ${REPOSITORY}"
2431
}
2532

2633
function checkForNewVersion() {
2734
REPOSITORY=$1
28-
COMMAND="$2"
35+
DOCKER_OPTIONS="$2"
2936
CURRENT_TAGS_FILE=`tagsFile ${REPOSITORY} "current"`
3037
NEW_TAGS_FILE=`tagsFile ${REPOSITORY} "new"`
3138
if [ -f ${CURRENT_TAGS_FILE} ];
@@ -36,19 +43,19 @@ function checkForNewVersion() {
3643
if [ "${FILES_DIFF}" != "" ];
3744
then
3845
getTags ${REPOSITORY} ${CURRENT_TAGS_FILE}
39-
restartContainer "${REPOSITORY}" "${COMMAND}"
46+
restartContainer "${REPOSITORY}" "${DOCKER_OPTIONS}"
4047
fi
4148
else
4249
getTags ${REPOSITORY} ${CURRENT_TAGS_FILE}
43-
restartContainer "${REPOSITORY}" "${COMMAND}"
50+
restartContainer "${REPOSITORY}" "${DOCKER_OPTIONS}"
4451
fi
4552
}
4653

4754

4855
DOCKER_HUB_REPOSITORY=$1
49-
COMMAND="$2"
56+
DOCKER_OPTIONS="$2"
5057

51-
checkForNewVersion ${DOCKER_HUB_REPOSITORY} "${COMMAND}"
58+
checkForNewVersion ${DOCKER_HUB_REPOSITORY} "${DOCKER_OPTIONS}"
5259

5360

5461

0 commit comments

Comments
 (0)