From 046c28f4c43737316f6cc6b12856778dcbaf5320 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Fri, 13 Sep 2024 16:23:57 +0200 Subject: [PATCH] new: [postgres-stub,postgres,*/postgres_database] new charm --- .../hooks/postgres_database-relation-joined | 7 ++- .../hooks/postgres_database-relation-joined | 6 +- .../hooks/postgres_database-relation-joined | 9 ++- gitea/hooks/postgres_database-relation-joined | 11 ++-- .../hooks/postgres_database-relation-joined | 6 +- .../hooks/postgres_database-relation-joined | 4 +- .../hooks/postgres_database-relation-joined | 9 ++- .../hooks/postgres_database-relation-joined | 9 ++- .../hooks/postgres_database-relation-joined | 8 ++- .../hooks/postgres_database-relation-joined | 11 ++-- postgres-stub/README.org | 46 ++++++++++++++ .../hooks/postgres_database-relation-joined | 62 +++++++++++++++++++ postgres-stub/lib/common | 29 +++++++++ postgres-stub/metadata.yml | 7 +++ .../hooks/postgres_database-relation-joined | 4 ++ .../hooks/postgres_database-relation-joined | 6 +- .../hooks/postgres_database-relation-joined | 10 +-- 17 files changed, 212 insertions(+), 32 deletions(-) create mode 100644 postgres-stub/README.org create mode 100755 postgres-stub/hooks/postgres_database-relation-joined create mode 100644 postgres-stub/lib/common create mode 100644 postgres-stub/metadata.yml diff --git a/cyclos/hooks/postgres_database-relation-joined b/cyclos/hooks/postgres_database-relation-joined index f544170..169e9a3 100755 --- a/cyclos/hooks/postgres_database-relation-joined +++ b/cyclos/hooks/postgres_database-relation-joined @@ -5,15 +5,18 @@ set -e PASSWORD="$(relation-get password)" USER="$(relation-get user)" DBNAME="$(relation-get dbname)" +HOST="$(relation-get host)" +PORT="$(relation-get port)" config-add "\ services: $MASTER_BASE_SERVICE_NAME: environment: - DB_HOST: \"$MASTER_TARGET_SERVICE_NAME\" + DB_HOST: \"$HOST\" + DB_PORT: \"$PORT\" DB_NAME: \"$DBNAME\" DB_USER: \"$USER\" DB_PASSWORD: \"$PASSWORD\" " -info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." +info "Configured $SERVICE_NAME code for $HOST:$PORT access." diff --git a/docuseal/hooks/postgres_database-relation-joined b/docuseal/hooks/postgres_database-relation-joined index ca195f3..6f112b4 100755 --- a/docuseal/hooks/postgres_database-relation-joined +++ b/docuseal/hooks/postgres_database-relation-joined @@ -5,13 +5,15 @@ set -e PASSWORD="$(relation-get password)" USER="$(relation-get user)" DBNAME="$(relation-get dbname)" +HOST="$(relation-get host)" +PORT="$(relation-get port)" config-add "\ services: $MASTER_BASE_SERVICE_NAME: environment: - DATABASE_URL: postgres://$USER:$PASSWORD@$TARGET_SERVICE_NAME/$DBNAME + DATABASE_URL: postgres://$USER:$PASSWORD@$HOST:$PORT/$DBNAME " -info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." +info "Configured $SERVICE_NAME code for $HOST:$PORT access." diff --git a/etherpad/hooks/postgres_database-relation-joined b/etherpad/hooks/postgres_database-relation-joined index 9d725c9..409c4d9 100755 --- a/etherpad/hooks/postgres_database-relation-joined +++ b/etherpad/hooks/postgres_database-relation-joined @@ -5,15 +5,18 @@ set -e PASSWORD="$(relation-get password)" USER="$(relation-get user)" DBNAME="$(relation-get dbname)" +HOST="$(relation-get host)" +PORT="$(relation-get port)" -control=$(echo -en "$USER\0$DBNAME\0$PASSWORD\0$ADMIN_PASSWORD" | md5_compat) +control=$(p0 "$USER" "$DBNAME" "$PASSWORD" "$ADMIN_PASSWORD" "$HOST" | md5_compat) config-add "\ services: $MASTER_BASE_SERVICE_NAME: environment: DB_TYPE: postgres - DB_HOST: \"$MASTER_TARGET_SERVICE_NAME\" + DB_HOST: \"$HOST\" + DB_PORT: \"$PORT\" DB_NAME: \"$DBNAME\" DB_PASS: \"$PASSWORD\" DB_USER: \"$USER\" @@ -23,4 +26,4 @@ services: relation-set control "$control" -info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." +info "Configured $SERVICE_NAME code for $HOST:$PORT access." diff --git a/gitea/hooks/postgres_database-relation-joined b/gitea/hooks/postgres_database-relation-joined index 1f4ce2a..f39ad61 100755 --- a/gitea/hooks/postgres_database-relation-joined +++ b/gitea/hooks/postgres_database-relation-joined @@ -5,11 +5,13 @@ set -e PASSWORD="$(relation-get password)" DBUSER="$(relation-get user)" DBNAME="$(relation-get dbname)" +HOST="$(relation-get host)" +PORT="$(relation-get port)" . lib/common -control=$(printf "%s\0" "$DBUSER" "$DBNAME" "$PASSWORD" | md5_compat) +control=$(p0 "$DBUSER" "$DBNAME" "$PASSWORD" "$HOST" "$PORT" | md5_compat) ## Note: we don't use environment variable as it will be ignored if ## $CONFIG_FILE is found. So better modify directly the config file. @@ -18,7 +20,8 @@ control=$(printf "%s\0" "$DBUSER" "$DBNAME" "$PASSWORD" | md5_compat) # $MASTER_BASE_SERVICE_NAME: # environment: # - DB_TYPE=postgres -# - DB_HOST=$MASTER_TARGET_SERVICE_NAME +# - DB_HOST=$HOST +# - DB_PORT=$PORT # - DB_NAME=$DBNAME # - DB_USER=$USER # - DB_PASSWD=$PASSWORD @@ -34,7 +37,7 @@ control=$(printf "%s\0" "$DBUSER" "$DBNAME" "$PASSWORD" | md5_compat) ini merge </dev/null) || { if [ -e "$CONFIG" ]; then ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g') @@ -22,7 +24,7 @@ ADMIN_PASSWORD=$(relation-base-compose-get admin-password 2>/dev/null) || { fi } -control=$(echo -en "$USER\0$DBNAME\0$PASSWORD\0$ADMIN_PASSWORD" | md5_compat) +control=$(p0 "$USER" "$DBNAME" "$PASSWORD" "$ADMIN_PASSWORD" "$HOST" "$PORT" | md5_compat) database=$(options-get database 2>/dev/null) || true database="${database:-$DBNAME}" @@ -34,7 +36,8 @@ services: - '--database=$database' ## All this is to please tecnativa image, but is quite redundant environment: - PGHOST: \"$MASTER_TARGET_SERVICE_NAME\" + PGHOST: \"$HOST\" + PGPORT: \"$PORT\" PGDATABASE: \"$DBNAME\" PGPASSWORD: \"$PASSWORD\" PGUSER: \"$USER\" @@ -59,4 +62,4 @@ chown "$odoo_uid" "$CONFIG" && chmod 600 "$CONFIG" relation-set control "$control" -info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." +info "Configured $SERVICE_NAME code for $HOST:$PORT access." diff --git a/onlyoffice/hooks/postgres_database-relation-joined b/onlyoffice/hooks/postgres_database-relation-joined index 56a93ba..d2803c7 100755 --- a/onlyoffice/hooks/postgres_database-relation-joined +++ b/onlyoffice/hooks/postgres_database-relation-joined @@ -7,6 +7,9 @@ set -e PASSWORD="$(relation-get password)" USER="$(relation-get user)" DBNAME="$(relation-get dbname)" +HOST="$(relation-get host)" +PORT="$(relation-get port)" + ADMIN_PASSWORD=$(relation-base-compose-get admin-password 2>/dev/null) || { if [ -e "$CONFIG" ]; then ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g') @@ -25,10 +28,11 @@ services: $MASTER_BASE_SERVICE_NAME: environment: DB_TYPE: \"postgres\" - DB_HOST: \"$MASTER_TARGET_SERVICE_NAME\" + DB_HOST: \"$HOST\" + DB_PORT: \"$PORT\" DB_NAME: \"$DBNAME\" DB_PWD: \"$PASSWORD\" DB_USER: \"$USER\" " -info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." +info "Configured $SERVICE_NAME code for $HOST:$PORT access." diff --git a/peertube/hooks/postgres_database-relation-joined b/peertube/hooks/postgres_database-relation-joined index f106b3b..b1c9ba0 100755 --- a/peertube/hooks/postgres_database-relation-joined +++ b/peertube/hooks/postgres_database-relation-joined @@ -9,14 +9,16 @@ HOST_CONFIG_DIR=$SERVICE_CONFIGSTORE/$PEERTUBE_CONFIG_DIR PASSWORD="$(relation-get password)" USER="$(relation-get user)" DBNAME="$(relation-get dbname)" +HOST="$(relation-get host)" +PORT="$(relation-get port)" cat <> "$HOST_CONFIG_DIR/local.yaml" database: - hostname: '$TARGET_SERVICE_NAME' + hostname: '$HOST' ## We had to patch peertube to have a direct dbname (doh!) name: '$DBNAME' - port: 5432 + port: '$PORT' username: '$USER' password: '$PASSWORD' @@ -33,10 +35,11 @@ services: $MASTER_BASE_SERVICE_NAME: environment: PGUSER: $USER - PGHOST: $TARGET_SERVICE_NAME + PGHOST: $HOST + PGPORT: $PORT PGDATABASE: $DBNAME volumes: - $SERVICE_CONFIGSTORE/root/.pgpass:/var/lib/peertube/.pgpass " -info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." +info "Configured $SERVICE_NAME code for $HOST:$PORT access." diff --git a/postgres-stub/README.org b/postgres-stub/README.org new file mode 100644 index 0000000..88e8fd4 --- /dev/null +++ b/postgres-stub/README.org @@ -0,0 +1,46 @@ +# -*- ispell-local-dictionary: "english" -*- + +* Description + +=postgres-stub= allows you to declare an external =postgres= so that +your local services can connect to it. It assumes you have the host, +port of the external postgres access, and a login, password, and a +database (already created) to connect to. + +For now, this stub can't manage an admin access to create database, and +more. This would be an easy addition thou. + +This can be useful for instance if you have an external provider giving +you access to their postgres (use SSL then!), or in your private cloud, +or on the same host but not managed by docker, either directly on the host +or by other virtual services (LXC, VMWare...). + +* Usage + +You can declare a stub service by specifying at least the host of your +external =postgres= database. + +#+begin_src yaml +my-postgres-stub: + charm: postgres-stub + options: + host: my-host.mydomain.fr:20432 +#+end_src + +If you don't specify a port, then it'll be defaulted to ~5432~ + +Any service that want to connect to this database could then: + +#+begin_src yaml +myservice: + # ... + relations: + postgres-database: + postgres-stub: + my-postgres-stub: + dbname: my-db-name + user: my-username + password: my-p4s5w0rd +#+end_src + + diff --git a/postgres-stub/hooks/postgres_database-relation-joined b/postgres-stub/hooks/postgres_database-relation-joined new file mode 100755 index 0000000..dbbb96e --- /dev/null +++ b/postgres-stub/hooks/postgres_database-relation-joined @@ -0,0 +1,62 @@ +#!/bin/bash + +## When writing relation script, remember: +## - they should be idempotents +## - they can be launched while the dockers is already up +## - they are launched from the host +## - the target of the link is launched first, and get a chance to ``relation-set`` +## - both side of the scripts get to use ``relation-get``. + + +DBNAME=$(relation-get dbname) || { + DBNAME="$BASE_SERVICE_NAME" + relation-set dbname "$DBNAME" +} + +USER=$(relation-get user) || { + USER="$BASE_SERVICE_NAME" + relation-set user "$USER" +} + +HOST=$(options-get host) || true +if [ -z "$HOST" ]; then + err "No host specified. Please specify ${WHITE}host${NORMAL}" \ + "in ${DARKYELLOW}$SERVICE_NAME${NORMAL}'s option." + exit 1 +fi + +if [[ "$HOST" =~ :[0-9]+$ ]]; then + PORT="${HOST##*:}" + HOST="${HOST%:*}" +fi +if ! [[ "$HOST" =~ ^[0-9a-zA-Z.-]+$ ]]; then + err "Invalid host specified: '$HOST'. Please provide a valid" \ + "network target as ${WHITE}host${NORMAL} value" \ + "in ${DARKYELLOW}$SERVICE_NAME${NORMAL}'s option." + exit 1 +fi + +PORT=${PORT:-5432} + +relation-set host "$HOST" +relation-set port "$PORT" + +. lib/common + +set -e + +if ! PASSWORD="$(relation-get password 2>/dev/null)"; then + err "Please provide a password" + exit 1 +fi + +echo "SELECT 1;" | ddb "$DBNAME" || { + err "Connection failed to $DBNAME on $HOST:$PORT with user $USER and given password" + exit 1 +} + +array_read-0 extensions < <(relation-get extensions 2>/dev/null | shyaml get-values-0) + +if [ "${#extensions[@]}" -gt 0 ]; then + db_install_extensions "$DBNAME" "${extensions[@]}" || exit 1 +fi diff --git a/postgres-stub/lib/common b/postgres-stub/lib/common new file mode 100644 index 0000000..73c2af2 --- /dev/null +++ b/postgres-stub/lib/common @@ -0,0 +1,29 @@ +# -*- mode: shell-script -*- + +POSTGRES_IMAGE=docker.0k.io/postgres:12.15.0-myc + + +ddb () { + docker run --rm -i \ + -e PGPASSWORD="$PASSWORD" \ + -e PGHOST="$HOST" \ + -e PGUSER="$USER" \ + -e PGPORT="$PORT" \ + --entrypoint psql \ + "${POSTGRES_IMAGE}" \ + -qAt "$@" +} + + +## +## Entrypoints +## + +db_install_extensions() { + local dbname="$1" + shift + for ext in "$@"; do + ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS $ext;") || return 1 + info "Installed $ext extension on database '$dbname'." + done +} diff --git a/postgres-stub/metadata.yml b/postgres-stub/metadata.yml new file mode 100644 index 0000000..994f877 --- /dev/null +++ b/postgres-stub/metadata.yml @@ -0,0 +1,7 @@ +type: stub + +provides: + postgres-database: + +default-options: + host: ## Can contain a port specification (if not, port is 5432) diff --git a/postgres/hooks/postgres_database-relation-joined b/postgres/hooks/postgres_database-relation-joined index 9c23f8b..7380d67 100755 --- a/postgres/hooks/postgres_database-relation-joined +++ b/postgres/hooks/postgres_database-relation-joined @@ -18,6 +18,10 @@ USER=$(relation-get user) || { relation-set user "$USER" } + +relation-set host "$MASTER_TARGET_SERVICE_NAME" +relation-set port "5432" + . lib/common set -e diff --git a/rallly/hooks/postgres_database-relation-joined b/rallly/hooks/postgres_database-relation-joined index d46bff4..a526016 100755 --- a/rallly/hooks/postgres_database-relation-joined +++ b/rallly/hooks/postgres_database-relation-joined @@ -5,14 +5,16 @@ set -e PASSWORD="$(relation-get password)" USER="$(relation-get user)" DBNAME="$(relation-get dbname)" +HOST="$(relation-get host)" +PORT="$(relation-get port)" config-add "\ services: $MASTER_BASE_SERVICE_NAME: environment: - DATABASE_URL: \"postgres://$USER:$PASSWORD@$TARGET_SERVICE_NAME/$DBNAME\" + DATABASE_URL: \"postgres://$USER:$PASSWORD@$HOST:$PORT/$DBNAME\" " -info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." +info "Configured $SERVICE_NAME code for $HOST:$PORT access." diff --git a/synapse/hooks/postgres_database-relation-joined b/synapse/hooks/postgres_database-relation-joined index cba3782..d189ba5 100755 --- a/synapse/hooks/postgres_database-relation-joined +++ b/synapse/hooks/postgres_database-relation-joined @@ -7,8 +7,10 @@ set -e PASSWORD="$(relation-get password)" USER="$(relation-get user)" DBNAME="$(relation-get dbname)" +HOST="$(relation-get host)" +PORT="$(relation-get port)" -control=$(echo -en "$USER\0$DBNAME\0$PASSWORD" | md5_compat) +control=$(p0 "$USER" "$DBNAME" "$PASSWORD" "$HOST" "$PORT" | md5_compat) cfg-merge " database: @@ -17,12 +19,12 @@ database: user: $USER password: $PASSWORD database: $DBNAME - host: $MASTER_TARGET_SERVICE_NAME - port: 5432 + host: $HOST + port: $PORT cp_min: 5 cp_max: 10 " relation-set control "$control" -info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." +info "Configured $SERVICE_NAME code for $HOST:$PORT access."