From 187c9b09716d9bd7f50bb1437322874fb66968f7 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 17 Aug 2021 23:10:57 +1000 Subject: [PATCH] Add server init script - Taken (mostly) from https://github.com/inventree/InvenTree/pull/1949 --- docker/Dockerfile | 2 +- docker/init-server.sh | 69 +++++++++++++++++++++++++++++++++++++++++++ tasks.py | 2 +- 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 docker/init-server.sh diff --git a/docker/Dockerfile b/docker/Dockerfile index 3d86d73eca..26d57e4e47 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -114,7 +114,7 @@ ENV INVENTREE_DEV_DIR="${INVENTREE_HOME}/dev" # Location for python virtual environment # If the INVENTREE_PY_ENV variable is set, the entrypoint script will use it! -ENV INVENTREE_PY_ENV="${INVENTREE_HOME}/dev/env" +ENV INVENTREE_PY_ENV="${INVENTREE_DEV}/env" # Override default path settings ENV INVENTREE_STATIC_ROOT="${INVENTREE_DEV_DIR}/static" diff --git a/docker/init-server.sh b/docker/init-server.sh new file mode 100644 index 0000000000..48603815fc --- /dev/null +++ b/docker/init-server.sh @@ -0,0 +1,69 @@ +#!/bin/sh +# exit when any command fails +set -e + +# Create required directory structure (if it does not already exist) +if [[ ! -d "$INVENTREE_STATIC_ROOT" ]]; then + echo "Creating directory $INVENTREE_STATIC_ROOT" + mkdir -p $INVENTREE_STATIC_ROOT +fi + +if [[ ! -d "$INVENTREE_MEDIA_ROOT" ]]; then + echo "Creating directory $INVENTREE_MEDIA_ROOT" + mkdir -p $INVENTREE_MEDIA_ROOT +fi + +# Check if "config.yaml" has been copied into the correct location +if test -f "$INVENTREE_CONFIG_FILE"; then + echo "$INVENTREE_CONFIG_FILE exists - skipping" +else + echo "Copying config file to $INVENTREE_CONFIG_FILE" + cp $INVENTREE_HOME/InvenTree/config_template.yaml $INVENTREE_CONFIG_FILE +fi + +# Setup a python virtual environment +# This should be done on the *mounted* filesystem, +# so that the installed modules persist! +if [[ -n "$INVENTREE_PY_ENV" ]]; then + echo "Using Python virtual environment: ${INVENTREE_PY_ENV}" + # Setup a virtual environment (within the "dev" directory) + python3 -m venv ${INVENTREE_PY_ENV} + + # Activate the virtual environment + source ${INVENTREE_PY_ENV}/bin/activate + + echo "Installing required packages..." + pip install --no-cache-dir -U -r ${INVENTREE_HOME}/requirements.txt +fi + +# Wait for the InvenTree database to be ready +cd ${INVENTREE_MNG_DIR} +echo "InvenTree: Waiting for database connection" +python3 manage.py wait_for_db && echo "InvenTree: db found, sleeping 10" || { echo "InvenTree: Failed to connect to db due to errors, aborting"; exit 1; } +sleep 10 + +# Check database migrations +cd ${INVENTREE_HOME} + +# We assume at this stage that the database is up and running +# Ensure that the database schema are up to date +echo "InvenTree: Checking database..." +invoke check || exit 1 +echo "InvenTree: Check successful" +echo "InvenTree: Database Migrations..." +invoke migrate || exit 1 +echo "InvenTree: Migrations successful" +echo "InvenTree: Collecting static files..." +# Note: "translate" calls "static" also +invoke translate || exit 1 +echo "InvenTree: static successful" + +# Can be run as a cron job or directly to clean out expired sessions. +cd ${INVENTREE_MNG_DIR} +python3 manage.py clearsessions || exit 1 +echo "InvenTree: migrations complete" + +#Launch the CMD +#echo "init-server launching $@" +#exec "$@" +#echo "init-server exiting" diff --git a/tasks.py b/tasks.py index b3aaab2f92..837207f1ec 100644 --- a/tasks.py +++ b/tasks.py @@ -156,7 +156,7 @@ def migrate(c): print("========================================") manage(c, "makemigrations") - manage(c, "migrate") + manage(c, "migrate --noinput") manage(c, "migrate --run-syncdb") manage(c, "check")