From 73cceaf4443a6ebaadd291911f6df55aa98c00d8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 14 Dec 2025 00:41:08 +0000 Subject: [PATCH] Simplify installer for modern approach - remove version-specific duplications Co-authored-by: ShaYmez <76499782+ShaYmez@users.noreply.github.com> --- README.md | 6 +- hblink3-docker-install.sh | 133 +++++++++++++++----------------------- 2 files changed, 55 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index 3282b74..5b8363c 100644 --- a/README.md +++ b/README.md @@ -146,15 +146,13 @@ Within this installation includes the new HBMonv2 by Weldek SP2ONG ### Technical Details - Python Package Management -**Debian 12+ (Bookworm/Trixie):** The installer uses modern Python package management following PEP 668 standards: +The installer uses modern Python package management following PEP 668 standards for all supported Debian versions: - HBMonv2 runs in an isolated Python virtual environment at `/opt/HBMonv2/venv` - All Python dependencies are installed within this virtual environment, avoiding system-wide package conflicts - The systemd service automatically uses the virtual environment's Python interpreter - This approach eliminates "externally-managed-environment" errors and conflicts with system packages -**Debian 11:** Standard pip installation to system Python is used for backward compatibility, as PEP 668 restrictions don't apply to Debian 11. - -This ensures clean, maintainable installations that follow modern Python best practices while maintaining compatibility with older Debian versions. +This ensures clean, maintainable installations that follow modern Python best practices across all Debian versions. ## Easy Installation And Upgrade The installation can be upgraded either by the use of a future scripts or by manually backing up your configuration and re-running the install script. Also the ability and really cool feature of docker-compose is that its easy to update the container with fresh images! Run by a simple command. Make sure you are in the /etc/hblink3 dir. diff --git a/hblink3-docker-install.sh b/hblink3-docker-install.sh index bda2f16..4b0eabb 100755 --- a/hblink3-docker-install.sh +++ b/hblink3-docker-install.sh @@ -53,17 +53,11 @@ LOCAL_IP=$(ip a | grep inet | grep "eth0\|en" | awk '{print $2}' | tr '/' ' ' | EXTERNAL_IP=$(curl -s --connect-timeout 5 https://ipecho.net/plain 2>/dev/null || echo "Unable to detect") ARC=$(lscpu | grep Arch | awk '{print $2}') VERSION=$(sed 's/\..*//' /etc/debian_version) -ARMv7l=https://get.docker.com | sh -ARMv8l=https://get.docker.com | sh -X32=https://get.docker.com | sh -X64=https://get.docker.com | sh INSDIR=/opt/tmp/ HBLINKTMP=/opt/tmp/hblink3 HBMONDIR=/opt/HBMonv2/ HBDIR=/etc/hblink3/ -DEP="wget curl git sudo python3 python3-dev python3-pip libffi-dev libssl-dev conntrack sed cargo apache2 php snapd figlet ca-certificates gnupg lsb-release" -DEP1="wget curl git sudo python3 python3-dev python3-pip libffi-dev libssl-dev conntrack sed cargo apache2 php snapd figlet ca-certificates gnupg lsb-release" -DEP2="wget sudo curl git python3 python3-dev python3-pip libffi-dev libssl-dev conntrack sed cargo apache2 php php-mysqli snapd figlet ca-certificates gnupg lsb-release" +DEP="wget curl git sudo python3 python3-dev python3-pip python3-venv libffi-dev libssl-dev conntrack sed cargo apache2 php snapd figlet ca-certificates gnupg lsb-release" HBGITREPO=https://github.com/ShaYmez/hblink3.git HBGITMONREPO=https://github.com/ShaYmez/HBMonv2.git echo "" @@ -75,14 +69,10 @@ install_docker_and_dependencies() { local version=$1 echo "Detected Debian version: $version" - # Install base dependencies + # Install base dependencies (python3-venv is included for all versions for consistency) + echo "Installing dependencies..." apt-get update - # For Debian 12+, add python3-venv to dependencies (PEP 668 compliance) - if [ $version -ge 12 ]; then - apt-get install -y $DEP python3-venv - else - apt-get install -y $DEP - fi + apt-get install -y $DEP sleep 2 # Remove old Docker versions if present @@ -262,64 +252,49 @@ echo "Installing HBMonv2 configuration....." echo "------------------------------------------------------------------------------" sleep 2 -# Helper function to install pip packages with Debian 12+ compatibility +# Helper function to install pip packages in virtual environment pip_install() { local args="$@" - if [ $VERSION -ge 12 ]; then - # For Debian 12+, use virtual environment (PEP 668 compliant) - echo "Installing Python packages for Debian $VERSION: $args" - if [ -z "$VIRTUAL_ENV" ]; then - echo "ERROR: Virtual environment not activated" - return 1 - fi - if pip3 install $args; then - echo "Successfully installed: $args" - return 0 - else - echo "ERROR: Failed to install: $args" - return 1 - fi + echo "Installing Python packages: $args" + if [ -z "$VIRTUAL_ENV" ]; then + echo "ERROR: Virtual environment not activated" + return 1 + fi + if pip3 install $args; then + echo "Successfully installed: $args" + return 0 else - # For Debian 11, use standard pip installation (pre-PEP 668) - echo "Installing Python packages for Debian $VERSION: $args" - if pip3 install $args; then - echo "Successfully installed: $args" - return 0 - else - echo "ERROR: Failed to install: $args" - return 1 - fi + echo "ERROR: Failed to install: $args" + return 1 fi } echo "Installing Python dependencies..." cd $HBMONDIR -# For Debian 12+, create and use a virtual environment (modern PEP 668 compliant approach) -if [ $VERSION -ge 12 ]; then - echo "Creating Python virtual environment for Debian $VERSION..." - - # Create virtual environment - if [ ! -d "$HBMONDIR/venv" ]; then - python3 -m venv "$HBMONDIR/venv" || { echo "ERROR: Failed to create virtual environment"; exit 1; } - echo "Virtual environment created successfully at $HBMONDIR/venv" - else - echo "Virtual environment already exists at $HBMONDIR/venv" - fi - - # Activate virtual environment - source "$HBMONDIR/venv/bin/activate" || { echo "ERROR: Failed to activate virtual environment"; exit 1; } - # Verify activation by checking VIRTUAL_ENV is set - if [ -z "$VIRTUAL_ENV" ]; then - echo "ERROR: Virtual environment activation failed - VIRTUAL_ENV not set" - exit 1 - fi - echo "Virtual environment activated" - - # Upgrade pip in the virtual environment - if ! pip3 install --upgrade pip; then - echo "WARNING: Failed to upgrade pip in virtual environment, continuing with existing version..." - fi +# Create and use a virtual environment (modern approach for all Debian versions) +echo "Creating Python virtual environment..." + +# Create virtual environment +if [ ! -d "$HBMONDIR/venv" ]; then + python3 -m venv "$HBMONDIR/venv" || { echo "ERROR: Failed to create virtual environment"; exit 1; } + echo "Virtual environment created successfully at $HBMONDIR/venv" +else + echo "Virtual environment already exists at $HBMONDIR/venv" +fi + +# Activate virtual environment +source "$HBMONDIR/venv/bin/activate" || { echo "ERROR: Failed to activate virtual environment"; exit 1; } +# Verify activation by checking VIRTUAL_ENV is set +if [ -z "$VIRTUAL_ENV" ]; then + echo "ERROR: Virtual environment activation failed - VIRTUAL_ENV not set" + exit 1 +fi +echo "Virtual environment activated" + +# Upgrade pip in the virtual environment +if ! pip3 install --upgrade pip; then + echo "WARNING: Failed to upgrade pip in virtual environment, continuing with existing version..." fi # Install setuptools and wheel first @@ -401,25 +376,23 @@ LOG_NAME = 'hbmon.log' EOF cp utils/hbmon.service /lib/systemd/system/ - # For Debian 12+, update the service file to use virtual environment - if [ $VERSION -ge 12 ]; then - echo "Updating hbmon.service to use virtual environment..." - # Update ExecStart to use venv Python (only if not already using venv) - if ! grep -q "$HBMONDIR/venv/bin/python3" /lib/systemd/system/hbmon.service; then - # Replace common Python interpreter paths with venv path - sed -i "s|ExecStart=/usr/bin/python3|ExecStart=$HBMONDIR/venv/bin/python3|g" /lib/systemd/system/hbmon.service - sed -i "s|ExecStart=python3 |ExecStart=$HBMONDIR/venv/bin/python3 |g" /lib/systemd/system/hbmon.service - - # Verify the service file was updated correctly - if grep -q "ExecStart=$HBMONDIR/venv/bin/python3" /lib/systemd/system/hbmon.service; then - echo "Service file updated to use virtual environment" - else - echo "WARNING: Service file update may not have completed correctly" - echo "Please manually verify /lib/systemd/system/hbmon.service uses $HBMONDIR/venv/bin/python3" - fi + # Update the service file to use virtual environment (for all Debian versions) + echo "Updating hbmon.service to use virtual environment..." + # Update ExecStart to use venv Python (only if not already using venv) + if ! grep -q "$HBMONDIR/venv/bin/python3" /lib/systemd/system/hbmon.service; then + # Replace common Python interpreter paths with venv path + sed -i "s|ExecStart=/usr/bin/python3|ExecStart=$HBMONDIR/venv/bin/python3|g" /lib/systemd/system/hbmon.service + sed -i "s|ExecStart=python3 |ExecStart=$HBMONDIR/venv/bin/python3 |g" /lib/systemd/system/hbmon.service + + # Verify the service file was updated correctly + if grep -q "ExecStart=$HBMONDIR/venv/bin/python3" /lib/systemd/system/hbmon.service; then + echo "Service file updated to use virtual environment" else - echo "Service file already configured to use virtual environment" + echo "WARNING: Service file update may not have completed correctly" + echo "Please manually verify /lib/systemd/system/hbmon.service uses $HBMONDIR/venv/bin/python3" fi + else + echo "Service file already configured to use virtual environment" fi cp utils/lastheard /etc/cron.daily/