Valentin Lab
3 months ago
17 changed files with 212 additions and 32 deletions
-
7cyclos/hooks/postgres_database-relation-joined
-
6docuseal/hooks/postgres_database-relation-joined
-
9etherpad/hooks/postgres_database-relation-joined
-
11gitea/hooks/postgres_database-relation-joined
-
6hedgedoc/hooks/postgres_database-relation-joined
-
4keycloak/hooks/postgres_database-relation-joined
-
9mattermost/hooks/postgres_database-relation-joined
-
9odoo-tecnativa/hooks/postgres_database-relation-joined
-
8onlyoffice/hooks/postgres_database-relation-joined
-
11peertube/hooks/postgres_database-relation-joined
-
46postgres-stub/README.org
-
62postgres-stub/hooks/postgres_database-relation-joined
-
29postgres-stub/lib/common
-
7postgres-stub/metadata.yml
-
4postgres/hooks/postgres_database-relation-joined
-
6rallly/hooks/postgres_database-relation-joined
-
10synapse/hooks/postgres_database-relation-joined
@ -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 |
|||
|
|||
|
@ -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 |
@ -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 |
|||
} |
@ -0,0 +1,7 @@ |
|||
type: stub |
|||
|
|||
provides: |
|||
postgres-database: |
|||
|
|||
default-options: |
|||
host: ## Can contain a port specification (if not, port is 5432) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue