#!/usr/bin/env bash set -euo pipefail if [[ "${EUID}" -ne 0 ]]; then echo "[ERROR] please run as root" exit 1 fi if [[ ! -f /etc/os-release ]]; then echo "[ERROR] /etc/os-release not found" exit 1 fi # shellcheck disable=SC1091 source /etc/os-release os_id="$(echo "${ID:-}" | tr '[:upper:]' '[:lower:]')" os_ver="${VERSION_ID:-}" is_ubuntu22=false is_amzn2023=false if [[ "${os_id}" == "ubuntu" && "${os_ver}" == 22.04* ]]; then is_ubuntu22=true fi if [[ "${os_id}" == "amzn" && "${os_ver}" == 2023* ]]; then is_amzn2023=true fi if [[ "${is_ubuntu22}" != "true" && "${is_amzn2023}" != "true" ]]; then echo "[ERROR] only Ubuntu 22.04 or Amazon Linux 2023 is supported. current: ID=${ID:-unknown}, VERSION_ID=${VERSION_ID:-unknown}" exit 1 fi if [[ "${is_ubuntu22}" == "true" ]]; then echo "[INFO] detected Ubuntu 22.04" echo "[INFO] installing prerequisites ..." apt-get update -y DEBIAN_FRONTEND=noninteractive apt-get install -y curl ca-certificates gnupg apt-transport-https lsb-release echo "[INFO] configuring ClickHouse apt repository ..." install -d -m 0755 /etc/apt/keyrings if [[ ! -f /etc/apt/keyrings/clickhouse.gpg ]]; then curl -fsSL https://packages.clickhouse.com/CLICKHOUSE-KEY.GPG | gpg --dearmor -o /etc/apt/keyrings/clickhouse.gpg fi cat >/etc/apt/sources.list.d/clickhouse.list <<'EOF' deb [signed-by=/etc/apt/keyrings/clickhouse.gpg arch=amd64,arm64] https://packages.clickhouse.com/deb stable main EOF echo "[INFO] installing clickhouse-server and clickhouse-client ..." apt-get update -y DEBIAN_FRONTEND=noninteractive apt-get install -y clickhouse-server clickhouse-client clickhouse-common-static fi if [[ "${is_amzn2023}" == "true" ]]; then echo "[INFO] detected Amazon Linux 2023" echo "[INFO] installing prerequisites ..." dnf makecache -y dnf install -y curl ca-certificates gnupg2 dnf-plugins-core echo "[INFO] configuring ClickHouse yum repository ..." cat >/etc/yum.repos.d/clickhouse.repo <<'EOF' [clickhouse-stable] name=ClickHouse Stable Repository baseurl=https://packages.clickhouse.com/rpm/stable/$basearch enabled=1 gpgcheck=1 gpgkey=https://packages.clickhouse.com/rpm/stable/repodata/repomd.xml.key https://packages.clickhouse.com/rpm/clickhouse-static.key EOF echo "[INFO] installing clickhouse-server and clickhouse-client ..." dnf clean all dnf makecache -y if ! dnf install -y clickhouse-server clickhouse-client clickhouse-common-static; then dnf install -y clickhouse-server clickhouse-client fi fi echo "[INFO] enabling clickhouse-server ..." systemctl enable clickhouse-server >/dev/null 2>&1 || true systemctl restart clickhouse-server sleep 2 if [[ -n "${CLICKHOUSE_DEFAULT_PASSWORD:-}" ]]; then echo "[INFO] setting default user password ..." if [[ "${CLICKHOUSE_DEFAULT_PASSWORD}" == *"'"* ]]; then echo "[ERROR] CLICKHOUSE_DEFAULT_PASSWORD contains single quote, please set password manually with clickhouse-client" exit 1 fi clickhouse-client --query "ALTER USER default IDENTIFIED WITH plaintext_password BY '${CLICKHOUSE_DEFAULT_PASSWORD}'" fi echo "[INFO] health check ..." clickhouse-client --query "SELECT version()" echo "[OK] ClickHouse install completed: ID=${ID:-unknown}, VERSION_ID=${VERSION_ID:-unknown}"