From de729abf419afa3984c996ec568c5bfcff870325 Mon Sep 17 00:00:00 2001 From: Herve Nicol Date: Wed, 21 Mar 2018 22:20:32 +0100 Subject: [PATCH] scripts fixes: better error handling and shellcheck compliance --- bin/1-build.sh | 19 +++++++++++++------ bin/2-push.sh | 5 +++++ bin/3-deploy.sh | 12 +++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/bin/1-build.sh b/bin/1-build.sh index 81a83d9..f1e1bec 100755 --- a/bin/1-build.sh +++ b/bin/1-build.sh @@ -1,20 +1,27 @@ +#!/bin/sh + +echo_exit() { + echo "$*" + exit 1 +} + # Copy resources needed in the image build phase cp ../template/index.mustache ../data/*.* ../www/ # Build image -cd ../www -docker image build -t lucj/sophia.events . +cd ../www || echo_exit "Could not switch to www dir" +docker image build -t lucj/sophia.events . || echo_exit "Could not use docker" # Run website -ID=$(docker run -d -p 80 lucj/sophia.events) +ID="$(docker run -d -p 80 lucj/sophia.events)" # Copy generated index.html and past.html locally -docker cp $ID:/usr/share/nginx/html/index.html . -docker cp $ID:/usr/share/nginx/html/past.html . +docker cp "$ID":/usr/share/nginx/html/index.html . || echo_exit "docker cp failed" +docker cp "$ID":/usr/share/nginx/html/past.html . || echo_exit "docker cp failed" # Remove files used for image build rm index.mustache events.json clean_events.js package.json # Get port -PORT=$(docker inspect --format '{{ (index (index .NetworkSettings.Ports "80/tcp") 0).HostPort }}' $ID) +PORT="$(docker inspect --format '{{ (index (index .NetworkSettings.Ports "80/tcp") 0).HostPort }}' "$ID")" echo "=> web site available on http://localhost:$PORT" diff --git a/bin/2-push.sh b/bin/2-push.sh index dec98de..0664075 100755 --- a/bin/2-push.sh +++ b/bin/2-push.sh @@ -1 +1,6 @@ +#!/bin/sh + +### Helper script to push docker image to registry +### Require lucj credentials + docker push lucj/sophia.events diff --git a/bin/3-deploy.sh b/bin/3-deploy.sh index 890e80a..cb23c48 100755 --- a/bin/3-deploy.sh +++ b/bin/3-deploy.sh @@ -1,12 +1,18 @@ #!/bin/sh -version=$1 +echo_exit() { + echo "$*" + exit 1 +} + +# a sepcific version can be passed as parameter +version="$1" if [ "$version" = "" ]; then version="latest" fi # Pull new image -docker pull lucj/sophia.events:$version +docker pull lucj/sophia.events:"$version" || echo_exit "Could not pull lucj/sophia.events:$version" # Update www service with the newly retrieved image -docker service update sophia_www --image=lucj/sophia.events:$version +docker service update sophia_www --image=lucj/sophia.events:"$version"