From 81b3f74c4d2d53cccd25e6d1b0ca5819c1c728f5 Mon Sep 17 00:00:00 2001
From: CanbiZ <47820557+MickLesk@users.noreply.github.com>
Date: Thu, 10 Jul 2025 15:38:49 +0200
Subject: [PATCH] clickhouse
---
install/rybbit-install.sh | 22 +--------------
misc/tools.func | 56 +++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 21 deletions(-)
diff --git a/install/rybbit-install.sh b/install/rybbit-install.sh
index e0af122d..0c8d8cc7 100644
--- a/install/rybbit-install.sh
+++ b/install/rybbit-install.sh
@@ -20,32 +20,12 @@ $STD apt-get install -y \
ca-certificates
msg_ok "Installed Dependencies"
+CLICKHOUSE_DB="rybbit_db" CLICKHOUSE_USER="rybbit" setup_clickhouse
PG_VERSION=17 setup_postgresql
NODE_VERSION="20" NODE_MODULE="next" setup_nodejs
-msg_info "Setup Clickhouse Repository"
-curl -fsSL 'https://packages.clickhouse.com/rpm/lts/repodata/repomd.xml.key' | sudo gpg --dearmor -o /usr/share/keyrings/clickhouse-keyring.gpg
-ARCH=$(dpkg --print-architecture)
-echo "deb [signed-by=/usr/share/keyrings/clickhouse-keyring.gpg arch=${ARCH}] https://packages.clickhouse.com/deb stable main" | sudo tee /etc/apt/sources.list.d/clickhouse.list
-$STD apt-get update
-$STD apt-get install -y clickhouse-server clickhouse-client
-$STD systemctl enable --now clickhouse-server
-sleep 3
-msg_ok "Set up Clickhouse Repository"
-
-msg_info "Setting up Clickhouse Database"
-CLICKHOUSE_DB="${CLICKHOUSE_DB:-rybbit_db}"
-CLICKHOUSE_USER="${CLICKHOUSE_USER:-rybbit}"
-CLICKHOUSE_PASS="${CLICKHOUSE_PASS:-$(openssl rand -base64 18 | cut -c1-13)}"
-CLICKHOUSE_HOST="localhost"
-
-clickhouse client --query "CREATE DATABASE IF NOT EXISTS $CLICKHOUSE_DB"
-clickhouse client --query="CREATE USER IF NOT EXISTS $CLICKHOUSE_USER IDENTIFIED WITH plaintext_password BY '$CLICKHOUSE_PASS'"
-clickhouse client --query="GRANT ALL ON $CLICKHOUSE_DB.* TO $CLICKHOUSE_USER"
#sed -i 's|default|read_only|' /etc/clickhouse-server/users.xml
#sed -i 's||DISABLED|' /etc/clickhouse-server/users.xml
-$STD systemctl restart clickhouse-server
-msg_ok "Set up Clickhouse Database"
msg_info "Setting up PostgreSQL Database"
DB_NAME=rybbit_db
diff --git a/misc/tools.func b/misc/tools.func
index bfa7e013..c34974c6 100644
--- a/misc/tools.func
+++ b/misc/tools.func
@@ -1867,3 +1867,59 @@ function setup_ffmpeg() {
ensure_usr_local_bin_persist
msg_ok "Setup FFmpeg $FINAL_VERSION"
}
+
+# ------------------------------------------------------------------------------
+# Installs ClickHouse server and client, sets up DB/user with credentials.
+#
+# Description:
+# - Adds official ClickHouse APT repo with GPG key
+# - Installs clickhouse-server and clickhouse-client
+# - Creates database and user (credentials optionally overrideable via env)
+#
+# Variables:
+# CLICKHOUSE_DB - Database name (default: analytics)
+# CLICKHOUSE_USER - Username (default: analytics_user)
+# CLICKHOUSE_PASS - Password (default: auto-generated)
+# ------------------------------------------------------------------------------
+
+function setup_clickhouse() {
+ local CLICKHOUSE_DB="${CLICKHOUSE_DB:-analytics}"
+ local CLICKHOUSE_USER="${CLICKHOUSE_USER:-analytics_user}"
+ local CLICKHOUSE_PASS="${CLICKHOUSE_PASS:-$(openssl rand -base64 18 | cut -c1-13)}"
+ local GPG_URL="https://packages.clickhouse.com/rpm/lts/repodata/repomd.xml.key"
+ local GPG_KEY_PATH="/usr/share/keyrings/clickhouse-keyring.gpg"
+ local ARCH
+ ARCH=$(dpkg --print-architecture)
+
+ msg_info "Adding ClickHouse repository"
+ if ! getent ahosts packages.clickhouse.com >/dev/null; then
+ msg_error "DNS resolution failed – possibly blocked (AdGuard, Pi-hole)"
+ return 1
+ fi
+
+ if ! curl -fsSL --connect-timeout 10 --retry 3 "$GPG_URL" | gpg --dearmor -o "$GPG_KEY_PATH"; then
+ msg_error "Failed to fetch ClickHouse GPG key"
+ return 1
+ fi
+
+ echo "deb [signed-by=$GPG_KEY_PATH arch=$ARCH] https://packages.clickhouse.com/deb stable main" >/etc/apt/sources.list.d/clickhouse.list
+ $STD apt-get update
+
+ msg_info "Installing ClickHouse"
+ $STD apt-get install -y clickhouse-server clickhouse-client
+ $STD systemctl enable --now clickhouse-server
+ sleep 3
+ msg_ok "Installed ClickHouse"
+
+ msg_info "Creating ClickHouse DB '$CLICKHOUSE_DB' and user '$CLICKHOUSE_USER'"
+ clickhouse client --query "CREATE DATABASE IF NOT EXISTS $CLICKHOUSE_DB"
+ clickhouse client --query "CREATE USER IF NOT EXISTS $CLICKHOUSE_USER IDENTIFIED WITH plaintext_password BY '$CLICKHOUSE_PASS'"
+ clickhouse client --query "GRANT ALL ON $CLICKHOUSE_DB.* TO $CLICKHOUSE_USER"
+ msg_ok "Created DB and user"
+
+ {
+ echo "ClickHouse DB: $CLICKHOUSE_DB"
+ echo "ClickHouse User: $CLICKHOUSE_USER"
+ echo "ClickHouse Pass: $CLICKHOUSE_PASS"
+ } >>~/clickhouse.creds
+}