diff --git a/supplemental/scripts/install-agent.sh b/supplemental/scripts/install-agent.sh index 15ee9fed..86088dda 100755 --- a/supplemental/scripts/install-agent.sh +++ b/supplemental/scripts/install-agent.sh @@ -373,6 +373,20 @@ else BIN_PATH="/opt/beszel-agent/beszel-agent" fi +# Stop existing service if it exists (for upgrades) +if [ -f "$BIN_PATH" ]; then + echo "Existing installation detected. Stopping service for upgrade..." + if is_alpine; then + rc-service beszel-agent stop 2>/dev/null || true + elif is_openwrt; then + /etc/init.d/beszel-agent stop 2>/dev/null || true + elif is_freebsd; then + service beszel-agent stop 2>/dev/null || true + else + systemctl stop beszel-agent.service 2>/dev/null || true + fi +fi + # Uninstall process if [ "$UNINSTALL" = true ]; then # Clean up SELinux contexts before removing files @@ -507,10 +521,14 @@ else echo "Warning: Please ensure 'tar' and 'curl' and 'sha256sum (coreutils)' are installed." fi -# If no SSH key is provided, ask for the SSH key interactively +# If no SSH key is provided, ask for the SSH key interactively (skip if upgrading) if [ -z "$KEY" ]; then - printf "Enter your SSH key: " - read KEY + if [ -f "$BIN_PATH" ]; then + echo "Upgrading existing installation. Using existing service configuration." + else + printf "Enter your SSH key: " + read KEY + fi fi # Remove newlines from KEY @@ -667,6 +685,11 @@ if ! tar -xzf "$FILE_NAME" beszel-agent; then exit 1 fi +if [ -f "$BIN_PATH" ]; then + echo "Backing up existing binary..." + cp "$BIN_PATH" "$BIN_PATH.bak" +fi + mv beszel-agent "$BIN_PATH" chown beszel:beszel "$BIN_PATH" chmod 755 "$BIN_PATH" @@ -695,8 +718,9 @@ detect_nvidia_devices() { # Modify service installation part, add Alpine check before systemd service creation if is_alpine; then - echo "Creating OpenRC service for Alpine Linux..." - cat >/etc/init.d/beszel-agent </etc/init.d/beszel-agent </etc/init.d/beszel-agent </etc/init.d/beszel-agent <"$AGENT_DIR/env" <"$AGENT_DIR/env" < /usr/local/etc/rc.d/beszel-agent + # Create the rc service file if it doesn't exist + if [ ! -f /usr/local/etc/rc.d/beszel-agent ]; then + echo "Creating FreeBSD rc service..." + generate_freebsd_rc_service > /usr/local/etc/rc.d/beszel-agent + # Set proper permissions for the rc script + chmod 755 /usr/local/etc/rc.d/beszel-agent + else + echo "FreeBSD rc service file already exists. Skipping creation." + fi - # Set proper permissions for the rc script - chmod 755 /usr/local/etc/rc.d/beszel-agent - # Enable and start the service echo "Enabling and starting the agent service..." sysrc beszel_agent_enable="YES" @@ -905,12 +942,13 @@ EOF else # Original systemd service installation code - echo "Creating the systemd service for the agent..." + if [ ! -f /etc/systemd/system/beszel-agent.service ]; then + echo "Creating the systemd service for the agent..." - # Detect NVIDIA devices and grant device permissions - NVIDIA_DEVICES=$(detect_nvidia_devices) + # Detect NVIDIA devices and grant device permissions + NVIDIA_DEVICES=$(detect_nvidia_devices) - cat >/etc/systemd/system/beszel-agent.service </etc/systemd/system/beszel-agent.service <