168 lines
4.3 KiB
Bash
Executable File
168 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# CONFIG
|
|
|
|
## Default
|
|
PROGRAM=`basename $0`
|
|
GIT_URL="git@github.com:eost/gissmo.git"
|
|
GIT_BRANCH="dev" # Default Git Branch
|
|
GIT_DIR_NAME="gissmo"
|
|
DOCKERFILE_NAME="Dockerfile"
|
|
DB_DIR="/dbdata"
|
|
POSTGRES_VERSION="9.5"
|
|
|
|
## User
|
|
. config || exit 1
|
|
|
|
## Composed
|
|
GISSMO_DIR="${PROJECT_DIR}/${GIT_DIR_NAME}"
|
|
DOCKERFILE="${GISSMO_DIR}/${DOCKERFILE_NAME}"
|
|
|
|
COMMAND="$1"
|
|
|
|
|
|
# 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:"
|
|
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 " - restore "
|
|
echo -e " - start "
|
|
echo -e " - stop "
|
|
}
|
|
|
|
check_command() {
|
|
if test $1 == "create"; then
|
|
return 0
|
|
elif test $1 == "init"; then
|
|
return 0
|
|
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
|
|
}
|
|
|
|
test_create() {
|
|
docker_cmd=`which docker`
|
|
if test -z "$docker_cmd"; then
|
|
error_and_quit "Docker command not found. Install docker or fill in the PATH."
|
|
fi
|
|
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
|
|
}
|
|
|
|
create() {
|
|
test_create
|
|
init
|
|
$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."
|
|
}
|
|
|
|
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
|
|
git clone -q $GIT_URL $GISSMO_DIR || error_and_quit "Fetch Git repository failed."
|
|
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."
|
|
}
|
|
|
|
restore() {
|
|
not_implemented
|
|
}
|
|
|
|
start() {
|
|
not_implemented
|
|
}
|
|
|
|
stop() {
|
|
not_implemented
|
|
}
|
|
|
|
generate_database_persistent_volume() {
|
|
not_implemented
|
|
}
|
|
|
|
|
|
|
|
# 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
|