From 9c8528bae14f352d98837e817c6ba793aa6f523a Mon Sep 17 00:00:00 2001 From: Arsierl <139133260+Arsierl@users.noreply.github.com> Date: Thu, 19 Dec 2024 06:41:15 +0800 Subject: [PATCH] Update install-agent.sh script to support Alpine (OpenRC) (#320) * Partial amendment * Update install-agent.sh * Update install-agent.sh Fix if is_alpine function with user creation and permission issues * Update install-agent.sh Adding an Uninstallation Process to Alpine Using deluser instead of userdel in Alpine --- supplemental/scripts/install-agent.sh | 228 ++++++++++++++++++++------ 1 file changed, 180 insertions(+), 48 deletions(-) diff --git a/supplemental/scripts/install-agent.sh b/supplemental/scripts/install-agent.sh index 82424b4..2bb77f5 100755 --- a/supplemental/scripts/install-agent.sh +++ b/supplemental/scripts/install-agent.sh @@ -1,5 +1,10 @@ #!/bin/sh +# Move is_alpine function to the top of the file +is_alpine() { + [ -f /etc/alpine-release ] +} + version=0.0.1 # Define default values PORT=45876 @@ -77,28 +82,52 @@ done # Uninstall process if [ "$UNINSTALL" = true ]; then - echo "Stopping and disabling the agent service..." - systemctl stop beszel-agent.service - systemctl disable beszel-agent.service + if is_alpine; then + echo "Stopping and disabling the agent service..." + rc-service beszel-agent stop + rc-update del beszel-agent default - echo "Removing the systemd service file..." - rm /etc/systemd/system/beszel-agent.service + echo "Removing the OpenRC service files..." + rm -f /etc/init.d/beszel-agent - # Remove the update timer and service if they exist - echo "Removing the daily update service and timer..." - systemctl stop beszel-agent-update.timer 2>/dev/null - systemctl disable beszel-agent-update.timer 2>/dev/null - rm -f /etc/systemd/system/beszel-agent-update.service - rm -f /etc/systemd/system/beszel-agent-update.timer + # Remove the update service if it exists + echo "Removing the daily update service..." + rc-service beszel-agent-update stop 2>/dev/null + rc-update del beszel-agent-update default 2>/dev/null + rm -f /etc/init.d/beszel-agent-update - systemctl daemon-reload + # Remove log files + echo "Removing log files..." + rm -f /var/log/beszel-agent.log /var/log/beszel-agent.err + + else + echo "Stopping and disabling the agent service..." + systemctl stop beszel-agent.service + systemctl disable beszel-agent.service + + echo "Removing the systemd service file..." + rm /etc/systemd/system/beszel-agent.service + + # Remove the update timer and service if they exist + echo "Removing the daily update service and timer..." + systemctl stop beszel-agent-update.timer 2>/dev/null + systemctl disable beszel-agent-update.timer 2>/dev/null + rm -f /etc/systemd/system/beszel-agent-update.service + rm -f /etc/systemd/system/beszel-agent-update.timer + + systemctl daemon-reload + fi echo "Removing the Beszel Agent directory..." rm -rf /opt/beszel-agent echo "Removing the dedicated user for the agent service..." - killall beszel-agent - userdel beszel + killall beszel-agent 2>/dev/null + if is_alpine; then + deluser beszel 2>/dev/null + else + userdel beszel 2>/dev/null + fi echo "Beszel Agent has been uninstalled successfully!" exit 0 @@ -124,7 +153,12 @@ package_installed() { } # Check for package manager and install necessary packages if not installed -if package_installed apt-get; then +if is_alpine; then + if ! package_installed tar || ! package_installed curl || ! package_installed coreutils; then + apk update + apk add tar curl coreutils shadow + fi +elif package_installed apt-get; then if ! package_installed tar || ! package_installed curl || ! package_installed sha256sum; then apt-get update apt-get install -y tar curl coreutils @@ -158,12 +192,21 @@ else fi # Create a dedicated user for the service if it doesn't exist -if ! id -u beszel >/dev/null 2>&1; then - echo "Creating a dedicated user for the Beszel Agent service..." - useradd -M -s /bin/false beszel +if is_alpine; then + if ! id -u beszel >/dev/null 2>&1; then + echo "Creating a dedicated user for the Beszel Agent service..." + adduser -D -H -s /sbin/nologin beszel + fi + # Add the user to the docker group to allow access to the Docker socket + addgroup beszel docker +else + if ! id -u beszel >/dev/null 2>&1; then + echo "Creating a dedicated user for the Beszel Agent service..." + useradd -M -s /bin/false beszel + fi + # Add the user to the docker group to allow access to the Docker socket + usermod -aG docker beszel fi -# Add the user to the docker group to allow access to the Docker socket -usermod -aG docker beszel # Create the directory for the Beszel Agent if [ ! -d "/opt/beszel-agent" ]; then @@ -221,9 +264,97 @@ chmod 755 /opt/beszel-agent/beszel-agent # Cleanup rm -rf "$TEMP_DIR" -# Create the systemd service -echo "Creating the systemd service for the agent..." -cat >/etc/systemd/system/beszel-agent.service < /etc/init.d/beszel-agent < /etc/init.d/beszel-agent-update </dev/null 2>&1; then + echo "Error: The Beszel Agent service is not running." + rc-service beszel-agent status + exit 1 + fi + +else + # Original systemd service installation code + echo "Creating the systemd service for the agent..." + cat >/etc/systemd/system/beszel-agent.service </etc/systemd/system/beszel-agent-update.service </etc/systemd/system/beszel-agent-update.service </etc/systemd/system/beszel-agent-update.timer </etc/systemd/system/beszel-agent-update.timer <