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_CONTAINER:=tactical-meshcentral}"
|
|
|
|
: "${MESH_USER:=meshcentral}"
|
|
|
|
: "${MESH_PASS:=meshcentralpass}"
|
|
|
|
: "${MESH_HOST:=tactical-meshcentral}"
|
|
|
|
: "${API_HOST:=tactical-backend}"
|
|
|
|
: "${APP_HOST:=tactical-frontend}"
|
|
|
|
: "${REDIS_HOST:=tactical-redis}"
|
2021-01-16 16:20:24 +00:00
|
|
|
: "${HTTP_PROTOCOL:=http}"
|
|
|
|
: "${APP_PORT:=8080}"
|
|
|
|
: "${API_PORT:=8000}"
|
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 {
|
|
|
|
until (echo > /dev/tcp/"${POSTGRES_HOST}"/"${POSTGRES_PORT}") &> /dev/null; do
|
|
|
|
echo "waiting for postgresql container to be ready..."
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
|
|
|
|
until (echo > /dev/tcp/"${MESH_CONTAINER}"/443) &> /dev/null; do
|
|
|
|
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)
|
|
|
|
|
|
|
|
localvars="$(cat << EOF
|
|
|
|
SECRET_KEY = '${DJANGO_SEKRET}'
|
|
|
|
|
|
|
|
DEBUG = True
|
|
|
|
|
|
|
|
DOCKER_BUILD = True
|
|
|
|
|
|
|
|
CERT_FILE = '/opt/tactical/certs/fullchain.pem'
|
|
|
|
KEY_FILE = '/opt/tactical/certs/privkey.pem'
|
|
|
|
|
|
|
|
SCRIPTS_DIR = '${WORKSPACE_DIR}/scripts'
|
|
|
|
|
2021-01-16 02:05:49 +00:00
|
|
|
ALLOWED_HOSTS = ['${API_HOST}', '*']
|
2021-01-02 05:05:37 +00:00
|
|
|
|
|
|
|
ADMIN_URL = 'admin/'
|
|
|
|
|
|
|
|
CORS_ORIGIN_ALLOW_ALL = True
|
|
|
|
|
|
|
|
DATABASES = {
|
|
|
|
'default': {
|
|
|
|
'ENGINE': 'django.db.backends.postgresql',
|
|
|
|
'NAME': '${POSTGRES_DB}',
|
|
|
|
'USER': '${POSTGRES_USER}',
|
|
|
|
'PASSWORD': '${POSTGRES_PASS}',
|
|
|
|
'HOST': '${POSTGRES_HOST}',
|
|
|
|
'PORT': '${POSTGRES_PORT}',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
REST_FRAMEWORK = {
|
|
|
|
'DATETIME_FORMAT': '%b-%d-%Y - %H:%M',
|
|
|
|
|
|
|
|
'DEFAULT_PERMISSION_CLASSES': (
|
|
|
|
'rest_framework.permissions.IsAuthenticated',
|
|
|
|
),
|
|
|
|
'DEFAULT_AUTHENTICATION_CLASSES': (
|
|
|
|
'knox.auth.TokenAuthentication',
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
if not DEBUG:
|
|
|
|
REST_FRAMEWORK.update({
|
|
|
|
'DEFAULT_RENDERER_CLASSES': (
|
|
|
|
'rest_framework.renderers.JSONRenderer',
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
MESH_USERNAME = '${MESH_USER}'
|
|
|
|
MESH_SITE = 'https://${MESH_HOST}'
|
|
|
|
MESH_TOKEN_KEY = '${MESH_TOKEN}'
|
|
|
|
REDIS_HOST = '${REDIS_HOST}'
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
|
|
|
|
echo "${localvars}" > ${WORKSPACE_DIR}/api/tacticalrmm/tacticalrmm/local_settings.py
|
|
|
|
|
|
|
|
# run migrations and init scripts
|
2021-01-28 04:31:34 +00:00
|
|
|
"${VIRTUAL_ENV}"/bin/python manage.py migrate --no-input
|
|
|
|
"${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-01-02 05:05:37 +00:00
|
|
|
|
|
|
|
# create super user
|
|
|
|
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}"
|
|
|
|
|
|
|
|
# setup Python virtual env and install dependencies
|
2021-02-24 04:45:08 +00:00
|
|
|
! test -e "${VIRTUAL_ENV}" && python -m venv ${VIRTUAL_ENV}
|
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
|
|
|
|
|
|
|
|
# create .env file for frontend
|
|
|
|
webenv="$(cat << EOF
|
2021-01-16 16:20:24 +00:00
|
|
|
PROD_URL = "${HTTP_PROTOCOL}://${API_HOST}"
|
|
|
|
DEV_URL = "${HTTP_PROTOCOL}://${API_HOST}"
|
|
|
|
APP_URL = https://${APP_HOST}
|
2021-01-02 05:05:37 +00:00
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
echo "${webenv}" | tee ${WORKSPACE_DIR}/web/.env > /dev/null
|
|
|
|
|
|
|
|
# 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
|
2021-01-28 04:31:34 +00:00
|
|
|
cp "${WORKSPACE_DIR}"/api/tacticalrmm/core/goinstaller/bin/goversioninfo /usr/local/bin/goversioninfo
|
2021-01-16 02:05:49 +00:00
|
|
|
chmod +x /usr/local/bin/goversioninfo
|
|
|
|
|
2021-01-02 05:05:37 +00:00
|
|
|
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
|