diff --git a/matomo/hooks/init b/matomo/hooks/init new file mode 100755 index 0000000..6ccd3c9 --- /dev/null +++ b/matomo/hooks/init @@ -0,0 +1,24 @@ +#!/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 + +## If no admin-password set, fail +options-get admin-password + + +matomo:init + +matomo:config diff --git a/matomo/hooks/mysql_database-relation-joined b/matomo/hooks/mysql_database-relation-joined new file mode 100755 index 0000000..548be35 --- /dev/null +++ b/matomo/hooks/mysql_database-relation-joined @@ -0,0 +1,26 @@ +#!/bin/bash + + +. lib/common + + +## XXXvlab: should get location of code +#CONFIG=$(echo $COMPOSE_CONFIG | shyaml get-value $SERVICE_NAME.relations.publish-dir...) +# CONFIG="$SERVICE_DATASTORE/opt/apps/piwigo/local/config/database.inc.php" + +set -e + +PASSWORD="$(relation-get password)" +USER="$(relation-get user)" +DBNAME="$(relation-get dbname)" + + +crudini --set "$MATOMO_CONFIG_FILE" database host "\"$TARGET_SERVICE_NAME\"" +crudini --set "$MATOMO_CONFIG_FILE" database username "\"$USER\"" +crudini --set "$MATOMO_CONFIG_FILE" database password "\"$PASSWORD\"" +crudini --set "$MATOMO_CONFIG_FILE" database dbname "\"$DBNAME\"" +crudini --set "$MATOMO_CONFIG_FILE" database tables_prefix "\"\"" +crudini --set "$MATOMO_CONFIG_FILE" database charset "\"utf8mb4\"" + +info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." + diff --git a/matomo/hooks/post_deploy b/matomo/hooks/post_deploy new file mode 100755 index 0000000..b5a4412 --- /dev/null +++ b/matomo/hooks/post_deploy @@ -0,0 +1,117 @@ +#!/bin/bash + +. lib/common + +set -e + +## If no admin-password set, then don't try to pre-initialize database +admin_password=$(options-get admin-password 2>/dev/null) || exit 0 +admin_email=$(options-get admin-email 2>/dev/null ) || true + +CONTROL="$SERVICE_DATASTORE/.control" +## Was it already properly propagated to database ? +control=$(H "${admin_password}" "${admin_email}") +if [ -e "$CONTROL" ]; then + if [ "$control" == "$(cat "$CONTROL")" ]; then + exit 0 + else + err "Changing admin password in compose file not yet supported" + exit 1 + fi +fi + + +if ! [ -d "$MATOMO_CODE/vendor" ]; then + + # XXXvlab: can't get real config here + if ! read-0 publish_dir_ts _ _ < <(get_service_relation "$SERVICE_NAME" "publish-dir"); then + err "Couldn't find relation ${DARKCYAN}publish-dir${NORMAL}." + exit 1 + fi + + publish_dir_relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$publish_dir_ts" "publish-dir") || { + err "Failed to find relation file" + exit 1 + } + + publish_dir_relation_config=$(cat "$publish_dir_relation_dir/data") || exit 2 + + domain=$(e "$publish_dir_relation_config" | shyaml get-value domain) || { + err "Couldn't get domain information in ${DARKCYAN}publish-dir${NORMAL} relation's data." + exit 1 + } + url=$(e "$publish_dir_relation_config" | shyaml get-value url) || { + err "Couldn't get url information in ${DARKCYAN}publish-dir${NORMAL} relation's data." + exit 1 + } + + + ## + ## Get domain in option of relation "publish-dir" + ## + + + container_id=$( + for container_id in $(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"); do + e "$container_id" + break + done + ) + + docker exec -i "$container_id" bash <&2 + exit 1 + } + mv -v /tmp/composer.phar /usr/local/bin/composer || exit 1 +} + +cd /var/www/$domain && +composer install + +EOF + +fi + + +## +## Required wizard +## + +if [ "$(crudini --get "$MATOMO_CONFIG_FILE" General installation_in_progress)" == 1 ]; then + curl "$url/index.php?action=tablesCreation" >/dev/null || { + err "Table creation failed." + exit 1 + } + orig_uid_gid=$(stat --format=%u:%g "$MATOMO_CONFIG_FILE") + uid_gid=$(stat --format=%u:%g "$SERVICE_DATASTORE/var/tmp/matomo/index.php") + chown "$uid_gid" "$MATOMO_CONFIG_FILE" + curl "$url/index.php?action=setupSuperUser" \ + --data-urlencode login="admin" \ + --data-urlencode password="$admin_password" \ + --data-urlencode password_bis="$admin_password" \ + --data-urlencode email="${admin_email:-admin@localhost.localnet}" \ + --data-urlencode subscribe_newsletter_piwikorg="0" \ + --data-urlencode subscribe_newsletter_piwikpro="0" || { + err "Setting admin account failed." + exit 1 + } + + chown "$orig_uid_gid" "$MATOMO_CONFIG_FILE" + crudini --set "$MATOMO_CONFIG_FILE" General installation_in_progress 0 + + #curl "$url/index.php?action=firstWebsiteSetup" \ + # --data-urlencode siteName="$SITE_NAME" \ + # --data-urlencode url="$url" \ + # --data-urlencode timezone="$(cat /etc/timezone)" \ + # --data-urlencode ecommerce="0" || { + # err "First Web Site Setup failed." + # exit 1 + #} + +fi + + +exit 0 diff --git a/matomo/hooks/publish_dir-relation-joined b/matomo/hooks/publish_dir-relation-joined new file mode 100755 index 0000000..2185e53 --- /dev/null +++ b/matomo/hooks/publish_dir-relation-joined @@ -0,0 +1,77 @@ +#!/bin/bash + +. lib/common + + + +set -e + +#domain=$(relation-get domain) || exit 1 +domain="matomo" +url=$(relation-get url) || exit 1 +# location=$CONFIGSTORE/$BASE_SERVICE_NAME/var/www/$domain + +upload_dir="${SERVICE_DATASTORE}/var/www/$domain/uploads" +if [ -d "$upload_dir" ]; then + uid_gid=$(stat --format=%u:%g "$upload_dir") +else + err "Upload dir '${upload_dir}' was not created. Can't continue." + exit 1 +fi + +dirs=( + /var/cache/matomo + /var/lib/matomo{,/assets} + /var/log/matomo + /var/tmp/matomo{,/tcpdf,/templates_c} +) + +to_create=() +for dir in "${dirs[@]}"; do + fdir="${SERVICE_DATASTORE}${dir}" + if ! [ -d "$fdir" ]; then + to_create+=("$fdir") + fi +done + +if [ "${#to_create[@]}" -gt 0 ]; then + mkdir -p "${to_create[@]}" && + chown -v "$uid_gid" "${to_create[@]}" && + chmod -v g+rwx "${to_create[@]}" +fi + + + +config-add " +services: + $MASTER_TARGET_SERVICE_NAME: + volumes: + - $SERVICE_DATASTORE/var/tmp/matomo:/var/www/$domain/tmp:rw + - $SERVICE_DATASTORE/var/cache/matomo:/var/www/$domain/tmp/cache:rw + - $SERVICE_DATASTORE/var/lib/matomo/assets:/var/www/$domain/tmp/assets:rw + - $SERVICE_DATASTORE/var/log/matomo:/var/www/$domain/tmp/logs:rw + - $SERVICE_DATASTORE/var/tmp/matomo/tcpdf:/var/www/$domain/tmp/tcpdf:rw + - $SERVICE_DATASTORE/var/tmp/matomo/templates_c:/var/www/$domain/tmp/templates_c:rw +" + + +# +#cat <> "${MATOMO_CODE}"/.env +# +#BASE_PROTOCOL=${url%%://*} +#BASE_URL=$domain +#BASE_PATH=/index.php +# +#EOF +# +# +#cat < "${MATOMO_CODE}"/web/.htaccess +# +#Options -MultiViews +#RewriteEngine On +#RewriteRule ^js/.* - [L] +#RewriteCond %{REQUEST_FILENAME} !-f +#RewriteRule ^(.*)$ index.php/\$1 [QSA,L] +# +#EOF +# diff --git a/matomo/lib/common b/matomo/lib/common new file mode 100644 index 0000000..1d04960 --- /dev/null +++ b/matomo/lib/common @@ -0,0 +1,95 @@ +# -*- mode: shell-script -*- + +MATOMO_DIR="/opt/apps/matomo" +MATOMO_CODE="$SERVICE_CONFIGSTORE$MATOMO_DIR" +MATOMO_RELEASE=4.16.0 +MATOMO_URL=https://github.com/matomo-org/matomo/archive/"${MATOMO_RELEASE}".tar.gz +#MATOMO_URL=https://docker.0k.io/downloads/matomo-"${MATOMO_RELEASE}".tar.gz +MATOMO_CONFIG_FILE="${MATOMO_CODE}"/config/config.ini.php + + +matomo:init() { + current_version="" + if [ -d "${MATOMO_CODE}" ]; then + current_version=$(cat "${MATOMO_CODE}"/.version) || { + err "Couldn't find ${MATOMO_CODE}/.version file." + echo " Your config dir is in a broken state." >&2 + return 1 + } + else + mkdir -p "${MATOMO_CODE}" && + cd "${MATOMO_CODE}" && + git init . && + git config user.email "root@localhost" && + git config user.name "Root" || { + err "Couldn't create directory ${MATOMO_CODE}, or init it with git." + return 1 + } + fi + if [ "$current_version" != "$MATOMO_RELEASE" ]; then + cd "${MATOMO_CODE}" || return 1 + [ -d "$MATOMO_CODE"/.git ] || { + err "Can't find the '.git' directory in ${MATOMO_CODE}." + return 1 + } + + rm -rf "$MATOMO_CODE"/* "$MATOMO_CODE"/{.version,.inited-*} || return 1 + + curl -L "$MATOMO_URL" | tar xzv || { + #if [ -f "$MATOMO_URL" ]; then + # git checkout HEAD + #else + # rmdir "$MATOMO_URL" + #fi + err "Couldn't download $MATOMO_URL." + return 1 + } + mv matomo-*/* matomo-*/{.bowerrc,.lfsconfig} . && rmdir matomo-* + echo "$MATOMO_RELEASE" > .version + git add -A . && + git commit -m "Release $MATOMO_RELEASE" + fi +} + + +matomo:config() { + [ -f "$MATOMO_CONFIG_FILE" ] || { + cat < "$MATOMO_CONFIG_FILE" +; DO NOT REMOVE THIS LINE +; file automatically generated or modified by Matomo; you can manually override the default values in global.ini.php by redefining them in this file. +EOF + } + crudini --get "$MATOMO_CONFIG_FILE" General salt >dev/null || { + salt=$(dd if=/dev/urandom bs=1 count=16 2>/dev/null | hexdump -v -e '/1 "%02x"') + crudini --set "$MATOMO_CONFIG_FILE" General salt \"$salt\" + crudini --set "$MATOMO_CONFIG_FILE" General installation_in_progress 1 + } + +} + + +matomo:curl() { + local url="$1" + curl "$url" + +} + + +# matomo:wizard() { + +# PAGES=( +# "systemCheck" +# "databaseSetup" +# "tablesCreation" +# "setupSuperUser" +# "setupSuperUser" +# "firstWebsiteSetup" +# "firstWebsiteSetup" +# "trackingCode" +# "finished" +# ) + +# matomo:curl + + +# } diff --git a/matomo/metadata.yml b/matomo/metadata.yml new file mode 100644 index 0000000..8dc6f12 --- /dev/null +++ b/matomo/metadata.yml @@ -0,0 +1,45 @@ +subordinate: true +requires: + web-publishing-directory: + interface: publish-dir + scope: container +default-options: + admin-password: admin + + + +uses: + publish-dir: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + scope: container + constraint: required + auto: summon + solves: + container: "main running server" + default-options: + location: !var-expand "$CONFIGSTORE/$BASE_SERVICE_NAME/opt/apps/matomo" + data-dirs: ## write permission for web-app + - uploads + mysql-database: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + constraint: required + auto: summon + solves: + database: "main storage" + backup: + constraint: recommended + auto: pair + solves: + backup: "Automatic regular backup" + default-options: + ## First pattern matching wins, no pattern matching includes. + ## include-patterns are checked first, then exclude-patterns + ## Patterns rules: + ## - ending / for directory + ## - '*' authorized + ## - must start with a '/', will start from $SERVICE_DATASTORE + exclude-patterns: + - "/var/cache/" + - "/var/tmp/"