update systemd install scripts to work if sudo not installed

This commit is contained in:
Henry Dollman
2024-10-27 13:58:12 -04:00
parent f8fc74116c
commit ec7cb53d93
2 changed files with 108 additions and 85 deletions

View File

@@ -1,4 +1,17 @@
#!/bin/bash #!/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 version=0.0.1
# Define default values # Define default values
PORT=45876 PORT=45876
@@ -24,24 +37,24 @@ done
if [ "$UNINSTALL" = "true" ]; then if [ "$UNINSTALL" = "true" ]; then
# Stop and disable the Beszel Agent service # Stop and disable the Beszel Agent service
echo "Stopping and disabling the Beszel Agent service..." echo "Stopping and disabling the Beszel Agent service..."
sudo systemctl stop beszel-agent.service systemctl stop beszel-agent.service
sudo systemctl disable beszel-agent.service systemctl disable beszel-agent.service
# Remove the systemd service file # Remove the systemd service file
echo "Removing 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 # Reload the systemd daemon
echo "Reloading the systemd daemon..." echo "Reloading the systemd daemon..."
sudo systemctl daemon-reload systemctl daemon-reload
# Remove the Beszel Agent directory # Remove the Beszel Agent directory
echo "Removing 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 # Remove the dedicated user for the Beszel Agent service
echo "Removing 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!" echo "The Beszel Agent has been uninstalled successfully!"
else else
@@ -53,16 +66,16 @@ else
# Check for package manager and install necessary packages if not installed # Check for package manager and install necessary packages if not installed
if package_installed apt-get; then if package_installed apt-get; then
if ! package_installed tar || ! package_installed curl; then if ! package_installed tar || ! package_installed curl; then
sudo apt-get update apt-get update
sudo apt-get install -y tar curl apt-get install -y tar curl
fi fi
elif package_installed yum; then elif package_installed yum; then
if ! package_installed tar || ! package_installed curl; then if ! package_installed tar || ! package_installed curl; then
sudo yum install -y tar curl yum install -y tar curl
fi fi
elif package_installed pacman; then elif package_installed pacman; then
if ! package_installed tar || ! package_installed curl; then if ! package_installed tar || ! package_installed curl; then
sudo pacman -Sy --noconfirm tar curl pacman -Sy --noconfirm tar curl
fi fi
else else
echo "Warning: Please ensure 'tar' and 'curl' are installed." 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 # Create a dedicated user for the service if it doesn't exist
if ! id -u beszel > /dev/null 2>&1; then if ! id -u beszel > /dev/null 2>&1; then
echo "Creating a dedicated user for the Beszel Agent service..." echo "Creating a dedicated user for the Beszel Agent service..."
sudo useradd -M -s /bin/false beszel useradd -M -s /bin/false beszel
fi fi
# Add the user to the docker group to allow access to the Docker socket # 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 # Create the directory for the Beszel Agent
if [ ! -d "/opt/beszel-agent" ]; then if [ ! -d "/opt/beszel-agent" ]; then
echo "Creating the directory for the Beszel Agent..." echo "Creating the directory for the Beszel Agent..."
sudo mkdir -p /opt/beszel-agent mkdir -p /opt/beszel-agent
sudo chown beszel:beszel /opt/beszel-agent chown beszel:beszel /opt/beszel-agent
sudo chmod 755 /opt/beszel-agent chmod 755 /opt/beszel-agent
fi fi
# Download and install the Beszel Agent # Download and install the Beszel Agent
echo "Downloading and installing 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 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 mv ./beszel-agent /opt/beszel-agent/beszel-agent
sudo chown beszel:beszel /opt/beszel-agent/beszel-agent chown beszel:beszel /opt/beszel-agent/beszel-agent
sudo chmod 755 /opt/beszel-agent/beszel-agent chmod 755 /opt/beszel-agent/beszel-agent
# Create the systemd service # Create the systemd service
echo "Creating the systemd service for the Beszel Agent..." echo "Creating the systemd service for the Beszel Agent..."
sudo tee /etc/systemd/system/beszel-agent.service <<EOF tee /etc/systemd/system/beszel-agent.service <<EOF
[Unit] [Unit]
Description=Beszel Agent Service Description=Beszel Agent Service
After=network.target After=network.target
@@ -117,9 +130,9 @@ EOF
# Load and start the service # Load and start the service
printf "\nLoading and starting the Beszel Agent service...\n" printf "\nLoading and starting the Beszel Agent service...\n"
sudo systemctl daemon-reload systemctl daemon-reload
sudo systemctl enable beszel-agent.service systemctl enable beszel-agent.service
sudo systemctl start beszel-agent.service systemctl start beszel-agent.service
# Wait for the service to start or fail # Wait for the service to start or fail
sleep 1 sleep 1

View File

@@ -1,4 +1,17 @@
#!/bin/bash #!/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 version=0.0.1
PORT=8090 # Default port PORT=8090 # Default port
@@ -21,70 +34,68 @@ done
if [ "$UNINSTALL" = "true" ]; then if [ "$UNINSTALL" = "true" ]; then
# Stop and disable the Beszel Hub service # Stop and disable the Beszel Hub service
echo "Stopping and disabling the Beszel Hub service..." echo "Stopping and disabling the Beszel Hub service..."
sudo systemctl stop beszel-hub.service systemctl stop beszel-hub.service
sudo systemctl disable beszel-hub.service systemctl disable beszel-hub.service
# Remove the systemd service file # Remove the systemd service file
echo "Removing 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 # Reload the systemd daemon
echo "Reloading the systemd daemon..." echo "Reloading the systemd daemon..."
sudo systemctl daemon-reload systemctl daemon-reload
# Remove the Beszel Hub binary # Remove the Beszel Hub binary and data
echo "Removing the Beszel Hub binary..." echo "Removing the Beszel Hub binary and data..."
sudo rm /opt/beszel/beszel rm -rf /opt/beszel
# Remove the Beszel Hub directory
echo "Removing the Beszel Hub directory..."
sudo rm -rf /opt/beszel
# Remove the dedicated user # Remove the dedicated user
echo "Removing the dedicated user..." echo "Removing the dedicated user..."
sudo userdel beszel userdel beszel
echo "The Beszel Hub has been uninstalled successfully!" 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 else
# Function to check if a package is installed echo "Warning: Please ensure 'tar' and 'curl' are installed."
package_installed() { fi
command -v "$1" >/dev/null 2>&1
}
# Check for package manager and install necessary packages if not installed # Create a dedicated user for the service if it doesn't exist
if package_installed apt-get; then if ! id -u beszel > /dev/null 2>&1; then
if ! package_installed tar || ! package_installed curl; then echo "Creating a dedicated user for the Beszel Hub service..."
sudo apt-get update useradd -M -s /bin/false beszel
sudo apt-get install -y tar curl fi
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 # Download and install the Beszel Hub
if ! id -u beszel > /dev/null 2>&1; then echo "Downloading and installing the Beszel Hub..."
echo "Creating a dedicated user for the Beszel Hub service..." 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 useradd -M -s /bin/false beszel mkdir -p /opt/beszel/beszel_data
fi mv ./beszel /opt/beszel/beszel
chown -R beszel:beszel /opt/beszel
# Download and install the Beszel Hub # Create the systemd service
echo "Downloading and installing the Beszel Hub..." printf "Creating the systemd service for the Beszel Hub...\n\n"
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 tee /etc/systemd/system/beszel-hub.service <<EOF
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 <<EOF
[Unit] [Unit]
Description=Beszel Hub Service Description=Beszel Hub Service
After=network.target After=network.target
@@ -99,21 +110,20 @@ Restart=always
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
# Load and start the service # Load and start the service
printf "\nLoading and starting the Beszel Hub service...\n" printf "\nLoading and starting the Beszel Hub service...\n"
sudo systemctl daemon-reload systemctl daemon-reload
sudo systemctl enable beszel-hub.service systemctl enable beszel-hub.service
sudo systemctl start beszel-hub.service systemctl start beszel-hub.service
# Wait for the service to start or fail # Wait for the service to start or fail
sleep 2 sleep 2
# Check if the service is running # Check if the service is running
if [ "$(systemctl is-active beszel-hub.service)" != "active" ]; then if [ "$(systemctl is-active beszel-hub.service)" != "active" ]; then
echo "Error: The Beszel Hub service is not running." echo "Error: The Beszel Hub service is not running."
echo "$(systemctl status beszel-hub.service)" echo "$(systemctl status beszel-hub.service)"
exit 1 exit 1
fi
echo "The Beszel Hub has been installed and configured successfully! It is now accessible on port $PORT."
fi fi
echo "The Beszel Hub has been installed and configured successfully! It is now accessible on port $PORT."