101 lines
2.9 KiB
Bash
101 lines
2.9 KiB
Bash
#!/usr/bin/env bash
|
|
# One-shot initializer for Seahub (collectstatic, migrate, optional admin creation)
|
|
# Run under supervisord before starting gunicorn
|
|
set -Eeuo pipefail
|
|
|
|
SEAFILE_HOME=${SEAFILE_HOME:-/opt/seafile}
|
|
SEAF_RELEASE_DIR_DEFAULT="${SEAFILE_HOME}/seafile-server-latest"
|
|
SEAF_RELEASE_DIR="${SEAF_RELEASE_DIR:-$SEAF_RELEASE_DIR_DEFAULT}"
|
|
SEAFILE_CONF_DIR=${SEAFILE_CONF_DIR:-/data/conf}
|
|
SEAFILE_DATA_DIR=${SEAFILE_DATA_DIR:-/data/seafile-data}
|
|
|
|
ADMIN_EMAIL=${ADMIN_EMAIL:-}
|
|
ADMIN_PASSWORD=${ADMIN_PASSWORD:-}
|
|
|
|
MYSQLADMIN_BIN=${MYSQLADMIN_BIN:-/usr/bin/mysqladmin}
|
|
MYSQL_SOCKET=${MYSQL_SOCKET:-/run/mysqld/mysqld.sock}
|
|
|
|
log() {
|
|
echo "[seahub-init $(date +'%Y-%m-%dT%H:%M:%S%z')] $*"
|
|
}
|
|
|
|
wait_for_mariadb() {
|
|
local timeout="${1:-120}"
|
|
local i=0
|
|
log "Waiting for MariaDB to become ready..."
|
|
while ! "${MYSQLADMIN_BIN}" ping --silent --protocol=socket --socket="${MYSQL_SOCKET}" >/dev/null 2>&1; do
|
|
sleep 1
|
|
i=$((i+1))
|
|
if [[ $i -ge $timeout ]]; then
|
|
log "MariaDB did not become ready in ${timeout}s"
|
|
return 1
|
|
fi
|
|
done
|
|
log "MariaDB is ready."
|
|
}
|
|
|
|
main() {
|
|
# Avoid re-running if we've already initialized (basic sentinel)
|
|
local sentinel="${SEAFILE_CONF_DIR}/.seahub_initialized"
|
|
if [[ -f "${sentinel}" ]]; then
|
|
log "Seahub already initialized, nothing to do."
|
|
return 0
|
|
fi
|
|
|
|
wait_for_mariadb || true
|
|
|
|
local venv="${SEAFILE_HOME}/venv"
|
|
local seahub_dir="${SEAF_RELEASE_DIR}/seahub"
|
|
|
|
if [[ ! -x "${venv}/bin/python" ]]; then
|
|
log "Python venv missing at ${venv}; skipping init."
|
|
return 0
|
|
fi
|
|
if [[ ! -d "${seahub_dir}" ]]; then
|
|
log "Seahub directory missing at ${seahub_dir}; skipping init."
|
|
return 0
|
|
fi
|
|
|
|
pushd "${seahub_dir}" >/dev/null
|
|
export PYTHONPATH="${SEAF_RELEASE_DIR}:${PYTHONPATH:-}"
|
|
export DJANGO_SETTINGS_MODULE="seahub.settings"
|
|
export CCNET_CONF_DIR="${SEAFILE_CONF_DIR}"
|
|
export SEAFILE_CENTRAL_CONF_DIR="${SEAFILE_CONF_DIR}"
|
|
export SEAFILE_CONF_DIR="${SEAFILE_CONF_DIR}"
|
|
export SEAFILE_DATA_DIR="${SEAFILE_DATA_DIR}"
|
|
|
|
log "Running collectstatic --noinput"
|
|
if ! "${venv}/bin/python" manage.py collectstatic --noinput; then
|
|
log "collectstatic failed (continuing)"
|
|
fi
|
|
|
|
log "Running migrate --noinput"
|
|
if ! "${venv}/bin/python" manage.py migrate --noinput; then
|
|
log "migrate failed (continuing)"
|
|
fi
|
|
|
|
if [[ -n "${ADMIN_EMAIL}" && -n "${ADMIN_PASSWORD}" ]]; then
|
|
log "Ensuring admin user ${ADMIN_EMAIL}"
|
|
"${venv}/bin/python" manage.py shell <<PY || true
|
|
from django.contrib.auth import get_user_model
|
|
User = get_user_model()
|
|
email="${ADMIN_EMAIL}"
|
|
password="${ADMIN_PASSWORD}"
|
|
if not User.objects.filter(email=email).exists():
|
|
User.objects.create_superuser(email=email, password=password)
|
|
else:
|
|
u = User.objects.get(email=email)
|
|
if not u.is_superuser:
|
|
u.is_superuser = True
|
|
u.save()
|
|
PY
|
|
fi
|
|
|
|
popd >/dev/null
|
|
|
|
touch "${sentinel}"
|
|
log "Seahub initialization complete."
|
|
}
|
|
|
|
main "$@"
|