Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix running in Kubernetes #424

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ RUN chmod +x *.sh
# Run any additional tasks here that are too tedious to put in
# this dockerfile directly.
RUN set -eux \
&& /scripts/setup.sh;rm /scripts/.pass_*
&& /scripts/setup.sh;rm /home/postgresuser/.pass_*
RUN echo 'figlet -t "Kartoza Docker PostGIS"' >> ~/.bashrc


Expand Down
31 changes: 6 additions & 25 deletions scripts/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,6 @@ figlet -t "Kartoza Docker PostGIS"

# Gosu preparations
if [[ ${RUN_AS_ROOT} =~ [Ff][Aa][Ll][Ss][Ee] ]];then
USER_ID=${POSTGRES_UID:-1000}
GROUP_ID=${POSTGRES_GID:-1000}
USER_NAME=${USER:-postgresuser}
DB_GROUP_NAME=${GROUP_NAME:-postgresusers}

export USER_NAME=${USER_NAME}
export DB_GROUP_NAME=${DB_GROUP_NAME}

# Add group
if [ ! $(getent group "${DB_GROUP_NAME}") ]; then
groupadd -r "${DB_GROUP_NAME}" -g "${GROUP_ID}"
fi

# Add user to system
if id "${USER_NAME}" &>/dev/null; then
echo ' skipping user creation'
else
useradd -l -m -d /home/"${USER_NAME}"/ -u "${USER_ID}" --gid "${GROUP_ID}" -s /bin/bash -G "${DB_GROUP_NAME}" "${USER_NAME}"
fi

if [[ "${REPLICATION}" =~ [Tt][Rr][Uu][Ee] ]] ; then
echo "/home/${USER_NAME}/.pgpass" > /tmp/pg_subs.txt
Expand All @@ -48,15 +29,15 @@ if [[ ${RUN_AS_ROOT} =~ [Ff][Aa][Ll][Ss][Ee] ]];then

fi

if [[ -f /scripts/.pass_20.txt ]]; then
USER_CREDENTIAL_PASS=$(cat /scripts/.pass_20.txt)
cp /scripts/.pass_20.txt /tmp/PGPASSWORD.txt
if [[ -f /home/"${USER_NAME}"/.pass_20.txt ]]; then
USER_CREDENTIAL_PASS=$(cat /home/"${USER_NAME}"//.pass_20.txt)
cp /home/"${USER_NAME}"/.pass_20.txt /tmp/PGPASSWORD.txt
echo -e "[Entrypoint] GENERATED Postgres PASSWORD: \e[1;31m $USER_CREDENTIAL_PASS \033[0m"
fi

if [[ -f /scripts/.pass_22.txt ]]; then
USER_CREDENTIAL_PASS=$(cat /scripts/.pass_22.txt)
cp /scripts/.pass_22.txt /tmp/REPLPASSWORD.txt
if [[ -f /home/"${USER_NAME}"/.pass_22.txt ]]; then
USER_CREDENTIAL_PASS=$(cat /home/"${USER_NAME}"//.pass_22.txt)
cp /home/"${USER_NAME}"/.pass_22.txt /tmp/REPLPASSWORD.txt
echo -e "[Entrypoint] GENERATED Replication PASSWORD: \e[1;34m $USER_CREDENTIAL_PASS \033[0m"
fi

Expand Down
60 changes: 49 additions & 11 deletions scripts/env-data.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
#!/usr/bin/env bash

if [ -z "${POSTGRES_UID}" ]; then
POSTGRES_UID=1000
fi
if [ -z "${POSTGRES_GID}" ]; then
POSTGRES_GID=1000
fi
if [ -z "${USER}" ]; then
USER=postgresuser
fi
if [ -z "${GROUP_NAME}" ]; then
GROUP_NAME=postgresusers
fi

export USER_ID=${POSTGRES_UID}
export GROUP_ID=${POSTGRES_GID}
export USER_NAME=${USER}
export DB_GROUP_NAME=${GROUP_NAME}

function create_local_user() {
local group_name="$1"
local group_id="$2"
local user_name="$3"
local user_id="$4"

# Add group if not exists
if ! getent group "${group_name}" >/dev/null; then
groupadd -r "${group_name}" -g "${group_id}"
fi

# Add user if not exists
if ! id "${user_name}" >/dev/null 2>&1; then
useradd -l -m -d "/home/${user_name}" -u "${user_id}" \
--gid "${group_id}" -s /bin/bash -G "${group_name}" "${user_name}"
fi
}

create_local_user "${DB_GROUP_NAME}" "${GROUP_ID}" "${USER_NAME}" "${USER_ID}"

POSTGRES_MAJOR_VERSION=$(cat /tmp/pg_version.txt)
POSTGIS_MAJOR=$(cat /tmp/pg_major_version.txt)
POSTGIS_MINOR_RELEASE=$(cat /tmp/pg_minor_version.txt)
DEFAULT_DATADIR="/var/lib/postgresql/${POSTGRES_MAJOR_VERSION}/main"
# Commented for documentation. You can specify the location of
# pg_wal directory/volume using the following environment variable:
# POSTGRES_INITDB_WALDIR (default value is unset)
DEFAULT_SCRIPTS_LOCKFILE_DIR="/docker-entrypoint.initdb.d"
DEFAULT_CONF_LOCKFILE_DIR="/settings"
DEFAULT_EXTRA_CONF_DIR="/settings"
DEFAULT_SCRIPTS_LOCKFILE_DIR="/home/"${USER_NAME}"/docker-entrypoint.initdb.d"
DEFAULT_CONF_LOCKFILE_DIR="/home/"${USER_NAME}"/settings"
DEFAULT_EXTRA_CONF_DIR="/home/"${USER_NAME}"/settings"
ROOT_CONF="/etc/postgresql/${POSTGRES_MAJOR_VERSION}/main"
PG_ENV="$ROOT_CONF/environment"
CONF="$ROOT_CONF/postgresql.conf"
DEFAULT_WAL_ARCHIVE="/opt/archivedir"
DEFAULT_WAL_ARCHIVE="/home/"${USER_NAME}"/archivedir"
RECOVERY_CONF="$ROOT_CONF/recovery.conf"
POSTGRES="/usr/lib/postgresql/${POSTGRES_MAJOR_VERSION}/bin/postgres"
INITDB="/usr/lib/postgresql/${POSTGRES_MAJOR_VERSION}/bin/initdb"
Expand All @@ -34,7 +73,7 @@ PG_PID="/var/run/postgresql/${POSTGRES_MAJOR_VERSION}-main.pid"
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
function file_env {
function file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
Expand Down Expand Up @@ -65,7 +104,7 @@ function boolean() {

file_env 'POSTGRES_PASS'
file_env 'POSTGRES_USER'
file_env 'POSTGRES_DBNAME'


function create_dir() {
DATA_PATH=$1
Expand All @@ -80,11 +119,10 @@ fi
function generate_random_string() {
STRING_LENGTH=$1
random_pass_string=$(cat /dev/urandom | tr -dc '[:alnum:]' | head -c "${STRING_LENGTH}")
if [[ ! -f /scripts/.pass_${STRING_LENGTH}.txt ]]; then
echo "${random_pass_string}" > /scripts/.pass_"${STRING_LENGTH}".txt
if [[ ! -f /home/"${USER_NAME}"/scripts/.pass_${STRING_LENGTH}.txt ]]; then
echo ${random_pass_string} > /home/"${USER_NAME}"/.pass_${STRING_LENGTH}.txt
fi
RAND=$(cat /scripts/.pass_"${STRING_LENGTH}".txt)
export RAND
export RAND=$(cat /home/"${USER_NAME}"/.pass_${STRING_LENGTH}.txt)
}

# Make sure we have a user set up
Expand Down Expand Up @@ -116,7 +154,7 @@ else
RECREATE_DATADIR=$(boolean ${RECREATE_DATADIR})
fi
if [ -z "${SSL_DIR}" ]; then
SSL_DIR="/ssl_certificates"
SSL_DIR="/home/"${USER_NAME}"/ssl_certificates"
fi

if [ -z "${WAL_ARCHIVE}" ]; then
Expand Down