Files
docmost-cloudron/start.sh

134 lines
4.3 KiB
Bash

#!/bin/bash
set -eu
echo "=> Starting Docmost setup"
# Initialize /app/data if it's empty (first run)
if [ ! -f /app/data/.initialized ]; then
echo "=> Initializing data directory"
mkdir -p /app/data/uploads /app/data/logs
chown -R cloudron:cloudron /app/data
touch /app/data/.initialized
fi
# Generate APP_SECRET if not exists
if [ ! -f /app/data/app_secret ]; then
echo "=> Generating APP_SECRET"
openssl rand -hex 32 > /app/data/app_secret
chown cloudron:cloudron /app/data/app_secret
chmod 600 /app/data/app_secret
fi
APP_SECRET=$(cat /app/data/app_secret)
# Set up environment variables
export NODE_ENV=production
export APP_URL="${CLOUDRON_APP_ORIGIN}"
export APP_SECRET="${APP_SECRET}"
export PORT=3001
# Database configuration
export DATABASE_URL="${CLOUDRON_POSTGRESQL_URL}"
# Redis configuration - parse Cloudron format to standard format
echo "=> Configuring Redis..."
# Cloudron provides Redis addon with specific environment variables
# The CLOUDRON_REDIS_URL is in format: redis://:password@host:port
# We need to ensure it's in the correct format for ioredis
if [ -n "${CLOUDRON_REDIS_URL}" ]; then
# Extract components from the URL
# Format: redis://:password@host:port/database
REDIS_PASSWORD=$(echo "$CLOUDRON_REDIS_URL" | sed -n 's|redis://:\([^@]*\)@.*|\1|p')
REDIS_HOST=$(echo "$CLOUDRON_REDIS_URL" | sed -n 's|redis://[^@]*@\([^:]*\):.*|\1|p')
REDIS_PORT=$(echo "$CLOUDRON_REDIS_URL" | sed -n 's|redis://[^@]*@[^:]*:\([0-9]*\).*|\1|p')
# Default port if not found
if [ -z "$REDIS_PORT" ]; then
REDIS_PORT="6379"
fi
# Export individual components for Docmost
export REDIS_HOST="${REDIS_HOST}"
export REDIS_PORT="${REDIS_PORT}"
export REDIS_PASSWORD="${REDIS_PASSWORD}"
# Also export the full URL in case it's needed
export REDIS_URL="redis://:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}/0"
echo "=> Redis configured: host=${REDIS_HOST}, port=${REDIS_PORT}"
else
echo "=> Warning: Redis URL not provided"
# Set dummy Redis URL to prevent validation errors
export REDIS_URL="redis://localhost:6379"
export REDIS_HOST="localhost"
export REDIS_PORT="6379"
export REDIS_PASSWORD=""
fi
# Email configuration
export MAIL_DRIVER=smtp
export SMTP_HOST="${CLOUDRON_MAIL_SMTP_SERVER}"
export SMTP_PORT="${CLOUDRON_MAIL_SMTP_PORT}"
export SMTP_USERNAME="${CLOUDRON_MAIL_SMTP_USERNAME}"
export SMTP_PASSWORD="${CLOUDRON_MAIL_SMTP_PASSWORD}"
export MAIL_FROM_ADDRESS="${CLOUDRON_MAIL_FROM}"
# Storage configuration (using local storage)
export STORAGE_DRIVER=local
export STORAGE_LOCAL_PATH="/app/data/uploads"
# Authentication will use built-in email/password system
echo "=> Using built-in email/password authentication"
# File upload configuration
export FILE_UPLOAD_SIZE_LIMIT="50mb"
# JWT configuration
export JWT_TOKEN_EXPIRES_IN="30d"
# Wait for Redis to be available
if [ -n "${CLOUDRON_REDIS_URL}" ]; then
echo "=> Waiting for Redis to be available..."
for i in {1..30}; do
if nc -z "${REDIS_HOST}" "${REDIS_PORT}" 2>/dev/null; then
echo "=> Redis is available"
break
fi
if [ $i -eq 30 ]; then
echo "=> Warning: Redis not reachable after 30 seconds"
fi
sleep 1
done
fi
echo "=> Running database migrations"
cd /app/code
chown -R cloudron:cloudron /app/data
# Find the correct prisma binary path
PRISMA_BIN=""
if [ -f "/app/code/apps/server/node_modules/.bin/prisma" ]; then
PRISMA_BIN="/app/code/apps/server/node_modules/.bin/prisma"
elif [ -f "/app/code/node_modules/.bin/prisma" ]; then
PRISMA_BIN="/app/code/node_modules/.bin/prisma"
elif [ -f "/app/code/node_modules/@prisma/cli/build/index.js" ]; then
PRISMA_BIN="/usr/bin/node /app/code/node_modules/@prisma/cli/build/index.js"
fi
if [ -n "$PRISMA_BIN" ]; then
echo "=> Found Prisma at: $PRISMA_BIN"
cd /app/code/apps/server
sudo -u cloudron $PRISMA_BIN migrate deploy || true
else
echo "=> Warning: Prisma binary not found, skipping migrations"
fi
# Create nginx temp directories
mkdir -p /tmp/nginx_client_temp /tmp/nginx_proxy_temp /tmp/nginx_fastcgi_temp /tmp/nginx_uwsgi_temp /tmp/nginx_scgi_temp
chown -R cloudron:cloudron /tmp/nginx_*
echo "=> Starting services with supervisor"
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf