From d0be1db5a3b4b4e6dc690d393b81be917e226f60 Mon Sep 17 00:00:00 2001 From: Christoph Schranz Date: Wed, 13 Nov 2019 15:45:07 +0100 Subject: [PATCH] Automated scripts for deployment --- .gitignore | 1 + add-to-swarm.sh | 57 +++++++++++++++++++++++++++++----------- docker-compose-swarm.yml | 2 +- docker-compose.yml | 15 +---------- remove-from-swarm.sh | 3 ++- start-local.sh | 23 ++++++++++++++++ stop-local.sh | 2 ++ 7 files changed, 72 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 01b2a9f..18dcb32 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,4 @@ venv.bak/ /data/* !/data/.gitkeep *.save* +*.envsubst diff --git a/add-to-swarm.sh b/add-to-swarm.sh index 586a91f..ac3530e 100755 --- a/add-to-swarm.sh +++ b/add-to-swarm.sh @@ -1,20 +1,47 @@ #!/usr/bin/env bash cd $(cd -P -- "$(dirname -- "$0")" && pwd -P) -if [[ -n "$1" ]]; then - result=$(docker network ls) - if [[ "$result" == *" $1 "* ]]; then - echo "Adding gpu-jupyter to the swarm in the network $1." - export JUPYTER_NETWORK=$1 - # echo $JUPYTER_NETWORK - docker-compose -f docker-compose-swarm.yml build - docker-compose -f docker-compose-swarm.yml push -# docker stack deploy --compose-file docker-compose-swarm.yml gpu - else - echo "Could not find network $1. Please provide a valid docker network." - fi -else - echo "Usage: $0 network" - echo "Please set the network" +# Fetching port and network as input +PORT=8888 +while [[ "$#" -gt 0 ]]; do case $1 in + -p|--port) PORT="$2"; shift;; + -n|--network) NETWORK="$2"; shift;; +# -u|--uglify) uglify=1;; + *) echo "Unknown parameter passed: $1"; exit 1;; +esac; shift; done + +# Check if arguments are valid +if [[ $PORT != [0-9][0-9][0-9][0-9]* ]]; then + echo "Given port is not valid." + echo "Usage: $0 -p [port] -n [docker-network] # port must be an integer with 4 or more digits." + exit 21 fi +if [[ $NETWORK == "" ]]; then + echo "No docker network was provided to which this gpu-jupyter should be added to." + echo "Usage: $0 -p [port] -n [docker-network] # port must be an integer with 4 or more digits." + exit 22 +fi +result=$(docker network ls) +if [[ "$result" != *" $NETWORK "* ]]; then + echo "Could not find network $NETWORK. Please provide a valid docker network." + echo "Please select a network:" + docker network ls + exit 23 +fi + +# starting in swarm +echo "Adding gpu-jupyter to the swarm in the network $NETWORK on port $PORT." +export JUPYTER_PORT=$PORT +export JUPYTER_NETWORK=$NETWORK +# echo $JUPYTER_NETWORK +envsubst < docker-compose-swarm.yml > .docker-compose-swarm.yml.envsubst +docker-compose -f .docker-compose-swarm.yml.envsubst build +docker-compose -f .docker-compose-swarm.yml.envsubst push +docker stack deploy --compose-file .docker-compose-swarm.yml.envsubst gpu +rm .docker-compose-swarm.yml.envsubst + +echo +echo "Added gpu-jupyter to docker swarm $NETWORK on port $JUPYTER_PORT." +echo "See 'docker service ps gpu_gpu-jupyter' for status info." +echo "See 'docker service logs -f gpu_gpu-jupyter' for logs." diff --git a/docker-compose-swarm.yml b/docker-compose-swarm.yml index c7a84a1..abb95f4 100755 --- a/docker-compose-swarm.yml +++ b/docker-compose-swarm.yml @@ -4,7 +4,7 @@ services: image: 127.0.0.1:5001/gpu-jupyter build: ./src/ ports: - - 8848:8888 + - $JUPYTER_PORT:8888 volumes: - ./data:/home/jovyan/work environment: diff --git a/docker-compose.yml b/docker-compose.yml index f26e992..1fc3c20 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: image: 127.0.0.1:5001/gpu-jupyter build: ./src/ ports: - - 8848:8888 + - ${JUPYTER_PORT}:8888 volumes: - ./data:/home/jovyan/work environment: @@ -13,16 +13,3 @@ services: # enable sudo permissions user: "root" - deploy: - placement: - constraints: [node.hostname == data-science] - resources: - limits: - cpus: "4" - memory: 8G - replicas: 1 - update_config: - parallelism: 2 - delay: 10s - restart_policy: - condition: on-failure diff --git a/remove-from-swarm.sh b/remove-from-swarm.sh index e17cb64..28671fd 100755 --- a/remove-from-swarm.sh +++ b/remove-from-swarm.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash cd $(cd -P -- "$(dirname -- "$0")" && pwd -P) -docker stack rm gpu_gpu-jupyter +echo "Removing gpu-jupyter from docker swarm." +docker stack rm gpu diff --git a/start-local.sh b/start-local.sh index 4ec3db7..bc59876 100755 --- a/start-local.sh +++ b/start-local.sh @@ -1,4 +1,27 @@ #!/usr/bin/env bash cd $(cd -P -- "$(dirname -- "$0")" && pwd -P) +# Fetching port and network as input +PORT=8888 +while [[ "$#" -gt 0 ]]; do case $1 in + -p|--port) PORT="$2"; shift;; +# -u|--uglify) uglify=1;; + *) echo "Unknown parameter passed: $1"; exit 1;; +esac; shift; done + +# Check if arguments are valid +if [[ $PORT != [0-9][0-9][0-9][0-9]* ]]; then + echo "Given port is not valid." + echo "Usage: $0 -p [port] -n [docker-network] # port must be an integer with 4 or more digits." + exit 21 +fi + +# starting in docker-compose +echo "Starting gpu-jupyter via docker-compose on port $PORT." +export JUPYTER_PORT=$PORT + +# echo $JUPYTER_PORT docker-compose up --build -d +echo +echo "Started gpu-jupyter via docker-compose on port $JUPYTER_PORT." +echo "See docker-compose logs -f for logs." diff --git a/stop-local.sh b/stop-local.sh index d0b7406..eb4e041 100755 --- a/stop-local.sh +++ b/stop-local.sh @@ -1,4 +1,6 @@ #!/usr/bin/env bash cd $(cd -P -- "$(dirname -- "$0")" && pwd -P) +# Export default port to make compose file valid. +export JUPYTER_PORT=8888 docker-compose down