Browse Source

new: [postgres-stub,postgres,*/postgres_database] new charm

upd
Valentin Lab 2 months ago
parent
commit
046c28f4c4
  1. 7
      cyclos/hooks/postgres_database-relation-joined
  2. 6
      docuseal/hooks/postgres_database-relation-joined
  3. 9
      etherpad/hooks/postgres_database-relation-joined
  4. 11
      gitea/hooks/postgres_database-relation-joined
  5. 6
      hedgedoc/hooks/postgres_database-relation-joined
  6. 4
      keycloak/hooks/postgres_database-relation-joined
  7. 9
      mattermost/hooks/postgres_database-relation-joined
  8. 9
      odoo-tecnativa/hooks/postgres_database-relation-joined
  9. 8
      onlyoffice/hooks/postgres_database-relation-joined
  10. 11
      peertube/hooks/postgres_database-relation-joined
  11. 46
      postgres-stub/README.org
  12. 62
      postgres-stub/hooks/postgres_database-relation-joined
  13. 29
      postgres-stub/lib/common
  14. 7
      postgres-stub/metadata.yml
  15. 4
      postgres/hooks/postgres_database-relation-joined
  16. 6
      rallly/hooks/postgres_database-relation-joined
  17. 10
      synapse/hooks/postgres_database-relation-joined

7
cyclos/hooks/postgres_database-relation-joined

@ -5,15 +5,18 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
config-add "\ config-add "\
services: services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: environment:
DB_HOST: \"$MASTER_TARGET_SERVICE_NAME\"
DB_HOST: \"$HOST\"
DB_PORT: \"$PORT\"
DB_NAME: \"$DBNAME\" DB_NAME: \"$DBNAME\"
DB_USER: \"$USER\" DB_USER: \"$USER\"
DB_PASSWORD: \"$PASSWORD\" DB_PASSWORD: \"$PASSWORD\"
" "
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."
info "Configured $SERVICE_NAME code for $HOST:$PORT access."

6
docuseal/hooks/postgres_database-relation-joined

@ -5,13 +5,15 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
config-add "\ config-add "\
services: services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: 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."

9
etherpad/hooks/postgres_database-relation-joined

@ -5,15 +5,18 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" 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 "\ config-add "\
services: services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: environment:
DB_TYPE: postgres DB_TYPE: postgres
DB_HOST: \"$MASTER_TARGET_SERVICE_NAME\"
DB_HOST: \"$HOST\"
DB_PORT: \"$PORT\"
DB_NAME: \"$DBNAME\" DB_NAME: \"$DBNAME\"
DB_PASS: \"$PASSWORD\" DB_PASS: \"$PASSWORD\"
DB_USER: \"$USER\" DB_USER: \"$USER\"
@ -23,4 +26,4 @@ services:
relation-set control "$control" relation-set control "$control"
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."
info "Configured $SERVICE_NAME code for $HOST:$PORT access."

11
gitea/hooks/postgres_database-relation-joined

@ -5,11 +5,13 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
DBUSER="$(relation-get user)" DBUSER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
. lib/common . 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 ## Note: we don't use environment variable as it will be ignored if
## $CONFIG_FILE is found. So better modify directly the config file. ## $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: # $MASTER_BASE_SERVICE_NAME:
# environment: # environment:
# - DB_TYPE=postgres # - DB_TYPE=postgres
# - DB_HOST=$MASTER_TARGET_SERVICE_NAME
# - DB_HOST=$HOST
# - DB_PORT=$PORT
# - DB_NAME=$DBNAME # - DB_NAME=$DBNAME
# - DB_USER=$USER # - DB_USER=$USER
# - DB_PASSWD=$PASSWORD # - DB_PASSWD=$PASSWORD
@ -34,7 +37,7 @@ control=$(printf "%s\0" "$DBUSER" "$DBNAME" "$PASSWORD" | md5_compat)
ini merge <<EOF ini merge <<EOF
[database] [database]
DB_TYPE = postgres DB_TYPE = postgres
HOST = $MASTER_TARGET_SERVICE_NAME
HOST = $HOST:$PORT
NAME = $DBNAME NAME = $DBNAME
USER = $DBUSER USER = $DBUSER
PASSWD = $PASSWORD PASSWD = $PASSWORD
@ -44,4 +47,4 @@ EOF
relation-set control "$control" relation-set control "$control"
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."
info "Configured $SERVICE_NAME code for $HOST:$PORT access."

6
hedgedoc/hooks/postgres_database-relation-joined

@ -5,13 +5,15 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
config-add "\ config-add "\
services: services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: environment:
CMD_DB_URL: postgres://$USER:$PASSWORD@$TARGET_SERVICE_NAME/$DBNAME
CMD_DB_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."

4
keycloak/hooks/postgres_database-relation-joined

@ -5,12 +5,14 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
config-add "\ config-add "\
services: services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: environment:
KC_DB_URL: \"jdbc:postgresql://$MASTER_TARGET_SERVICE_NAME:5432/$DBNAME\"
KC_DB_URL: \"jdbc:postgresql://$HOST:$PORT/$DBNAME\"
KC_DB_USERNAME: \"$USER\" KC_DB_USERNAME: \"$USER\"
KC_DB_PASSWORD: \"$PASSWORD\" KC_DB_PASSWORD: \"$PASSWORD\"
KC_DB: \"postgres\" KC_DB: \"postgres\"

9
mattermost/hooks/postgres_database-relation-joined

@ -5,14 +5,17 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" 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)
config-add "\ config-add "\
services: services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: environment:
DB_HOST: $MASTER_TARGET_SERVICE_NAME
DB_HOST: $HOST
DB_PORT_NUMBER: $PORT
MM_DBNAME: $DBNAME MM_DBNAME: $DBNAME
MM_PASSWORD: $PASSWORD MM_PASSWORD: $PASSWORD
MM_USERNAME: $USER MM_USERNAME: $USER
@ -28,4 +31,4 @@ fi
relation-set control "$control" relation-set control "$control"
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."
info "Configured $SERVICE_NAME code for $HOST:$PORT access."

9
odoo-tecnativa/hooks/postgres_database-relation-joined

@ -12,6 +12,8 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
ADMIN_PASSWORD=$(relation-base-compose-get admin-password 2>/dev/null) || { ADMIN_PASSWORD=$(relation-base-compose-get admin-password 2>/dev/null) || {
if [ -e "$CONFIG" ]; then if [ -e "$CONFIG" ]; then
ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g') 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 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=$(options-get database 2>/dev/null) || true
database="${database:-$DBNAME}" database="${database:-$DBNAME}"
@ -34,7 +36,8 @@ services:
- '--database=$database' - '--database=$database'
## All this is to please tecnativa image, but is quite redundant ## All this is to please tecnativa image, but is quite redundant
environment: environment:
PGHOST: \"$MASTER_TARGET_SERVICE_NAME\"
PGHOST: \"$HOST\"
PGPORT: \"$PORT\"
PGDATABASE: \"$DBNAME\" PGDATABASE: \"$DBNAME\"
PGPASSWORD: \"$PASSWORD\" PGPASSWORD: \"$PASSWORD\"
PGUSER: \"$USER\" PGUSER: \"$USER\"
@ -59,4 +62,4 @@ chown "$odoo_uid" "$CONFIG" && chmod 600 "$CONFIG"
relation-set control "$control" relation-set control "$control"
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."
info "Configured $SERVICE_NAME code for $HOST:$PORT access."

8
onlyoffice/hooks/postgres_database-relation-joined

@ -7,6 +7,9 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
ADMIN_PASSWORD=$(relation-base-compose-get admin-password 2>/dev/null) || { ADMIN_PASSWORD=$(relation-base-compose-get admin-password 2>/dev/null) || {
if [ -e "$CONFIG" ]; then if [ -e "$CONFIG" ]; then
ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g') ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
@ -25,10 +28,11 @@ services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: environment:
DB_TYPE: \"postgres\" DB_TYPE: \"postgres\"
DB_HOST: \"$MASTER_TARGET_SERVICE_NAME\"
DB_HOST: \"$HOST\"
DB_PORT: \"$PORT\"
DB_NAME: \"$DBNAME\" DB_NAME: \"$DBNAME\"
DB_PWD: \"$PASSWORD\" DB_PWD: \"$PASSWORD\"
DB_USER: \"$USER\" DB_USER: \"$USER\"
" "
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."
info "Configured $SERVICE_NAME code for $HOST:$PORT access."

11
peertube/hooks/postgres_database-relation-joined

@ -9,14 +9,16 @@ HOST_CONFIG_DIR=$SERVICE_CONFIGSTORE/$PEERTUBE_CONFIG_DIR
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
cat <<EOF >> "$HOST_CONFIG_DIR/local.yaml" cat <<EOF >> "$HOST_CONFIG_DIR/local.yaml"
database: database:
hostname: '$TARGET_SERVICE_NAME'
hostname: '$HOST'
## We had to patch peertube to have a direct dbname (doh!) ## We had to patch peertube to have a direct dbname (doh!)
name: '$DBNAME' name: '$DBNAME'
port: 5432
port: '$PORT'
username: '$USER' username: '$USER'
password: '$PASSWORD' password: '$PASSWORD'
@ -33,10 +35,11 @@ services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: environment:
PGUSER: $USER PGUSER: $USER
PGHOST: $TARGET_SERVICE_NAME
PGHOST: $HOST
PGPORT: $PORT
PGDATABASE: $DBNAME PGDATABASE: $DBNAME
volumes: volumes:
- $SERVICE_CONFIGSTORE/root/.pgpass:/var/lib/peertube/.pgpass - $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."

46
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

62
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

29
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
}

7
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)

4
postgres/hooks/postgres_database-relation-joined

@ -18,6 +18,10 @@ USER=$(relation-get user) || {
relation-set user "$USER" relation-set user "$USER"
} }
relation-set host "$MASTER_TARGET_SERVICE_NAME"
relation-set port "5432"
. lib/common . lib/common
set -e set -e

6
rallly/hooks/postgres_database-relation-joined

@ -5,14 +5,16 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" DBNAME="$(relation-get dbname)"
HOST="$(relation-get host)"
PORT="$(relation-get port)"
config-add "\ config-add "\
services: services:
$MASTER_BASE_SERVICE_NAME: $MASTER_BASE_SERVICE_NAME:
environment: 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."

10
synapse/hooks/postgres_database-relation-joined

@ -7,8 +7,10 @@ set -e
PASSWORD="$(relation-get password)" PASSWORD="$(relation-get password)"
USER="$(relation-get user)" USER="$(relation-get user)"
DBNAME="$(relation-get dbname)" 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 " cfg-merge "
database: database:
@ -17,12 +19,12 @@ database:
user: $USER user: $USER
password: $PASSWORD password: $PASSWORD
database: $DBNAME database: $DBNAME
host: $MASTER_TARGET_SERVICE_NAME
port: 5432
host: $HOST
port: $PORT
cp_min: 5 cp_min: 5
cp_max: 10 cp_max: 10
" "
relation-set control "$control" relation-set control "$control"
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."
info "Configured $SERVICE_NAME code for $HOST:$PORT access."
Loading…
Cancel
Save