[IMP] Complete Docker launch script:

* Add start command
  * Add stop command
  * Add migrate command
This commit is contained in:
Olivier DOSSMANN 2016-01-12 17:55:42 +01:00
parent 857d12c7da
commit d27d04398a
2 changed files with 91 additions and 12 deletions

11
config
View File

@ -1,7 +1,16 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#
# config
#
# This file gives parameters to launch Docker containers for Gissmo projects.
#
# PROJECT_DIR: will contains all python libraries and gissmo git repository
# GISSMO_VERSION: used to generated Gissmo Docker image name.
PROJECT_DIR="/home/olivier/gissmo_project" PROJECT_DIR="/home/olivier/gissmo_project"
POSTGRES_VERSION="9.5" POSTGRES_VERSION="9.5"
POSTGRES_DOCKER_NAME="gissmo_db" POSTGRES_DOCKER_NAME="gissmo_db"
DB_VOLUME_NAME="dbdata" DB_VOLUME_NAME="dbdata"
GIT_BRANCH="restful"
GISSMO_VERSION="1.5"
GISSMO_DOCKER_PORT="8002"

View File

@ -4,12 +4,15 @@
## Default ## Default
PROGRAM=`basename $0` PROGRAM=`basename $0`
docker_cmd=`which docker`
CURRENT_DIR=$(pwd)
GIT_URL="git@github.com:eost/gissmo.git" GIT_URL="git@github.com:eost/gissmo.git"
GIT_BRANCH="dev" # Default Git Branch GIT_BRANCH="dev" # Default Git Branch
GIT_DIR_NAME="gissmo" GIT_DIR_NAME="gissmo"
DOCKERFILE_NAME="Dockerfile" DOCKERFILE_NAME="Dockerfile"
DB_DIR="/dbdata" DB_DIR="/dbdata"
POSTGRES_VERSION="9.5" POSTGRES_VERSION="9.5"
GISSMO_DOCKER_NAME="gissmo"
## User ## User
. config || exit 1 . config || exit 1
@ -19,6 +22,7 @@ GISSMO_DIR="${PROJECT_DIR}/${GIT_DIR_NAME}"
DOCKERFILE="${GISSMO_DIR}/${DOCKERFILE_NAME}" DOCKERFILE="${GISSMO_DIR}/${DOCKERFILE_NAME}"
COMMAND="$1" COMMAND="$1"
shift 1; ARGS="$@"
# METHODS # METHODS
@ -44,9 +48,13 @@ show_help() {
echo -e " Where COMMAND is one of:" echo -e " Where COMMAND is one of:"
echo -e " - create Create postgreSQL database volume and postgreSQL Docker container" echo -e " - create Create postgreSQL database volume and postgreSQL Docker container"
echo -e " - init Create project directory as python virtual environment, fetch git repository and install Python dependancies" echo -e " - init Create project directory as python virtual environment, fetch git repository and install Python dependancies"
echo -e " - migrate Launch migration command: python manage.py migrate"
echo -e " - restore " echo -e " - restore "
echo -e " - start " echo -e " - start Launch Gissmo Docker container (created by 'create' command) with a name given in GISSMO_DOCKER_NAME variable"
echo -e " - stop " echo -e " - stop Stop Gissmo Docker container which name is GISSMO_DOCKER_NAME variable content"
# TODO: Add clean() command to STOP and DELETE Gissmo Docker container
# TODO: Add clean_db() command to STOP and DELETE postgreSQL Docker container
# TODO: Add clean_all() command that makes clean() and clean_db()
} }
check_command() { check_command() {
@ -54,6 +62,8 @@ check_command() {
return 0 return 0
elif test $1 == "init"; then elif test $1 == "init"; then
return 0 return 0
elif test $1 == "migrate"; then
return 0
elif test $1 == "restore"; then elif test $1 == "restore"; then
return 0 return 0
elif test $1 == "start"; then elif test $1 == "start"; then
@ -66,11 +76,30 @@ check_command() {
exit 1 exit 1
} }
test_create() { test_docker_cmd() {
docker_cmd=`which docker`
if test -z "$docker_cmd"; then if test -z "$docker_cmd"; then
error_and_quit "Docker command not found. Install docker or fill in the PATH." error_and_quit "Docker command not found. Install docker or fill in the PATH."
fi fi
return 0
}
test_postgresql_exists() {
PSQL_RUNNING=$($docker_cmd inspect --format="{{ .State.Running }}" ${POSTGRES_DOCKER_NAME} 2> /dev/null)
if [ $? -eq 1 ]; then
error_and_quit "postgreSQL Docker container not found: ${POSTGRES_DOCKER_NAME}. Please launch '$PROGRAM create' first."
fi
}
test_postgresql_launched() {
PSQL_RUNNING=$($docker_cmd inspect --format="{{ .State.Running }}" ${POSTGRES_DOCKER_NAME} 2> /dev/null)
if test $PSQL_RUNNING == "false"; then
return 1
fi
return 0
}
test_create() {
test_docker_cmd
if test -z "$DB_DIR"; then if test -z "$DB_DIR"; then
error_and_quit "No database directory given (DB_DIR in config file)." error_and_quit "No database directory given (DB_DIR in config file)."
fi fi
@ -105,7 +134,7 @@ init() {
sudo apt-get install -qq libpq-dev python3-dev python-virtualenv git-core python3-pip || error_and_quit "Dependancies installation failed." sudo apt-get install -qq libpq-dev python3-dev python-virtualenv git-core python3-pip || error_and_quit "Dependancies installation failed."
info "Dependancies via apt-get checked/installed." info "Dependancies via apt-get checked/installed."
if ! test -d "$GISSMO_DIR/.git/"; then if ! test -d "$GISSMO_DIR/.git/"; then
git clone -q $GIT_URL $GISSMO_DIR || error_and_quit "Fetch Git repository failed." git clone -q -b "$GIT_BRANCH" "$GIT_URL" "$GISSMO_DIR" || error_and_quit "Fetch Git repository failed."
else else
GIT_WORK_TREE="${GISSMO_DIR}/" GIT_DIR="${GISSMO_DIR}/.git/" git pull -q origin master || error_and_quit "Pull command on Git repository failed." GIT_WORK_TREE="${GISSMO_DIR}/" GIT_DIR="${GISSMO_DIR}/.git/" git pull -q origin master || error_and_quit "Pull command on Git repository failed."
fi fi
@ -117,20 +146,61 @@ init() {
deactivate || error_and_quit "Failed exiting python virtual environment." deactivate || error_and_quit "Failed exiting python virtual environment."
} }
test_migrate() {
test_start
}
migrate() {
test_migrate
$docker_cmd run -it --rm --link ${POSTGRES_DOCKER_NAME}:db gissmo:${GISSMO_VERSION} python manage.py migrate || error_and_quit "Failed to launch Gissmo migration."
}
restore() { restore() {
not_implemented not_implemented
} }
test_start() {
if test -z "$GISSMO_VERSION"; then
error_and_quit "No Gissmo version given (GISSMO_VERSION in config file)."
fi
if test -z "$GISSMO_DOCKER_PORT"; then
error_and_quit "No Gissmo Docker container port given (GISSMO_DOCKER_PORT in config file)."
fi
test_postgresql_exists
}
start() { start() {
not_implemented test_start
# Build Gissmo Docker images if missing
if [[ "$($docker_cmd images -q gissmo:${GISSMO_VERSION} 2> /dev/null)" == "" ]]; then
cd $GISSMO_DIR
$docker_cmd build -t gissmo:${GISSMO_VERSION} . || error_and_quit "Failed to build Gissmo Docker container."
cd $CURRENT_DIR
fi
# Check postgreSQL Docker container exists and is launched.
test_postgresql_launched
if [ $? -eq 1 ]; then
$docker_cmd start ${POSTGRES_DOCKER_NAME}
fi
# Run existing Docker container, otherwise create a new one
GISSMO_RUNNING=$($docker_cmd inspect --format="{{ .State.Running }}" ${GISSMO_DOCKER_NAME} 2> /dev/null)
if [ $? -eq 1 ]; then
$docker_cmd run -it --rm --link ${POSTGRES_DOCKER_NAME}:db -p ${GISSMO_DOCKER_PORT}:8000 --name ${GISSMO_DOCKER_NAME} gissmo:${GISSMO_VERSION} $ARGS || error_and_quit "Failed to create and launch ${GISSMO_DOCKER_NAME} container."
else
error_and_quit "${GISSMO_DOCKER_NAME} is already launched!"
fi
}
test_stop(){
GISSMO_RUNNING=$($docker_cmd inspect --format="{{ .State.Running }}" ${GISSMO_DOCKER_NAME})
if [ $? -eq 1 ]; then
error_and_quit "Gissmo Docker container not found: ${GISSMO_DOCKER_NAME}. Please launch '$PROGRAM launch' first."
fi
} }
stop() { stop() {
not_implemented $docker_cmd stop $GISSMO_DOCKER_NAME || error_and_quit "Failed to stop $GISSMO_DOCKER_NAME Docker container."
} info "${GISSMO_DOCKER_NAME} Docker container stopped successfully."
generate_database_persistent_volume() {
not_implemented
} }