Simplify init scripts

Single script init.sh which performs the following tasks:
- Creates required directory structure
- Activates python venv (if required)
- Waits for database connection
- Runs command
This commit is contained in:
Oliver 2021-08-18 09:52:27 +10:00
parent 3b8ee48581
commit 7bfddd6d51
8 changed files with 34 additions and 176 deletions

View File

@ -73,8 +73,6 @@ RUN apk add --no-cache git make bash \
# Install required python packages # Install required python packages
RUN pip install --no-cache-dir -U psycopg2 mysqlclient pgcli mariadb RUN pip install --no-cache-dir -U psycopg2 mysqlclient pgcli mariadb
ENTRYPOINT ["/sbin/tini", "--"]
FROM base as production FROM base as production
# Clone source code # Clone source code
@ -96,7 +94,7 @@ USER inventree
RUN pip3 install --no-cache-dir --disable-pip-version-check -r ${INVENTREE_HOME}/requirements.txt RUN pip3 install --no-cache-dir --disable-pip-version-check -r ${INVENTREE_HOME}/requirements.txt
# Server init entrypoint # Server init entrypoint
ENTRYPOINT ./docker/init-server.sh ENTRYPOINT ["/bin/bash", "./docker/init.sh"]
# Launch the production server # Launch the production server
CMD ["gunicorn -c ./docker/gunicorn.conf.py -b ${INVENTREE_WEB_ADDR}:${INVENTREE_WEB_PORT} InvenTree.wsgi"] CMD ["gunicorn -c ./docker/gunicorn.conf.py -b ${INVENTREE_WEB_ADDR}:${INVENTREE_WEB_PORT} InvenTree.wsgi"]
@ -120,8 +118,9 @@ ENV INVENTREE_SECRET_KEY_FILE="${INVENTREE_DEV_DIR}/secret_key.txt"
WORKDIR ${INVENTREE_HOME} WORKDIR ${INVENTREE_HOME}
# Entrypoint # Entrypoint ensures that we are running in the python virtual environment
ENTRYPOINT ./docker/init-server.sh ENTRYPOINT ["/bin/bash", "./docker/init.sh"]
# Launch the development server # Launch the development server
CMD ["python3 manage.py runserver ${INVENTREE_WEB_ADDR}:${INVENTREE_WEB_PORT}"] CMD ["invoke", "server", "-a", "${INVENTREE_WEB_ADDR}:${INVENTREE_WEB_PORT}"]

View File

@ -35,7 +35,7 @@ services:
build: build:
context: . context: .
target: dev target: dev
entrypoint: /home/inventree/docker/start_dev_worker.sh command: invoke worker
depends_on: depends_on:
- inventree-dev-server - inventree-dev-server
volumes: volumes:

View File

@ -32,38 +32,25 @@ if [[ -n "$INVENTREE_PY_ENV" ]]; then
# Activate the virtual environment # Activate the virtual environment
source ${INVENTREE_PY_ENV}/bin/activate source ${INVENTREE_PY_ENV}/bin/activate
echo "Installing required packages..." # Note: Python packages will have to be installed on first run
pip install --no-cache-dir -U -r ${INVENTREE_HOME}/requirements.txt # e.g docker-compose -f docker-compose.dev.yml run inventree-dev-server invoke install
fi fi
# Wait for the InvenTree database to be ready # Wait for the InvenTree database to be ready
cd ${INVENTREE_MNG_DIR} # cd ${INVENTREE_MNG_DIR}
echo "InvenTree: Waiting for database connection" # 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; } # invoke wait && echo "InvenTree: Database connection successful" || { echo "InvenTree: Failed to connect to db due to errors, aborting"; exit 1; }
sleep 10 # sleep 5
# Check database migrations
cd ${INVENTREE_HOME} cd ${INVENTREE_HOME}
# We assume at this stage that the database is up and running # 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..."
echo "InvenTree: Checking database..." # invoke check || exit 1
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. # Can be run as a cron job or directly to clean out expired sessions.
cd ${INVENTREE_MNG_DIR} # cd ${INVENTREE_MNG_DIR}
python3 manage.py clearsessions || exit 1 # python3 manage.py clearsessions || exit 1
echo "InvenTree: migrations complete"
#Launch the CMD # Launch the CMD *after* the ENTRYPOINT completes
#echo "init-server launching $@" exec "$@"
#exec "$@"
#echo "init-server exiting"

View File

@ -1,51 +0,0 @@
#!/bin/sh
# 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 virtual environment (within the "dev" directory)
python3 -m venv ./dev/env
# Activate the virtual environment
source ./dev/env/bin/activate
echo "Installing required packages..."
pip install --no-cache-dir -U -r ${INVENTREE_HOME}/requirements.txt
echo "Starting InvenTree server..."
# Wait for the database to be ready
cd ${INVENTREE_HOME}/InvenTree
python3 manage.py wait_for_db
sleep 10
echo "Running InvenTree database migrations..."
# We assume at this stage that the database is up and running
# Ensure that the database schema are up to date
python3 manage.py check || exit 1
python3 manage.py migrate --noinput || exit 1
python3 manage.py migrate --run-syncdb || exit 1
python3 manage.py clearsessions || exit 1
invoke static
# Launch a development server
python3 manage.py runserver ${INVENTREE_WEB_ADDR}:${INVENTREE_WEB_PORT}

View File

@ -1,19 +0,0 @@
#!/bin/sh
echo "Starting InvenTree worker..."
cd $INVENTREE_HOME
# Activate virtual environment
source ./dev/env/bin/activate
sleep 5
# Wait for the database to be ready
cd InvenTree
python3 manage.py wait_for_db
sleep 10
# Now we can launch the background worker process
python3 manage.py qcluster

View File

@ -1,42 +0,0 @@
#!/bin/sh
# 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
echo "Starting InvenTree server..."
# Wait for the database to be ready
cd $INVENTREE_MNG_DIR
python3 manage.py wait_for_db
sleep 10
echo "Running InvenTree database migrations and collecting static files..."
# We assume at this stage that the database is up and running
# Ensure that the database schema are up to date
python3 manage.py check || exit 1
python3 manage.py migrate --noinput || exit 1
python3 manage.py migrate --run-syncdb || exit 1
python3 manage.py prerender || exit 1
python3 manage.py collectstatic --noinput || exit 1
python3 manage.py clearsessions || exit 1
# Now we can launch the server
gunicorn -c $INVENTREE_HOME/gunicorn.conf.py InvenTree.wsgi -b 0.0.0.0:$INVENTREE_WEB_PORT

View File

@ -1,14 +0,0 @@
#!/bin/sh
echo "Starting InvenTree worker..."
sleep 5
# Wait for the database to be ready
cd $INVENTREE_MNG_DIR
python3 manage.py wait_for_db
sleep 10
# Now we can launch the background worker process
python3 manage.py qcluster

View File

@ -65,7 +65,7 @@ def manage(c, cmd, pty=False):
cmd - django command to run cmd - django command to run
""" """
c.run('cd "{path}" && python3 manage.py {cmd}'.format( result = c.run('cd "{path}" && python3 manage.py {cmd}'.format(
path=managePyDir(), path=managePyDir(),
cmd=cmd cmd=cmd
), pty=pty) ), pty=pty)
@ -80,14 +80,6 @@ def install(c):
# Install required Python packages with PIP # Install required Python packages with PIP
c.run('pip3 install -U -r requirements.txt') c.run('pip3 install -U -r requirements.txt')
# If a config.yaml file does not exist, copy from the template!
CONFIG_FILE = os.path.join(localDir(), 'InvenTree', 'config.yaml')
CONFIG_TEMPLATE_FILE = os.path.join(localDir(), 'InvenTree', 'config_template.yaml')
if not os.path.exists(CONFIG_FILE):
print("Config file 'config.yaml' does not exist - copying from template.")
copyfile(CONFIG_TEMPLATE_FILE, CONFIG_FILE)
@task @task
def shell(c): def shell(c):
@ -97,13 +89,6 @@ def shell(c):
manage(c, 'shell', pty=True) manage(c, 'shell', pty=True)
@task
def worker(c):
"""
Run the InvenTree background worker process
"""
manage(c, 'qcluster', pty=True)
@task @task
def superuser(c): def superuser(c):
@ -113,6 +98,7 @@ def superuser(c):
manage(c, 'createsuperuser', pty=True) manage(c, 'createsuperuser', pty=True)
@task @task
def check(c): def check(c):
""" """
@ -121,13 +107,24 @@ def check(c):
manage(c, "check") manage(c, "check")
@task @task
def wait(c): def wait(c):
""" """
Wait until the database connection is ready Wait until the database connection is ready
""" """
manage(c, "wait_for_db") return manage(c, "wait_for_db")
@task(pre=[wait])
def worker(c):
"""
Run the InvenTree background worker process
"""
manage(c, 'qcluster', pty=True)
@task @task
def rebuild(c): def rebuild(c):
@ -137,6 +134,7 @@ def rebuild(c):
manage(c, "rebuild_models") manage(c, "rebuild_models")
@task @task
def clean_settings(c): def clean_settings(c):
""" """
@ -145,7 +143,7 @@ def clean_settings(c):
manage(c, "clean_settings") manage(c, "clean_settings")
@task @task(post=[rebuild])
def migrate(c): def migrate(c):
""" """
Performs database migrations. Performs database migrations.