|
| 1 | +# NOTE: This Dockerfile will require a more recent version of Docker that has heredoc support. |
| 2 | +# |
| 3 | +# Example of how to build this Dockerfile (requires being launched from the root of checkout): |
| 4 | +# docker build --build-arg PG_VER=15 --build-arg PLRUST_VER=1.0.2 -f .github/docker/Dockerfile.debian-artifact-build -t plrust-debian . |
| 5 | +# |
| 6 | +# Example of how to run an image after it has been built (assuming previous step has been done): |
| 7 | +# docker run -it --rm plrust-debian /bin/bash |
| 8 | +# |
| 9 | +# Any artifacts produced will be in the /out directory of the running Docker container. With a container running, an example |
| 10 | +# of how to copy out artifacts: |
| 11 | +# docker cp container_name:/out . |
| 12 | +# which will copy the container's /out directory to the current directory of the host |
| 13 | + |
| 14 | +FROM debian:bullseye |
| 15 | + |
| 16 | +# Args to pass in at build-time. Note that these are defaulted. |
| 17 | +ARG PG_VER=15 |
| 18 | +ARG PLRUST_VER=0.0.0 |
| 19 | + |
| 20 | +# Change this value if the entire build cache needs to be busted for any reason. |
| 21 | +# Any random value can work here. UUIDs seemed to be a good choice. |
| 22 | +ARG CACHE_KEY=2d912233-4423-48f9-b7ba-afc6b2ef3a44 |
| 23 | + |
| 24 | +SHELL ["/bin/bash", "-c"] |
| 25 | + |
| 26 | +# Install system-level dependencies. Prefer to install Postgres from the official |
| 27 | +# Postgres Debian packages |
| 28 | +RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \ |
| 29 | + apt-get update && \ |
| 30 | + apt-get install -y --no-install-recommends \ |
| 31 | + ca-certificates \ |
| 32 | + gnupg \ |
| 33 | + lsb-release \ |
| 34 | + wget && \ |
| 35 | + sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \ |
| 36 | + wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null && \ |
| 37 | + apt-get update -y -qq --fix-missing && \ |
| 38 | + apt-get install -y --no-install-recommends \ |
| 39 | + build-essential \ |
| 40 | + clang \ |
| 41 | + clang-11 \ |
| 42 | + gcc \ |
| 43 | + git \ |
| 44 | + jq \ |
| 45 | + libssl-dev \ |
| 46 | + llvm-11 \ |
| 47 | + make \ |
| 48 | + postgresql-${PG_VER} \ |
| 49 | + postgresql-server-dev-${PG_VER} \ |
| 50 | + pkg-config \ |
| 51 | + ruby && \ |
| 52 | + rm -rf /var/lib/apt/lists/* |
| 53 | + |
| 54 | +# Install fpm for the creation of the .deb file, |
| 55 | +# and install toml so TOML files can be parsed later |
| 56 | +RUN gem install --no-document fpm toml |
| 57 | + |
| 58 | +# Set up permissions so that the postgres user can install the plrust plugin |
| 59 | +RUN chmod a+rwx `$(which pg_config) --pkglibdir` `$(which pg_config) --sharedir`/extension |
| 60 | + |
| 61 | +# Create the directory for the outgoing .deb package |
| 62 | +RUN mkdir /out && chmod 777 /out |
| 63 | + |
| 64 | +# The 'postgres' user is the default user that the official Postgres Debian packages create and set up |
| 65 | +USER postgres |
| 66 | +ENV USER postgres |
| 67 | + |
| 68 | +# Copy in plrust source |
| 69 | +COPY --chown=${USER} . src/ |
| 70 | +WORKDIR /src |
| 71 | + |
| 72 | +# Obtain the toolchain version from rust-toolchain.toml and store that into a file. |
| 73 | +RUN ruby <<EOF |
| 74 | +require 'toml' |
| 75 | +toml=TOML.load_file('/src/rust-toolchain.toml') |
| 76 | +if ver=toml['toolchain']['channel'] |
| 77 | + File.open('/tmp/.toolchain-ver', 'w') { |file| file.write(ver) } |
| 78 | +else |
| 79 | + raise 'Could not determine toolchain channel version. Is rust-toolchain.toml missing or malformed?' |
| 80 | +end |
| 81 | +EOF |
| 82 | + |
| 83 | +# Install Rust |
| 84 | +RUN TOOLCHAIN_VER=$(</tmp/.toolchain-ver) && wget -qO- https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain=$TOOLCHAIN_VER |
| 85 | +ENV PATH="/var/lib/postgresql/.cargo/bin:${PATH}" |
| 86 | + |
| 87 | +# Install the version of PGRX that is dictated by Cargo.toml for this project |
| 88 | +RUN PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version') && \ |
| 89 | + cargo install cargo-pgrx --force --version "$PGRX_VERSION" |
| 90 | + |
| 91 | +# Necessary to build plrustc and company |
| 92 | +RUN rustup component add llvm-tools-preview rustc-dev |
| 93 | + |
| 94 | +# Build and install plrstuc |
| 95 | +RUN cd /src/plrustc && ./build.sh && cp ../build/bin/plrustc ~/.cargo/bin |
| 96 | + |
| 97 | +# Init cargo pgrx |
| 98 | +RUN cargo pgrx init --pg${PG_VER} $(which pg_config) |
| 99 | + |
| 100 | +# Build plrust with postgrestd. Architecture is determined by the host running this Dockerfile |
| 101 | +# (i.e. no cross-compiling) |
| 102 | +RUN cd /src/plrust && STD_TARGETS="$(uname -m)-postgres-linux-gnu" ./build |
| 103 | + |
| 104 | +# Use cargo pgrx package to create everything necessary, placed into RELEASE_DIR below |
| 105 | +RUN cd /src/plrust && cargo pgrx package --features trusted |
| 106 | + |
| 107 | +# This is the root of all the files necessary to package up into the .deb file. This specific path is generated by |
| 108 | +# 'cargo pgrx package' above |
| 109 | +ENV RELEASE_DIR /src/target/release/plrust-pg${PG_VER} |
| 110 | + |
| 111 | +# Create the plrustc location for our distribution, then copy in the plrustc program into the correct spot |
| 112 | +RUN mkdir -p ${RELEASE_DIR}$HOME/.cargo/bin && \ |
| 113 | + cp $HOME/.cargo/bin/plrustc ${RELEASE_DIR}$HOME/.cargo/bin |
| 114 | + |
| 115 | +# The .deb file distribution needs to include the correct postgrestd toolchain files. |
| 116 | +RUN cd $HOME/.rustup && \ |
| 117 | + export TOOLCHAIN_DIR="$(pwd)/$(find * -type d -name '*postgres*' | head -n 1)" && \ |
| 118 | + mkdir -p "${RELEASE_DIR}$TOOLCHAIN_DIR" && \ |
| 119 | + cp -r "$TOOLCHAIN_DIR/." "${RELEASE_DIR}$TOOLCHAIN_DIR" |
| 120 | + |
| 121 | +# Creates a PGRX configuration Toml file, as it is required to build plrust user-defined functions |
| 122 | +RUN mkdir -p ${RELEASE_DIR}$HOME/.pgrx && \ |
| 123 | + echo '[configs]' >> ${RELEASE_DIR}$HOME/.pgrx/config.toml && \ |
| 124 | + echo "pg${PG_VER}='$(command -v pg_config)'" >> ${RELEASE_DIR}$HOME/.pgrx/config.toml |
| 125 | + |
| 126 | +# Create a Debian preinst script that will run before the installation of the pl/Rust package. |
| 127 | +# This will check to be sure the proper Rust toolchain and components are set up beforehand. |
| 128 | +RUN TOOLCHAIN_VER=$(</tmp/.toolchain-ver) && cat <<EOF > /tmp/preinst |
| 129 | +#!/usr/bin/env bash |
| 130 | + |
| 131 | +if ! id -u postgres 2&> /dev/null; then |
| 132 | + echo "[!] User 'postgres' does not exist. Have the official Postgres packages been installed yet?" |
| 133 | + exit 1 |
| 134 | +fi |
| 135 | + |
| 136 | +if ! runuser -l postgres -c 'command -v rustup'; then |
| 137 | + echo "[!] The 'postgres' user does not have rustup installed. Please see package installation instructions on how to do this."; |
| 138 | + exit 1 |
| 139 | +fi |
| 140 | + |
| 141 | +if ! runuser -l postgres -c 'command -v rustc'; then |
| 142 | + echo "[!] The 'postgres' user does not have Rust installed. Please see package installation instructions on how to do this."; |
| 143 | + exit 1 |
| 144 | +fi |
| 145 | + |
| 146 | +if ! runuser -l postgres -c 'rustup toolchain list | grep -q "^$TOOLCHAIN_VER-$(uname -m)\b"'; then |
| 147 | + echo "[!] The 'postgres' user does not have the Rust toolchain $TOOLCHAIN_VER-$(uname -m) installed. Please see package installation instructions on how to do this."; |
| 148 | + exit 1 |
| 149 | +fi |
| 150 | + |
| 151 | +if ! runuser -l postgres -c 'rustup component list | grep -q "^rustc-dev-$(uname -m)\b.*(installed)$"'; then |
| 152 | + echo "[!] The 'postgres' user does not have the Rust component 'rustc-dev' installed. Please see package installation instructions on how to do this."; |
| 153 | + exit 1 |
| 154 | +fi |
| 155 | + |
| 156 | +if ! runuser -l postgres -c 'rustup toolchain list | grep -q "^$TOOLCHAIN_VER-$(uname -m)\b.*(default)$"'; then |
| 157 | + echo "[!] The 'postgres' user does not have the default toolchain set to $TOOLCHAIN_VER. Please see package installation instructions on how to do this."; |
| 158 | + exit 1 |
| 159 | +fi |
| 160 | + |
| 161 | +EOF |
| 162 | + |
| 163 | +# Package everything up based on whatever's in RELEASE_DIR, and send the resulting |
| 164 | +# .deb file to the /out directory |
| 165 | +RUN TOOLCHAIN_VER=$(</tmp/.toolchain-ver) && \ |
| 166 | + DEB_FILENAME="plrust-trusted-${PLRUST_VER}_$TOOLCHAIN_VER-debian-pg${PG_VER}-$(dpkg --print-architecture).deb" && \ |
| 167 | + cd ${RELEASE_DIR} && fpm \ |
| 168 | + -s dir \ |
| 169 | + -t deb \ |
| 170 | + -n plrust \ |
| 171 | + --deb-pre-depends "postgresql-${PG_VER}" \ |
| 172 | + -d "postgresql-${PG_VER}" \ |
| 173 | + -d "postgresql-server-dev-${PG_VER}" \ |
| 174 | + -m 'Technology Concepts & Design, Inc. <support@tcdi.com>' \ |
| 175 | + --description 'PL/Rust is a loadable procedural language that enables writing PostgreSQL functions in the Rust programming language.' \ |
| 176 | + -v ${PLRUST_VER} \ |
| 177 | + --url 'https://github.com/tcdi/plrust' \ |
| 178 | + --license 'The PostgreSQL License' \ |
| 179 | + --category 'Databases' \ |
| 180 | + --deb-no-default-config-files \ |
| 181 | + --before-install /tmp/preinst \ |
| 182 | + -p /out/$DEB_FILENAME \ |
| 183 | + --deb-user postgres \ |
| 184 | + --deb-group postgres \ |
| 185 | + -a native \ |
| 186 | + . |
0 commit comments