Refactor: Healthchecks

This commit is contained in:
CanbiZ
2025-11-16 09:51:27 +01:00
committed by GitHub
parent 5cc11c27d2
commit 7f28f12989

View File

@@ -1,10 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright (c) 2021-2025 community-scripts ORG
# Author: MickLesk (Canbiz)
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
# Source: https://healthchecks.io/
source /dev/stdin <<<"$FUNCTIONS_FILE_PATH" source /dev/stdin <<<"$FUNCTIONS_FILE_PATH"
color color
verb_ip6 verb_ip6
@@ -16,43 +11,44 @@ update_os
msg_info "Installing Dependencies" msg_info "Installing Dependencies"
$STD apt install -y \ $STD apt install -y \
gcc \ gcc \
python3 \
python3-dev \
python3-venv \
libpq-dev \ libpq-dev \
libcurl4-openssl-dev \ libcurl4-openssl-dev \
libssl-dev \ libssl-dev \
caddy caddy
mkdir -p ~/.config/pip
cat > ~/.config/pip/pip.conf << EOF
[global]
break-system-packages = true
EOF
msg_ok "Installed Dependencies" msg_ok "Installed Dependencies"
PYTHON_VERSION="3.13" setup_uv
PG_VERSION=16 setup_postgresql PG_VERSION=16 setup_postgresql
PG_DB_NAME="healthchecks_db" PG_DB_USER="hc_user" PG_DB_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13) setup_postgresql_db
msg_info "Setup Database" msg_info "Setup Keys (Admin / Secret)"
DB_NAME=healthchecks_db
DB_USER=hc_user
DB_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13)
SECRET_KEY="$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | cut -c1-32)" SECRET_KEY="$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | cut -c1-32)"
ADMIN_EMAIL="admin@helper-scripts.local" ADMIN_EMAIL="admin@helper-scripts.local"
ADMIN_PASSWORD="$DB_PASS" ADMIN_PASSWORD="$DB_PASS"
$STD sudo -u postgres psql -c "CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';"
$STD sudo -u postgres psql -c "CREATE DATABASE $DB_NAME WITH OWNER $DB_USER ENCODING 'UTF8' TEMPLATE template0;"
$STD sudo -u postgres psql -c "ALTER ROLE $DB_USER SET client_encoding TO 'utf8';"
$STD sudo -u postgres psql -c "ALTER ROLE $DB_USER SET default_transaction_isolation TO 'read committed';"
$STD sudo -u postgres psql -c "ALTER ROLE $DB_USER SET timezone TO 'UTC'"
{ {
echo "healthchecks-Credentials"
echo "healthchecks Database User: $DB_USER"
echo "healthchecks Database Password: $DB_PASS"
echo "healthchecks Database Name: $DB_NAME"
echo "healthchecks Admin Email: $ADMIN_EMAIL" echo "healthchecks Admin Email: $ADMIN_EMAIL"
echo "healthchecks Admin Password: $ADMIN_PASSWORD" echo "healthchecks Admin Password: $ADMIN_PASSWORD"
} >>~/healthchecks.creds } >>~/healthchecks.creds
msg_ok "Set up Database" msg_ok "Set up Keys"
fetch_and_deploy_gh_release "healthchecks" "healthchecks/healthchecks" fetch_and_deploy_gh_release "healthchecks" "healthchecks/healthchecks"
msg_info "Setup healthchecks" msg_info "Installing Healthchecks (venv)"
cd /opt/healthchecks cd /opt/healthchecks
mkdir -p /opt/healthchecks/static-collected/ python3 -m venv venv
$STD uv pip install wheel gunicorn -r requirements.txt --system source venv/bin/activate
$STD pip install --upgrade pip wheel
$STD pip install gunicorn -r requirements.txt
msg_ok "Installed Python packages"
LOCAL_IP=$(hostname -I | awk '{print $1}') LOCAL_IP=$(hostname -I | awk '{print $1}')
cat <<EOF >/opt/healthchecks/hc/local_settings.py cat <<EOF >/opt/healthchecks/hc/local_settings.py
@@ -83,18 +79,19 @@ DATABASES = {
} }
EOF EOF
$STD uv run -- python manage.py makemigrations msg_info "Running Django setup"
$STD uv run -- python manage.py migrate --noinput $STD python manage.py makemigrations
$STD uv run -- python manage.py collectstatic --noinput $STD python manage.py migrate --noinput
$STD uv run -- python manage.py compress $STD python manage.py collectstatic --noinput
$STD python manage.py compress
cat <<EOF | $STD uv run -- python manage.py shell python <<EOF
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
User = get_user_model() User = get_user_model()
if not User.objects.filter(email="${ADMIN_EMAIL}").exists(): if not User.objects.filter(email="${ADMIN_EMAIL}").exists():
User.objects.create_superuser("${ADMIN_EMAIL}", "${ADMIN_EMAIL}", "${ADMIN_PASSWORD}") User.objects.create_superuser("${ADMIN_EMAIL}", "${ADMIN_EMAIL}", "${ADMIN_PASSWORD}")
EOF EOF
msg_ok "Installed healthchecks" msg_ok "Configured Django"
msg_info "Configuring Caddy" msg_info "Configuring Caddy"
cat <<EOF >/etc/caddy/Caddyfile cat <<EOF >/etc/caddy/Caddyfile
@@ -108,7 +105,7 @@ ${LOCAL_IP} {
EOF EOF
msg_ok "Configured Caddy" msg_ok "Configured Caddy"
msg_info "Creating Service" msg_info "Creating systemd service"
cat <<EOF >/etc/systemd/system/healthchecks.service cat <<EOF >/etc/systemd/system/healthchecks.service
[Unit] [Unit]
Description=Healthchecks Service Description=Healthchecks Service
@@ -116,12 +113,13 @@ After=network.target postgresql.service
[Service] [Service]
WorkingDirectory=/opt/healthchecks/ WorkingDirectory=/opt/healthchecks/
ExecStart=/usr/local/bin/uv run -- gunicorn hc.wsgi:application --bind 127.0.0.1:8000 ExecStart=/opt/healthchecks/venv/bin/gunicorn hc.wsgi:application --bind 127.0.0.1:8000
Restart=always Restart=always
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
systemctl enable -q --now healthchecks caddy systemctl enable -q --now healthchecks caddy
systemctl reload caddy systemctl reload caddy
msg_ok "Created Service" msg_ok "Created Service"