Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -Eeo pipefail
  3. # TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables)
  4.  
  5. # usage: file_env VAR [DEFAULT]
  6. # ie: file_env 'XYZ_DB_PASSWORD' 'example'
  7. # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
  8. # "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
  9. file_env() {
  10. local var="$1"
  11. local fileVar="${var}_FILE"
  12. local def="${2:-}"
  13. if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
  14. echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
  15. exit 1
  16. fi
  17. local val="$def"
  18. if [ "${!var:-}" ]; then
  19. val="${!var}"
  20. elif [ "${!fileVar:-}" ]; then
  21. val="$(< "${!fileVar}")"
  22. fi
  23. export "$var"="$val"
  24. unset "$fileVar"
  25. }
  26.  
  27. if [ "${1:0:1}" = '-' ]; then
  28. set -- postgres "$@"
  29. fi
  30.  
  31. # allow the container to be started with `--user`
  32. if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
  33. mkdir -p "$PGDATA"
  34. chown -R postgres "$PGDATA"
  35. chmod 700 "$PGDATA"
  36.  
  37. mkdir -p /var/run/postgresql
  38. chown -R postgres /var/run/postgresql
  39. chmod 775 /var/run/postgresql
  40.  
  41. # Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user
  42. if [ "$POSTGRES_INITDB_WALDIR" ]; then
  43. mkdir -p "$POSTGRES_INITDB_WALDIR"
  44. chown -R postgres "$POSTGRES_INITDB_WALDIR"
  45. chmod 700 "$POSTGRES_INITDB_WALDIR"
  46. fi
  47.  
  48. exec su-exec postgres "$BASH_SOURCE" "$@"
  49. fi
  50.  
  51. if [ "$1" = 'postgres' ]; then
  52. mkdir -p "$PGDATA"
  53. chown -R "$(id -u)" "$PGDATA" 2>/dev/null || :
  54. chmod 700 "$PGDATA" 2>/dev/null || :
  55.  
  56. # look specifically for PG_VERSION, as it is expected in the DB dir
  57. if [ ! -s "$PGDATA/PG_VERSION" ]; then
  58. # "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary
  59. # see https://github.com/docker-library/postgres/pull/253, https://github.com/docker-library/postgres/issues/359, https://cwrap.org/nss_wrapper.html
  60. if ! getent passwd "$(id -u)" &> /dev/null && [ -e /usr/lib/libnss_wrapper.so ]; then
  61. export LD_PRELOAD='/usr/lib/libnss_wrapper.so'
  62. export NSS_WRAPPER_PASSWD="$(mktemp)"
  63. export NSS_WRAPPER_GROUP="$(mktemp)"
  64. echo "postgres:x:$(id -u):$(id -g):PostgreSQL:$PGDATA:/bin/false" > "$NSS_WRAPPER_PASSWD"
  65. echo "postgres:x:$(id -g):" > "$NSS_WRAPPER_GROUP"
  66. fi
  67.  
  68. file_env 'POSTGRES_USER' 'postgres'
  69. file_env 'POSTGRES_PASSWORD'
  70.  
  71. file_env 'POSTGRES_INITDB_ARGS'
  72. if [ "$POSTGRES_INITDB_WALDIR" ]; then
  73. export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --waldir $POSTGRES_INITDB_WALDIR"
  74. fi
  75. eval 'initdb --username="$POSTGRES_USER" --pwfile=<(echo "$POSTGRES_PASSWORD") '"$POSTGRES_INITDB_ARGS"
  76.  
  77. # unset/cleanup "nss_wrapper" bits
  78. if [ "${LD_PRELOAD:-}" = '/usr/lib/libnss_wrapper.so' ]; then
  79. rm -f "$NSS_WRAPPER_PASSWD" "$NSS_WRAPPER_GROUP"
  80. unset LD_PRELOAD NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP
  81. fi
  82.  
  83. # check password first so we can output the warning before postgres
  84. # messes it up
  85. if [ -n "$POSTGRES_PASSWORD" ]; then
  86. authMethod=md5
  87. else
  88. # The - option suppresses leading tabs but *not* spaces. :)
  89. cat >&2 <<-'EOWARN'
  90. ****************************************************
  91. WARNING: No password has been set for the database.
  92. This will allow anyone with access to the
  93. Postgres port to access your database. In
  94. Docker's default configuration, this is
  95. effectively any other container on the same
  96. system.
  97.  
  98. Use "-e POSTGRES_PASSWORD=password" to set
  99. it in "docker run".
  100. ****************************************************
  101. EOWARN
  102.  
  103. authMethod=trust
  104. fi
  105.  
  106. {
  107. echo
  108. echo "host all all all $authMethod"
  109. } >> "$PGDATA/pg_hba.conf"
  110.  
  111. # internal start of server in order to allow set-up using psql-client
  112. # does not listen on external TCP/IP and waits until start finishes
  113. PGUSER="${PGUSER:-$POSTGRES_USER}" \
  114. pg_ctl -D "$PGDATA" \
  115. -o "-c listen_addresses=''" \
  116. -w start
  117.  
  118. file_env 'POSTGRES_DB' "$POSTGRES_USER"
  119.  
  120. export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
  121. psql=( psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password )
  122.  
  123. if [ "$POSTGRES_DB" != 'postgres' ]; then
  124. "${psql[@]}" --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
  125. CREATE DATABASE :"db" ;
  126. EOSQL
  127. echo
  128. fi
  129. psql+=( --dbname "$POSTGRES_DB" )
  130.  
  131. echo
  132. for f in /docker-entrypoint-initdb.d/*; do
  133. case "$f" in
  134. *.sh)
  135. # https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
  136. # https://github.com/docker-library/postgres/pull/452
  137. if [ -x "$f" ]; then
  138. echo "$0: running $f"
  139. "$f"
  140. else
  141. echo "$0: sourcing $f"
  142. . "$f"
  143. fi
  144. ;;
  145. *.sql) echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;;
  146. *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
  147. *) echo "$0: ignoring $f" ;;
  148. esac
  149. echo
  150. done
  151.  
  152. PGUSER="${PGUSER:-$POSTGRES_USER}" \
  153. pg_ctl -D "$PGDATA" -m fast -w stop
  154.  
  155. unset PGPASSWORD
  156.  
  157. echo
  158. echo 'PostgreSQL init process complete; ready for start up.'
  159. echo
  160. fi
  161. fi
  162.  
  163. exec "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement