|
1 | 1 | #!/bin/sh |
2 | | -set -e |
3 | 2 |
|
4 | | -# Check if DATABASE_URL is not set |
5 | | -if [ -z "$DATABASE_URL" ]; then |
6 | | - # Check if the individual database variables are set and construct the URL |
7 | | - if [ -n "$DATABASE_HOST" ] && [ -n "$DATABASE_USERNAME" ] && [ -n "$DATABASE_PASSWORD" ] && [ -n "$DATABASE_NAME" ]; then |
8 | | - DATABASE_URL="postgresql://${DATABASE_USERNAME}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}" |
| 3 | +# Exit immediately if a command fails |
| 4 | +set -e |
| 5 | +# Disable auto-exporting of variables |
| 6 | +set +a |
| 7 | + |
| 8 | +# If a CONFIG_PATH is set, resolve the environment overrides from the config file. |
| 9 | +# The overrides will be written into variables scopped to the current shell. This is |
| 10 | +# required in case one of the variables used in this entrypoint is overriden (e.g., |
| 11 | +# DATABASE_URL, REDIS_URL, etc.) |
| 12 | +if [ -n "$CONFIG_PATH" ]; then |
| 13 | + echo -e "\e[34m[Info] Resolving environment overrides from $CONFIG_PATH...\e[0m" |
| 14 | + |
| 15 | + set +e # Disable exist on error so we can capture EXIT_CODE |
| 16 | + OVERRIDES_OUTPUT=$(SKIP_ENV_VALIDATION=1 yarn tool:resolve-env-overrides 2>&1) |
| 17 | + EXIT_CODE=$? |
| 18 | + set -e # Re-enable exit on error |
| 19 | + |
| 20 | + if [ $EXIT_CODE -eq 0 ]; then |
| 21 | + eval "$OVERRIDES_OUTPUT" |
| 22 | + else |
| 23 | + echo -e "\e[31m[Error] Failed to resolve environment overrides.\e[0m" |
| 24 | + echo "$OVERRIDES_OUTPUT" |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | +fi |
9 | 28 |
|
10 | | - if [ -n "$DATABASE_ARGS" ]; then |
11 | | - DATABASE_URL="${DATABASE_URL}?$DATABASE_ARGS" |
12 | | - fi |
| 29 | +# Descontruct the database URL from the individual variables if DATABASE_URL is not set |
| 30 | +if [ -z "$DATABASE_URL" ] && [ -n "$DATABASE_HOST" ] && [ -n "$DATABASE_USERNAME" ] && [ -n "$DATABASE_PASSWORD" ] && [ -n "$DATABASE_NAME" ]; then |
| 31 | + DATABASE_URL="postgresql://${DATABASE_USERNAME}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}" |
13 | 32 |
|
14 | | - export DATABASE_URL |
15 | | - else |
16 | | - # Otherwise, fallback to a default value |
17 | | - DATABASE_URL="postgresql://postgres@localhost:5432/sourcebot" |
18 | | - export DATABASE_URL |
| 33 | + if [ -n "$DATABASE_ARGS" ]; then |
| 34 | + DATABASE_URL="${DATABASE_URL}?$DATABASE_ARGS" |
19 | 35 | fi |
20 | 36 | fi |
21 | 37 |
|
22 | | -if [ "$DATABASE_URL" = "postgresql://postgres@localhost:5432/sourcebot" ]; then |
23 | | - DATABASE_EMBEDDED="true" |
| 38 | +if [ -z "$DATABASE_URL" ]; then |
| 39 | + echo -e "\e[34m[Info] DATABASE_URL is not set. Using embeded database.\e[0m" |
| 40 | + export DATABASE_EMBEDDED="true" |
| 41 | + export DATABASE_URL="postgresql://postgres@localhost:5432/sourcebot" |
| 42 | +else |
| 43 | + export DATABASE_EMBEDDED="false" |
| 44 | +fi |
| 45 | + |
| 46 | +if [ -z "$REDIS_URL" ]; then |
| 47 | + echo -e "\e[34m[Info] REDIS_URL is not set. Using embeded redis.\e[0m" |
| 48 | + export REDIS_EMBEDDED="true" |
| 49 | + export REDIS_URL="redis://localhost:6379" |
| 50 | +else |
| 51 | + export REDIS_EMBEDDED="false" |
24 | 52 | fi |
25 | 53 |
|
| 54 | + |
26 | 55 | echo -e "\e[34m[Info] Sourcebot version: $NEXT_PUBLIC_SOURCEBOT_VERSION\e[0m" |
27 | 56 |
|
28 | 57 | # If we don't have a PostHog key, then we need to disable telemetry. |
@@ -59,7 +88,7 @@ if [ "$DATABASE_EMBEDDED" = "true" ] && [ ! -d "$DATABASE_DATA_DIR" ]; then |
59 | 88 | fi |
60 | 89 |
|
61 | 90 | # Create the redis data directory if it doesn't exist |
62 | | -if [ ! -d "$REDIS_DATA_DIR" ]; then |
| 91 | +if [ "$REDIS_EMBEDDED" = "true" ] && [ ! -d "$REDIS_DATA_DIR" ]; then |
63 | 92 | mkdir -p $REDIS_DATA_DIR |
64 | 93 | fi |
65 | 94 |
|
|
149 | 178 |
|
150 | 179 | echo "{\"version\": \"$NEXT_PUBLIC_SOURCEBOT_VERSION\", \"install_id\": \"$SOURCEBOT_INSTALL_ID\"}" > "$FIRST_RUN_FILE" |
151 | 180 |
|
152 | | - |
153 | 181 | # Start the database and wait for it to be ready before starting any other service |
154 | 182 | if [ "$DATABASE_EMBEDDED" = "true" ]; then |
155 | 183 | su postgres -c "postgres -D $DATABASE_DATA_DIR" & |
|
171 | 199 |
|
172 | 200 | # Run a Database migration |
173 | 201 | echo -e "\e[34m[Info] Running database migration...\e[0m" |
174 | | -yarn workspace @sourcebot/db prisma:migrate:prod |
| 202 | +DATABASE_URL="$DATABASE_URL" yarn workspace @sourcebot/db prisma:migrate:prod |
175 | 203 |
|
176 | 204 | # Create the log directory |
177 | 205 | mkdir -p /var/log/sourcebot |
|
0 commit comments