From 49e2f398c59a98592f120bff1c4ad5e0ea69d211 Mon Sep 17 00:00:00 2001 From: default Date: Tue, 13 May 2025 15:20:05 +0200 Subject: [PATCH] new: [plausible] add charm --- clickhouse/README.org | 7 +++ clickhouse/hooks/init | 21 +++++++ clickhouse/metadata.yml | 21 +++++++ .../clickhouse-server/config.d/ipv4-only.xml | 3 + .../etc/clickhouse-server/config.d/logs.xml | 28 +++++++++ .../config.d/low-resources.xml | 23 ++++++++ plausible/README.org | 7 +++ plausible/hooks/event_db-relation-joined | 14 +++++ plausible/hooks/init | 26 +++++++++ .../hooks/postgres_database-relation-joined | 17 ++++++ plausible/hooks/smtp_server-relation-joined | 21 +++++++ plausible/hooks/web_proxy-relation-joined | 16 +++++ plausible/metadata.yml | 58 +++++++++++++++++++ 13 files changed, 262 insertions(+) create mode 100644 clickhouse/README.org create mode 100755 clickhouse/hooks/init create mode 100644 clickhouse/metadata.yml create mode 100644 clickhouse/resources/etc/clickhouse-server/config.d/ipv4-only.xml create mode 100644 clickhouse/resources/etc/clickhouse-server/config.d/logs.xml create mode 100644 clickhouse/resources/etc/clickhouse-server/config.d/low-resources.xml create mode 100644 plausible/README.org create mode 100755 plausible/hooks/event_db-relation-joined create mode 100755 plausible/hooks/init create mode 100755 plausible/hooks/postgres_database-relation-joined create mode 100755 plausible/hooks/smtp_server-relation-joined create mode 100755 plausible/hooks/web_proxy-relation-joined create mode 100644 plausible/metadata.yml diff --git a/clickhouse/README.org b/clickhouse/README.org new file mode 100644 index 0000000..60a5e15 --- /dev/null +++ b/clickhouse/README.org @@ -0,0 +1,7 @@ +# -*- ispell-local-dictionary: "english" -*- + +* Info + +This charm is provided to work with plausible charm + +* Usage \ No newline at end of file diff --git a/clickhouse/hooks/init b/clickhouse/hooks/init new file mode 100755 index 0000000..f50b6b6 --- /dev/null +++ b/clickhouse/hooks/init @@ -0,0 +1,21 @@ +#!/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 + +set -e + +init-config-add " +$SERVICE_NAME: + environment: + CLICKHOUSE_SKIP_USER_SETUP: 1 + healthcheck: + test: [ \"CMD-SHELL\", \"wget --no-verbose --tries=1 -O - http://127.0.0.1:8123/ping || exit 1\" ] +" \ No newline at end of file diff --git a/clickhouse/metadata.yml b/clickhouse/metadata.yml new file mode 100644 index 0000000..e52a27e --- /dev/null +++ b/clickhouse/metadata.yml @@ -0,0 +1,21 @@ +docker-image: docker.0k.io/clickhouse:24.12-alpine +#docker-image: clickhouse/clickhouse-server:24.12-alpine + +data-resources: + - /var/lib/clickhouse + - /var/log/clickhouse-server + +charm-resources: + - /etc/clickhouse-server/config.d/logs.xml + - /etc/clickhouse-server/config.d/ipv4-only.xml + - /etc/clickhouse-server/config.d/low-resources.xml + +provides: + event-db: + +uses: + log-rotate: + constraint: recommended + auto: pair + solves: + disk-leak: "/var/log/clickhouse-server" diff --git a/clickhouse/resources/etc/clickhouse-server/config.d/ipv4-only.xml b/clickhouse/resources/etc/clickhouse-server/config.d/ipv4-only.xml new file mode 100644 index 0000000..7eb0a5c --- /dev/null +++ b/clickhouse/resources/etc/clickhouse-server/config.d/ipv4-only.xml @@ -0,0 +1,3 @@ + + 0.0.0.0 + diff --git a/clickhouse/resources/etc/clickhouse-server/config.d/logs.xml b/clickhouse/resources/etc/clickhouse-server/config.d/logs.xml new file mode 100644 index 0000000..3ffd303 --- /dev/null +++ b/clickhouse/resources/etc/clickhouse-server/config.d/logs.xml @@ -0,0 +1,28 @@ + + + warning + true + + + + system + query_log
+ 7500 + + ENGINE = MergeTree + PARTITION BY event_date + ORDER BY (event_time) + TTL event_date + interval 30 day + SETTINGS ttl_only_drop_parts=1 + +
+ + + + + + + + + +
\ No newline at end of file diff --git a/clickhouse/resources/etc/clickhouse-server/config.d/low-resources.xml b/clickhouse/resources/etc/clickhouse-server/config.d/low-resources.xml new file mode 100644 index 0000000..aab3cd8 --- /dev/null +++ b/clickhouse/resources/etc/clickhouse-server/config.d/low-resources.xml @@ -0,0 +1,23 @@ + + + + 524288000 + + + + + 1 + + 8192 + + 1 + + 0 + + 0 + + + \ No newline at end of file diff --git a/plausible/README.org b/plausible/README.org new file mode 100644 index 0000000..f8d1908 --- /dev/null +++ b/plausible/README.org @@ -0,0 +1,7 @@ +# -*- ispell-local-dictionary: "english" -*- + +* Info + +From: https://github.com/plausible/community-edition/ + +* Usage \ No newline at end of file diff --git a/plausible/hooks/event_db-relation-joined b/plausible/hooks/event_db-relation-joined new file mode 100755 index 0000000..acd8ec5 --- /dev/null +++ b/plausible/hooks/event_db-relation-joined @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e + + + +config-add "\ +services: + $MASTER_BASE_SERVICE_NAME: + environment: + CLICKHOUSE_DATABASE_URL: http://$TARGET_SERVICE_NAME:8123/$TARGET_SERVICE_NAME +" + +info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." diff --git a/plausible/hooks/init b/plausible/hooks/init new file mode 100755 index 0000000..1e37017 --- /dev/null +++ b/plausible/hooks/init @@ -0,0 +1,26 @@ +#!/bin/bash + +SECRET_KEY_BASE="$SERVICE_DATASTORE"/secret-key + +SHARE_DIR="$SERVICE_DATASTORE"/var/lib/plausible + +uid=$(docker_get_uid "$SERVICE_NAME" "plausible") + +if ! [ -f "$SECRET_KEY_BASE" ]; then + info "Generating secret key" + mkdir -p "${SECRET_KEY_BASE%/*}" + umask 077 + openssl rand -base64 64 > "$SECRET_KEY_BASE" +else + info "Using existing secret key" +fi + +secret_key_base=$(cat "$SECRET_KEY_BASE") + +init-config-add " +$SERVICE_NAME: + environment: + SECRET_KEY_BASE: \"$secret_key_base\" +" + +chown -v "$uid" "$SHARE_DIR" diff --git a/plausible/hooks/postgres_database-relation-joined b/plausible/hooks/postgres_database-relation-joined new file mode 100755 index 0000000..c898aa7 --- /dev/null +++ b/plausible/hooks/postgres_database-relation-joined @@ -0,0 +1,17 @@ +#!/bin/bash + +set -e + +PASSWORD="$(relation-get password)" +USER="$(relation-get user)" +DBNAME="$(relation-get dbname)" + + +config-add "\ +services: + $MASTER_BASE_SERVICE_NAME: + environment: + DATABASE_URL: postgres://$USER:$PASSWORD@$TARGET_SERVICE_NAME:5432/$DBNAME +" + +info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access." \ No newline at end of file diff --git a/plausible/hooks/smtp_server-relation-joined b/plausible/hooks/smtp_server-relation-joined new file mode 100755 index 0000000..725769b --- /dev/null +++ b/plausible/hooks/smtp_server-relation-joined @@ -0,0 +1,21 @@ +#!/bin/bash + +set -e + +host=$(relation-get host) || exit 1 +port=$(relation-get port) || exit 1 +user=$(relation-get login) || exit 1 +password="$(relation-get password)" || exit 1 + + +config-add "\ +services: + $MASTER_BASE_SERVICE_NAME: + environment: + SMTP_HOST_NAME: \"$user\" + SMTP_HOST_PASS: \"${password//\$/\$\$}\" + SMTP_HOST_ADDR: \"$host\" + SMTP_HOST_PORT: \"$port\" + # SMTP_HOST_SSL_ENABLE: \"true\" +" + diff --git a/plausible/hooks/web_proxy-relation-joined b/plausible/hooks/web_proxy-relation-joined new file mode 100755 index 0000000..bfb69cd --- /dev/null +++ b/plausible/hooks/web_proxy-relation-joined @@ -0,0 +1,16 @@ +#!/bin/bash + +set -e + +DOMAIN=$(relation-get domain) || { + echo "Failed to get domain" + exit 1 +} + +config-add "\ +services: + $MASTER_BASE_SERVICE_NAME: + environment: + BASE_URL: \"https:\/\/${DOMAIN}\" +" + diff --git a/plausible/metadata.yml b/plausible/metadata.yml new file mode 100644 index 0000000..cdc46f5 --- /dev/null +++ b/plausible/metadata.yml @@ -0,0 +1,58 @@ +docker-image: docker.0k.io/plausible:3.0.1 +#docker-image: ghcr.io/plausible/community-edition:v3.0.1 + +data-resources: + - /var/lib/plausible + +docker-compose: + entrypoint: sh -c "/entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run" + #entrypoint: sh -c "/entrypoint.sh run" +uses: + event-db: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + constraint: required + auto: summon + solves: + database: "event db" + postgres-database: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + constraint: required + auto: summon + solves: + database: "main storage" + default-options: + extensions: + - citext + smtp-server: + constraint: required + auto: summon + solves: + proxy: "Public access" + web-proxy: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + constraint: recommended + auto: pair + solves: + proxy: "Public access" + default-options: + target: !var-expand ${MASTER_BASE_SERVICE_NAME}:8000 + apache-custom-rules: + - !var-expand | + ProxyPreserveHost On + + #Set web sockets + RewriteEngine On + RewriteCond %{HTTP:Upgrade} =websocket [NC] + RewriteCond %{HTTP:Connection} upgrade [NC] + RewriteRule ^/(live/websocket)$ ws://${MASTER_BASE_SERVICE_NAME}:8000/\$1 [P,L] + + + backup: + constraint: recommended + auto: pair + solves: + backup: "Automatic regular backup" + default-options: