diff --git a/hblink3-docker-install.sh b/hblink3-docker-install.sh index 5b5ba01..8e871f1 100755 --- a/hblink3-docker-install.sh +++ b/hblink3-docker-install.sh @@ -76,7 +76,12 @@ install_docker_and_dependencies() { # Install base dependencies apt-get update - apt-get install -y $DEP + # 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 sleep 2 # Remove old Docker versions if present @@ -234,20 +239,18 @@ sleep 2 pip_install() { local args="$@" if [ $VERSION -ge 12 ]; then - # For Debian 12+, try with --break-system-packages flag first + # For Debian 12+, use virtual environment (PEP 668 compliant) echo "Installing Python packages for Debian $VERSION: $args" - if pip3 install --break-system-packages $args; then + 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 "Warning: Installation with --break-system-packages failed, trying standard installation..." - 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 else # For Debian 10-11, use standard pip installation @@ -265,6 +268,39 @@ pip_install() { 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..." + # Install python3-venv if not already installed + if ! dpkg -l | grep -q python3-venv; then + echo "Installing python3-venv package..." + apt-get install -y python3-venv + fi + + # Create virtual environment + if [ ! -d "$HBMONDIR/venv" ]; then + python3 -m venv "$HBMONDIR/venv" + if [ $? -ne 0 ]; then + echo "ERROR: Failed to create virtual environment" + exit 1 + fi + 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" + if [ $? -ne 0 ]; then + echo "ERROR: Failed to activate virtual environment" + exit 1 + fi + echo "Virtual environment activated" + + # Upgrade pip in the virtual environment + pip3 install --upgrade pip +fi + # Install setuptools and wheel first if ! pip_install setuptools wheel; then echo "ERROR: Failed to install setuptools and wheel" @@ -343,6 +379,18 @@ 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 + 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 + # Also handle cases where the path might be relative + sed -i "s|ExecStart=\([^/]\)|ExecStart=$HBMONDIR/venv/bin/python3 \1|g" /lib/systemd/system/hbmon.service + echo "Service file updated to use virtual environment" + fi + cp utils/lastheard /etc/cron.daily/ chmod +x /etc/cron.daily/lastheard echo ""