2016-01-12 14:03:52 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# CONFIG
|
|
|
|
|
|
|
|
## Default
|
|
|
|
PROGRAM=`basename $0`
|
2016-01-12 16:55:42 +00:00
|
|
|
docker_cmd=`which docker`
|
|
|
|
CURRENT_DIR=$(pwd)
|
2016-01-12 14:03:52 +00:00
|
|
|
GIT_URL="git@github.com:eost/gissmo.git"
|
|
|
|
GIT_BRANCH="dev" # Default Git Branch
|
|
|
|
GIT_DIR_NAME="gissmo"
|
|
|
|
DOCKERFILE_NAME="Dockerfile"
|
2016-01-12 14:42:05 +00:00
|
|
|
DB_DIR="/dbdata"
|
|
|
|
POSTGRES_VERSION="9.5"
|
2016-01-12 16:55:42 +00:00
|
|
|
GISSMO_DOCKER_NAME="gissmo"
|
2016-01-12 14:03:52 +00:00
|
|
|
|
|
|
|
## User
|
|
|
|
. config || exit 1
|
|
|
|
|
|
|
|
## Composed
|
|
|
|
GISSMO_DIR="${PROJECT_DIR}/${GIT_DIR_NAME}"
|
|
|
|
DOCKERFILE="${GISSMO_DIR}/${DOCKERFILE_NAME}"
|
|
|
|
|
|
|
|
COMMAND="$1"
|
2016-01-12 16:55:42 +00:00
|
|
|
shift 1; ARGS="$@"
|
2016-01-12 14:03:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# METHODS
|
|
|
|
error_and_continue() {
|
|
|
|
echo "ERROR: $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
error_and_quit() {
|
|
|
|
error_and_continue "$1"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
info() {
|
|
|
|
echo "INFO: $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
not_implemented() {
|
|
|
|
error_and_quit "Not implemented"
|
|
|
|
}
|
|
|
|
|
|
|
|
show_help() {
|
|
|
|
echo "$PROGRAM [COMMAND]"
|
|
|
|
echo -e " Where COMMAND is one of:"
|
2016-01-12 14:42:05 +00:00
|
|
|
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"
|
2016-01-12 16:55:42 +00:00
|
|
|
echo -e " - migrate Launch migration command: python manage.py migrate"
|
2016-01-12 14:03:52 +00:00
|
|
|
echo -e " - restore "
|
2016-01-12 16:55:42 +00:00
|
|
|
echo -e " - start Launch Gissmo Docker container (created by 'create' command) with a name given in GISSMO_DOCKER_NAME variable"
|
|
|
|
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()
|
2016-01-12 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
check_command() {
|
|
|
|
if test $1 == "create"; then
|
|
|
|
return 0
|
|
|
|
elif test $1 == "init"; then
|
|
|
|
return 0
|
2016-01-12 16:55:42 +00:00
|
|
|
elif test $1 == "migrate"; then
|
|
|
|
return 0
|
2016-01-12 14:03:52 +00:00
|
|
|
elif test $1 == "restore"; then
|
|
|
|
return 0
|
|
|
|
elif test $1 == "start"; then
|
|
|
|
return 0
|
|
|
|
elif test $1 == "stop"; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
error_and_continue "Unknown command."
|
|
|
|
show_help
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2016-01-12 16:55:42 +00:00
|
|
|
test_docker_cmd() {
|
2016-01-12 14:42:05 +00:00
|
|
|
if test -z "$docker_cmd"; then
|
|
|
|
error_and_quit "Docker command not found. Install docker or fill in the PATH."
|
|
|
|
fi
|
2016-01-12 16:55:42 +00:00
|
|
|
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
|
2016-01-12 14:42:05 +00:00
|
|
|
if test -z "$DB_DIR"; then
|
|
|
|
error_and_quit "No database directory given (DB_DIR in config file)."
|
|
|
|
fi
|
|
|
|
if test -z "$DB_VOLUME_NAME"; then
|
|
|
|
error_and_quit "No database volume name given (DB_VOLUME_NAME in config file)."
|
|
|
|
fi
|
|
|
|
if test -z "$POSTGRES_VERSION"; then
|
|
|
|
error_and_quit "No postgres version given (POSTGRES_VERSION in config file)."
|
|
|
|
fi
|
|
|
|
if test -z "$POSTGRES_DOCKER_NAME"; then
|
|
|
|
error_and_quit "You need a name for your Docker database container (POSTGRES_DOCKER_NAME in config file)."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-01-12 14:03:52 +00:00
|
|
|
create() {
|
2016-01-12 14:42:05 +00:00
|
|
|
test_create
|
2016-01-12 14:03:52 +00:00
|
|
|
init
|
2016-01-12 14:42:05 +00:00
|
|
|
$docker_cmd create -v "${DB_DIR}:/var/lib/postgresql/data" --name "${DB_VOLUME_NAME}" "postgres:${POSTGRES_VERSION}" || error_and_quit "Initial database 'docker create' failed."
|
|
|
|
$docker_cmd run -d --volumes-from "${DB_VOLUME_NAME}" --name "${POSTGRES_DOCKER_NAME}" "postgres:${POSTGRES_VERSION}" || error_and_quit "Launching database docker container failed."
|
2016-01-12 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
# check directory existence
|
|
|
|
if ! test -d "$PROJECT_DIR"; then
|
|
|
|
mkdir -p "$PROJECT_DIR" || error_and_quit "Create ${PROJECT_DIR} failed."
|
|
|
|
info "$PROJECT_DIR created."
|
|
|
|
echo "${PROJECT_DIR} created."
|
|
|
|
virtualenv -p python3 $PROJECT_DIR || error_and_quit "Create ${PROJECT_DIR} python virtual environment failed."
|
|
|
|
info "Python virtual environment initialized."
|
|
|
|
fi
|
|
|
|
cd $PROJECT_DIR
|
|
|
|
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."
|
|
|
|
if ! test -d "$GISSMO_DIR/.git/"; then
|
2016-01-12 16:55:42 +00:00
|
|
|
git clone -q -b "$GIT_BRANCH" "$GIT_URL" "$GISSMO_DIR" || error_and_quit "Fetch Git repository failed."
|
2016-01-12 14:03:52 +00:00
|
|
|
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."
|
|
|
|
fi
|
|
|
|
info "Git repository checked."
|
|
|
|
. bin/activate || error_and_quit "Failed entering python virtual environment"
|
|
|
|
cd $GISSMO_DIR
|
|
|
|
pip install -q -r requirements.txt || error_and_quit "Python dependancies installation failed."
|
|
|
|
info "Python dependancies checked."
|
|
|
|
deactivate || error_and_quit "Failed exiting python virtual environment."
|
|
|
|
}
|
|
|
|
|
2016-01-12 16:55:42 +00:00
|
|
|
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."
|
|
|
|
}
|
|
|
|
|
2016-01-12 14:03:52 +00:00
|
|
|
restore() {
|
|
|
|
not_implemented
|
|
|
|
}
|
|
|
|
|
2016-01-12 16:55:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-12 14:03:52 +00:00
|
|
|
start() {
|
2016-01-12 16:55:42 +00:00
|
|
|
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
|
2016-01-12 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 16:55:42 +00:00
|
|
|
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
|
2016-01-12 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 16:55:42 +00:00
|
|
|
stop() {
|
|
|
|
$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."
|
2016-01-12 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# CHECK CONFIG
|
|
|
|
if test -z $PROJECT_DIR; then
|
|
|
|
error_and_quit "No project directory given."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test -z $GIT_BRANCH; then
|
|
|
|
error_and_quit "No git branch given."
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Don't check repository if INIT COMMAND is given
|
|
|
|
if ! test -d $GIT_REPOSITORY && ! test "$COMMAND" == "init"; then
|
|
|
|
error_and_quit "Repository don't exists: ${GIT_REPOSITORY}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test -z $DOCKERFILE_NAME; then
|
|
|
|
error_and_quit "No docker filename given."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! test -f $DOCKERFILE && ! test "$COMMAND" == "init"; then
|
|
|
|
error_and_quit "Docker file don't exists: ${DOCKERFILE}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! test -z $COMMAND; then
|
|
|
|
check_command "$COMMAND"
|
|
|
|
$COMMAND
|
|
|
|
else
|
|
|
|
show_help
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 1
|