diff --git a/supplemental/scripts/install-agent.sh b/supplemental/scripts/install-agent.sh index 704696d..d4496ce 100755 --- a/supplemental/scripts/install-agent.sh +++ b/supplemental/scripts/install-agent.sh @@ -1,4 +1,17 @@ #!/bin/bash + +# Check if running as root +if [ "$(id -u)" != "0" ]; then + if command -v sudo >/dev/null 2>&1; then + exec sudo "$0" "$@" + else + echo "This script must be run as root. Please either:" + echo "1. Run this script as root (su root)" + echo "2. Install sudo and run with sudo" + exit 1 + fi +fi + version=0.0.1 # Define default values PORT=45876 @@ -24,24 +37,24 @@ done if [ "$UNINSTALL" = "true" ]; then # Stop and disable the Beszel Agent service echo "Stopping and disabling the Beszel Agent service..." - sudo systemctl stop beszel-agent.service - sudo systemctl disable beszel-agent.service + systemctl stop beszel-agent.service + systemctl disable beszel-agent.service # Remove the systemd service file echo "Removing the systemd service file..." - sudo rm /etc/systemd/system/beszel-agent.service + rm /etc/systemd/system/beszel-agent.service # Reload the systemd daemon echo "Reloading the systemd daemon..." - sudo systemctl daemon-reload + systemctl daemon-reload # Remove the Beszel Agent directory echo "Removing the Beszel Agent directory..." - sudo rm -rf /opt/beszel-agent + rm -rf /opt/beszel-agent # Remove the dedicated user for the Beszel Agent service echo "Removing the dedicated user for the Beszel Agent service..." - sudo userdel beszel + userdel beszel echo "The Beszel Agent has been uninstalled successfully!" else @@ -53,16 +66,16 @@ else # Check for package manager and install necessary packages if not installed if package_installed apt-get; then if ! package_installed tar || ! package_installed curl; then - sudo apt-get update - sudo apt-get install -y tar curl + apt-get update + apt-get install -y tar curl fi elif package_installed yum; then if ! package_installed tar || ! package_installed curl; then - sudo yum install -y tar curl + yum install -y tar curl fi elif package_installed pacman; then if ! package_installed tar || ! package_installed curl; then - sudo pacman -Sy --noconfirm tar curl + pacman -Sy --noconfirm tar curl fi else echo "Warning: Please ensure 'tar' and 'curl' are installed." @@ -76,29 +89,29 @@ else # 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..." - sudo useradd -M -s /bin/false beszel + useradd -M -s /bin/false beszel fi # Add the user to the docker group to allow access to the Docker socket - sudo usermod -aG docker beszel + usermod -aG docker beszel # Create the directory for the Beszel Agent if [ ! -d "/opt/beszel-agent" ]; then echo "Creating the directory for the Beszel Agent..." - sudo mkdir -p /opt/beszel-agent - sudo chown beszel:beszel /opt/beszel-agent - sudo chmod 755 /opt/beszel-agent + mkdir -p /opt/beszel-agent + chown beszel:beszel /opt/beszel-agent + chmod 755 /opt/beszel-agent fi # Download and install the Beszel Agent echo "Downloading and installing the Beszel Agent..." curl -sL "https://github.com/henrygd/beszel/releases/latest/download/beszel-agent_$(uname -s)_$(uname -m | sed 's/x86_64/amd64/' | sed 's/armv7l/arm/' | sed 's/aarch64/arm64/').tar.gz" | tar -xz -O beszel-agent | tee ./beszel-agent >/dev/null - sudo mv ./beszel-agent /opt/beszel-agent/beszel-agent - sudo chown beszel:beszel /opt/beszel-agent/beszel-agent - sudo chmod 755 /opt/beszel-agent/beszel-agent + mv ./beszel-agent /opt/beszel-agent/beszel-agent + chown beszel:beszel /opt/beszel-agent/beszel-agent + chmod 755 /opt/beszel-agent/beszel-agent # Create the systemd service echo "Creating the systemd service for the Beszel Agent..." - sudo tee /etc/systemd/system/beszel-agent.service </dev/null 2>&1; then + exec sudo "$0" "$@" + else + echo "This script must be run as root. Please either:" + echo "1. Run this script as root (su root)" + echo "2. Install sudo and run with sudo" + exit 1 + fi +fi + version=0.0.1 PORT=8090 # Default port @@ -21,70 +34,68 @@ done if [ "$UNINSTALL" = "true" ]; then # Stop and disable the Beszel Hub service echo "Stopping and disabling the Beszel Hub service..." - sudo systemctl stop beszel-hub.service - sudo systemctl disable beszel-hub.service + systemctl stop beszel-hub.service + systemctl disable beszel-hub.service # Remove the systemd service file echo "Removing the systemd service file..." - sudo rm /etc/systemd/system/beszel-hub.service + rm /etc/systemd/system/beszel-hub.service # Reload the systemd daemon echo "Reloading the systemd daemon..." - sudo systemctl daemon-reload + systemctl daemon-reload - # Remove the Beszel Hub binary - echo "Removing the Beszel Hub binary..." - sudo rm /opt/beszel/beszel - - # Remove the Beszel Hub directory - echo "Removing the Beszel Hub directory..." - sudo rm -rf /opt/beszel + # Remove the Beszel Hub binary and data + echo "Removing the Beszel Hub binary and data..." + rm -rf /opt/beszel # Remove the dedicated user echo "Removing the dedicated user..." - sudo userdel beszel + userdel beszel echo "The Beszel Hub has been uninstalled successfully!" + exit 0 +fi + +# Function to check if a package is installed +package_installed() { + command -v "$1" >/dev/null 2>&1 +} + +# Check for package manager and install necessary packages if not installed +if package_installed apt-get; then + if ! package_installed tar || ! package_installed curl; then + apt-get update + apt-get install -y tar curl + fi +elif package_installed yum; then + if ! package_installed tar || ! package_installed curl; then + yum install -y tar curl + fi +elif package_installed pacman; then + if ! package_installed tar || ! package_installed curl; then + pacman -Sy --noconfirm tar curl + fi else - # Function to check if a package is installed - package_installed() { - command -v "$1" >/dev/null 2>&1 - } + echo "Warning: Please ensure 'tar' and 'curl' are installed." +fi - # Check for package manager and install necessary packages if not installed - if package_installed apt-get; then - if ! package_installed tar || ! package_installed curl; then - sudo apt-get update - sudo apt-get install -y tar curl - fi - elif package_installed yum; then - if ! package_installed tar || ! package_installed curl; then - sudo yum install -y tar curl - fi - elif package_installed pacman; then - if ! package_installed tar || ! package_installed curl; then - sudo pacman -Sy --noconfirm tar curl - fi - else - echo "Warning: Please ensure 'tar' and 'curl' are installed." - 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 Hub service..." + useradd -M -s /bin/false beszel +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 Hub service..." - sudo useradd -M -s /bin/false beszel - fi +# Download and install the Beszel Hub +echo "Downloading and installing the Beszel Hub..." +curl -sL "https://github.com/henrygd/beszel/releases/latest/download/beszel_$(uname -s)_$(uname -m | sed 's/x86_64/amd64/' | sed 's/armv7l/arm/' | sed 's/aarch64/arm64/').tar.gz" | tar -xz -O beszel | tee ./beszel >/dev/null && chmod +x beszel +mkdir -p /opt/beszel/beszel_data +mv ./beszel /opt/beszel/beszel +chown -R beszel:beszel /opt/beszel - # Download and install the Beszel Hub - echo "Downloading and installing the Beszel Hub..." - curl -sL "https://github.com/henrygd/beszel/releases/latest/download/beszel_$(uname -s)_$(uname -m | sed 's/x86_64/amd64/' | sed 's/armv7l/arm/' | sed 's/aarch64/arm64/').tar.gz" | tar -xz -O beszel | tee ./beszel >/dev/null && chmod +x beszel - sudo mkdir -p /opt/beszel/beszel_data - sudo mv ./beszel /opt/beszel/beszel - sudo chown -R beszel:beszel /opt/beszel - - # Create the systemd service - printf "Creating the systemd service for the Beszel Hub...\n\n" - sudo tee /etc/systemd/system/beszel-hub.service <