Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

docker-entrypoint.sh 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env bash
  2. set -e
  3. if [ "${POSTGRES_INDEX}" != "" ]
  4. then
  5. set_var_value()
  6. {
  7. var_name="${1}"_"${POSTGRES_INDEX}"
  8. export "${1}"="${!var_name}"
  9. }
  10. set_var_value POSTGRES_DISABLED
  11. set_var_value POSTGRES_SLAVE_MODE
  12. set_var_value POSTGRES_MASTER_MODE
  13. set_var_value POSTGRES_NO_BACKUP
  14. set_var_value POSTGRES_HOST
  15. set_var_value POSTGRES_USER
  16. set_var_value POSTGRES_PASSWORD
  17. set_var_value POSTGRES_DB
  18. set_var_value POSTGRES_REP_HOST
  19. set_var_value POSTGRES_REP_PORT
  20. set_var_value POSTGRES_REP_USER
  21. set_var_value POSTGRES_REP_PASSWORD
  22. set_var_value POSTGRES_REP_ALLOWED_HOST
  23. set_var_value POSTGRES_RO_USER
  24. set_var_value POSTGRES_RO_PASSWORD
  25. fi
  26. if [ "${POSTGRES_DISABLED}" == 1 ]
  27. then
  28. echo "Database ${POSTGRES_INDEX} is disabled. Exiting."
  29. exit 0
  30. fi
  31. if [ "${POSTGRES_SLAVE_MODE}" == 1 ] && [ "${POSTGRES_MASTER_MODE}" == 1 ]
  32. then
  33. echo "Misconfiguration: master and slave mode are both enabled. Exiting."
  34. exit 64
  35. fi
  36. if [ "$(id -u)" == "0" ]
  37. then
  38. cron
  39. fi
  40. # usage: file_env VAR [DEFAULT]
  41. # ie: file_env 'XYZ_DB_PASSWORD' 'example'
  42. # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
  43. # "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
  44. file_env() {
  45. local var="$1"
  46. local fileVar="${var}_FILE"
  47. local def="${2:-}"
  48. if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
  49. echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
  50. exit 1
  51. fi
  52. local val="$def"
  53. if [ "${!var:-}" ]; then
  54. val="${!var}"
  55. elif [ "${!fileVar:-}" ]; then
  56. val="$(< "${!fileVar}")"
  57. fi
  58. export "$var"="$val"
  59. unset "$fileVar"
  60. }
  61. if [ "${1:0:1}" = '-' ]; then
  62. set -- postgres "$@"
  63. fi
  64. # allow the container to be started with `--user`
  65. if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
  66. mkdir -p "$PGDATA"
  67. chown -R postgres "$PGDATA"
  68. chmod 700 "$PGDATA"
  69. mkdir -p /var/run/postgresql
  70. chown -R postgres /var/run/postgresql
  71. chmod 775 /var/run/postgresql
  72. # Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user
  73. if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
  74. mkdir -p "$POSTGRES_INITDB_XLOGDIR"
  75. chown -R postgres "$POSTGRES_INITDB_XLOGDIR"
  76. chmod 700 "$POSTGRES_INITDB_XLOGDIR"
  77. fi
  78. exec gosu postgres "$BASH_SOURCE" "$@"
  79. fi
  80. if [ "$1" = 'postgres' ]; then
  81. mkdir -p "$PGDATA"
  82. chown -R "$(id -u)" "$PGDATA" 2>/dev/null || :
  83. chmod 700 "$PGDATA" 2>/dev/null || :
  84. if [ "${POSTGRES_SLAVE_MODE}" != 1 ]
  85. then
  86. # look specifically for PG_VERSION, as it is expected in the DB dir
  87. if [ ! -s "$PGDATA/PG_VERSION" ]; then
  88. file_env 'POSTGRES_INITDB_ARGS'
  89. if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
  90. export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR"
  91. fi
  92. eval "initdb --username=postgres $POSTGRES_INITDB_ARGS"
  93. # check password first so we can output the warning before postgres
  94. # messes it up
  95. file_env 'POSTGRES_PASSWORD'
  96. if [ "$POSTGRES_PASSWORD" ]; then
  97. pass="PASSWORD '$POSTGRES_PASSWORD'"
  98. authMethod=md5
  99. else
  100. # The - option suppresses leading tabs but *not* spaces. :)
  101. cat >&2 <<-'EOWARN'
  102. ****************************************************
  103. WARNING: No password has been set for the database.
  104. This will allow anyone with access to the
  105. Postgres port to access your database. In
  106. Docker's default configuration, this is
  107. effectively any other container on the same
  108. system.
  109. Use "-e POSTGRES_PASSWORD=password" to set
  110. it in "docker run".
  111. ****************************************************
  112. EOWARN
  113. pass=
  114. authMethod=trust
  115. fi
  116. {
  117. echo
  118. echo "host all all all $authMethod"
  119. } >> "$PGDATA/pg_hba.conf"
  120. # internal start of server in order to allow set-up using psql-client
  121. # does not listen on external TCP/IP and waits until start finishes
  122. PGUSER="${PGUSER:-postgres}" \
  123. pg_ctl -D "$PGDATA" \
  124. -o "-c listen_addresses='localhost'" \
  125. -w start
  126. file_env 'POSTGRES_USER' 'postgres'
  127. file_env 'POSTGRES_DB' "$POSTGRES_USER"
  128. echo
  129. export PGUSER="postgres"
  130. export PGDATABASE="postgres"
  131. echo "Running core migrate"
  132. migrate.py --folder /docker-entrypoint-initdb-core.d/ --init
  133. export PGUSER="${POSTGRES_USER}"
  134. export PGDATABASE="${POSTGRES_DB}"
  135. echo "Running user migrate"
  136. migrate.py --folder /docker-entrypoint-initdb.d/ --init
  137. PGUSER="${PGUSER:-postgres}" \
  138. pg_ctl -D "$PGDATA" -m fast -w stop
  139. echo
  140. echo 'PostgreSQL init process complete; ready for start up.'
  141. echo
  142. fi
  143. else
  144. echo "Setting up slave..."
  145. rm -rf "${PGDATA}"/*
  146. PGPASSWORD="${POSTGRES_REP_PASSWORD}" pg_basebackup -h "${POSTGRES_REP_HOST}" -p "${POSTGRES_REP_PORT}" -D "${PGDATA}" -U "${POSTGRES_REP_USER}" -v
  147. export PGUSER="postgres"
  148. export PGDATABASE="postgres"
  149. echo "Running core migrate"
  150. migrate.py --folder /docker-entrypoint-initdb-core.d/ --init
  151. echo
  152. echo 'PostgreSQL init process complete; ready for start up.'
  153. echo
  154. fi
  155. fi
  156. exec "$@"