Skip to content

Commit 59b8003

Browse files
authored
Refactor: Healthchecks (#9188)
* Refactor: Healthchecks * Enhance healthchecks update process with backup and venv Added backup creation for existing healthchecks installation before updating. Enhanced the update process by recreating the Python virtual environment and installing requirements. * - Added copyright, author, and license information.
1 parent 1294b89 commit 59b8003

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

ct/healthchecks.sh

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,42 @@ function update_script() {
2828
msg_error "No ${APP} Installation Found!"
2929
exit
3030
fi
31+
3132
if check_for_gh_release "healthchecks" "healthchecks/healthchecks"; then
3233
msg_info "Stopping Services"
3334
systemctl stop healthchecks
3435
msg_ok "Stopped Services"
3536

36-
PYTHON_VERSION="3.13" setup_uv
37+
msg_info "Backing up existing installation"
38+
BACKUP="/opt/healthchecks-backup-$(date +%F-%H%M)"
39+
cp -a /opt/healthchecks "$BACKUP"
40+
msg_ok "Backup created at $BACKUP"
41+
3742
fetch_and_deploy_gh_release "healthchecks" "healthchecks/healthchecks"
3843

39-
msg_info "Updating healthchecks"
4044
cd /opt/healthchecks
41-
mkdir -p /opt/healthchecks/static-collected/
42-
$STD uv pip install wheel gunicorn -r requirements.txt --system
43-
$STD uv run -- python manage.py makemigrations
44-
$STD uv run -- python manage.py migrate --noinput
45-
$STD uv run -- python manage.py collectstatic --noinput
46-
$STD uv run -- python manage.py compress
47-
msg_ok "Updated healthchecks"
45+
if [[ -d venv ]]; then
46+
rm -rf venv
47+
fi
48+
msg_info "Recreating Python venv"
49+
$STD python3 -m venv venv
50+
$STD source venv/bin/activate
51+
$STD pip install --upgrade pip wheel
52+
msg_ok "Created venv"
53+
54+
msg_info "Installing requirements"
55+
$STD pip install gunicorn -r requirements.txt
56+
msg_ok "Installed requirements"
57+
58+
msg_info "Running Django migrations"
59+
$STD python manage.py migrate --noinput
60+
$STD python manage.py collectstatic --noinput
61+
$STD python manage.py compress
62+
msg_ok "Completed Django migrations and static build"
63+
4864
msg_info "Starting Services"
4965
systemctl start healthchecks
50-
systemctl restart caddy
66+
systemctl reload caddy
5167
msg_ok "Started Services"
5268
msg_ok "Updated successfully!"
5369
fi

install/healthchecks-install.sh

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env bash
2-
32
# Copyright (c) 2021-2025 community-scripts ORG
43
# Author: MickLesk (Canbiz)
54
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
@@ -16,43 +15,44 @@ update_os
1615
msg_info "Installing Dependencies"
1716
$STD apt install -y \
1817
gcc \
18+
python3 \
19+
python3-dev \
20+
python3-venv \
1921
libpq-dev \
2022
libcurl4-openssl-dev \
2123
libssl-dev \
2224
caddy
25+
26+
mkdir -p ~/.config/pip
27+
cat > ~/.config/pip/pip.conf << EOF
28+
[global]
29+
break-system-packages = true
30+
EOF
2331
msg_ok "Installed Dependencies"
2432

25-
PYTHON_VERSION="3.13" setup_uv
2633
PG_VERSION=16 setup_postgresql
34+
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
2735

28-
msg_info "Setup Database"
29-
DB_NAME=healthchecks_db
30-
DB_USER=hc_user
31-
DB_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13)
36+
msg_info "Setup Keys (Admin / Secret)"
3237
SECRET_KEY="$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | cut -c1-32)"
3338
ADMIN_EMAIL="[email protected]"
3439
ADMIN_PASSWORD="$DB_PASS"
35-
$STD sudo -u postgres psql -c "CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';"
36-
$STD sudo -u postgres psql -c "CREATE DATABASE $DB_NAME WITH OWNER $DB_USER ENCODING 'UTF8' TEMPLATE template0;"
37-
$STD sudo -u postgres psql -c "ALTER ROLE $DB_USER SET client_encoding TO 'utf8';"
38-
$STD sudo -u postgres psql -c "ALTER ROLE $DB_USER SET default_transaction_isolation TO 'read committed';"
39-
$STD sudo -u postgres psql -c "ALTER ROLE $DB_USER SET timezone TO 'UTC'"
4040
{
41-
echo "healthchecks-Credentials"
42-
echo "healthchecks Database User: $DB_USER"
43-
echo "healthchecks Database Password: $DB_PASS"
44-
echo "healthchecks Database Name: $DB_NAME"
4541
echo "healthchecks Admin Email: $ADMIN_EMAIL"
4642
echo "healthchecks Admin Password: $ADMIN_PASSWORD"
4743
} >>~/healthchecks.creds
48-
msg_ok "Set up Database"
44+
msg_ok "Set up Keys"
4945

5046
fetch_and_deploy_gh_release "healthchecks" "healthchecks/healthchecks"
5147

52-
msg_info "Setup healthchecks"
48+
msg_info "Installing Healthchecks (venv)"
5349
cd /opt/healthchecks
54-
mkdir -p /opt/healthchecks/static-collected/
55-
$STD uv pip install wheel gunicorn -r requirements.txt --system
50+
python3 -m venv venv
51+
source venv/bin/activate
52+
53+
$STD pip install --upgrade pip wheel
54+
$STD pip install gunicorn -r requirements.txt
55+
msg_ok "Installed Python packages"
5656

5757
LOCAL_IP=$(hostname -I | awk '{print $1}')
5858
cat <<EOF >/opt/healthchecks/hc/local_settings.py
@@ -83,18 +83,19 @@ DATABASES = {
8383
}
8484
EOF
8585

86-
$STD uv run -- python manage.py makemigrations
87-
$STD uv run -- python manage.py migrate --noinput
88-
$STD uv run -- python manage.py collectstatic --noinput
89-
$STD uv run -- python manage.py compress
86+
msg_info "Running Django setup"
87+
$STD python manage.py makemigrations
88+
$STD python manage.py migrate --noinput
89+
$STD python manage.py collectstatic --noinput
90+
$STD python manage.py compress
9091

91-
cat <<EOF | $STD uv run -- python manage.py shell
92+
python <<EOF
9293
from django.contrib.auth import get_user_model
9394
User = get_user_model()
9495
if not User.objects.filter(email="${ADMIN_EMAIL}").exists():
9596
User.objects.create_superuser("${ADMIN_EMAIL}", "${ADMIN_EMAIL}", "${ADMIN_PASSWORD}")
9697
EOF
97-
msg_ok "Installed healthchecks"
98+
msg_ok "Configured Django"
9899

99100
msg_info "Configuring Caddy"
100101
cat <<EOF >/etc/caddy/Caddyfile
@@ -108,20 +109,21 @@ ${LOCAL_IP} {
108109
EOF
109110
msg_ok "Configured Caddy"
110111

111-
msg_info "Creating Service"
112+
msg_info "Creating systemd service"
112113
cat <<EOF >/etc/systemd/system/healthchecks.service
113114
[Unit]
114115
Description=Healthchecks Service
115116
After=network.target postgresql.service
116117
117118
[Service]
118119
WorkingDirectory=/opt/healthchecks/
119-
ExecStart=/usr/local/bin/uv run -- gunicorn hc.wsgi:application --bind 127.0.0.1:8000
120+
ExecStart=/opt/healthchecks/venv/bin/gunicorn hc.wsgi:application --bind 127.0.0.1:8000
120121
Restart=always
121122
122123
[Install]
123124
WantedBy=multi-user.target
124125
EOF
126+
125127
systemctl enable -q --now healthchecks caddy
126128
systemctl reload caddy
127129
msg_ok "Created Service"

0 commit comments

Comments
 (0)