From 517accd8824cf07d36cd23d6abdbca5995e61c31 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Wed, 17 Jan 2024 15:55:25 +0100 Subject: [PATCH] new: [nextcloud] remove the need to call ``occ`` in ``web-proxy`` relation --- nextcloud/hooks/web_proxy-relation-joined | 25 +++++-------- nextcloud/lib/common | 45 ++++++++++++++++++++++- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/nextcloud/hooks/web_proxy-relation-joined b/nextcloud/hooks/web_proxy-relation-joined index c612bb1..b8faa48 100755 --- a/nextcloud/hooks/web_proxy-relation-joined +++ b/nextcloud/hooks/web_proxy-relation-joined @@ -1,25 +1,20 @@ #!/bin/bash +. lib/common + set -e DOMAIN=$(relation-get domain) || exit 1 URL="$(relation-get url)" || exit 1 PROTO="${URL%%://*}" -occ_opts=( - ## necessary as nextcloud do not detect correctly those, and behind - ## a proxy, it will generate a lot of URL that are not detected - ## by means of ``ReverseProxyPass`` on apache for instance +nextcloud:config:simple:add overwritehost "$DOMAIN" || { + err "Failed to set ${WHITE}overwritehost${NORMAL} to '$DOMAIN'." + exit 1 +} - config:system:set overwritehost --value="$DOMAIN" \; - config:system:set overwriteprotocol --value="$PROTO" -) -compose --no-relations --no-init occ "$MASTER_BASE_SERVICE_NAME" "${occ_opts[@]}" || { - err "Failure to execute these ${WHITE}occ${NORMAL} commands:" - echo " ${WHITE}$(printf ' %q' "${occ_opts[@]}")${NORMAL}" | - sed -r "s/\\;/$'n'/g" | prefix " ${DARKGRAY}>${NORMAL} " >&2 - echo " " - echo " If the code of nextcloud is already there (command occ is found), but " >&2 - echo " the database is not yet created, this situation will arise." >&2 +nextcloud:config:simple:add overwriteprotocol "$PROTO" || { + err "Failed to set ${WHITE}overwriteprotocol${NORMAL} to '$PROTO'." exit 1 -} \ No newline at end of file +} + diff --git a/nextcloud/lib/common b/nextcloud/lib/common index 6b10366..a923608 100644 --- a/nextcloud/lib/common +++ b/nextcloud/lib/common @@ -124,7 +124,50 @@ occ() { -v "$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/src/occ.batch:/var/www/html/occ.batch" \ -T --rm -u www-data "$SERVICE_NAME" /var/www/html/occ.batch "$@" | cat - return "${PIPESTATUS[0]}" + if [ "${PIPESTATUS[0]}" != 0 ]; then + err "Failure to execute these ${WHITE}occ${NORMAL} commands:" + printf '%s ' "$@" | + sed -r "s/\\;/\n/g" | + sed -r "s/^\s*(.*)\s*$/${WHITE}\1${NORMAL}/g" | + prefix " ${DARKGRAY}>${NORMAL} " >&2 + echo "" >&2 + echo "" >&2 + echo " If the code of nextcloud is already there (command occ is found), but " >&2 + echo " the database is not yet created, this situation will arise." >&2 + return "${PIPESTATUS[0]}" + fi + } + +nextcloud:config:simple:add() { + local key="$1" value="$2" + create_occ_if_not_exists || return 1 + + if ![ -e "$CONFIGFILE" ]; then + err "Config file '$CONFIGFILE' does not exist." + return 1 + fi + + if [ -z "$value" ]; then + err "Value for '$key' is empty. Skipping." + return 1 + fi + ## check for \ and ' in value and key + if [[ "$value" =~ [\\\'] ]]; then + err "Unsupported value for '$key' contains a backslash or a single quote." + return 1 + fi + if [[ "$key" =~ [\\\'] ]]; then + err "Key '$key' contains a backslash or a single quote." + return 1 + fi + + if grep "^ '$key' => '" "$CONFIGFILE" >/dev/null; then + sed -ri "s/^( '$key' => ')(.*)(',)$/\1${value}\3/g" "$CONFIGFILE" + return 0 + fi + ## Add '$key' => 'value', to the end of the file, before the closing paren. + sed -ri "s/^(\);)$/ '$key' => '${value}',\n\1/g" "$CONFIGFILE" +} \ No newline at end of file