diff --git a/1/trixie/Dockerfile b/1/trixie/Dockerfile index 3d437b4..ac12fd7 100644 --- a/1/trixie/Dockerfile +++ b/1/trixie/Dockerfile @@ -112,16 +112,16 @@ RUN chmod +x /usr/local/bin/gosu FROM debian:trixie-slim -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR 1 ENV IVORY_VERSION 1.17 ARG USE_CHINA_MIRROR=false RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - # Use China mirror if specified RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ @@ -137,13 +137,11 @@ RUN apt-get update && \ libldap2 \ libldap2-dev \ libreadline-dev \ - libicu-dev && \ + libicu-dev \ + libossp-uuid-dev && \ apt-get clean -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin - ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) diff --git a/3/bookworm/Dockerfile b/3/bookworm/Dockerfile index b4356a9..6266ed3 100644 --- a/3/bookworm/Dockerfile +++ b/3/bookworm/Dockerfile @@ -112,16 +112,16 @@ RUN chmod +x /usr/local/bin/gosu FROM debian:bookworm-slim -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR 3 ENV IVORY_VERSION 3.4 ARG USE_CHINA_MIRROR=false RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - # Use China mirror if specified RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ @@ -137,13 +137,11 @@ RUN apt-get update && \ libldap-2.5-0 \ libldap2-dev \ libreadline-dev \ - libicu-dev && \ + libicu-dev \ + libossp-uuid-dev && \ apt-get clean -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin - ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) diff --git a/3/trixie/Dockerfile b/3/trixie/Dockerfile index bd4f00c..e74260b 100644 --- a/3/trixie/Dockerfile +++ b/3/trixie/Dockerfile @@ -112,16 +112,16 @@ RUN chmod +x /usr/local/bin/gosu FROM debian:trixie-slim -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR 3 ENV IVORY_VERSION 3.4 ARG USE_CHINA_MIRROR=false RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - # Use China mirror if specified RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ @@ -137,13 +137,11 @@ RUN apt-get update && \ libldap2 \ libldap2-dev \ libreadline-dev \ - libicu-dev && \ + libicu-dev \ + libossp-uuid-dev && \ apt-get clean -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin - ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) diff --git a/3/ubi8/Dockerfile b/3/ubi8/Dockerfile index 21b897a..390c6a8 100644 --- a/3/ubi8/Dockerfile +++ b/3/ubi8/Dockerfile @@ -83,21 +83,24 @@ RUN ARCH="$(uname -m)"; \ FROM redhat/ubi8:latest -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR 3 ENV IVORY_VERSION 3.4 RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - -RUN dnf install -y \ - lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel && dnf -y clean all +RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ + sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin +RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm; \ + dnf install -y lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel; \ + dnf install -y e2fsprogs-devel libuuid-devel;\ + dnf install -y --enablerepo=*ower*ools uuid-devel;\ + dnf -y clean all ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) diff --git a/4/bookworm/Dockerfile b/4/bookworm/Dockerfile index edd295c..2b1226d 100644 --- a/4/bookworm/Dockerfile +++ b/4/bookworm/Dockerfile @@ -112,16 +112,16 @@ RUN chmod +x /usr/local/bin/gosu FROM debian:bookworm-slim -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR 4 ENV IVORY_VERSION 4.6 ARG USE_CHINA_MIRROR=false RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - # Use China mirror if specified RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ @@ -137,13 +137,11 @@ RUN apt-get update && \ libldap-2.5-0 \ libldap2-dev \ libreadline-dev \ - libicu-dev && \ + libicu-dev \ + libossp-uuid-dev && \ apt-get clean -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin - ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) diff --git a/4/trixie/Dockerfile b/4/trixie/Dockerfile index 1cace98..553efc9 100644 --- a/4/trixie/Dockerfile +++ b/4/trixie/Dockerfile @@ -112,16 +112,16 @@ RUN chmod +x /usr/local/bin/gosu FROM debian:trixie-slim -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR 4 ENV IVORY_VERSION 4.6 ARG USE_CHINA_MIRROR=false RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - # Use China mirror if specified RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ @@ -137,13 +137,11 @@ RUN apt-get update && \ libldap2 \ libldap2-dev \ libreadline-dev \ - libicu-dev && \ + libicu-dev \ + libossp-uuid-dev && \ apt-get clean -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin - ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) diff --git a/4/ubi8/Dockerfile b/4/ubi8/Dockerfile index 51f901e..2dfd210 100644 --- a/4/ubi8/Dockerfile +++ b/4/ubi8/Dockerfile @@ -83,21 +83,24 @@ RUN ARCH="$(uname -m)"; \ FROM redhat/ubi8:latest -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR 4 ENV IVORY_VERSION 4.6 RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - -RUN dnf install -y \ - lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel && dnf -y clean all +RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ + sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin +RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm; \ + dnf install -y lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel; \ + dnf install -y e2fsprogs-devel libuuid-devel;\ + dnf install -y --enablerepo=*ower*ools uuid-devel;\ + dnf -y clean all ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) diff --git a/5/bookworm/Dockerfile b/5/bookworm/Dockerfile new file mode 100644 index 0000000..01af31c --- /dev/null +++ b/5/bookworm/Dockerfile @@ -0,0 +1,165 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + +# Use Debian as the base image +FROM debian:bookworm-slim as builder + +# explicitly set user/group IDs +RUN groupadd -g 1000 ivorysql; \ + useradd -u 1000 -g ivorysql -d /var/local/ivorysql -s /bin/sh ivorysql; \ + mkdir -p /var/local/ivorysql; \ + mkdir -p /usr/src/ivorysql; \ + mkdir -p /var/lib/ivorysql; \ + chown -R ivorysql:ivorysql /var/local/ivorysql; \ + chown -R ivorysql:ivorysql /usr/src/ivorysql; \ + chown -R ivorysql:ivorysql /var/lib/ivorysql + +RUN mkdir /docker-entrypoint-initdb.d + +# Set environment variables for IvorySQL +ENV IVORY_MAJOR 5 +ENV IVORY_VERSION 5.0 +ARG USE_CHINA_MIRROR=false + +# Use China mirror if specified +RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ + sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ + fi + +# Update package list and install necessary dependencies +RUN apt-get update +RUN apt-get install -y make \ + libreadline-dev \ + zlib1g-dev \ + libssl-dev \ + flex \ + wget \ + dpkg \ + build-essential \ + llvm \ + llvm-dev \ + liblz4-dev \ + clang \ + libkrb5-dev \ + libselinux1-dev \ + libxml2-dev \ + libsasl2-dev \ + libicu-dev \ + e2fslibs-dev \ + libsepol-dev \ + libsystemd-dev \ + libxslt1-dev \ + libldap2-dev \ + libssl-dev \ + libpam0g-dev \ + uuid-dev \ + python3-dev \ + libreadline-dev \ + tcl-dev \ + zlib1g-dev \ + perl \ + libperl-dev \ + perl \ + perl-base \ + perl-modules \ + perl-doc \ + m4 \ + libc6 \ + libbison-dev\ + libossp-uuid-dev\ + curl + +RUN wget https://mirrors.ustc.edu.cn/gnu/bison/bison-3.0.4.tar.gz; \ + tar -xzvf bison-3.0.4.tar.gz; \ + cd bison-3.0.4 ; \ + sed -i 's/IO_ftrylockfile/IO_EOF_SEEN/' lib/*.c ;\ + echo "#define _IO_IN_BACKUP 0x100" >> lib/stdio-impl.h ;\ + mkdir build && cd build ;\ + ../configure ;\ + make && make install ; + +RUN wget -O ivorysql.tar.gz "https://github.com/IvorySQL/IvorySQL/archive/refs/tags/IvorySQL_$IVORY_VERSION.tar.gz"; \ + tar \ + --extract \ + --file ivorysql.tar.gz \ + --directory /usr/src/ivorysql \ + --strip-components 1 \ + ; \ + rm ivorysql.tar.gz; \ + cd /usr/src/ivorysql; \ + ./configure \ + --prefix=/var/local/ivorysql/ivorysql-$IVORY_MAJOR \ + --enable-cassert --enable-debug --enable-rpath --with-tcl \ + --with-python --with-gssapi --with-pam --with-ldap \ + --with-openssl --with-libedit-preferred --with-uuid=e2fs \ + --with-ossp-uuid --with-libxml --with-libxslt --with-perl \ + --without-icu \ + ;\ + make && make install; + +RUN rm -rf \ + /usr/src/ivorysql \ + /usr/local/share/doc \ + /usr/local/share/man + +RUN chown -R ivorysql:ivorysql /var/local/ivorysql/ivorysql-$IVORY_MAJOR +RUN wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/1.11/gosu-$(dpkg --print-architecture)" # amd64 | arm64v8 + +RUN chmod +x /usr/local/bin/gosu + +FROM debian:bookworm-slim + +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu + +ENV IVORY_MAJOR 5 +ENV IVORY_VERSION 5.0 +ARG USE_CHINA_MIRROR=false +RUN mkdir /docker-entrypoint-initdb.d + +# Use China mirror if specified +RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ + sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ + fi + +RUN apt-get update && \ + apt-get install -y \ + liblz4-1 \ + liblz4-dev \ + libkrb5-dev \ + libxslt1-dev \ + libldap-common \ + libldap-2.5-0 \ + libldap2-dev \ + libreadline-dev \ + libicu-dev \ + libossp-uuid-dev && \ + apt-get clean + + +ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data + +# this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) +RUN mkdir -p "$PGDATA" && chown -R ivorysql:ivorysql "$PGDATA" && chmod 1777 "$PGDATA" +RUN mkdir -p /var/run/postgresql && chown -R ivorysql:ivorysql /var/run/postgresql && chmod 3777 /var/run/postgresql + +VOLUME ["/var/local/ivorysql/ivorysql-$IVORY_MAJOR/data","/var/local/ivorysql/data"] + +ENV PATH $PATH:/var/local/ivorysql/ivorysql-$IVORY_MAJOR/bin + +COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/ +RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh +RUN chmod +x /usr/local/bin/docker-entrypoint.sh + +ENTRYPOINT ["docker-entrypoint.sh"] + +STOPSIGNAL SIGINT + +EXPOSE 5432 5866 1521 + +CMD ["postgres"] diff --git a/5/bookworm/docker-ensure-initdb.sh b/5/bookworm/docker-ensure-initdb.sh new file mode 100755 index 0000000..2a97586 --- /dev/null +++ b/5/bookworm/docker-ensure-initdb.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +# +# This script is intended for three main use cases: +# +# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior +# +# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution +# (no-op if database is already initialized) +# +# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use +# (error if database is already initialized) +# + +source /usr/local/bin/docker-entrypoint.sh + +# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there) +if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then + set -- postgres "$@" +fi + +# see also "_main" in "docker-entrypoint.sh" + +docker_setup_env +# setup data directories and permissions (when run as root) +docker_create_db_directories +if [ "$(id -u)" = '0' ]; then + # then restart script as postgres user + exec su-exec postgres "$BASH_SOURCE" "$@" +fi + +# only run initialization on an empty data directory +if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + docker_verify_minimum_env + + # check dir permissions to reduce likelihood of half-initialized database + ls /docker-entrypoint-initdb.d/ > /dev/null + + docker_init_database_dir + pg_setup_hba_conf "$@" + + # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless + # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS + export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}" + docker_temp_server_start "$@" + + docker_setup_db + docker_process_init_files /docker-entrypoint-initdb.d/* + + docker_temp_server_stop + unset PGPASSWORD +else + self="$(basename "$0")" + case "$self" in + docker-ensure-initdb.sh) + echo >&2 "$self: note: database already initialized in '$PGDATA'!" + exit 0 + ;; + + docker-enforce-initdb.sh) + echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!" + exit 1 + ;; + + *) + echo >&2 "$self: error: unknown file name: $self" + exit 99 + ;; + esac +fi diff --git a/5/bookworm/docker-entrypoint.sh b/5/bookworm/docker-entrypoint.sh new file mode 100755 index 0000000..87b4677 --- /dev/null +++ b/5/bookworm/docker-entrypoint.sh @@ -0,0 +1,359 @@ +#!/usr/bin/env bash +set -Eeo pipefail +# TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables) + +# usage: file_env VAR [DEFAULT] +# 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) +file_env() { + local var="$1" + local fileVar="${var}_FILE" + local def="${2:-}" + if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then + printf >&2 'error: both %s and %s are set (but are exclusive)\n' "$var" "$fileVar" + exit 1 + fi + local val="$def" + if [ "${!var:-}" ]; then + val="${!var}" + elif [ "${!fileVar:-}" ]; then + val="$(< "${!fileVar}")" + fi + export "$var"="$val" + unset "$fileVar" +} + +# check to see if this file is being run or sourced from another script +_is_sourced() { + # https://unix.stackexchange.com/a/215279 + [ "${#FUNCNAME[@]}" -ge 2 ] \ + && [ "${FUNCNAME[0]}" = '_is_sourced' ] \ + && [ "${FUNCNAME[1]}" = 'source' ] +} + +# used to create initial postgres directories and if run as root, ensure ownership to the "postgres" user +docker_create_db_directories() { + local user; user="$(id -u)" + + mkdir -p "$PGDATA" + # ignore failure since there are cases where we can't chmod (and PostgreSQL might fail later anyhow - it's picky about permissions of this directory) + chmod 00700 "$PGDATA" || : + + # ignore failure since it will be fine when using the image provided directory; see also https://github.com/docker-library/postgres/pull/289 + mkdir -p /var/run/postgresql || : + chmod 03775 /var/run/postgresql || : + + # Create the transaction log directory before initdb is run so the directory is owned by the correct user + if [ -n "${POSTGRES_INITDB_WALDIR:-}" ]; then + mkdir -p "$POSTGRES_INITDB_WALDIR" + if [ "$user" = '0' ]; then + find "$POSTGRES_INITDB_WALDIR" \! -user ivorysql -exec chown ivorysql '{}' + + fi + chmod 700 "$POSTGRES_INITDB_WALDIR" + fi + + # allow the container to be started with `--user` + if [ "$user" = '0' ]; then + find "$PGDATA" \! -user ivorysql -exec chown ivorysql '{}' + + find /var/run/postgresql \! -user ivorysql -exec chown ivorysql '{}' + + fi +} + +# initialize empty PGDATA directory with new database via 'initdb' +# arguments to `initdb` can be passed via POSTGRES_INITDB_ARGS or as arguments to this function +# `initdb` automatically creates the "postgres", "template0", and "template1" dbnames +# this is also where the database user is created, specified by `IVORYSQL_USER` env +docker_init_database_dir() { + # "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary + # see https://github.com/docker-library/postgres/pull/253, https://github.com/docker-library/postgres/issues/359, https://cwrap.org/nss_wrapper.html + local uid; uid="$(id -u)" + if ! getent passwd "$uid" &> /dev/null; then + # see if we can find a suitable "libnss_wrapper.so" (https://salsa.debian.org/sssd-team/nss-wrapper/-/commit/b9925a653a54e24d09d9b498a2d913729f7abb15) + local wrapper + for wrapper in {/usr,}/lib{/*,}/libnss_wrapper.so; do + if [ -s "$wrapper" ]; then + NSS_WRAPPER_PASSWD="$(mktemp)" + NSS_WRAPPER_GROUP="$(mktemp)" + export LD_PRELOAD="$wrapper" NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP + local gid; gid="$(id -g)" + printf 'postgres:x:%s:%s:PostgreSQL:%s:/bin/false\n' "$uid" "$gid" "$PGDATA" > "$NSS_WRAPPER_PASSWD" + printf 'postgres:x:%s:\n' "$gid" > "$NSS_WRAPPER_GROUP" + break + fi + done + fi + + if [ -n "${POSTGRES_INITDB_WALDIR:-}" ]; then + set -- --waldir "$POSTGRES_INITDB_WALDIR" "$@" + fi + + # --pwfile refuses to handle a properly-empty file (hence the "\n"): https://github.com/docker-library/postgres/issues/1025 + eval 'initdb --username="$IVORYSQL_USER" --pwfile=<(printf "%s\n" "$IVORYSQL_PASSWORD") '"$POSTGRES_INITDB_ARGS"' "$@"' + sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" $PGDATA/postgresql.conf + if [ -e "$PGDATA/ivorysql.conf" ]; then + sed -ri "s!^#?(ivorysql.listen_addresses)\s*=\s*\S+.*!\1 = '*'!" $PGDATA/ivorysql.conf + fi + # unset/cleanup "nss_wrapper" bits + if [[ "${LD_PRELOAD:-}" == */libnss_wrapper.so ]]; then + rm -f "$NSS_WRAPPER_PASSWD" "$NSS_WRAPPER_GROUP" + unset LD_PRELOAD NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP + fi +} + +# print large warning if IVORYSQL_PASSWORD is long +# error if both IVORYSQL_PASSWORD is empty and POSTGRES_HOST_AUTH_METHOD is not 'trust' +# print large warning if POSTGRES_HOST_AUTH_METHOD is set to 'trust' +# assumes database is not set up, ie: [ -z "$DATABASE_ALREADY_EXISTS" ] +docker_verify_minimum_env() { + case "${IVORY_MAJOR:-}" in + 12 | 13) # https://github.com/postgres/postgres/commit/67a472d71c98c3d2fa322a1b4013080b20720b98 + # check password first so we can output the warning before postgres + # messes it up + if [ "${#IVORYSQL_PASSWORD}" -ge 100 ]; then + cat >&2 <<-'EOWARN' + + WARNING: The supplied IVORYSQL_PASSWORD is 100+ characters. + + This will not work if used via PGPASSWORD with "psql". + + https://www.postgresql.org/message-id/flat/E1Rqxp2-0004Qt-PL%40wrigleys.postgresql.org (BUG #6412) + https://github.com/docker-library/postgres/issues/507 + + EOWARN + fi + ;; + esac + if [ -z "$IVORYSQL_PASSWORD" ] && [ 'trust' != "$POSTGRES_HOST_AUTH_METHOD" ]; then + # The - option suppresses leading tabs but *not* spaces. :) + cat >&2 <<-'EOE' + Error: Database is uninitialized and superuser password is not specified. + You must specify IVORYSQL_PASSWORD to a non-empty value for the + superuser. For example, "-e IVORYSQL_PASSWORD=password" on "docker run". + + You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all + connections without a password. This is *not* recommended. + + See PostgreSQL documentation about "trust": + https://www.postgresql.org/docs/current/auth-trust.html + EOE + exit 1 + fi + if [ 'trust' = "$POSTGRES_HOST_AUTH_METHOD" ]; then + cat >&2 <<-'EOWARN' + ******************************************************************************** + WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow + anyone with access to the Postgres port to access your database without + a password, even if IVORYSQL_PASSWORD is set. See PostgreSQL + documentation about "trust": + https://www.postgresql.org/docs/current/auth-trust.html + In Docker's default configuration, this is effectively any other + container on the same system. + + It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace + it with "-e IVORYSQL_PASSWORD=password" instead to set a password in + "docker run". + ******************************************************************************** + EOWARN + fi +} + +# usage: docker_process_init_files [file [file [...]]] +# ie: docker_process_init_files /always-initdb.d/* +# process initializer files, based on file extensions and permissions +docker_process_init_files() { + # psql here for backwards compatibility "${psql[@]}" + psql=( docker_process_sql ) + + printf '\n' + local f + for f; do + case "$f" in + *.sh) + # https://github.com/docker-library/postgres/issues/450#issuecomment-393167936 + # https://github.com/docker-library/postgres/pull/452 + if [ -x "$f" ]; then + printf '%s: running %s\n' "$0" "$f" + "$f" + else + printf '%s: sourcing %s\n' "$0" "$f" + . "$f" + fi + ;; + *.sql) printf '%s: running %s\n' "$0" "$f"; docker_process_sql -f "$f"; printf '\n' ;; + *.sql.gz) printf '%s: running %s\n' "$0" "$f"; gunzip -c "$f" | docker_process_sql; printf '\n' ;; + *.sql.xz) printf '%s: running %s\n' "$0" "$f"; xzcat "$f" | docker_process_sql; printf '\n' ;; + *.sql.zst) printf '%s: running %s\n' "$0" "$f"; zstd -dc "$f" | docker_process_sql; printf '\n' ;; + *) printf '%s: ignoring %s\n' "$0" "$f" ;; + esac + printf '\n' + done +} + +# Execute sql script, passed via stdin (or -f flag of pqsl) +# usage: docker_process_sql [psql-cli-args] +# ie: docker_process_sql --dbname=mydb <<<'INSERT ...' +# ie: docker_process_sql -f my-file.sql +# ie: docker_process_sql > "$PGDATA/pg_hba.conf" +} + +# start socket-only postgresql server for setting up or running scripts +# all arguments will be passed along as arguments to `postgres` (via pg_ctl) +docker_temp_server_start() { + if [ "$1" = 'postgres' ]; then + shift + fi + + # internal start of server in order to allow setup using psql client + # does not listen on external TCP/IP and waits until start finishes + set -- "$@" -c listen_addresses='' -p "${PGPORT:-5866}" + + PGUSER="${PGUSER:-$IVORYSQL_USER}" \ + pg_ctl -D "$PGDATA" \ + -o "$(printf '%q ' "$@")" \ + -w start +} + +# stop postgresql server after done setting up user and running scripts +docker_temp_server_stop() { + PGUSER="${PGUSER:-$IVORYSQL_USER}" \ + pg_ctl -D "$PGDATA" -m fast -w stop +} + +# check arguments for an option that would cause postgres to stop +# return true if there is one +_pg_want_help() { + local arg + for arg; do + case "$arg" in + # postgres --help | grep 'then exit' + # leaving out -C on purpose since it always fails and is unhelpful: + # postgres: could not access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory + -'?'|--help|--describe-config|-V|--version) + return 0 + ;; + esac + done + return 1 +} + +_main() { + # if first arg looks like a flag, assume we want to run postgres server + if [ "${1:0:1}" = '-' ]; then + set -- postgres "$@" + fi + + if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then + docker_setup_env + # setup data directories and permissions (when run as root) + docker_create_db_directories + if [ "$(id -u)" = '0' ]; then + # then restart script as ivorysql user + exec gosu ivorysql "$BASH_SOURCE" "$@" + fi + + # only run initialization on an empty data directory + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + docker_verify_minimum_env + + # check dir permissions to reduce likelihood of half-initialized database + ls /docker-entrypoint-initdb.d/ > /dev/null + + docker_init_database_dir + pg_setup_hba_conf "$@" + + # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless + # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS + export PGPASSWORD="${PGPASSWORD:-$IVORYSQL_PASSWORD}" + docker_temp_server_start "$@" + + docker_setup_db + docker_process_init_files /docker-entrypoint-initdb.d/* + + docker_temp_server_stop + unset PGPASSWORD + + cat <<-'EOM' + + PostgreSQL init process complete; ready for start up. + + EOM + else + cat <<-'EOM' + + PostgreSQL Database directory appears to contain a database; Skipping initialization + + EOM + fi + fi + + exec "$@" +} + +if ! _is_sourced; then + _main "$@" +fi diff --git a/5/trixie/Dockerfile b/5/trixie/Dockerfile new file mode 100644 index 0000000..3f37bce --- /dev/null +++ b/5/trixie/Dockerfile @@ -0,0 +1,165 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + +# Use Debian as the base image +FROM debian:trixie-slim as builder + +# explicitly set user/group IDs +RUN groupadd -g 1000 ivorysql; \ + useradd -u 1000 -g ivorysql -d /var/local/ivorysql -s /bin/sh ivorysql; \ + mkdir -p /var/local/ivorysql; \ + mkdir -p /usr/src/ivorysql; \ + mkdir -p /var/lib/ivorysql; \ + chown -R ivorysql:ivorysql /var/local/ivorysql; \ + chown -R ivorysql:ivorysql /usr/src/ivorysql; \ + chown -R ivorysql:ivorysql /var/lib/ivorysql + +RUN mkdir /docker-entrypoint-initdb.d + +# Set environment variables for IvorySQL +ENV IVORY_MAJOR 5 +ENV IVORY_VERSION 5.0 +ARG USE_CHINA_MIRROR=false + +# Use China mirror if specified +RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ + sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ + fi + +# Update package list and install necessary dependencies +RUN apt-get update +RUN apt-get install -y make \ + libreadline-dev \ + zlib1g-dev \ + libssl-dev \ + flex \ + wget \ + dpkg \ + build-essential \ + llvm \ + llvm-dev \ + liblz4-dev \ + clang \ + libkrb5-dev \ + libselinux1-dev \ + libxml2-dev \ + libsasl2-dev \ + libicu-dev \ + e2fslibs-dev \ + libsepol-dev \ + libsystemd-dev \ + libxslt1-dev \ + libldap2-dev \ + libssl-dev \ + libpam0g-dev \ + uuid-dev \ + python3-dev \ + libreadline-dev \ + tcl-dev \ + zlib1g-dev \ + perl \ + libperl-dev \ + perl \ + perl-base \ + perl-modules \ + perl-doc \ + m4 \ + libc6 \ + libbison-dev\ + libossp-uuid-dev\ + curl + +RUN wget https://mirrors.ustc.edu.cn/gnu/bison/bison-3.0.4.tar.gz; \ + tar -xzvf bison-3.0.4.tar.gz; \ + cd bison-3.0.4 ; \ + sed -i 's/IO_ftrylockfile/IO_EOF_SEEN/' lib/*.c ;\ + echo "#define _IO_IN_BACKUP 0x100" >> lib/stdio-impl.h ;\ + mkdir build && cd build ;\ + ../configure ;\ + make && make install ; + +RUN wget -O ivorysql.tar.gz "https://github.com/IvorySQL/IvorySQL/archive/refs/tags/IvorySQL_$IVORY_VERSION.tar.gz"; \ + tar \ + --extract \ + --file ivorysql.tar.gz \ + --directory /usr/src/ivorysql \ + --strip-components 1 \ + ; \ + rm ivorysql.tar.gz; \ + cd /usr/src/ivorysql; \ + ./configure \ + --prefix=/var/local/ivorysql/ivorysql-$IVORY_MAJOR \ + --enable-cassert --enable-debug --enable-rpath --with-tcl \ + --with-python --with-gssapi --with-pam --with-ldap \ + --with-openssl --with-libedit-preferred --with-uuid=e2fs \ + --with-ossp-uuid --with-libxml --with-libxslt --with-perl \ + --without-icu \ + ;\ + make && make install; + +RUN rm -rf \ + /usr/src/ivorysql \ + /usr/local/share/doc \ + /usr/local/share/man + +RUN chown -R ivorysql:ivorysql /var/local/ivorysql/ivorysql-$IVORY_MAJOR +RUN wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/1.11/gosu-$(dpkg --print-architecture)" # amd64 | arm64v8 + +RUN chmod +x /usr/local/bin/gosu + +FROM debian:trixie-slim + +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu + +ENV IVORY_MAJOR 5 +ENV IVORY_VERSION 5.0 +ARG USE_CHINA_MIRROR=false +RUN mkdir /docker-entrypoint-initdb.d + +# Use China mirror if specified +RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ + sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ + fi + +RUN apt-get update && \ + apt-get install -y \ + liblz4-1 \ + liblz4-dev \ + libkrb5-dev \ + libxslt1-dev \ + libldap-common \ + libldap2 \ + libldap2-dev \ + libreadline-dev \ + libicu-dev \ + libossp-uuid-dev && \ + apt-get clean + + +ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data + +# this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) +RUN mkdir -p "$PGDATA" && chown -R ivorysql:ivorysql "$PGDATA" && chmod 1777 "$PGDATA" +RUN mkdir -p /var/run/postgresql && chown -R ivorysql:ivorysql /var/run/postgresql && chmod 3777 /var/run/postgresql + +VOLUME ["/var/local/ivorysql/ivorysql-$IVORY_MAJOR/data","/var/local/ivorysql/data"] + +ENV PATH $PATH:/var/local/ivorysql/ivorysql-$IVORY_MAJOR/bin + +COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/ +RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh +RUN chmod +x /usr/local/bin/docker-entrypoint.sh + +ENTRYPOINT ["docker-entrypoint.sh"] + +STOPSIGNAL SIGINT + +EXPOSE 5432 5866 1521 + +CMD ["postgres"] diff --git a/5/trixie/docker-ensure-initdb.sh b/5/trixie/docker-ensure-initdb.sh new file mode 100755 index 0000000..2a97586 --- /dev/null +++ b/5/trixie/docker-ensure-initdb.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +# +# This script is intended for three main use cases: +# +# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior +# +# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution +# (no-op if database is already initialized) +# +# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use +# (error if database is already initialized) +# + +source /usr/local/bin/docker-entrypoint.sh + +# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there) +if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then + set -- postgres "$@" +fi + +# see also "_main" in "docker-entrypoint.sh" + +docker_setup_env +# setup data directories and permissions (when run as root) +docker_create_db_directories +if [ "$(id -u)" = '0' ]; then + # then restart script as postgres user + exec su-exec postgres "$BASH_SOURCE" "$@" +fi + +# only run initialization on an empty data directory +if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + docker_verify_minimum_env + + # check dir permissions to reduce likelihood of half-initialized database + ls /docker-entrypoint-initdb.d/ > /dev/null + + docker_init_database_dir + pg_setup_hba_conf "$@" + + # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless + # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS + export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}" + docker_temp_server_start "$@" + + docker_setup_db + docker_process_init_files /docker-entrypoint-initdb.d/* + + docker_temp_server_stop + unset PGPASSWORD +else + self="$(basename "$0")" + case "$self" in + docker-ensure-initdb.sh) + echo >&2 "$self: note: database already initialized in '$PGDATA'!" + exit 0 + ;; + + docker-enforce-initdb.sh) + echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!" + exit 1 + ;; + + *) + echo >&2 "$self: error: unknown file name: $self" + exit 99 + ;; + esac +fi diff --git a/5/trixie/docker-entrypoint.sh b/5/trixie/docker-entrypoint.sh new file mode 100755 index 0000000..87b4677 --- /dev/null +++ b/5/trixie/docker-entrypoint.sh @@ -0,0 +1,359 @@ +#!/usr/bin/env bash +set -Eeo pipefail +# TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables) + +# usage: file_env VAR [DEFAULT] +# 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) +file_env() { + local var="$1" + local fileVar="${var}_FILE" + local def="${2:-}" + if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then + printf >&2 'error: both %s and %s are set (but are exclusive)\n' "$var" "$fileVar" + exit 1 + fi + local val="$def" + if [ "${!var:-}" ]; then + val="${!var}" + elif [ "${!fileVar:-}" ]; then + val="$(< "${!fileVar}")" + fi + export "$var"="$val" + unset "$fileVar" +} + +# check to see if this file is being run or sourced from another script +_is_sourced() { + # https://unix.stackexchange.com/a/215279 + [ "${#FUNCNAME[@]}" -ge 2 ] \ + && [ "${FUNCNAME[0]}" = '_is_sourced' ] \ + && [ "${FUNCNAME[1]}" = 'source' ] +} + +# used to create initial postgres directories and if run as root, ensure ownership to the "postgres" user +docker_create_db_directories() { + local user; user="$(id -u)" + + mkdir -p "$PGDATA" + # ignore failure since there are cases where we can't chmod (and PostgreSQL might fail later anyhow - it's picky about permissions of this directory) + chmod 00700 "$PGDATA" || : + + # ignore failure since it will be fine when using the image provided directory; see also https://github.com/docker-library/postgres/pull/289 + mkdir -p /var/run/postgresql || : + chmod 03775 /var/run/postgresql || : + + # Create the transaction log directory before initdb is run so the directory is owned by the correct user + if [ -n "${POSTGRES_INITDB_WALDIR:-}" ]; then + mkdir -p "$POSTGRES_INITDB_WALDIR" + if [ "$user" = '0' ]; then + find "$POSTGRES_INITDB_WALDIR" \! -user ivorysql -exec chown ivorysql '{}' + + fi + chmod 700 "$POSTGRES_INITDB_WALDIR" + fi + + # allow the container to be started with `--user` + if [ "$user" = '0' ]; then + find "$PGDATA" \! -user ivorysql -exec chown ivorysql '{}' + + find /var/run/postgresql \! -user ivorysql -exec chown ivorysql '{}' + + fi +} + +# initialize empty PGDATA directory with new database via 'initdb' +# arguments to `initdb` can be passed via POSTGRES_INITDB_ARGS or as arguments to this function +# `initdb` automatically creates the "postgres", "template0", and "template1" dbnames +# this is also where the database user is created, specified by `IVORYSQL_USER` env +docker_init_database_dir() { + # "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary + # see https://github.com/docker-library/postgres/pull/253, https://github.com/docker-library/postgres/issues/359, https://cwrap.org/nss_wrapper.html + local uid; uid="$(id -u)" + if ! getent passwd "$uid" &> /dev/null; then + # see if we can find a suitable "libnss_wrapper.so" (https://salsa.debian.org/sssd-team/nss-wrapper/-/commit/b9925a653a54e24d09d9b498a2d913729f7abb15) + local wrapper + for wrapper in {/usr,}/lib{/*,}/libnss_wrapper.so; do + if [ -s "$wrapper" ]; then + NSS_WRAPPER_PASSWD="$(mktemp)" + NSS_WRAPPER_GROUP="$(mktemp)" + export LD_PRELOAD="$wrapper" NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP + local gid; gid="$(id -g)" + printf 'postgres:x:%s:%s:PostgreSQL:%s:/bin/false\n' "$uid" "$gid" "$PGDATA" > "$NSS_WRAPPER_PASSWD" + printf 'postgres:x:%s:\n' "$gid" > "$NSS_WRAPPER_GROUP" + break + fi + done + fi + + if [ -n "${POSTGRES_INITDB_WALDIR:-}" ]; then + set -- --waldir "$POSTGRES_INITDB_WALDIR" "$@" + fi + + # --pwfile refuses to handle a properly-empty file (hence the "\n"): https://github.com/docker-library/postgres/issues/1025 + eval 'initdb --username="$IVORYSQL_USER" --pwfile=<(printf "%s\n" "$IVORYSQL_PASSWORD") '"$POSTGRES_INITDB_ARGS"' "$@"' + sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" $PGDATA/postgresql.conf + if [ -e "$PGDATA/ivorysql.conf" ]; then + sed -ri "s!^#?(ivorysql.listen_addresses)\s*=\s*\S+.*!\1 = '*'!" $PGDATA/ivorysql.conf + fi + # unset/cleanup "nss_wrapper" bits + if [[ "${LD_PRELOAD:-}" == */libnss_wrapper.so ]]; then + rm -f "$NSS_WRAPPER_PASSWD" "$NSS_WRAPPER_GROUP" + unset LD_PRELOAD NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP + fi +} + +# print large warning if IVORYSQL_PASSWORD is long +# error if both IVORYSQL_PASSWORD is empty and POSTGRES_HOST_AUTH_METHOD is not 'trust' +# print large warning if POSTGRES_HOST_AUTH_METHOD is set to 'trust' +# assumes database is not set up, ie: [ -z "$DATABASE_ALREADY_EXISTS" ] +docker_verify_minimum_env() { + case "${IVORY_MAJOR:-}" in + 12 | 13) # https://github.com/postgres/postgres/commit/67a472d71c98c3d2fa322a1b4013080b20720b98 + # check password first so we can output the warning before postgres + # messes it up + if [ "${#IVORYSQL_PASSWORD}" -ge 100 ]; then + cat >&2 <<-'EOWARN' + + WARNING: The supplied IVORYSQL_PASSWORD is 100+ characters. + + This will not work if used via PGPASSWORD with "psql". + + https://www.postgresql.org/message-id/flat/E1Rqxp2-0004Qt-PL%40wrigleys.postgresql.org (BUG #6412) + https://github.com/docker-library/postgres/issues/507 + + EOWARN + fi + ;; + esac + if [ -z "$IVORYSQL_PASSWORD" ] && [ 'trust' != "$POSTGRES_HOST_AUTH_METHOD" ]; then + # The - option suppresses leading tabs but *not* spaces. :) + cat >&2 <<-'EOE' + Error: Database is uninitialized and superuser password is not specified. + You must specify IVORYSQL_PASSWORD to a non-empty value for the + superuser. For example, "-e IVORYSQL_PASSWORD=password" on "docker run". + + You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all + connections without a password. This is *not* recommended. + + See PostgreSQL documentation about "trust": + https://www.postgresql.org/docs/current/auth-trust.html + EOE + exit 1 + fi + if [ 'trust' = "$POSTGRES_HOST_AUTH_METHOD" ]; then + cat >&2 <<-'EOWARN' + ******************************************************************************** + WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow + anyone with access to the Postgres port to access your database without + a password, even if IVORYSQL_PASSWORD is set. See PostgreSQL + documentation about "trust": + https://www.postgresql.org/docs/current/auth-trust.html + In Docker's default configuration, this is effectively any other + container on the same system. + + It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace + it with "-e IVORYSQL_PASSWORD=password" instead to set a password in + "docker run". + ******************************************************************************** + EOWARN + fi +} + +# usage: docker_process_init_files [file [file [...]]] +# ie: docker_process_init_files /always-initdb.d/* +# process initializer files, based on file extensions and permissions +docker_process_init_files() { + # psql here for backwards compatibility "${psql[@]}" + psql=( docker_process_sql ) + + printf '\n' + local f + for f; do + case "$f" in + *.sh) + # https://github.com/docker-library/postgres/issues/450#issuecomment-393167936 + # https://github.com/docker-library/postgres/pull/452 + if [ -x "$f" ]; then + printf '%s: running %s\n' "$0" "$f" + "$f" + else + printf '%s: sourcing %s\n' "$0" "$f" + . "$f" + fi + ;; + *.sql) printf '%s: running %s\n' "$0" "$f"; docker_process_sql -f "$f"; printf '\n' ;; + *.sql.gz) printf '%s: running %s\n' "$0" "$f"; gunzip -c "$f" | docker_process_sql; printf '\n' ;; + *.sql.xz) printf '%s: running %s\n' "$0" "$f"; xzcat "$f" | docker_process_sql; printf '\n' ;; + *.sql.zst) printf '%s: running %s\n' "$0" "$f"; zstd -dc "$f" | docker_process_sql; printf '\n' ;; + *) printf '%s: ignoring %s\n' "$0" "$f" ;; + esac + printf '\n' + done +} + +# Execute sql script, passed via stdin (or -f flag of pqsl) +# usage: docker_process_sql [psql-cli-args] +# ie: docker_process_sql --dbname=mydb <<<'INSERT ...' +# ie: docker_process_sql -f my-file.sql +# ie: docker_process_sql > "$PGDATA/pg_hba.conf" +} + +# start socket-only postgresql server for setting up or running scripts +# all arguments will be passed along as arguments to `postgres` (via pg_ctl) +docker_temp_server_start() { + if [ "$1" = 'postgres' ]; then + shift + fi + + # internal start of server in order to allow setup using psql client + # does not listen on external TCP/IP and waits until start finishes + set -- "$@" -c listen_addresses='' -p "${PGPORT:-5866}" + + PGUSER="${PGUSER:-$IVORYSQL_USER}" \ + pg_ctl -D "$PGDATA" \ + -o "$(printf '%q ' "$@")" \ + -w start +} + +# stop postgresql server after done setting up user and running scripts +docker_temp_server_stop() { + PGUSER="${PGUSER:-$IVORYSQL_USER}" \ + pg_ctl -D "$PGDATA" -m fast -w stop +} + +# check arguments for an option that would cause postgres to stop +# return true if there is one +_pg_want_help() { + local arg + for arg; do + case "$arg" in + # postgres --help | grep 'then exit' + # leaving out -C on purpose since it always fails and is unhelpful: + # postgres: could not access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory + -'?'|--help|--describe-config|-V|--version) + return 0 + ;; + esac + done + return 1 +} + +_main() { + # if first arg looks like a flag, assume we want to run postgres server + if [ "${1:0:1}" = '-' ]; then + set -- postgres "$@" + fi + + if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then + docker_setup_env + # setup data directories and permissions (when run as root) + docker_create_db_directories + if [ "$(id -u)" = '0' ]; then + # then restart script as ivorysql user + exec gosu ivorysql "$BASH_SOURCE" "$@" + fi + + # only run initialization on an empty data directory + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + docker_verify_minimum_env + + # check dir permissions to reduce likelihood of half-initialized database + ls /docker-entrypoint-initdb.d/ > /dev/null + + docker_init_database_dir + pg_setup_hba_conf "$@" + + # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless + # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS + export PGPASSWORD="${PGPASSWORD:-$IVORYSQL_PASSWORD}" + docker_temp_server_start "$@" + + docker_setup_db + docker_process_init_files /docker-entrypoint-initdb.d/* + + docker_temp_server_stop + unset PGPASSWORD + + cat <<-'EOM' + + PostgreSQL init process complete; ready for start up. + + EOM + else + cat <<-'EOM' + + PostgreSQL Database directory appears to contain a database; Skipping initialization + + EOM + fi + fi + + exec "$@" +} + +if ! _is_sourced; then + _main "$@" +fi diff --git a/5/ubi8/Dockerfile b/5/ubi8/Dockerfile new file mode 100644 index 0000000..de4e55b --- /dev/null +++ b/5/ubi8/Dockerfile @@ -0,0 +1,123 @@ +# +# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh" +# +# PLEASE DO NOT EDIT IT DIRECTLY. +# + +FROM redhat/ubi8:latest as builder + +# explicitly set user/group IDs +RUN groupadd -g 1000 ivorysql; \ + useradd -u 1000 -g ivorysql -d /var/local/ivorysql -s /bin/sh ivorysql; \ + mkdir -p /var/local/ivorysql; \ + mkdir -p /usr/src/ivorysql; \ + mkdir -p /var/lib/ivorysql; \ + chown -R ivorysql:ivorysql /var/local/ivorysql; \ + chown -R ivorysql:ivorysql /usr/src/ivorysql; \ + chown -R ivorysql:ivorysql /var/lib/ivorysql + +RUN mkdir /docker-entrypoint-initdb.d + +ENV IVORY_MAJOR 5 +ENV IVORY_VERSION 5.0 + +RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ + sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo + +RUN mkdir -p /usr/src/ivorysql; \ + dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm; \ + dnf install -y epel-release;\ + dnf install -y \ + make \ + readline-devel \ + zlib-devel \ + openssl-devel \ + flex \ + wget \ + dpkg; \ + dnf groupinstall -y "Development Tools";\ + dnf install -y \ + llvm-toolset llvm-devel lz4 lz4-devel lz4-libs clang-devel \ + krb5-devel libselinux-devel libxml2-devel cyrus-sasl-gssapi \ + libicu-devel e2fsprogs-devel selinux-policy systemd-devel \ + libxslt-devel openldap-devel openssl-devel pam-devel \ + libuuid-devel python3-devel readline-devel tcl-devel zlib-devel \ + perl perl-devel perl-ExtUtils-Embed;\ + dnf install -y --enablerepo=*ower*ools perl-IPC-Run perl-Time-HiRes perl-Test-Simple uuid-devel;\ + wget -O ivorysql.tar.gz "https://github.com/IvorySQL/IvorySQL/archive/refs/tags/IvorySQL_$IVORY_VERSION.tar.gz"; \ + tar \ + --extract \ + --file ivorysql.tar.gz \ + --directory /usr/src/ivorysql \ + --strip-components 1 \ + ; \ + rm ivorysql.tar.gz; \ + cd /usr/src/ivorysql; \ + wget https://repo.almalinux.org/almalinux/8/PowerTools/$(arch)/os/Packages/bison-devel-3.0.4-10.el8.$(arch).rpm; \ + dnf install -y bison-devel-3.0.4-10.el8.$(arch).rpm; \ + wget https://repo.almalinux.org/almalinux/8/AppStream/$(arch)/os/Packages/bison-3.0.4-10.el8.$(arch).rpm; \ + dnf install -y bison-3.0.4-10.el8.$(arch).rpm; \ + ./configure \ + --prefix=/var/local/ivorysql/ivorysql-$IVORY_MAJOR \ + --enable-cassert --enable-debug --enable-rpath --with-tcl \ + --with-python --with-gssapi --with-pam --with-ldap \ + --with-openssl --with-libedit-preferred --with-uuid=e2fs \ + --with-ossp-uuid --with-libxml --with-libxslt --with-perl \ + --with-icu \ + ; \ + make && make install; \ + rm -rf \ + /usr/src/ivorysql \ + /usr/local/share/doc \ + /usr/local/share/man + +RUN chown -R ivorysql:ivorysql /var/local/ivorysql/ivorysql-$IVORY_MAJOR +RUN ARCH="$(uname -m)"; \ + case "${ARCH}" in \ + x86_64) ARCH='amd64' ;; \ + aarch64) ARCH='arm64' ;; \ + *) echo "Unsupported architecture: ${ARCH}"; exit 1 ;; \ + esac; \ + curl -o /usr/local/bin/gosu -SL "https://github.com/tianon/gosu/releases/download/1.11/gosu-${ARCH}"; \ + chmod +x /usr/local/bin/gosu + +FROM redhat/ubi8:latest + +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu + +ENV IVORY_MAJOR 5 +ENV IVORY_VERSION 5.0 + +RUN mkdir /docker-entrypoint-initdb.d + +RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ + sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo + +RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm; \ + dnf install -y lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel; \ + dnf install -y e2fsprogs-devel libuuid-devel;\ + dnf install -y --enablerepo=*ower*ools uuid-devel;\ + dnf -y clean all + +ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data +# this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) +RUN mkdir -p "$PGDATA" && chown -R ivorysql:ivorysql "$PGDATA" && chmod 1777 "$PGDATA" +RUN mkdir -p /var/run/postgresql && chown -R ivorysql:ivorysql /var/run/postgresql && chmod 3777 /var/run/postgresql + +VOLUME $PGDATA + +ENV PATH $PATH:/var/local/ivorysql/ivorysql-$IVORY_MAJOR/bin + +COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/ +RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh +RUN chmod +x /usr/local/bin/docker-entrypoint.sh +ENTRYPOINT ["docker-entrypoint.sh"] + +STOPSIGNAL SIGINT + +EXPOSE 5432 5866 1521 + +CMD ["postgres"] diff --git a/5/ubi8/docker-ensure-initdb.sh b/5/ubi8/docker-ensure-initdb.sh new file mode 100755 index 0000000..2a97586 --- /dev/null +++ b/5/ubi8/docker-ensure-initdb.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +# +# This script is intended for three main use cases: +# +# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior +# +# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution +# (no-op if database is already initialized) +# +# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use +# (error if database is already initialized) +# + +source /usr/local/bin/docker-entrypoint.sh + +# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there) +if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then + set -- postgres "$@" +fi + +# see also "_main" in "docker-entrypoint.sh" + +docker_setup_env +# setup data directories and permissions (when run as root) +docker_create_db_directories +if [ "$(id -u)" = '0' ]; then + # then restart script as postgres user + exec su-exec postgres "$BASH_SOURCE" "$@" +fi + +# only run initialization on an empty data directory +if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + docker_verify_minimum_env + + # check dir permissions to reduce likelihood of half-initialized database + ls /docker-entrypoint-initdb.d/ > /dev/null + + docker_init_database_dir + pg_setup_hba_conf "$@" + + # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless + # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS + export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}" + docker_temp_server_start "$@" + + docker_setup_db + docker_process_init_files /docker-entrypoint-initdb.d/* + + docker_temp_server_stop + unset PGPASSWORD +else + self="$(basename "$0")" + case "$self" in + docker-ensure-initdb.sh) + echo >&2 "$self: note: database already initialized in '$PGDATA'!" + exit 0 + ;; + + docker-enforce-initdb.sh) + echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!" + exit 1 + ;; + + *) + echo >&2 "$self: error: unknown file name: $self" + exit 99 + ;; + esac +fi diff --git a/5/ubi8/docker-entrypoint.sh b/5/ubi8/docker-entrypoint.sh new file mode 100755 index 0000000..87b4677 --- /dev/null +++ b/5/ubi8/docker-entrypoint.sh @@ -0,0 +1,359 @@ +#!/usr/bin/env bash +set -Eeo pipefail +# TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables) + +# usage: file_env VAR [DEFAULT] +# 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) +file_env() { + local var="$1" + local fileVar="${var}_FILE" + local def="${2:-}" + if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then + printf >&2 'error: both %s and %s are set (but are exclusive)\n' "$var" "$fileVar" + exit 1 + fi + local val="$def" + if [ "${!var:-}" ]; then + val="${!var}" + elif [ "${!fileVar:-}" ]; then + val="$(< "${!fileVar}")" + fi + export "$var"="$val" + unset "$fileVar" +} + +# check to see if this file is being run or sourced from another script +_is_sourced() { + # https://unix.stackexchange.com/a/215279 + [ "${#FUNCNAME[@]}" -ge 2 ] \ + && [ "${FUNCNAME[0]}" = '_is_sourced' ] \ + && [ "${FUNCNAME[1]}" = 'source' ] +} + +# used to create initial postgres directories and if run as root, ensure ownership to the "postgres" user +docker_create_db_directories() { + local user; user="$(id -u)" + + mkdir -p "$PGDATA" + # ignore failure since there are cases where we can't chmod (and PostgreSQL might fail later anyhow - it's picky about permissions of this directory) + chmod 00700 "$PGDATA" || : + + # ignore failure since it will be fine when using the image provided directory; see also https://github.com/docker-library/postgres/pull/289 + mkdir -p /var/run/postgresql || : + chmod 03775 /var/run/postgresql || : + + # Create the transaction log directory before initdb is run so the directory is owned by the correct user + if [ -n "${POSTGRES_INITDB_WALDIR:-}" ]; then + mkdir -p "$POSTGRES_INITDB_WALDIR" + if [ "$user" = '0' ]; then + find "$POSTGRES_INITDB_WALDIR" \! -user ivorysql -exec chown ivorysql '{}' + + fi + chmod 700 "$POSTGRES_INITDB_WALDIR" + fi + + # allow the container to be started with `--user` + if [ "$user" = '0' ]; then + find "$PGDATA" \! -user ivorysql -exec chown ivorysql '{}' + + find /var/run/postgresql \! -user ivorysql -exec chown ivorysql '{}' + + fi +} + +# initialize empty PGDATA directory with new database via 'initdb' +# arguments to `initdb` can be passed via POSTGRES_INITDB_ARGS or as arguments to this function +# `initdb` automatically creates the "postgres", "template0", and "template1" dbnames +# this is also where the database user is created, specified by `IVORYSQL_USER` env +docker_init_database_dir() { + # "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary + # see https://github.com/docker-library/postgres/pull/253, https://github.com/docker-library/postgres/issues/359, https://cwrap.org/nss_wrapper.html + local uid; uid="$(id -u)" + if ! getent passwd "$uid" &> /dev/null; then + # see if we can find a suitable "libnss_wrapper.so" (https://salsa.debian.org/sssd-team/nss-wrapper/-/commit/b9925a653a54e24d09d9b498a2d913729f7abb15) + local wrapper + for wrapper in {/usr,}/lib{/*,}/libnss_wrapper.so; do + if [ -s "$wrapper" ]; then + NSS_WRAPPER_PASSWD="$(mktemp)" + NSS_WRAPPER_GROUP="$(mktemp)" + export LD_PRELOAD="$wrapper" NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP + local gid; gid="$(id -g)" + printf 'postgres:x:%s:%s:PostgreSQL:%s:/bin/false\n' "$uid" "$gid" "$PGDATA" > "$NSS_WRAPPER_PASSWD" + printf 'postgres:x:%s:\n' "$gid" > "$NSS_WRAPPER_GROUP" + break + fi + done + fi + + if [ -n "${POSTGRES_INITDB_WALDIR:-}" ]; then + set -- --waldir "$POSTGRES_INITDB_WALDIR" "$@" + fi + + # --pwfile refuses to handle a properly-empty file (hence the "\n"): https://github.com/docker-library/postgres/issues/1025 + eval 'initdb --username="$IVORYSQL_USER" --pwfile=<(printf "%s\n" "$IVORYSQL_PASSWORD") '"$POSTGRES_INITDB_ARGS"' "$@"' + sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" $PGDATA/postgresql.conf + if [ -e "$PGDATA/ivorysql.conf" ]; then + sed -ri "s!^#?(ivorysql.listen_addresses)\s*=\s*\S+.*!\1 = '*'!" $PGDATA/ivorysql.conf + fi + # unset/cleanup "nss_wrapper" bits + if [[ "${LD_PRELOAD:-}" == */libnss_wrapper.so ]]; then + rm -f "$NSS_WRAPPER_PASSWD" "$NSS_WRAPPER_GROUP" + unset LD_PRELOAD NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP + fi +} + +# print large warning if IVORYSQL_PASSWORD is long +# error if both IVORYSQL_PASSWORD is empty and POSTGRES_HOST_AUTH_METHOD is not 'trust' +# print large warning if POSTGRES_HOST_AUTH_METHOD is set to 'trust' +# assumes database is not set up, ie: [ -z "$DATABASE_ALREADY_EXISTS" ] +docker_verify_minimum_env() { + case "${IVORY_MAJOR:-}" in + 12 | 13) # https://github.com/postgres/postgres/commit/67a472d71c98c3d2fa322a1b4013080b20720b98 + # check password first so we can output the warning before postgres + # messes it up + if [ "${#IVORYSQL_PASSWORD}" -ge 100 ]; then + cat >&2 <<-'EOWARN' + + WARNING: The supplied IVORYSQL_PASSWORD is 100+ characters. + + This will not work if used via PGPASSWORD with "psql". + + https://www.postgresql.org/message-id/flat/E1Rqxp2-0004Qt-PL%40wrigleys.postgresql.org (BUG #6412) + https://github.com/docker-library/postgres/issues/507 + + EOWARN + fi + ;; + esac + if [ -z "$IVORYSQL_PASSWORD" ] && [ 'trust' != "$POSTGRES_HOST_AUTH_METHOD" ]; then + # The - option suppresses leading tabs but *not* spaces. :) + cat >&2 <<-'EOE' + Error: Database is uninitialized and superuser password is not specified. + You must specify IVORYSQL_PASSWORD to a non-empty value for the + superuser. For example, "-e IVORYSQL_PASSWORD=password" on "docker run". + + You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all + connections without a password. This is *not* recommended. + + See PostgreSQL documentation about "trust": + https://www.postgresql.org/docs/current/auth-trust.html + EOE + exit 1 + fi + if [ 'trust' = "$POSTGRES_HOST_AUTH_METHOD" ]; then + cat >&2 <<-'EOWARN' + ******************************************************************************** + WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow + anyone with access to the Postgres port to access your database without + a password, even if IVORYSQL_PASSWORD is set. See PostgreSQL + documentation about "trust": + https://www.postgresql.org/docs/current/auth-trust.html + In Docker's default configuration, this is effectively any other + container on the same system. + + It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace + it with "-e IVORYSQL_PASSWORD=password" instead to set a password in + "docker run". + ******************************************************************************** + EOWARN + fi +} + +# usage: docker_process_init_files [file [file [...]]] +# ie: docker_process_init_files /always-initdb.d/* +# process initializer files, based on file extensions and permissions +docker_process_init_files() { + # psql here for backwards compatibility "${psql[@]}" + psql=( docker_process_sql ) + + printf '\n' + local f + for f; do + case "$f" in + *.sh) + # https://github.com/docker-library/postgres/issues/450#issuecomment-393167936 + # https://github.com/docker-library/postgres/pull/452 + if [ -x "$f" ]; then + printf '%s: running %s\n' "$0" "$f" + "$f" + else + printf '%s: sourcing %s\n' "$0" "$f" + . "$f" + fi + ;; + *.sql) printf '%s: running %s\n' "$0" "$f"; docker_process_sql -f "$f"; printf '\n' ;; + *.sql.gz) printf '%s: running %s\n' "$0" "$f"; gunzip -c "$f" | docker_process_sql; printf '\n' ;; + *.sql.xz) printf '%s: running %s\n' "$0" "$f"; xzcat "$f" | docker_process_sql; printf '\n' ;; + *.sql.zst) printf '%s: running %s\n' "$0" "$f"; zstd -dc "$f" | docker_process_sql; printf '\n' ;; + *) printf '%s: ignoring %s\n' "$0" "$f" ;; + esac + printf '\n' + done +} + +# Execute sql script, passed via stdin (or -f flag of pqsl) +# usage: docker_process_sql [psql-cli-args] +# ie: docker_process_sql --dbname=mydb <<<'INSERT ...' +# ie: docker_process_sql -f my-file.sql +# ie: docker_process_sql > "$PGDATA/pg_hba.conf" +} + +# start socket-only postgresql server for setting up or running scripts +# all arguments will be passed along as arguments to `postgres` (via pg_ctl) +docker_temp_server_start() { + if [ "$1" = 'postgres' ]; then + shift + fi + + # internal start of server in order to allow setup using psql client + # does not listen on external TCP/IP and waits until start finishes + set -- "$@" -c listen_addresses='' -p "${PGPORT:-5866}" + + PGUSER="${PGUSER:-$IVORYSQL_USER}" \ + pg_ctl -D "$PGDATA" \ + -o "$(printf '%q ' "$@")" \ + -w start +} + +# stop postgresql server after done setting up user and running scripts +docker_temp_server_stop() { + PGUSER="${PGUSER:-$IVORYSQL_USER}" \ + pg_ctl -D "$PGDATA" -m fast -w stop +} + +# check arguments for an option that would cause postgres to stop +# return true if there is one +_pg_want_help() { + local arg + for arg; do + case "$arg" in + # postgres --help | grep 'then exit' + # leaving out -C on purpose since it always fails and is unhelpful: + # postgres: could not access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory + -'?'|--help|--describe-config|-V|--version) + return 0 + ;; + esac + done + return 1 +} + +_main() { + # if first arg looks like a flag, assume we want to run postgres server + if [ "${1:0:1}" = '-' ]; then + set -- postgres "$@" + fi + + if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then + docker_setup_env + # setup data directories and permissions (when run as root) + docker_create_db_directories + if [ "$(id -u)" = '0' ]; then + # then restart script as ivorysql user + exec gosu ivorysql "$BASH_SOURCE" "$@" + fi + + # only run initialization on an empty data directory + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + docker_verify_minimum_env + + # check dir permissions to reduce likelihood of half-initialized database + ls /docker-entrypoint-initdb.d/ > /dev/null + + docker_init_database_dir + pg_setup_hba_conf "$@" + + # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless + # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS + export PGPASSWORD="${PGPASSWORD:-$IVORYSQL_PASSWORD}" + docker_temp_server_start "$@" + + docker_setup_db + docker_process_init_files /docker-entrypoint-initdb.d/* + + docker_temp_server_stop + unset PGPASSWORD + + cat <<-'EOM' + + PostgreSQL init process complete; ready for start up. + + EOM + else + cat <<-'EOM' + + PostgreSQL Database directory appears to contain a database; Skipping initialization + + EOM + fi + fi + + exec "$@" +} + +if ! _is_sourced; then + _main "$@" +fi diff --git a/Dockerfile-debian.template b/Dockerfile-debian.template index 0703a7a..bcef36e 100644 --- a/Dockerfile-debian.template +++ b/Dockerfile-debian.template @@ -106,16 +106,16 @@ RUN chmod +x /usr/local/bin/gosu FROM debian:{{ env.variant }}-slim -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR {{ env.version }} ENV IVORY_VERSION {{ .[env.variant].version }} ARG USE_CHINA_MIRROR=false RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - # Use China mirror if specified RUN if [ "$USE_CHINA_MIRROR" = "true" ]; then \ sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources ;\ @@ -131,13 +131,11 @@ RUN apt-get update && \ {{ if env.variant == "bookworm" then "libldap-2.5-0 " else "libldap2" end }} \ libldap2-dev \ libreadline-dev \ - libicu-dev && \ + libicu-dev \ + libossp-uuid-dev && \ apt-get clean -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin - ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) @@ -158,4 +156,4 @@ STOPSIGNAL SIGINT EXPOSE 5432 5866 1521 -CMD ["postgres"] \ No newline at end of file +CMD ["postgres"] diff --git a/Dockerfile-redhat.template b/Dockerfile-redhat.template index 7e33332..a4f5ec4 100644 --- a/Dockerfile-redhat.template +++ b/Dockerfile-redhat.template @@ -77,21 +77,24 @@ RUN ARCH="$(uname -m)"; \ FROM redhat/{{ env.variant }}:latest -COPY --from=builder /var/local/ivorysql /var/local/ivorysql/ -COPY --from=builder /usr/local/bin/gosu /usr/local/bin/gosu +RUN useradd -u 1000 ivorysql + +COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql +COPY --from=builder --chown=ivorysql:ivorysql /usr/local/bin/gosu /usr/local/bin/gosu ENV IVORY_MAJOR {{ env.version}} ENV IVORY_VERSION {{ .[env.variant].version }} RUN mkdir /docker-entrypoint-initdb.d -RUN useradd -u 1000 ivorysql - -RUN dnf install -y \ - lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel && dnf -y clean all +RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ + sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo -RUN chown -R ivorysql:ivorysql /var/local/ivorysql -RUN chown -R ivorysql:ivorysql /usr/local/bin +RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm; \ + dnf install -y lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel; \ + dnf install -y e2fsprogs-devel libuuid-devel;\ + dnf install -y --enablerepo=*ower*ools uuid-devel;\ + dnf -y clean all ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values) diff --git a/docker-cluster/Dockerfile b/docker-cluster/Dockerfile index 7570e86..521cd79 100644 --- a/docker-cluster/Dockerfile +++ b/docker-cluster/Dockerfile @@ -1,8 +1,11 @@ FROM redhat/ubi8:latest as builder +ARG MAJOR_VERSION="5" +ARG FULL_VERSION="5.0" + # explicitly set user/group IDs RUN groupadd -g 1000 ivorysql; \ - useradd -u 1000 -g ivorysql -d /var/local/ivorysql -s /bin/sh ivorysql; \ + useradd -u 1000 -g ivorysql -d /var/local/ivorysql -s /bin/sh ivorysql; \ mkdir -p /var/local/ivorysql; \ mkdir -p /usr/src/ivorysql; \ mkdir -p /var/lib/ivorysql; \ @@ -11,9 +14,9 @@ RUN groupadd -g 1000 ivorysql; \ chown -R ivorysql:ivorysql /var/lib/ivorysql RUN mkdir /docker-entrypoint-initdb.d -# install ivorysql 4.6 -ENV IVORY_MAJOR 4 -ENV IVORY_VERSION 4.6 +# install ivorysql +ENV IVORY_MAJOR $MAJOR_VERSION +ENV IVORY_VERSION $FULL_VERSION RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo @@ -40,11 +43,11 @@ RUN mkdir -p /usr/src/ivorysql; \ dnf install -y --enablerepo=*ower*ools perl-IPC-Run perl-Time-HiRes perl-Test-Simple uuid-devel;\ wget -O ivorysql.tar.gz "https://github.com/IvorySQL/IvorySQL/archive/refs/tags/IvorySQL_$IVORY_VERSION.tar.gz"; \ tar \ - --extract \ - --file ivorysql.tar.gz \ - --directory /usr/src/ivorysql \ - --strip-components 1 \ - ; \ + --extract \ + --file ivorysql.tar.gz \ + --directory /usr/src/ivorysql \ + --strip-components 1 \ + ; \ rm ivorysql.tar.gz; \ cd /usr/src/ivorysql; \ wget https://repo.almalinux.org/almalinux/8/PowerTools/$(arch)/os/Packages/bison-devel-3.0.4-10.el8.$(arch).rpm; \ @@ -61,21 +64,31 @@ RUN mkdir -p /usr/src/ivorysql; \ ; \ make && make install; \ rm -rf \ - /usr/src/ivorysql \ - /usr/local/share/doc \ - /usr/local/share/man + /usr/src/ivorysql \ + /usr/local/share/doc \ + /usr/local/share/man FROM redhat/ubi8:latest -RUN useradd -u 1000 ivorysql +ARG MAJOR_VERSION="5" +ARG FULL_VERSION="5.0" + +RUN groupadd -g 1000 ivorysql; \ + useradd -u 1000 -g ivorysql -d /var/local/ivorysql -s /bin/sh ivorysql; COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ -ENV IVORY_MAJOR 4 -ENV IVORY_VERSION 4.6 +ENV IVORY_MAJOR $MAJOR_VERSION +ENV IVORY_VERSION $FULL_VERSION + +RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ + sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo -RUN dnf install -y \ - lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel && dnf -y clean all +RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm; \ + dnf install -y lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel; \ + dnf install -y e2fsprogs-devel libuuid-devel;\ + dnf install -y --enablerepo=*ower*ools uuid-devel;\ + dnf -y clean all #install patroni 4.0.6 env PATRONI_VER 4.0.6 @@ -88,7 +101,7 @@ RUN dnf -y install --nodocs \ && dnf -y clean all RUN pip3 install --upgrade python-dateutil \ - && pip3 install patroni[etcd]=="${PATRONI_VER}" + && pip3 install patroni[etcd]=="${PATRONI_VER}" ENV PGDATA /var/local/ivorysql/ivorysql-$IVORY_MAJOR/data ENV PATRONICONF /var/local/ivorysql/ivorysql-$IVORY_MAJOR/patroni @@ -107,4 +120,4 @@ EXPOSE 5432 5866 1521 USER ivorysql -ENTRYPOINT ["/bin/sh", "/docker-entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/bin/sh", "/docker-entrypoint.sh"] diff --git a/docker-cluster/docker-compose/docker-compose-ivypatroni_1.yml b/docker-cluster/docker-compose/docker-compose-ivypatroni_1.yml index 54540dd..1db9a78 100644 --- a/docker-cluster/docker-compose/docker-compose-ivypatroni_1.yml +++ b/docker-cluster/docker-compose/docker-compose-ivypatroni_1.yml @@ -1,17 +1,17 @@ -services: - ivypatroni1: - image: ivorysql/docker-compose-ha-cluster:4.6-4.0.6-ubi8 - restart: always - container_name: ivyhac1 - user: "ivorysql" - environment: - PATRONI_SCOPE: ivory-cluster - ETCD_HOSTS: :2379,:2379,:2379 - IVORYSQL_PASSWORD: 123456 - IVORYSQL_HOST_AUTH_METHOD: md5 - PATRONI_SERVICE_NAME: ivypatroni1 - IVORYSQL_HOST: - network_mode: host - volumes: - - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-4/patroni/ - - /home/ivorysql/ivydata:/var/local/ivorysql/ivorysql-4/data/ +services: + ivypatroni1: + image: ivorysql/docker-compose-ha-cluster:5.0-4.0.6-ubi8 + restart: always + container_name: ivyhac1 + user: "ivorysql" + environment: + PATRONI_SCOPE: ivory-cluster + ETCD_HOSTS: :2379,:2379,:2379 + IVORYSQL_PASSWORD: 123456 + IVORYSQL_HOST_AUTH_METHOD: md5 + PATRONI_SERVICE_NAME: ivypatroni1 + IVORYSQL_HOST: + network_mode: host + volumes: + - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-5/patroni/ + - /home/ivorysql/ivydata:/var/local/ivorysql/ivorysql-5/data/ diff --git a/docker-cluster/docker-compose/docker-compose-ivypatroni_2.yml b/docker-cluster/docker-compose/docker-compose-ivypatroni_2.yml index cacd07d..68adb02 100644 --- a/docker-cluster/docker-compose/docker-compose-ivypatroni_2.yml +++ b/docker-cluster/docker-compose/docker-compose-ivypatroni_2.yml @@ -1,17 +1,17 @@ -services: - ivypatroni2: - image: ivorysql/docker-compose-ha-cluster:4.6-4.0.6-ubi8 - restart: always - container_name: ivyhac2 - user: "ivorysql" - environment: - PATRONI_SCOPE: ivory-cluster - ETCD_HOSTS: :2379,:2379,:2379 - IVORYSQL_PASSWORD: 123456 - IVORYSQL_HOST_AUTH_METHOD: md5 - PATRONI_SERVICE_NAME: ivypatroni2 - IVORYSQL_HOST: - network_mode: host - volumes: - - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-4/patroni/ - - /home/ivorysql/ivydata:/var/local/ivorysql/ivorysql-4/data/ +services: + ivypatroni2: + image: ivorysql/docker-compose-ha-cluster:5.0-4.0.6-ubi8 + restart: always + container_name: ivyhac2 + user: "ivorysql" + environment: + PATRONI_SCOPE: ivory-cluster + ETCD_HOSTS: :2379,:2379,:2379 + IVORYSQL_PASSWORD: 123456 + IVORYSQL_HOST_AUTH_METHOD: md5 + PATRONI_SERVICE_NAME: ivypatroni2 + IVORYSQL_HOST: + network_mode: host + volumes: + - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-5/patroni/ + - /home/ivorysql/ivydata:/var/local/ivorysql/ivorysql-5/data/ diff --git a/docker-cluster/docker-compose/docker-compose-ivypatroni_3.yml b/docker-cluster/docker-compose/docker-compose-ivypatroni_3.yml index bba9463..2bbab4d 100644 --- a/docker-cluster/docker-compose/docker-compose-ivypatroni_3.yml +++ b/docker-cluster/docker-compose/docker-compose-ivypatroni_3.yml @@ -1,17 +1,17 @@ -services: - ivypatroni3: - image: ivorysql/docker-compose-ha-cluster:4.6-4.0.6-ubi8 - restart: always - container_name: ivyhac3 - user: "ivorysql" - environment: - PATRONI_SCOPE: ivory-cluster - ETCD_HOSTS: :2379,:2379,:2379 - IVORYSQL_PASSWORD: 123456 - IVORYSQL_HOST_AUTH_METHOD: md5 - PATRONI_SERVICE_NAME: ivypatroni3 - IVORYSQL_HOST: - network_mode: host - volumes: - - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-4/patroni/ - - /home/ivorysql/ivydata:/var/local/ivorysql/ivorysql-4/data/ +services: + ivypatroni3: + image: ivorysql/docker-compose-ha-cluster:5.0-4.0.6-ubi8 + restart: always + container_name: ivyhac3 + user: "ivorysql" + environment: + PATRONI_SCOPE: ivory-cluster + ETCD_HOSTS: :2379,:2379,:2379 + IVORYSQL_PASSWORD: 123456 + IVORYSQL_HOST_AUTH_METHOD: md5 + PATRONI_SERVICE_NAME: ivypatroni3 + IVORYSQL_HOST: + network_mode: host + volumes: + - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-5/patroni/ + - /home/ivorysql/ivydata:/var/local/ivorysql/ivorysql-5/data/ diff --git a/docker-cluster/docker-swarm/docker-swarm-ivypatroni.yml b/docker-cluster/docker-swarm/docker-swarm-ivypatroni.yml index 1b784fd..993c173 100644 --- a/docker-cluster/docker-swarm/docker-swarm-ivypatroni.yml +++ b/docker-cluster/docker-swarm/docker-swarm-ivypatroni.yml @@ -1,64 +1,64 @@ -version: '3.7' - -services: - ivypatroni1: - image: ivorysql/docker-swarm-ha-cluster:4.6-4.0.6-ubi8 - environment: - IVORYSQL_PASSWORD: 123456 - PATRONI_SCOPE: hgdb-cluster1 - PATRONI_SERVICE_NAME: ivypatroni1 - IVORYSQL_DB_MODE: oracle - IVORYSQL_HOST_AUTH_METHOD: scram-sha-256 - ETCD_HOSTS: etcd1:2379,etcd2:2379,etcd3:2379 - IVORYSQL_HOST: ivypatroni1 - volumes: - - /home/ivorysql/data:/var/local/ivorysql/ivorysql-4/data - - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-4/patroni - networks: - - ivoryhac-etcd_etcd-net - deploy: - replicas: 1 - resources: - limits: - cpus: "0.5" - memory: 512M - restart_policy: - condition: on-failure - placement: - constraints: [node.hostname == manager-node1] - ports: - - "5432:5432" - - "1521:1521" - - ivypatroni2: - image: ivorysql/docker-swarm-ha-cluster:4.6-4.0.6-ubi8 - environment: - IVORYSQL_PASSWORD: 123456 - PATRONI_SCOPE: hgdb-cluster1 - PATRONI_SERVICE_NAME: ivypatroni2 - IVORYSQL_DB_MODE: oracle - IVORYSQL_HOST_AUTH_METHOD: scram-sha-256 - ETCD_HOSTS: etcd1:2379,etcd2:2379,etcd3:2379 - IVORYSQL_HOST: ivypatroni2 - volumes: - - /home/ivorysql/data:/var/local/ivorysql/ivorysql-4/data - - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-4/patroni - networks: - - ivoryhac-etcd_etcd-net - deploy: - replicas: 1 - resources: - limits: - cpus: "0.5" - memory: 512M - restart_policy: - condition: on-failure - placement: - constraints: [node.hostname == manager-node2] - ports: - - "5433:5432" - - "1522:1521" - -networks: - ivoryhac-etcd_etcd-net: - external: true +version: '3.7' + +services: + ivypatroni1: + image: ivorysql/docker-swarm-ha-cluster:5.0-4.0.6-ubi8 + environment: + IVORYSQL_PASSWORD: 123456 + PATRONI_SCOPE: hgdb-cluster1 + PATRONI_SERVICE_NAME: ivypatroni1 + IVORYSQL_DB_MODE: oracle + IVORYSQL_HOST_AUTH_METHOD: scram-sha-256 + ETCD_HOSTS: etcd1:2379,etcd2:2379,etcd3:2379 + IVORYSQL_HOST: ivypatroni1 + volumes: + - /home/ivorysql/data:/var/local/ivorysql/ivorysql-5/data + - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-5/patroni + networks: + - ivoryhac-etcd_etcd-net + deploy: + replicas: 1 + resources: + limits: + cpus: "0.5" + memory: 512M + restart_policy: + condition: on-failure + placement: + constraints: [node.hostname == manager-node1] + ports: + - "5432:5432" + - "1521:1521" + + ivypatroni2: + image: ivorysql/docker-swarm-ha-cluster:5.0-4.0.6-ubi8 + environment: + IVORYSQL_PASSWORD: 123456 + PATRONI_SCOPE: hgdb-cluster1 + PATRONI_SERVICE_NAME: ivypatroni2 + IVORYSQL_DB_MODE: oracle + IVORYSQL_HOST_AUTH_METHOD: scram-sha-256 + ETCD_HOSTS: etcd1:2379,etcd2:2379,etcd3:2379 + IVORYSQL_HOST: ivypatroni2 + volumes: + - /home/ivorysql/data:/var/local/ivorysql/ivorysql-5/data + - /home/ivorysql/patroni:/var/local/ivorysql/ivorysql-5/patroni + networks: + - ivoryhac-etcd_etcd-net + deploy: + replicas: 1 + resources: + limits: + cpus: "0.5" + memory: 512M + restart_policy: + condition: on-failure + placement: + constraints: [node.hostname == manager-node2] + ports: + - "5433:5432" + - "1522:1521" + +networks: + ivoryhac-etcd_etcd-net: + external: true diff --git a/k8s-cluster/ha-cluster/Dockerfile b/k8s-cluster/ha-cluster/Dockerfile index 8f17a72..b330530 100644 --- a/k8s-cluster/ha-cluster/Dockerfile +++ b/k8s-cluster/ha-cluster/Dockerfile @@ -1,5 +1,8 @@ FROM redhat/ubi8:latest as builder +ARG MAJOR_VERSION="5" +ARG FULL_VERSION="5.0" + # explicitly set user/group IDs RUN groupadd -g 1000 ivorysql; \ useradd -u 1000 -g ivorysql -d /var/local/ivorysql -s /bin/sh ivorysql; \ @@ -11,9 +14,9 @@ RUN groupadd -g 1000 ivorysql; \ chown -R ivorysql:ivorysql /var/lib/ivorysql RUN mkdir /docker-entrypoint-initdb.d -# install ivorysql 4.6 -ENV IVORY_MAJOR 4 -ENV IVORY_VERSION 4.6 +# install ivorysql +ENV IVORY_MAJOR $MAJOR_VERSION +ENV IVORY_VERSION $FULL_VERSION RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo @@ -67,15 +70,25 @@ RUN mkdir -p /usr/src/ivorysql; \ FROM redhat/ubi8:latest -RUN useradd -u 1000 ivorysql +ARG MAJOR_VERSION="5" +ARG FULL_VERSION="5.0" + +RUN groupadd -g 1000 ivorysql; \ + useradd -u 1000 -g ivorysql -d /var/local/ivorysql -s /bin/sh ivorysql; COPY --from=builder --chown=ivorysql:ivorysql /var/local/ivorysql /var/local/ivorysql/ -ENV IVORY_MAJOR 4 -ENV IVORY_VERSION 4.6 +ENV IVORY_MAJOR $MAJOR_VERSION +ENV IVORY_VERSION $FULL_VERSION + +RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo; \ + sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo -RUN dnf install -y \ - lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel && dnf -y clean all +RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm; \ + dnf install -y lz4 lz4-devel lz4-libs krb5-devel libxslt-devel libicu-devel; \ + dnf install -y e2fsprogs-devel libuuid-devel;\ + dnf install -y --enablerepo=*ower*ools uuid-devel;\ + dnf -y clean all #install patroni 4.0.6 env PATRONI_VER 4.0.6 diff --git a/k8s-cluster/ha-cluster/helm_charts/values.yaml b/k8s-cluster/ha-cluster/helm_charts/values.yaml index d5140d4..a30a553 100644 --- a/k8s-cluster/ha-cluster/helm_charts/values.yaml +++ b/k8s-cluster/ha-cluster/helm_charts/values.yaml @@ -1,90 +1,90 @@ -# Default values for ivorysql-patroni-hac. -# This is a YAML-formatted file. -# Declare variables to be passed into your templates. - -replicaCount: 3 -namespace: ivorysql -clusterName: ivorysql-patroni-hac -svcAccountName: ivorysql-patroni-hac -roleName: ivorysql-patroni-hac -endpointName: ivorysql-patroni-hac -roleBindingName: ivorysql-patroni-hac -podServiceName: ivorysql-patroni-hac-pods -clusterRoleName: ivorysql-patroni-hac-ep-access -configMapName: ivyhac -applicationName: ivyhac -serviceConfigName: ivorysql-patroni-hac-config -databaseServiceName: ivorysql-patroni-hac -ivyHacReplicaServiceName: ivorysql-patroni-hac-repl -majorVersion: 4 -compatibleMode: oracle - -image: - repository: ivorysql/k8s-ha-cluster - pullPolicy: IfNotPresent - # Overrides the image tag whose default is the chart appVersion. - tag: "4.6-4.0.6-ubi8" - -imagePullSecrets: [] -nameOverride: "" -fullnameOverride: "" -encryption: "md5" -serviceAccount: - # Specifies whether a service account should be created - create: true - # Annotations to add to the service account - annotations: {} - # The name of the service account to use. - # If not set and create is true, a name is generated using the fullname template - name: "" - -podAnnotations: {} - -podSecurityContext: {} - -securityContext: {} - -service: - type: ClusterIP - port: 80 - db: - type: NodePort -port: - dbPort: 32766 - dbReplPort: 32765 -pvc: - storageClassName: incloud-nfs - accessModes: ReadWriteOnce - storageSize: 1Gi - confStorageSize: 16Mi -ingress: - enabled: false - className: "" - annotations: {} - hosts: - - host: chart-example.local - paths: - - path: / - pathType: ImplementationSpecific - tls: [] - -resources: - limits: - cpu: 1 - memory: 1Gi - requests: - cpu: 200m - memory: 256Mi - -autoscaling: - enabled: false - minReplicas: 1 - maxReplicas: 100 - targetCPUUtilizationPercentage: 80 - # targetMemoryUtilizationPercentage: 80 - -nodeSelector: {} - -tolerations: [] - -affinity: {} +# Default values for ivorysql-patroni-hac. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 3 +namespace: ivorysql +clusterName: ivorysql-patroni-hac +svcAccountName: ivorysql-patroni-hac +roleName: ivorysql-patroni-hac +endpointName: ivorysql-patroni-hac +roleBindingName: ivorysql-patroni-hac +podServiceName: ivorysql-patroni-hac-pods +clusterRoleName: ivorysql-patroni-hac-ep-access +configMapName: ivyhac +applicationName: ivyhac +serviceConfigName: ivorysql-patroni-hac-config +databaseServiceName: ivorysql-patroni-hac +ivyHacReplicaServiceName: ivorysql-patroni-hac-repl +majorVersion: 4 +compatibleMode: oracle + +image: + repository: ivorysql/k8s-ha-cluster + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "5.0-4.0.6-ubi8" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" +encryption: "md5" +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + +securityContext: {} + +service: + type: ClusterIP + port: 80 + db: + type: NodePort +port: + dbPort: 32766 + dbReplPort: 32765 +pvc: + storageClassName: incloud-nfs + accessModes: ReadWriteOnce + storageSize: 1Gi + confStorageSize: 16Mi +ingress: + enabled: false + className: "" + annotations: {} + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + +resources: + limits: + cpu: 1 + memory: 1Gi + requests: + cpu: 200m + memory: 256Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/k8s-cluster/single/statefulset.yaml b/k8s-cluster/single/statefulset.yaml index ce5c60f..6cac4e4 100644 --- a/k8s-cluster/single/statefulset.yaml +++ b/k8s-cluster/single/statefulset.yaml @@ -1,75 +1,75 @@ -apiVersion: v1 -kind: Service -metadata: - name: ivorysql-svc - namespace: default - labels: - application: ivorysql -spec: - type: NodePort - selector: - application: ivorysql - ports: - - protocol: TCP - name: ivorysql - port: 5432 - targetPort: 5432 - - protocol: TCP - name: ivorysql-oracle - port: 1521 - targetPort: 1521 ---- -apiVersion: apps/v1 -kind: StatefulSet -metadata: - name: ivorysql - namespace: default - labels: - application: ivorysql -spec: - serviceName: ivorysql-svc - selector: - matchLabels: - application: ivorysql - template: - metadata: - labels: - application: ivorysql - spec: - containers: - - name: ivorysql - image: ivorysql/ivorysql:4.6-ubi8 - imagePullPolicy: IfNotPresent - readinessProbe: - exec: - command: - - "pg_isready" - initialDelaySeconds: 3 - periodSeconds: 10 - timeoutSeconds: 5 - successThreshold: 1 - failureThreshold: 3 - ports: - - containerPort: 5432 - protocol: TCP - - containerPort: 1521 - protocol: TCP - volumeMounts: - - mountPath: /var/local/ivorysql/ivorysql-4/data - name: pgdata - env: - - name: IVORYSQL_PASSWORD - value: 'Hello@123' - terminationGracePeriodSeconds: 0 - volumeClaimTemplates: - - metadata: - labels: - application: ivorysql - name: pgdata - spec: - storageClassName: incloud-nfs - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 1Gi +apiVersion: v1 +kind: Service +metadata: + name: ivorysql-svc + namespace: default + labels: + application: ivorysql +spec: + type: NodePort + selector: + application: ivorysql + ports: + - protocol: TCP + name: ivorysql + port: 5432 + targetPort: 5432 + - protocol: TCP + name: ivorysql-oracle + port: 1521 + targetPort: 1521 +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: ivorysql + namespace: default + labels: + application: ivorysql +spec: + serviceName: ivorysql-svc + selector: + matchLabels: + application: ivorysql + template: + metadata: + labels: + application: ivorysql + spec: + containers: + - name: ivorysql + image: ivorysql/ivorysql:5.0-ubi8 + imagePullPolicy: IfNotPresent + readinessProbe: + exec: + command: + - "pg_isready" + initialDelaySeconds: 3 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 3 + ports: + - containerPort: 5432 + protocol: TCP + - containerPort: 1521 + protocol: TCP + volumeMounts: + - mountPath: /var/local/ivorysql/ivorysql-4/data + name: pgdata + env: + - name: IVORYSQL_PASSWORD + value: 'Hello@123' + terminationGracePeriodSeconds: 0 + volumeClaimTemplates: + - metadata: + labels: + application: ivorysql + name: pgdata + spec: + storageClassName: incloud-nfs + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi diff --git a/versions.json b/versions.json index fa3cd1d..5c08aa8 100644 --- a/versions.json +++ b/versions.json @@ -67,6 +67,40 @@ ], "version": "4.6" }, + "5": { + "ubi8": { + "arches": [ + "amd64" + ], + "version": "5.0" + }, + "redhat": "ubi8", + "debian": [ + "bookworm", + "trixie" + ], + "bookworm": { + "arches": [ + "amd64", + "arm64v8" + ], + "version": "5.0" + }, + "trixie": { + "arches": [ + "amd64", + "arm64v8" + ], + "version": "5.0" + }, + "major": 5, + "variants": [ + "ubi8", + "bookworm", + "trixie" + ], + "version": "5.0" + }, "1": { "debian": "trixie", "trixie": {