From 019a6d6dccfcf9011fdd31fab90c11bf68f6f53d Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Wed, 10 Jan 2024 20:06:38 +0100 Subject: [PATCH] new: [nextcloud] add compatibility with mysql database --- .../hooks/mysql_database-relation-joined | 51 +++++++++++++++++++ nextcloud/metadata.yml | 7 +++ 2 files changed, 58 insertions(+) create mode 100755 nextcloud/hooks/mysql_database-relation-joined diff --git a/nextcloud/hooks/mysql_database-relation-joined b/nextcloud/hooks/mysql_database-relation-joined new file mode 100755 index 0000000..792d8f4 --- /dev/null +++ b/nextcloud/hooks/mysql_database-relation-joined @@ -0,0 +1,51 @@ +#!/bin/bash + +. lib/common + +set -e + +PASSWORD="$(relation-get password)" +USER="$(relation-get user)" +DBNAME="$(relation-get dbname)" + + +## This check adds purely arbitrary limits to what could be a password +## if we need to open that more, just consider the next script where we'll +## need to write in a PHP structure, or in YAML structure. + +## Note that here, "[]" chars are not accepted just because it doesn't seem evident +## to test for those in bash. +if ! [[ "$PASSWORD" =~ ^[a-zA-Z0-9~\`\&+=@\#^\*/\\_%\$:\;\!?.,\<\>{}()\"\'|-]*$ ]]; then + err "Invalid password chosen for mysql database." + exit 1 +fi + +## if config is not existent +if [ -e "$CONFIGFILE" ] && grep "^ 'dbuser' => '" "$CONFIGFILE" >/dev/null; then + + ## 'occ' can't be used as it will try to connect to mysql before running and + ## will fail if user/password is not correct + + ## We need to get through bash, and sed interpretation, then PHP single quoted strings. + quoted_user="${USER//\\/\\\\\\\\\\}" + quoted_user="${quoted_user//\'/\\\\\'}" + quoted_password="${PASSWORD//\\/\\\\\\\\\\}" + quoted_password="${quoted_password//\'/\\\\\'}" + sed -ri "s/^( 'dbuser' => ')(.*)(',)$/\1${quoted_user}\3/g;\ + s/^( 'dbpassword' => ')(.*)(',)$/\1${quoted_password}\3/g;" "$CONFIGFILE" +else + + ## These variable are not used by current docker image after first install + + config-add "\ +services: + $MASTER_BASE_SERVICE_NAME: + environment: + MYSQL_HOST: $MASTER_TARGET_SERVICE_NAME + MYSQL_DATABASE: $DBNAME + MYSQL_PASSWORD: $PASSWORD + MYSQL_USER: $USER +" +fi + +info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." diff --git a/nextcloud/metadata.yml b/nextcloud/metadata.yml index f338f54..475433e 100644 --- a/nextcloud/metadata.yml +++ b/nextcloud/metadata.yml @@ -14,6 +14,13 @@ uses: auto: summon solves: database: "main storage" + mysql-database: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + constraint: optional + auto: pair + solves: + database: "main storage" web-proxy: #constraint: required | recommended | optional #auto: pair | summon | none ## default: pair