run 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/command/with-contenv /bin/bash
  2. # shellcheck shell=bash
  3. # shellcheck disable=SC1091,SC1090
  4. # ================================
  5. # Postgres longrun service
  6. # ================================
  7. SCRIPT_ROOT=/etc/s6-overlay/scripts
  8. source "$SCRIPT_ROOT/base.sh"
  9. # The config should be ready before starting
  10. source "$GPUSTACK_POSTGRES_CONFIG"
  11. source "$SCRIPT_ROOT/default-variables.sh"
  12. PGDATA="${POSTGRES_DATA_DIR}"
  13. PGLOG="${LOG_DIR}/postgresql"
  14. PASS_FILE="${DATA_DIR}/postgres_root_pass"
  15. # Ensure data directory
  16. if [ ! -d "$PGDATA" ]; then
  17. echo "[INFO] Creating Postgres data directory at $PGDATA."
  18. createDir "$PGDATA" \
  19. && chown -R postgres:postgres "$PGDATA" \
  20. && chmod 0700 "$PGDATA"
  21. fi
  22. # Ensure log directory
  23. if [ ! -d "$PGLOG" ]; then
  24. echo "[INFO] Creating Postgres log directory at $PGLOG."
  25. createDir "$PGLOG" \
  26. && chown -R postgres:postgres "$PGLOG" \
  27. && chmod 0755 "$PGLOG"
  28. fi
  29. # Password setup
  30. ROOT_PASS=""
  31. if [ ! -f "$PASS_FILE" ]; then
  32. if [ -n "$POSTGRES_PASSWORD" ]; then
  33. ROOT_PASS="$POSTGRES_PASSWORD"
  34. else
  35. ROOT_PASS=$(tr -dc 'A-Za-z0-9!@#$%^&*_+=' </dev/urandom | head -c 16)
  36. ROOT_PASS="$(tr -dc 'A-Za-z' </dev/urandom | head -c 1)${ROOT_PASS:1}"
  37. fi
  38. echo "$ROOT_PASS" > "$PASS_FILE"
  39. chmod 600 "$PASS_FILE"
  40. echo "[INFO] Postgres root password written to $PASS_FILE."
  41. elif [ -f "$PASS_FILE" ]; then
  42. ROOT_PASS=$(cat "$PASS_FILE")
  43. echo "[INFO] Postgres root password read from $PASS_FILE."
  44. fi
  45. # reconfigure Postgres port
  46. PGCONFIG_FILE=/etc/postgresql/main/postgresql.conf
  47. gosu postgres sed -i "s/^port[[:space:]]*=[[:space:]]*[0-9]\+/port = $EMBEDDED_DATABASE_PORT/" "$PGCONFIG_FILE"
  48. gosu postgres sed -i "s|^#log_directory = 'log'|log_directory = '$PGLOG'|" "$PGCONFIG_FILE"
  49. # Initialize database
  50. if [ ! -s "$PGDATA/PG_VERSION" ]; then
  51. echo "[INFO] Initializing Postgres database."
  52. gosu postgres initdb -D "$PGDATA" > /dev/null
  53. fi
  54. gosu postgres pg_ctl -s -D "$PGDATA" -o "-c listen_addresses='localhost' -c port='$EMBEDDED_DATABASE_PORT'" -w start > /dev/null
  55. if ! gosu postgres psql -p "$EMBEDDED_DATABASE_PORT" -tAc "SELECT 1 FROM pg_roles WHERE rolname='root'" 2>/dev/null | grep -q 1; then
  56. gosu postgres psql -p "$EMBEDDED_DATABASE_PORT" -q --command "CREATE USER root WITH PASSWORD '$ROOT_PASS';" > /dev/null
  57. fi
  58. if ! gosu postgres psql -p "$EMBEDDED_DATABASE_PORT" -tAc "SELECT 1 FROM pg_database WHERE datname='gpustack'" 2>/dev/null | grep -q 1; then
  59. gosu postgres psql -p "$EMBEDDED_DATABASE_PORT" -q --command "CREATE DATABASE gpustack OWNER root;" > /dev/null
  60. fi
  61. ## correct collation version
  62. PG_COLLATION_QUERY="SELECT datname, datcollversion AS recorded_version, pg_database_collation_actual_version(oid) AS actual_version FROM pg_database WHERE datcollversion IS NOT NULL;"
  63. gosu postgres psql -p "$EMBEDDED_DATABASE_PORT" -c "$PG_COLLATION_QUERY" 2>/dev/null
  64. while IFS='|' read -r DBNAME RECORDED_VERSION ACTUAL_VERSION; do
  65. if [ -z "$DBNAME" ]; then continue; fi
  66. if [ "$RECORDED_VERSION" != "$ACTUAL_VERSION" ]; then
  67. echo "[INFO] Altering Postgres collation version for database '$DBNAME' from '$RECORDED_VERSION' to '$ACTUAL_VERSION'."
  68. echo "[INFO] - Reindexing all indexes for database '$DBNAME' (!!! THIS MAY TAKE A LONG TIME, DO NOT INTERRUPT !!!)."
  69. gosu postgres psql -p "$EMBEDDED_DATABASE_PORT" -d "$DBNAME" -tAc "REINDEX DATABASE \"$DBNAME\";" >/dev/null 2>&1
  70. echo "[INFO] - Updating recorded collation version for database '$DBNAME'."
  71. gosu postgres psql -p "$EMBEDDED_DATABASE_PORT" -d "$DBNAME" -tAc "ALTER DATABASE \"$DBNAME\" REFRESH COLLATION VERSION;" >/dev/null 2>&1
  72. echo "[INFO] Altered Postgres collation version for database '$DBNAME' to '$ACTUAL_VERSION'."
  73. fi
  74. done <<< "$(gosu postgres psql -p "$EMBEDDED_DATABASE_PORT" -tAc "$PG_COLLATION_QUERY" 2>/dev/null)"
  75. gosu postgres pg_ctl -p "$EMBEDDED_DATABASE_PORT" -s -D "$PGDATA" -m fast -w stop > /dev/null
  76. # Start Postgres
  77. echo "[INFO] Starting Postgres."
  78. exec s6-notifyoncheck \
  79. -d -w 5000 -n 10 -s 3000 \
  80. -- \
  81. gosu postgres /usr/bin/postgres \
  82. -D "$PGDATA" \
  83. -p "$EMBEDDED_DATABASE_PORT" \
  84. -c config_file=/etc/postgresql/main/postgresql.conf \
  85. -c hba_file=/etc/postgresql/main/pg_hba.conf