2024-08-31 01:36:53 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
set -e
|
|
|
|
|
|
2024-10-17 18:50:07 +00:00
|
|
|
echo -e "\e[34m[Info] Sourcebot version: $SOURCEBOT_VERSION\e[0m"
|
|
|
|
|
|
2024-11-26 05:04:52 +00:00
|
|
|
# If we don't have a PostHog key, then we need to disable telemetry.
|
|
|
|
|
if [ -z "$POSTHOG_PAPIK" ]; then
|
|
|
|
|
echo -e "\e[33m[Warning] POSTHOG_PAPIK was not set. Setting SOURCEBOT_TELEMETRY_DISABLED.\e[0m"
|
2025-03-24 04:21:14 +00:00
|
|
|
export SOURCEBOT_TELEMETRY_DISABLED=true
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -n "$SOURCEBOT_TELEMETRY_DISABLED" ]; then
|
|
|
|
|
# Validate that SOURCEBOT_TELEMETRY_DISABLED is either "true" or "false"
|
|
|
|
|
if [ "$SOURCEBOT_TELEMETRY_DISABLED" != "true" ] && [ "$SOURCEBOT_TELEMETRY_DISABLED" != "false" ]; then
|
|
|
|
|
echo -e "\e[31m[Error] SOURCEBOT_TELEMETRY_DISABLED must be either 'true' or 'false'. Got '$SOURCEBOT_TELEMETRY_DISABLED'\e[0m"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
export SOURCEBOT_TELEMETRY_DISABLED=false
|
2024-11-26 05:04:52 +00:00
|
|
|
fi
|
|
|
|
|
|
2024-09-28 23:47:07 +00:00
|
|
|
# Issue a info message about telemetry
|
2025-03-24 04:21:14 +00:00
|
|
|
if [ "$SOURCEBOT_TELEMETRY_DISABLED" = "true" ]; then
|
2024-09-28 23:47:07 +00:00
|
|
|
echo -e "\e[34m[Info] Disabling telemetry since SOURCEBOT_TELEMETRY_DISABLED was set.\e[0m"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check if DATA_CACHE_DIR exists, if not create it
|
|
|
|
|
if [ ! -d "$DATA_CACHE_DIR" ]; then
|
|
|
|
|
mkdir -p "$DATA_CACHE_DIR"
|
|
|
|
|
fi
|
|
|
|
|
|
2025-01-23 18:23:46 +00:00
|
|
|
# Check if DB_DATA_DIR exists, if not initialize it
|
|
|
|
|
if [ ! -d "$DB_DATA_DIR" ]; then
|
|
|
|
|
echo -e "\e[34m[Info] Initializing database at $DB_DATA_DIR...\e[0m"
|
|
|
|
|
mkdir -p $DB_DATA_DIR && chown -R postgres:postgres "$DB_DATA_DIR"
|
|
|
|
|
su postgres -c "initdb -D $DB_DATA_DIR"
|
|
|
|
|
fi
|
2025-01-14 21:37:31 +00:00
|
|
|
|
2025-01-27 22:07:07 +00:00
|
|
|
if [ -z "$SOURCEBOT_ENCRYPTION_KEY" ]; then
|
2025-02-13 21:23:30 +00:00
|
|
|
echo -e "\e[33m[Warning] SOURCEBOT_ENCRYPTION_KEY is not set.\e[0m"
|
2025-01-27 22:07:07 +00:00
|
|
|
|
|
|
|
|
if [ -f "$DATA_CACHE_DIR/.secret" ]; then
|
|
|
|
|
echo -e "\e[34m[Info] Loading environment variables from $DATA_CACHE_DIR/.secret\e[0m"
|
|
|
|
|
else
|
|
|
|
|
echo -e "\e[34m[Info] Generating a new encryption key...\e[0m"
|
|
|
|
|
SOURCEBOT_ENCRYPTION_KEY=$(openssl rand -base64 24)
|
|
|
|
|
echo "SOURCEBOT_ENCRYPTION_KEY=\"$SOURCEBOT_ENCRYPTION_KEY\"" >> "$DATA_CACHE_DIR/.secret"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
set -a
|
|
|
|
|
. "$DATA_CACHE_DIR/.secret"
|
|
|
|
|
set +a
|
|
|
|
|
fi
|
|
|
|
|
|
2025-02-13 21:23:30 +00:00
|
|
|
# @see : https://authjs.dev/getting-started/deployment#auth_secret
|
|
|
|
|
if [ -z "$AUTH_SECRET" ]; then
|
|
|
|
|
echo -e "\e[33m[Warning] AUTH_SECRET is not set.\e[0m"
|
|
|
|
|
|
|
|
|
|
if [ -f "$DATA_CACHE_DIR/.authjs-secret" ]; then
|
|
|
|
|
echo -e "\e[34m[Info] Loading environment variables from $DATA_CACHE_DIR/.authjs-secret\e[0m"
|
|
|
|
|
else
|
|
|
|
|
echo -e "\e[34m[Info] Generating a new encryption key...\e[0m"
|
|
|
|
|
AUTH_SECRET=$(openssl rand -base64 33)
|
|
|
|
|
echo "AUTH_SECRET=\"$AUTH_SECRET\"" >> "$DATA_CACHE_DIR/.authjs-secret"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
set -a
|
|
|
|
|
. "$DATA_CACHE_DIR/.authjs-secret"
|
|
|
|
|
set +a
|
|
|
|
|
fi
|
|
|
|
|
|
2025-03-24 04:21:14 +00:00
|
|
|
if [ -z "$AUTH_URL" ]; then
|
|
|
|
|
echo -e "\e[33m[Warning] AUTH_URL is not set.\e[0m"
|
|
|
|
|
export AUTH_URL="http://localhost:3000"
|
|
|
|
|
fi
|
|
|
|
|
|
2024-09-28 23:47:07 +00:00
|
|
|
# In order to detect if this is the first run, we create a `.installed` file in
|
|
|
|
|
# the cache directory.
|
2024-11-10 00:40:07 +00:00
|
|
|
FIRST_RUN_FILE="$DATA_CACHE_DIR/.installedv2"
|
|
|
|
|
|
2024-09-28 23:47:07 +00:00
|
|
|
if [ ! -f "$FIRST_RUN_FILE" ]; then
|
|
|
|
|
touch "$FIRST_RUN_FILE"
|
2024-11-10 00:40:07 +00:00
|
|
|
export SOURCEBOT_INSTALL_ID=$(uuidgen)
|
|
|
|
|
|
2024-09-28 23:47:07 +00:00
|
|
|
# If this is our first run, send a `install` event to PostHog
|
|
|
|
|
# (if telemetry is enabled)
|
2025-03-24 04:21:14 +00:00
|
|
|
if [ "$SOURCEBOT_TELEMETRY_DISABLED" = "false" ]; then
|
2025-01-14 23:09:20 +00:00
|
|
|
if ! ( curl -L --output /dev/null --silent --fail --header "Content-Type: application/json" -d '{
|
2024-11-26 05:04:52 +00:00
|
|
|
"api_key": "'"$POSTHOG_PAPIK"'",
|
2024-09-28 23:47:07 +00:00
|
|
|
"event": "install",
|
2024-11-10 00:40:07 +00:00
|
|
|
"distinct_id": "'"$SOURCEBOT_INSTALL_ID"'",
|
2024-10-17 18:50:07 +00:00
|
|
|
"properties": {
|
|
|
|
|
"sourcebot_version": "'"$SOURCEBOT_VERSION"'"
|
|
|
|
|
}
|
2025-01-14 23:09:20 +00:00
|
|
|
}' https://us.i.posthog.com/capture/ ) then
|
|
|
|
|
echo -e "\e[33m[Warning] Failed to send install event.\e[0m"
|
|
|
|
|
fi
|
2024-09-28 23:47:07 +00:00
|
|
|
fi
|
2024-11-10 00:40:07 +00:00
|
|
|
else
|
|
|
|
|
export SOURCEBOT_INSTALL_ID=$(cat "$FIRST_RUN_FILE" | jq -r '.install_id')
|
|
|
|
|
PREVIOUS_VERSION=$(cat "$FIRST_RUN_FILE" | jq -r '.version')
|
|
|
|
|
|
|
|
|
|
# If the version has changed, we assume an upgrade has occurred.
|
|
|
|
|
if [ "$PREVIOUS_VERSION" != "$SOURCEBOT_VERSION" ]; then
|
|
|
|
|
echo -e "\e[34m[Info] Upgraded from version $PREVIOUS_VERSION to $SOURCEBOT_VERSION\e[0m"
|
|
|
|
|
|
2025-03-24 04:21:14 +00:00
|
|
|
if [ "$SOURCEBOT_TELEMETRY_DISABLED" = "false" ]; then
|
2025-01-14 23:09:20 +00:00
|
|
|
if ! ( curl -L --output /dev/null --silent --fail --header "Content-Type: application/json" -d '{
|
2024-11-26 05:04:52 +00:00
|
|
|
"api_key": "'"$POSTHOG_PAPIK"'",
|
2024-11-10 00:40:07 +00:00
|
|
|
"event": "upgrade",
|
|
|
|
|
"distinct_id": "'"$SOURCEBOT_INSTALL_ID"'",
|
|
|
|
|
"properties": {
|
|
|
|
|
"from_version": "'"$PREVIOUS_VERSION"'",
|
|
|
|
|
"to_version": "'"$SOURCEBOT_VERSION"'"
|
|
|
|
|
}
|
2025-01-14 23:09:20 +00:00
|
|
|
}' https://us.i.posthog.com/capture/ ) then
|
|
|
|
|
echo -e "\e[33m[Warning] Failed to send upgrade event.\e[0m"
|
|
|
|
|
fi
|
2024-11-10 00:40:07 +00:00
|
|
|
fi
|
|
|
|
|
fi
|
2024-09-28 23:47:07 +00:00
|
|
|
fi
|
|
|
|
|
|
2024-11-10 00:40:07 +00:00
|
|
|
echo "{\"version\": \"$SOURCEBOT_VERSION\", \"install_id\": \"$SOURCEBOT_INSTALL_ID\"}" > "$FIRST_RUN_FILE"
|
|
|
|
|
|
2024-09-28 06:38:32 +00:00
|
|
|
|
2025-01-23 18:23:46 +00:00
|
|
|
# Start the database and wait for it to be ready before starting any other service
|
2025-02-22 01:13:35 +00:00
|
|
|
if [ "$DATABASE_URL" = "postgresql://postgres@localhost:5432/sourcebot" ]; then
|
|
|
|
|
su postgres -c "postgres -D $DB_DATA_DIR" &
|
|
|
|
|
until pg_isready -h localhost -p 5432 -U postgres; do
|
|
|
|
|
echo -e "\e[34m[Info] Waiting for the database to be ready...\e[0m"
|
|
|
|
|
sleep 1
|
|
|
|
|
done
|
2025-01-23 18:23:46 +00:00
|
|
|
|
2025-02-22 01:13:35 +00:00
|
|
|
# Check if the database already exists, and create it if it dne
|
|
|
|
|
EXISTING_DB=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'")
|
2025-01-23 18:23:46 +00:00
|
|
|
|
2025-02-22 01:13:35 +00:00
|
|
|
if [ "$EXISTING_DB" = "1" ]; then
|
|
|
|
|
echo "Database '$DB_NAME' already exists; skipping creation."
|
|
|
|
|
else
|
|
|
|
|
echo "Creating database '$DB_NAME'..."
|
|
|
|
|
psql -U postgres -c "CREATE DATABASE \"$DB_NAME\""
|
|
|
|
|
fi
|
2025-01-23 18:23:46 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Run a Database migration
|
|
|
|
|
echo -e "\e[34m[Info] Running database migration...\e[0m"
|
|
|
|
|
yarn workspace @sourcebot/db prisma:migrate:prod
|
|
|
|
|
|
2025-03-01 00:38:42 +00:00
|
|
|
# Create the log directory
|
|
|
|
|
mkdir -p /var/log/sourcebot
|
|
|
|
|
|
2024-11-18 20:09:26 +00:00
|
|
|
# Run supervisord
|
2024-09-05 20:00:27 +00:00
|
|
|
exec supervisord -c /etc/supervisor/conf.d/supervisord.conf
|