run 4.2 KB

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