From 2611659402c30c736f249f11f326fe127f94054e Mon Sep 17 00:00:00 2001 From: Michael Nahkies Date: Sun, 24 Nov 2024 11:49:51 +0000 Subject: [PATCH 1/2] feat!: load config file / auto-update - look for and `source` `.env.sh` if present - `git pull` to auto-update if clone is in a clean working state - adjust option names for consistency (*breaking change!*) --- .env.example.sh | 11 +++++++++++ .gitignore | 1 + README.md | 8 +++++--- bin/test-image.sh | 2 +- ensure_role_and_database_exists.c | 2 +- start-ephemeral-postgres.sh | 28 ++++++++++++++++++++++++---- 6 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 .env.example.sh diff --git a/.env.example.sh b/.env.example.sh new file mode 100644 index 0000000..3cae919 --- /dev/null +++ b/.env.example.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +export POSTGRES_VERSION="17" +export POSTGRES_USER="postgres" +export POSTGRES_PASSWORD="postgres" +export POSTGRES_HOST_AUTH_METHOD="trust" +export POSTGRES_ROLE_ATTRIBUTES="LOGIN CREATEDB" +export POSTGRES_EXTENSIONS="ltree postgis" + +export EPHEMERAL_POSTGRES_AUTO_UPDATE="1" +export EPHEMERAL_POSTGRES_FORCE_BUILD="0" diff --git a/.gitignore b/.gitignore index 485dee6..a3e8c5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +.env.sh diff --git a/README.md b/README.md index f90801a..b193da9 100644 --- a/README.md +++ b/README.md @@ -32,14 +32,16 @@ start-ephemeral-postgres.sh ``` **Options** - - `POSTGRES_VERSION=17` - `POSTGRES_USER=postgres` - `POSTGRES_PASSWORD=postgres` - `POSTGRES_HOST_AUTH_METHOD=trust` - could be `scram-sha-256` / `md5` / etc -- `ROLE_ATTRIBUTES='LOGIN CREATEDB'` - could be `SUPERUSER` / `CREATEROLE BYPASSRLS` / etc +- `POSTGRES_ROLE_ATTRIBUTES='LOGIN CREATEDB'` - could be `SUPERUSER` / `CREATEROLE BYPASSRLS` / etc - `POSTGRES_EXTENSIONS=` - could be `postgis ltree` / etc -- `FORCE_BUILD=0` - force building the docker image locally instead of pulling a prebuilt image +- `EPHEMERAL_POSTGRES_FORCE_BUILD=0` - force building the docker image locally instead of pulling a prebuilt image +- `EPHEMERAL_POSTGRES_AUTO_UPDATE=1` - whether to automatically check for updates to `ephemeral-postgres` + +You can also create a `.env.sh` file and this will be automatically loaded by `start-postgres.sh Connect using `psql`: diff --git a/bin/test-image.sh b/bin/test-image.sh index 2888852..2a7ebb2 100755 --- a/bin/test-image.sh +++ b/bin/test-image.sh @@ -15,7 +15,7 @@ fi export POSTGRES_VERSION="${1}" export POSTGRES_EXTENSIONS="ltree postgis" # reuse the image we just built -export FORCE_BUILD=1 +export EPHEMERAL_POSTGRES_FORCE_BUILD=1 ../start-ephemeral-postgres.sh diff --git a/ensure_role_and_database_exists.c b/ensure_role_and_database_exists.c index 4a670ea..3628207 100644 --- a/ensure_role_and_database_exists.c +++ b/ensure_role_and_database_exists.c @@ -23,7 +23,7 @@ static void ensure_role_and_database_exists(Port *port, int status) { return; } - role_attributes = getenv("ROLE_ATTRIBUTES"); + role_attributes = getenv("POSTGRES_ROLE_ATTRIBUTES"); fprintf(stderr, "ensuring user_name '%s' exists with attributes '%s'\n", port->user_name, role_attributes); asprintf(&cmd, diff --git a/start-ephemeral-postgres.sh b/start-ephemeral-postgres.sh index d6947b1..757ab8f 100755 --- a/start-ephemeral-postgres.sh +++ b/start-ephemeral-postgres.sh @@ -7,19 +7,39 @@ __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" pushd $__dir trap popd EXIT +: "${EPHEMERAL_POSTGRES_AUTO_UPDATE:=1}" : "${POSTGRES_VERSION:=17}" : "${POSTGRES_USER:=postgres}" : "${POSTGRES_PASSWORD:=postgres}" : "${POSTGRES_HOST_AUTH_METHOD:=trust}" -: "${ROLE_ATTRIBUTES:=LOGIN CREATEDB}" +: "${POSTGRES_ROLE_ATTRIBUTES:=LOGIN CREATEDB}" : "${POSTGRES_EXTENSIONS:=}" -: "${FORCE_BUILD:=0}" +: "${EPHEMERAL_POSTGRES_FORCE_BUILD:=0}" + +if [ -f .env.sh ]; then + echo "loading config from '.env.sh'" + source .env.sh +fi + +if [[ "${EPHEMERAL_POSTGRES_AUTO_UPDATE}" -eq 1 ]]; then + if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then + if [ -z "$(git status --porcelain)" ]; then + echo "Repository is clean. Pulling latest changes..." + git pull + else + echo "Repository has uncommitted changes. Skipping pull." + fi + else + echo "Current directory is not a Git repository." + fi +fi + IMAGE=mnahkies/ephemeral-postgres:$POSTGRES_VERSION docker stop postgres || echo 'already stopped' docker rm postgres || echo 'already removed' -if [[ "${FORCE_BUILD}" -ne 0 ]]; then +if [[ "${EPHEMERAL_POSTGRES_FORCE_BUILD}" -ne 0 ]]; then echo "Force build enabled. Skipping pull and building Docker image with POSTGRES_VERSION=$POSTGRES_VERSION" docker build --build-arg POSTGRES_VERSION="${POSTGRES_VERSION}" . -t "${IMAGE}" else @@ -40,7 +60,7 @@ docker run -d --rm --name postgres $MNT \ -e POSTGRES_USER="${POSTGRES_USER}" \ -e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \ -e POSTGRES_HOST_AUTH_METHOD="${POSTGRES_HOST_AUTH_METHOD}" \ - -e ROLE_ATTRIBUTES="${ROLE_ATTRIBUTES}" \ + -e POSTGRES_ROLE_ATTRIBUTES="${POSTGRES_ROLE_ATTRIBUTES}" \ -p 5432:5432 "${IMAGE}" \ -c shared_buffers=256MB \ -c 'shared_preload_libraries=$libdir/ensure_role_and_database_exists' From ce2aedb7b06cfb3bd7bc7555e306d1b1e43a05fb Mon Sep 17 00:00:00 2001 From: Michael Nahkies Date: Sun, 24 Nov 2024 11:55:21 +0000 Subject: [PATCH 2/2] fix: no auto-update in ci --- .github/workflows/cd.yaml | 5 +++++ start-ephemeral-postgres.sh | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index 36fc71a..34cf987 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -12,6 +12,10 @@ on: jobs: ci: runs-on: ubuntu-latest + + env: + EPHEMERAL_POSTGRES_AUTO_UPDATE: 0 + strategy: matrix: postgres-version: [ @@ -20,6 +24,7 @@ jobs: 16, 17 ] + steps: - uses: actions/checkout@v4 with: diff --git a/start-ephemeral-postgres.sh b/start-ephemeral-postgres.sh index 757ab8f..e36e9bd 100755 --- a/start-ephemeral-postgres.sh +++ b/start-ephemeral-postgres.sh @@ -7,13 +7,14 @@ __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" pushd $__dir trap popd EXIT -: "${EPHEMERAL_POSTGRES_AUTO_UPDATE:=1}" : "${POSTGRES_VERSION:=17}" : "${POSTGRES_USER:=postgres}" : "${POSTGRES_PASSWORD:=postgres}" : "${POSTGRES_HOST_AUTH_METHOD:=trust}" : "${POSTGRES_ROLE_ATTRIBUTES:=LOGIN CREATEDB}" : "${POSTGRES_EXTENSIONS:=}" + +: "${EPHEMERAL_POSTGRES_AUTO_UPDATE:=1}" : "${EPHEMERAL_POSTGRES_FORCE_BUILD:=0}" if [ -f .env.sh ]; then