From ce9a5e912cea49b06a8fa898ae83ef38ce4a562c Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Thu, 12 Sep 2019 17:55:18 +0200 Subject: [PATCH] new: [nextcloud] new charm --- nextcloud/actions/occ | 32 ++++++++++++++ nextcloud/hooks/init | 44 +++++++++++++++++++ .../hooks/postgres_database-relation-joined | 37 ++++++++++++++++ nextcloud/hooks/web_proxy-relation-joined | 26 +++++++++++ nextcloud/lib/common | 3 ++ nextcloud/metadata.yml | 26 +++++++++++ nextcloud/src/occ.batch | 18 ++++++++ 7 files changed, 186 insertions(+) create mode 100755 nextcloud/actions/occ create mode 100755 nextcloud/hooks/init create mode 100755 nextcloud/hooks/postgres_database-relation-joined create mode 100755 nextcloud/hooks/web_proxy-relation-joined create mode 100644 nextcloud/lib/common create mode 100644 nextcloud/metadata.yml create mode 100755 nextcloud/src/occ.batch diff --git a/nextcloud/actions/occ b/nextcloud/actions/occ new file mode 100755 index 0000000..979cd9b --- /dev/null +++ b/nextcloud/actions/occ @@ -0,0 +1,32 @@ +#!/bin/bash + +if [ -z "$SERVICE_DATASTORE" ]; then + echo "This script is meant to be run through 'compose' to work properly." >&2 + exit 1 +fi + + +export COMPOSE_IGNORE_ORPHANS=True + + +if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then + ## Here we use a nasty trick to launch only the initialisation + ## part of the ``entrypoint.sh``. By setting 'apache' as first + ## call argument, we satisfy the big first 'if' condition + ## triggering the installation if necessary, and will fail to + ## launch any apache + + ## Last, we do not want the relation web-proxy to run in this + ## bare-minimum nextcloud run AND we will use occ to set some info + ## in this very same relation. + compose --without-relation="$SERVICE_NAME":web-proxy run \ + --rm --entrypoint /entrypoint.sh "$SERVICE_NAME" apache >&2 || true +fi + + +## occ.batch will require /var/www/html to be populated ('occ' is +## supposed to exist). For that we need to make sure nextcloud have +## be ran and setup prior to running this next command. +compose -q --no-init --no-relations run \ + -v "$CHARM_PATH/src/occ.batch:/var/www/html/occ.batch" \ + -T --rm -u www-data "$SERVICE_NAME" /var/www/html/occ.batch "$@" | cat diff --git a/nextcloud/hooks/init b/nextcloud/hooks/init new file mode 100755 index 0000000..84a4487 --- /dev/null +++ b/nextcloud/hooks/init @@ -0,0 +1,44 @@ +#!/bin/bash + +## Init is run on host +## For now it is run every time the script is launched, but +## it should be launched only once after build. + +## Accessible variables are: +## - SERVICE_NAME Name of current service +## - DOCKER_BASE_IMAGE Base image from which this service might be built if any +## - SERVICE_DATASTORE Location on host of the DATASTORE of this service +## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service + + +. lib/common + +set -e + +admin_user=$(options-get admin.user 2>&1) || { + admin_user="admin" +} + +admin_password=$(options-get admin.password 2>&1) || { + admin_password="$(gen_password)" +} + + +## XXXvlab: the directory here for datadir violates DRY as it is also +## in ``metadata.yml`` +init-config-add "\ +$MASTER_BASE_SERVICE_NAME: + environment: + NEXTCLOUD_ADMIN_USER: $admin_user + NEXTCLOUD_ADMIN_PASSWORD: $admin_password + NEXTCLOUD_DATA_DIR: /var/lib/nextcloud/data +" + +## ensuring data directories are accessible by nextcloud + +uid=$(docker_get_uid "$MASTER_BASE_SERVICE_NAME" "www-data") + + +dirs=("$SERVICE_DATASTORE/var/lib/nextcloud/data" "$SERVICE_DATASTORE/var/www/html") +mkdir -p "${dirs[@]}" +chown -R "$uid" "${dirs[@]}" diff --git a/nextcloud/hooks/postgres_database-relation-joined b/nextcloud/hooks/postgres_database-relation-joined new file mode 100755 index 0000000..f2852bb --- /dev/null +++ b/nextcloud/hooks/postgres_database-relation-joined @@ -0,0 +1,37 @@ +#!/bin/bash + +. lib/common + +set -e + +PASSWORD="$(relation-get password)" +USER="$(relation-get user)" +DBNAME="$(relation-get dbname)" +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') + fi + if [ -z "$ADMIN_PASSWORD" ]; then + info "Generating odoo admin password" + ADMIN_PASSWORD=$(gen_password) + fi +} + +# control=$(echo -en "$USER\0$DBNAME\0$PASSWORD\0$ADMIN_PASSWORD" | md5_compat) + +config-add "\ +services: + $MASTER_BASE_SERVICE_NAME: + environment: + POSTGRES_HOST: $MASTER_TARGET_SERVICE_NAME + POSTGRES_DB: $DBNAME + POSTGRES_PASSWORD: $PASSWORD + POSTGRES_USER: $USER +" + +# [ "$control" == "$(relation-get control 2>/dev/null)" ] && exit 0 + + +# relation-set control "$control" + +info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." diff --git a/nextcloud/hooks/web_proxy-relation-joined b/nextcloud/hooks/web_proxy-relation-joined new file mode 100755 index 0000000..aa899b3 --- /dev/null +++ b/nextcloud/hooks/web_proxy-relation-joined @@ -0,0 +1,26 @@ +#!/bin/bash + +set -e + +DOMAIN=$(relation-get domain) || exit 1 +URL="$(relation-get url)" || exit 1 +PROTO="${URL%%://*}" + +trusted_domains="$( + compose -q --no-relations --no-init occ "$MASTER_BASE_SERVICE_NAME" \ + config:system:get trusted_domains)" + +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 + + config:system:set overwritehost --value="$DOMAIN" \; + config:system:set overwriteprotocol --value="$PROTO" +) +if ! [[ "$'\n'$trusted_domains$'\n'" == *"$'\n'$MASTER_BASE_SERVICE_NAME$'\n'"* ]]; then + trusted_index=$(echo "$trusted_domains" | wc -l) + debug "Adding $MASTER_TARGET_SERVICE_NAME to ${WHITE}trusted_domains${NORMAL}." + occ_opts+=( \; config:system:set trusted_domains "$trusted_index" --value="$MASTER_BASE_SERVICE_NAME") +fi +compose --no-relations --no-init occ "$MASTER_BASE_SERVICE_NAME" "${occ_opts[@]}" \ No newline at end of file diff --git a/nextcloud/lib/common b/nextcloud/lib/common new file mode 100644 index 0000000..1558c9a --- /dev/null +++ b/nextcloud/lib/common @@ -0,0 +1,3 @@ +# -*- mode: shell-script -*- + + diff --git a/nextcloud/metadata.yml b/nextcloud/metadata.yml new file mode 100644 index 0000000..1449eb6 --- /dev/null +++ b/nextcloud/metadata.yml @@ -0,0 +1,26 @@ +docker-image: docker.0k.io/nextcloud:1.0.0 +data-resources: + - /var/www/html + - /var/lib/nextcloud/data +config-resources: + - /var/www/html/config + +provides: + nextcloud-app: +uses: + postgres-database: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + constraint: required + auto: summon + solves: + database: "main storage" + web-proxy: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + constraint: required + auto: summon + solves: + proxy: "Public access" + default-options: + target: !var-expand ${MASTER_BASE_SERVICE_NAME}:80 diff --git a/nextcloud/src/occ.batch b/nextcloud/src/occ.batch new file mode 100755 index 0000000..b0e081d --- /dev/null +++ b/nextcloud/src/occ.batch @@ -0,0 +1,18 @@ +#!/bin/bash + +occ() { + /var/www/html/occ --no-warnings "$@" +} + +args=() +for arg in "$@"; do + if [ "$arg" == ";" ]; then + echo "running: occ " "${args[@]}" >&2 + occ "${args[@]}" # || exit 1 + args=() + continue + fi + args+=("$arg") +done +occ "${args[@]}" +