|
5 | 5 | # Disable auto-exporting of variables |
6 | 6 | set +a |
7 | 7 |
|
| 8 | +# Detect if running as root |
| 9 | +IS_ROOT=false |
| 10 | +if [ "$(id -u)" -eq 0 ]; then |
| 11 | + IS_ROOT=true |
| 12 | +fi |
| 13 | + |
| 14 | +if [ "$IS_ROOT" = "true" ]; then |
| 15 | + echo -e "\e[34m[Info] Running as root user.\e[0m" |
| 16 | +else |
| 17 | + echo -e "\e[34m[Info] Running as non-root user.\e[0m" |
| 18 | +fi |
| 19 | + |
8 | 20 | # If a CONFIG_PATH is set, resolve the environment overrides from the config file. |
9 | 21 | # The overrides will be written into variables scopped to the current shell. This is |
10 | 22 | # required in case one of the variables used in this entrypoint is overriden (e.g., |
|
83 | 95 | # Check if DATABASE_DATA_DIR exists, if not initialize it |
84 | 96 | if [ "$DATABASE_EMBEDDED" = "true" ] && [ ! -d "$DATABASE_DATA_DIR" ]; then |
85 | 97 | echo -e "\e[34m[Info] Initializing database at $DATABASE_DATA_DIR...\e[0m" |
86 | | - mkdir -p $DATABASE_DATA_DIR && chown -R postgres:postgres "$DATABASE_DATA_DIR" |
87 | | - su postgres -c "initdb -D $DATABASE_DATA_DIR" |
| 98 | + mkdir -p $DATABASE_DATA_DIR |
| 99 | + if [ "$IS_ROOT" = "true" ]; then |
| 100 | + chown -R postgres:postgres "$DATABASE_DATA_DIR" |
| 101 | + su postgres -c "initdb -D $DATABASE_DATA_DIR" |
| 102 | + else |
| 103 | + initdb -D "$DATABASE_DATA_DIR" -U postgres |
| 104 | + fi |
88 | 105 | fi |
89 | 106 |
|
90 | 107 | # Create the redis data directory if it doesn't exist |
@@ -180,13 +197,31 @@ echo "{\"version\": \"$NEXT_PUBLIC_SOURCEBOT_VERSION\", \"install_id\": \"$SOURC |
180 | 197 |
|
181 | 198 | # Start the database and wait for it to be ready before starting any other service |
182 | 199 | if [ "$DATABASE_EMBEDDED" = "true" ]; then |
183 | | - su postgres -c "postgres -D $DATABASE_DATA_DIR" & |
| 200 | + if [ "$IS_ROOT" = "true" ]; then |
| 201 | + su postgres -c "postgres -D $DATABASE_DATA_DIR" & |
| 202 | + else |
| 203 | + postgres -D "$DATABASE_DATA_DIR" & |
| 204 | + fi |
| 205 | + |
184 | 206 | until pg_isready -h localhost -p 5432 -U postgres; do |
185 | 207 | echo -e "\e[34m[Info] Waiting for the database to be ready...\e[0m" |
186 | 208 | sleep 1 |
| 209 | + |
| 210 | + # As postgres runs in the background, we must check if it is still |
| 211 | + # running, otherwise the "until" loop will be running indefinitely. |
| 212 | + if ! pgrep -x "postgres" > /dev/null; then |
| 213 | + echo "postgres failed to run" |
| 214 | + exit 1 |
| 215 | + fi |
187 | 216 | done |
| 217 | + |
| 218 | + if [ "$IS_ROOT" = "false" ]; then |
| 219 | + # Running as non-root we need to ensure the postgres account is created. |
| 220 | + psql -U postgres -tc "SELECT 1 FROM pg_roles WHERE rolname='postgres'" | grep -q 1 \ |
| 221 | + || createuser postgres -s |
| 222 | + fi |
188 | 223 |
|
189 | | - # Check if the database already exists, and create it if it dne |
| 224 | + # Check if the database already exists, and create it if it doesn't exist |
190 | 225 | EXISTING_DB=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = 'sourcebot'") |
191 | 226 |
|
192 | 227 | if [ "$EXISTING_DB" = "1" ]; then |
|
201 | 236 | echo -e "\e[34m[Info] Running database migration...\e[0m" |
202 | 237 | DATABASE_URL="$DATABASE_URL" yarn workspace @sourcebot/db prisma:migrate:prod |
203 | 238 |
|
204 | | -# Create the log directory |
| 239 | +# Create the log directory if it doesn't exist |
205 | 240 | mkdir -p /var/log/sourcebot |
206 | 241 |
|
207 | 242 | # Run supervisord |
|
0 commit comments