tacticalrmm/.devcontainer/entrypoint.sh

194 lines
6.3 KiB
Bash
Raw Normal View History

2021-01-02 05:05:37 +00:00
#!/usr/bin/env bash
set -e
: "${TRMM_USER:=tactical}"
: "${TRMM_PASS:=tactical}"
: "${POSTGRES_HOST:=tactical-postgres}"
: "${POSTGRES_PORT:=5432}"
: "${POSTGRES_USER:=tactical}"
: "${POSTGRES_PASS:=tactical}"
: "${POSTGRES_DB:=tacticalrmm}"
: "${MESH_SERVICE:=tactical-meshcentral}"
: "${MESH_WS_URL:=ws://${MESH_SERVICE}:4443}"
2021-01-02 05:05:37 +00:00
: "${MESH_USER:=meshcentral}"
: "${MESH_PASS:=meshcentralpass}"
: "${MESH_HOST:=tactical-meshcentral}"
: "${API_HOST:=tactical-backend}"
: "${REDIS_HOST:=tactical-redis}"
2021-01-16 16:20:24 +00:00
: "${API_PORT:=8000}"
2021-01-02 05:05:37 +00:00
2022-01-06 14:56:33 +00:00
: "${CERT_PRIV_PATH:=${TACTICAL_DIR}/certs/privkey.pem}"
: "${CERT_PUB_PATH:=${TACTICAL_DIR}/certs/fullchain.pem}"
2021-01-02 05:05:37 +00:00
# Add python venv to path
export PATH="${VIRTUAL_ENV}/bin:$PATH"
function check_tactical_ready {
sleep 15
until [ -f "${TACTICAL_READY_FILE}" ]; do
echo "waiting for init container to finish install or update..."
sleep 10
done
}
function django_setup {
2024-10-06 06:17:15 +00:00
until (echo >/dev/tcp/"${POSTGRES_HOST}"/"${POSTGRES_PORT}") &>/dev/null; do
2021-01-02 05:05:37 +00:00
echo "waiting for postgresql container to be ready..."
sleep 5
done
2024-10-06 06:17:15 +00:00
until (echo >/dev/tcp/"${MESH_SERVICE}"/4443) &>/dev/null; do
2021-01-02 05:05:37 +00:00
echo "waiting for meshcentral container to be ready..."
sleep 5
done
echo "setting up django environment"
# configure django settings
2021-01-28 04:31:34 +00:00
MESH_TOKEN="$(cat ${TACTICAL_DIR}/tmp/mesh_token)"
2021-01-02 05:05:37 +00:00
DJANGO_SEKRET=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 80 | head -n 1)
2024-10-06 06:17:15 +00:00
2024-10-31 19:06:39 +00:00
BASE_DOMAIN=$(echo "import tldextract; no_fetch_extract = tldextract.TLDExtract(suffix_list_urls=()); extracted = no_fetch_extract('${API_HOST}'); print(f'{extracted.domain}.{extracted.suffix}')" | python)
2024-09-16 02:22:45 +00:00
2024-10-06 06:17:15 +00:00
localvars="$(
cat <<EOF
2021-01-02 05:05:37 +00:00
SECRET_KEY = '${DJANGO_SEKRET}'
DEBUG = True
DOCKER_BUILD = True
SWAGGER_ENABLED = True
CERT_FILE = '${CERT_PUB_PATH}'
KEY_FILE = '${CERT_PRIV_PATH}'
2021-01-02 05:05:37 +00:00
SCRIPTS_DIR = '/community-scripts'
2021-01-02 05:05:37 +00:00
ADMIN_URL = 'admin/'
2024-09-16 02:22:45 +00:00
ALLOWED_HOSTS = ['${API_HOST}', '${APP_HOST}', '*']
2024-10-18 21:50:21 +00:00
CORS_ORIGIN_WHITELIST = ['https://${APP_HOST}']
2024-09-16 02:22:45 +00:00
SESSION_COOKIE_DOMAIN = '${BASE_DOMAIN}'
CSRF_COOKIE_DOMAIN = '${BASE_DOMAIN}'
CSRF_TRUSTED_ORIGINS = ['https://${API_HOST}', 'https://${APP_HOST}']
2024-10-31 19:06:39 +00:00
HEADLESS_FRONTEND_URLS = {'socialaccount_login_error': 'https://${APP_HOST}/account/provider/callback'}
2021-01-02 05:05:37 +00:00
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '${POSTGRES_DB}',
'USER': '${POSTGRES_USER}',
'PASSWORD': '${POSTGRES_PASS}',
'HOST': '${POSTGRES_HOST}',
'PORT': '${POSTGRES_PORT}',
2023-04-29 19:32:39 +00:00
},
'reporting': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '${POSTGRES_DB}',
'USER': 'reporting_user',
'PASSWORD': 'read_password',
'HOST': '${POSTGRES_HOST}',
'PORT': '${POSTGRES_PORT}',
'OPTIONS': {
'options': '-c default_transaction_read_only=on'
}
2021-01-02 05:05:37 +00:00
}
}
MESH_USERNAME = '${MESH_USER}'
MESH_SITE = 'https://${MESH_HOST}'
MESH_TOKEN_KEY = '${MESH_TOKEN}'
REDIS_HOST = '${REDIS_HOST}'
MESH_WS_URL = '${MESH_WS_URL}'
2021-02-27 23:19:35 +00:00
ADMIN_ENABLED = True
2023-11-23 04:40:47 +00:00
TRMM_INSECURE = True
2021-01-02 05:05:37 +00:00
EOF
2024-10-06 06:17:15 +00:00
)"
2021-01-02 05:05:37 +00:00
2024-10-06 06:17:15 +00:00
echo "${localvars}" >${WORKSPACE_DIR}/api/tacticalrmm/tacticalrmm/local_settings.py
2021-01-02 05:05:37 +00:00
# run migrations and init scripts
"${VIRTUAL_ENV}"/bin/python manage.py pre_update_tasks
2021-01-28 04:31:34 +00:00
"${VIRTUAL_ENV}"/bin/python manage.py migrate --no-input
2023-05-03 21:58:19 +00:00
"${VIRTUAL_ENV}"/bin/python manage.py generate_json_schemas
2021-01-28 04:31:34 +00:00
"${VIRTUAL_ENV}"/bin/python manage.py collectstatic --no-input
"${VIRTUAL_ENV}"/bin/python manage.py initial_db_setup
"${VIRTUAL_ENV}"/bin/python manage.py initial_mesh_setup
"${VIRTUAL_ENV}"/bin/python manage.py load_chocos
"${VIRTUAL_ENV}"/bin/python manage.py load_community_scripts
"${VIRTUAL_ENV}"/bin/python manage.py reload_nats
2021-11-13 21:30:01 +00:00
"${VIRTUAL_ENV}"/bin/python manage.py create_natsapi_conf
"${VIRTUAL_ENV}"/bin/python manage.py create_installer_user
"${VIRTUAL_ENV}"/bin/python manage.py post_update_tasks
2021-01-02 05:05:37 +00:00
2024-10-06 06:17:15 +00:00
# create super user
2021-01-02 05:05:37 +00:00
echo "from accounts.models import User; User.objects.create_superuser('${TRMM_USER}', 'admin@example.com', '${TRMM_PASS}') if not User.objects.filter(username='${TRMM_USER}').exists() else 0;" | python manage.py shell
}
if [ "$1" = 'tactical-init-dev' ]; then
# make directories if they don't exist
2021-01-28 04:31:34 +00:00
mkdir -p "${TACTICAL_DIR}/tmp"
2021-01-02 05:05:37 +00:00
test -f "${TACTICAL_READY_FILE}" && rm "${TACTICAL_READY_FILE}"
mkdir -p /meshcentral-data
mkdir -p ${TACTICAL_DIR}/tmp
mkdir -p ${TACTICAL_DIR}/certs
2023-05-03 21:58:19 +00:00
mkdir -p ${TACTICAL_DIR}/reporting
mkdir -p ${TACTICAL_DIR}/reporting/assets
mkdir -p /mongo/data/db
mkdir -p /redis/data
touch /meshcentral-data/.initialized && chown -R 1000:1000 /meshcentral-data
touch ${TACTICAL_DIR}/tmp/.initialized && chown -R 1000:1000 ${TACTICAL_DIR}
touch ${TACTICAL_DIR}/certs/.initialized && chown -R 1000:1000 ${TACTICAL_DIR}/certs
touch /mongo/data/db/.initialized && chown -R 1000:1000 /mongo/data/db
touch /redis/data/.initialized && chown -R 1000:1000 /redis/data
2023-05-03 21:58:19 +00:00
touch ${TACTICAL_DIR}/reporting && chown -R 1000:1000 ${TACTICAL_DIR}/reporting
mkdir -p ${TACTICAL_DIR}/api/tacticalrmm/private/exe
mkdir -p ${TACTICAL_DIR}/api/tacticalrmm/private/log
touch ${TACTICAL_DIR}/api/tacticalrmm/private/log/django_debug.log
2021-01-02 05:05:37 +00:00
# setup Python virtual env and install dependencies
2021-02-24 04:45:08 +00:00
! test -e "${VIRTUAL_ENV}" && python -m venv ${VIRTUAL_ENV}
2022-03-10 00:57:55 +00:00
"${VIRTUAL_ENV}"/bin/python -m pip install --upgrade pip
"${VIRTUAL_ENV}"/bin/pip install --no-cache-dir setuptools wheel
2021-01-28 04:31:34 +00:00
"${VIRTUAL_ENV}"/bin/pip install --no-cache-dir -r /requirements.txt
2021-01-02 05:05:37 +00:00
django_setup
# chown everything to tactical user
chown -R "${TACTICAL_USER}":"${TACTICAL_USER}" "${WORKSPACE_DIR}"
chown -R "${TACTICAL_USER}":"${TACTICAL_USER}" "${TACTICAL_DIR}"
# create install ready file
su -c "echo 'tactical-init' > ${TACTICAL_READY_FILE}" "${TACTICAL_USER}"
fi
if [ "$1" = 'tactical-api' ]; then
check_tactical_ready
2021-01-28 04:31:34 +00:00
"${VIRTUAL_ENV}"/bin/python manage.py runserver 0.0.0.0:"${API_PORT}"
2021-01-02 05:05:37 +00:00
fi
if [ "$1" = 'tactical-celery-dev' ]; then
check_tactical_ready
2021-01-28 04:31:34 +00:00
"${VIRTUAL_ENV}"/bin/celery -A tacticalrmm worker -l debug
2021-01-02 05:05:37 +00:00
fi
if [ "$1" = 'tactical-celerybeat-dev' ]; then
check_tactical_ready
test -f "${WORKSPACE_DIR}/api/tacticalrmm/celerybeat.pid" && rm "${WORKSPACE_DIR}/api/tacticalrmm/celerybeat.pid"
2021-01-28 04:31:34 +00:00
"${VIRTUAL_ENV}"/bin/celery -A tacticalrmm beat -l debug
2021-01-02 05:05:37 +00:00
fi
if [ "$1" = 'tactical-websockets-dev' ]; then
check_tactical_ready
"${VIRTUAL_ENV}"/bin/daphne tacticalrmm.asgi:application --port 8383 -b 0.0.0.0
fi