Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: addon-postgres tests
on:
pull_request:
push:
branches:
- master
workflow_dispatch:

permissions:
contents: read

jobs:
test:
name: ${{ matrix.name }}
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
include:
- name: provision
dockerfile: tests/Dockerfile
script: provision.sh
- name: package
dockerfile: tests/package.Dockerfile
script: package.sh
steps:
- uses: actions/checkout@v7.0.1
with:
persist-credentials: false
- name: Build test image
env:
DOCKERFILE: ${{ matrix.dockerfile }}
IMAGE: addon-postgres-${{ matrix.name }}
run: docker build -f "$DOCKERFILE" -t "$IMAGE" tests
- name: Run test
env:
IMAGE: addon-postgres-${{ matrix.name }}
SCRIPT: ${{ matrix.script }}
run: docker run --rm -v "$PWD:/source:ro" "$IMAGE" "/source/tests/$SCRIPT"
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

PostgreSQL addon for pkgr.

If autoinstall mode is selected, it will attempt to setup a PostgreSQL v10 cluster, using the packages from https://www.postgresql.org/download/linux/.
New autoinstallations provision PostgreSQL 18 with pgvector available using packages from https://www.postgresql.org/download/linux/.

Supported distributions:
Existing configured installations retain their PostgreSQL major, are checked for connectivity, and attempt to install the matching pgvector package. Reused external databases are not modified; install pgvector on those servers yourself. OpenProject handles extension creation during application migrations.

* SLES12
* Enterprise Linux 7
* Ubuntu 14.04, 16.04, 18.04
* Debian 7, 8, 9
Automatic installation is supported for OpenProject's Debian 11/12, Ubuntu 22.04, and Enterprise Linux 9 package targets. SLES requires a separately managed PostgreSQL installation.

## Development

Expand All @@ -23,3 +20,7 @@ Supported distributions:

sudo -E INSTALLER_DEBUG=no WIZ_RECONFIGURE=no DATABASE=/tmp/test1 ./bin/configure
sudo -E INSTALLER_DEBUG=no WIZ_RECONFIGURE=no DATABASE=/tmp/test1 ./bin/preinstall

## Tests

See [the test instructions](tests/README.md) for provisioning and Debian package installation checks.
6 changes: 3 additions & 3 deletions bin/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

. "${INSTALLER_DIR}/wizard"

OUTPUT="/etc/${APP_NAME}/conf.d/00_addon_postgres"
CONF_FILE="/etc/${APP_NAME}/conf.d/00_addon_postgres"

function generate_database_template() {
PGPASSWORD="$(wiz_get "postgres/db_password")"
Expand All @@ -15,8 +15,8 @@ function generate_database_template() {

local encoded_password="$(wiz_urlencode "$PGPASSWORD")"

echo "export DATABASE_URL=\"postgres://${PGUSER}:${encoded_password}@${PGHOST}:${PGPORT}/${PGDATABASE}\"" > "$OUTPUT"
chown "${APP_USER}.${APP_GROUP}" "$OUTPUT"
echo "export DATABASE_URL=\"postgres://${PGUSER}:${encoded_password}@${PGHOST}:${PGPORT}/${PGDATABASE}\"" > "$CONF_FILE"
chown "${APP_USER}.${APP_GROUP}" "$CONF_FILE"
}

case "$(wiz_get "postgres/autoinstall")" in
Expand Down
164 changes: 86 additions & 78 deletions bin/preinstall
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ set -e

ADDON_VERSION="v1"

MSG_CONFLICTING_INSTALL="ERROR: It seems like you already have PostgreSQL v10 installed. Please run '$APP_NAME reconfigure' and choose to use the existing installation instead."
MSG_CONFLICTING_INSTALL="ERROR: It seems like you already have PostgreSQL installed. Please run '$APP_NAME reconfigure' and choose to use the existing installation instead."
MSG_UNKNOWN_DISTRIBUTION_VERSION="ERROR: The current distribution is not supported by this wizard. Please install PostgreSQL manually and retry."
MSG_CONNECTION_KO="ERROR: Connection KO. Aborting postgres installation. You should check the wizard configuration in /etc/$APP_NAME/installer.dat"
MSG_PGVECTOR_FAILED="WARNING: Could not install pgvector. Features that depend on it stay disabled. See https://www.openproject.org/docs/installation-and-operations/system-requirements/#pgvector"

PGHOST="$(wiz_get "postgres/db_host" || echo "127.0.0.1")"
PGPORT="$(wiz_get "postgres/db_port" || echo "5432")"
PGDATABASE="$(wiz_get "postgres/db_name" || echo "$APP_SAFE_NAME")"
PGUSER="$(wiz_get "postgres/db_username" || echo "$APP_SAFE_NAME")"
PGPASSWORD="$(wiz_get "postgres/db_password" || echo "")"

CONF_FILE="/etc/${APP_NAME}/conf.d/00_addon_postgres"

install_postgres_server() {
case $(wiz_fact "osfamily") in
"debian")
Expand All @@ -34,7 +37,10 @@ install_postgres_server() {
;;
"11")
codename="bullseye"
;;
;;
"12")
codename="bookworm"
;;
"14.04")
codename="trusty"
;;
Expand All @@ -47,26 +53,28 @@ install_postgres_server() {
"20.04")
codename="focal"
;;
"22.04")
codename="jammy"
;;
esac

if [ "$codename" != "" ]; then
if ! wiz_check_package "postgresql-10" ; then
_ensure_port_available
echo "deb https://apt.postgresql.org/pub/repos/apt/ ${codename}-pgdg main" > /etc/apt/sources.list.d/pgdg.list
echo -n "INFO: Adding repository signing key from PostgreSQL... "
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
echo "INFO: Installing PostgreSQL v10..."
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install postgresql-10 -y

su - postgres -c "/usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -w -t 10 stop || true"
_configure_postgres_server "/etc/postgresql/10/main"
sleep 3
su - postgres -c "/usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -w -t 10 -l logfile restart"
else
_error_if_conflicting_install
fi
_configure_database_and_user
if [ "$codename" != "" ]; then
if ! wiz_check_package "postgresql-18" ; then
_ensure_port_available
echo "deb https://apt.postgresql.org/pub/repos/apt/ ${codename}-pgdg main" > /etc/apt/sources.list.d/pgdg.list
echo -n "INFO: Adding repository signing key from PostgreSQL... "
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
echo "INFO: Installing PostgreSQL v18..."
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install postgresql-18 postgresql-18-pgvector -y

pg_ctlcluster 18 main stop || true
_configure_postgres_server "/etc/postgresql/18/main"
pg_ctlcluster 18 main start
else
_error_if_conflicting_install
fi
_configure_database_and_user
else
echo "$MSG_UNKNOWN_DISTRIBUTION_VERSION"
exit 1
Expand All @@ -75,69 +83,41 @@ install_postgres_server() {
"redhat")
case $(wiz_fact "osversion") in
7*)
if ! wiz_check_package "postgresql10-server" ; then
_ensure_port_available
echo "INFO: Installing PostgreSQL v10..."
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql10-server postgresql10
PGSETUP_INITDB_OPTIONS="--auth-local peer --auth-host md5" /usr/pgsql-10/bin/postgresql-10-setup initdb

_configure_postgres_server "/var/lib/pgsql/10/data"
systemctl enable postgresql-10
systemctl restart postgresql-10
else
_error_if_conflicting_install
fi
_configure_database_and_user
repo="https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
;;
8*)
if ! wiz_check_package "postgresql10-server" ; then
_ensure_port_available
set -x
echo "INFO: Installing PostgreSQL v10..."
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
dnf -qy module disable postgresql
dnf install -y postgresql10 postgresql10-server
PGSETUP_INITDB_OPTIONS="--auth-local peer --auth-host md5" /usr/pgsql-10/bin/postgresql-10-setup initdb

_configure_postgres_server "/var/lib/pgsql/10/data"
systemctl enable postgresql-10
systemctl restart postgresql-10
else
_error_if_conflicting_install
fi
_configure_database_and_user
repo="https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
dnf -qy module disable postgresql
;;
9*)
repo="http://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
dnf -qy module disable postgresql
;;
*)
echo "$MSG_UNKNOWN_DISTRIBUTION_VERSION"
exit 1
;;
esac

if ! wiz_check_package "postgresql18-server" ; then
_ensure_port_available
echo "INFO: Installing PostgreSQL v18..."
yum install -y "$repo"
yum install -y postgresql18-server postgresql18 postgresql18-contrib pgvector_18
PGSETUP_INITDB_OPTIONS="--auth-local peer --auth-host md5" /usr/pgsql-18/bin/postgresql-18-setup initdb

_configure_postgres_server "/var/lib/pgsql/18/data"
wiz_service_enable postgresql-18
wiz_service_restart postgresql-18
else
_error_if_conflicting_install
fi
_configure_database_and_user

;;
"suse")
case $(wiz_fact "osversion") in
12)
if ! wiz_check_package "postgresql10-server" ; then
_ensure_port_available
rpm --import https://download.postgresql.org/pub/repos/zypp/10/suse/sles-12-x86_64/repodata/repomd.xml.key
echo "INFO: Installing PostgreSQL v10..."
zypper addrepo https://download.postgresql.org/pub/repos/zypp/repo/pgdg-sles-10.repo
zypper install -y postgresql10-server

PGSETUP_INITDB_OPTIONS="--auth-local peer --auth-host md5" /usr/pgsql-10/bin/postgresql-10-setup initdb
_configure_postgres_server "/var/lib/pgsql/10/data"
systemctl enable postgresql-10
systemctl restart postgresql-10
else
_error_if_conflicting_install
fi
_configure_database_and_user
;;
*)
echo "$MSG_UNKNOWN_DISTRIBUTION_VERSION"
exit 1
;;
esac
echo "$MSG_UNKNOWN_DISTRIBUTION_VERSION"
exit 1
;;
esac
}
Expand Down Expand Up @@ -191,14 +171,36 @@ function _configure_postgres_server() {
chown -R postgres:postgres $base_dir/conf.d
}

function _install_pgvector() {
local major
major="$(su - postgres -c "psql -p $PGPORT --no-readline -tAc \"SELECT current_setting('server_version_num')::int / 10000\"")" || { echo "$MSG_PGVECTOR_FAILED"; return 0; }

echo "INFO: Installing pgvector for PostgreSQL v${major}..."
case "$(wiz_fact "osfamily")" in
"debian")
wiz_check_package "postgresql-${major}-pgvector" || DEBIAN_FRONTEND=noninteractive apt-get install -y "postgresql-${major}-pgvector"
;;
"redhat")
wiz_check_package "pgvector_${major}" || yum install -y "pgvector_${major}"
;;
esac || echo "$MSG_PGVECTOR_FAILED"
}

function _configure_database_and_user() {
if su - postgres -c "createuser -p $PGPORT --createdb --login --superuser $PGUSER 2>&1 || true" | grep -q "already exists" ; then
echo "INFO: user $PGUSER already exists"
fi
su - postgres -c "psql -p $PGPORT --no-readline --no-password -c \"alter user $PGUSER with encrypted password '$PGPASSWORD';\""

if su - postgres -c "createdb -p $PGPORT -O $PGUSER $PGDATABASE 2>&1 || true" | grep -q "already exists" ; then
local database_exists
database_exists=$(runuser -u postgres -- psql -p "$PGPORT" -d postgres -At -v ON_ERROR_STOP=1 -v database="$PGDATABASE" <<'SQL'
SELECT 1 FROM pg_database WHERE datname = :'database';
SQL
)
if [ "$database_exists" = 1 ]; then
echo "INFO: database $PGDATABASE already exists"
else
runuser -u postgres -- createdb -p "$PGPORT" -O "$PGUSER" -- "$PGDATABASE"
fi
}

Expand All @@ -207,10 +209,16 @@ case "$(wiz_get "postgres/autoinstall")" in
echo "INFO: autoinstall=skip. Skipping to next step."
;;
"install")
echo "INFO: autoinstall=install. A new PostgreSQL v10 server will be installed locally unless a conflicting installation is found."
install_postgres_server
wiz_set "postgres/addon_version" "$ADDON_VERSION"
_ensure_connection_ok
echo "INFO: autoinstall=install. A new PostgreSQL v18 server will be installed locally unless a conflicting installation is found."
if [ -f "$CONF_FILE" ]; then
echo "INFO: existing installation found. Skipping to next step."
_ensure_connection_ok
else
install_postgres_server
wiz_set "postgres/addon_version" "$ADDON_VERSION"
_ensure_connection_ok
fi
_install_pgvector
;;
"reuse")
echo "INFO: autoinstall=reuse. Skipping to next step."
Expand Down
2 changes: 1 addition & 1 deletion templates
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Default: reuse
Description: Do you want to use this wizard to help setup your PostgreSQL database?
If you choose to SKIP this wizard, you will have to then manually configure the database used by the _APP_NAME_.
.
If you choose to INSTALL, a new PostgreSQL v10 server will be downloaded and installed on your server, and the database will be automatically created.
If you choose to INSTALL, a new PostgreSQL v18 server with pgvector will be downloaded and installed on your server, and the database will be automatically created. OpenProject handles extension creation during application migrations.
Make sure this does not conflict with any existing PostgreSQL installation.
If you choose to USE an existing installation, you must create the database beforehand and provide the credentials in the next page.

Expand Down
34 changes: 34 additions & 0 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# See COPYRIGHT and LICENSE files for more details.
#++

FROM debian:bookworm
RUN printf '#!/bin/sh\nexit 101\n' > /usr/sbin/policy-rc.d && chmod +x /usr/sbin/policy-rc.d \
&& apt-get update -qq && apt-get install -y --no-install-recommends wget gnupg ca-certificates procps postgresql-common \
&& echo 'create_main_cluster = false' >> /etc/postgresql-common/createcluster.conf \
&& /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y \
&& apt-get install -y --no-install-recommends postgresql-17 postgresql-17-pgvector postgresql-18
ENTRYPOINT ["bash"]
17 changes: 17 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# PostgreSQL addon tests

Run from the repository root with Docker available:

```shell
docker build -t addon-postgres-test tests
docker run --rm -v "$PWD:/source:ro" addon-postgres-test /source/tests/provision.sh

docker build -f tests/package.Dockerfile -t addon-postgres-package-test tests
docker run --rm -v "$PWD:/source:ro" addon-postgres-package-test /source/tests/package.sh
```

`provision.sh` uses an image with PostgreSQL 17, its pgvector package, and PostgreSQL 18 (without pgvector) from PGDG. It checks that reconfiguring an existing PostgreSQL 17 database retains its major version and leaves vector inactive. It then provisions an addon-managed PostgreSQL 18 server and checks that the addon installs `postgresql-18-pgvector` from PGDG and creates the database without activating vector.

`package.sh` installs PostgreSQL 18 and pgvector on clean Debian 12 using the addon, then checks connectivity on the configured port, the server major version, the installed pgvector package, and vector availability without activation.

Both tests run real database and package commands against Debian and PGDG and substitute only the wizard functions. They fail if the addon prints a `WARNING:` or `ERROR:` line. They test the addon, not installation of a complete OpenProject package, and run in CI via [`test.yml`](../.github/workflows/test.yml). Enterprise Linux is not covered.
Loading