update hub install script to support freebsd

This commit is contained in:
henrygd
2025-12-09 15:08:59 -05:00
parent c8d4f7427d
commit 2a8796c38d

View File

@@ -1,22 +1,8 @@
#!/bin/bash #!/bin/sh
# Check if running as root is_freebsd() {
if [ "$(id -u)" != "0" ]; then [ "$(uname -s)" = "FreeBSD" ]
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
# Define default values
version=0.0.1
PORT=8090 # Default port
GITHUB_PROXY_URL="https://ghfast.top/" # Default proxy URL
AUTO_UPDATE_FLAG="false" # default to no auto-updates, "true" means enable
# Function to ensure the proxy URL ends with a / # Function to ensure the proxy URL ends with a /
ensure_trailing_slash() { ensure_trailing_slash() {
@@ -30,14 +16,155 @@ ensure_trailing_slash() {
fi fi
} }
# Ensure the proxy URL ends with a / # Generate FreeBSD rc service content
GITHUB_PROXY_URL=$(ensure_trailing_slash "$GITHUB_PROXY_URL") generate_freebsd_rc_service() {
cat <<'EOF'
#!/bin/sh
# PROVIDE: beszel_hub
# REQUIRE: DAEMON NETWORKING
# BEFORE: LOGIN
# KEYWORD: shutdown
# Add the following lines to /etc/rc.conf to configure Beszel Hub:
#
# beszel_hub_enable (bool): Set to YES to enable Beszel Hub
# Default: YES
# beszel_hub_port (str): Port to listen on
# Default: 8090
# beszel_hub_user (str): Beszel Hub daemon user
# Default: beszel
# beszel_hub_bin (str): Path to the beszel binary
# Default: /usr/local/sbin/beszel
# beszel_hub_data (str): Path to the beszel data directory
# Default: /usr/local/etc/beszel/beszel_data
# beszel_hub_flags (str): Extra flags passed to beszel command invocation
# Default:
. /etc/rc.subr
name="beszel_hub"
rcvar=beszel_hub_enable
load_rc_config $name
: ${beszel_hub_enable:="YES"}
: ${beszel_hub_port:="8090"}
: ${beszel_hub_user:="beszel"}
: ${beszel_hub_flags:=""}
: ${beszel_hub_bin:="/usr/local/sbin/beszel"}
: ${beszel_hub_data:="/usr/local/etc/beszel/beszel_data"}
logfile="/var/log/${name}.log"
pidfile="/var/run/${name}.pid"
procname="/usr/sbin/daemon"
start_precmd="${name}_prestart"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
extra_commands="upgrade"
upgrade_cmd="beszel_hub_upgrade"
beszel_hub_prestart()
{
if [ ! -d "${beszel_hub_data}" ]; then
echo "Creating data directory ${beszel_hub_data}"
mkdir -p "${beszel_hub_data}"
chown "${beszel_hub_user}:${beszel_hub_user}" "${beszel_hub_data}"
fi
}
beszel_hub_start()
{
echo "Starting ${name}"
cd "$(dirname "${beszel_hub_data}")" || exit 1
/usr/sbin/daemon -f \
-P "${pidfile}" \
-o "${logfile}" \
-u "${beszel_hub_user}" \
"${beszel_hub_bin}" serve --http "0.0.0.0:${beszel_hub_port}" ${beszel_hub_flags}
}
beszel_hub_stop()
{
pid="$(check_pidfile "${pidfile}" "${procname}")"
if [ -n "${pid}" ]; then
echo "Stopping ${name} (pid=${pid})"
kill -- "-${pid}"
wait_for_pids "${pid}"
else
echo "${name} isn't running"
fi
}
beszel_hub_upgrade()
{
echo "Upgrading ${name}"
if command -v sudo >/dev/null; then
sudo -u "${beszel_hub_user}" -- "${beszel_hub_bin}" update
else
su -m "${beszel_hub_user}" -c "${beszel_hub_bin} update"
fi
}
run_rc_command "$1"
EOF
}
# Detect system architecture
detect_architecture() {
arch=$(uname -m)
case "$arch" in
x86_64)
arch="amd64"
;;
armv7l)
arch="arm"
;;
aarch64)
arch="arm64"
;;
esac
echo "$arch"
}
# Build sudo args by properly quoting everything
build_sudo_args() {
QUOTED_ARGS=""
while [ $# -gt 0 ]; do
if [ -n "$QUOTED_ARGS" ]; then
QUOTED_ARGS="$QUOTED_ARGS "
fi
QUOTED_ARGS="$QUOTED_ARGS'$(echo "$1" | sed "s/'/'\\\\''/g")'"
shift
done
echo "$QUOTED_ARGS"
}
# Check if running as root and re-execute with sudo if needed
if [ "$(id -u)" != "0" ]; then
if command -v sudo >/dev/null 2>&1; then
SUDO_ARGS=$(build_sudo_args "$@")
eval "exec sudo $0 $SUDO_ARGS"
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
# Define default values
PORT=8090
GITHUB_PROXY_URL="https://ghfast.top/"
AUTO_UPDATE_FLAG="false"
UNINSTALL=false
# Parse command line arguments # Parse command line arguments
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "$1" in case "$1" in
-u) -u)
UNINSTALL="true" UNINSTALL=true
shift shift
;; ;;
-h|--help) -h|--help)
@@ -72,7 +199,44 @@ while [ $# -gt 0 ]; do
esac esac
done done
if [ "$UNINSTALL" = "true" ]; then # Ensure the proxy URL ends with a /
GITHUB_PROXY_URL=$(ensure_trailing_slash "$GITHUB_PROXY_URL")
# Set paths based on operating system
if is_freebsd; then
HUB_DIR="/usr/local/etc/beszel"
BIN_PATH="/usr/local/sbin/beszel"
else
HUB_DIR="/opt/beszel"
BIN_PATH="/opt/beszel/beszel"
fi
# Uninstall process
if [ "$UNINSTALL" = true ]; then
if is_freebsd; then
echo "Stopping and disabling the Beszel Hub service..."
service beszel-hub stop 2>/dev/null
sysrc beszel_hub_enable="NO" 2>/dev/null
echo "Removing the FreeBSD service files..."
rm -f /usr/local/etc/rc.d/beszel-hub
echo "Removing the daily update cron job..."
rm -f /etc/cron.d/beszel-hub
echo "Removing log files..."
rm -f /var/log/beszel_hub.log
echo "Removing the Beszel Hub binary and data..."
rm -f "$BIN_PATH"
rm -rf "$HUB_DIR"
echo "Removing the dedicated user..."
pw user del beszel 2>/dev/null
echo "The Beszel Hub has been uninstalled successfully!"
exit 0
else
# 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..."
systemctl stop beszel-hub.service systemctl stop beszel-hub.service
@@ -95,7 +259,7 @@ if [ "$UNINSTALL" = "true" ]; then
# Remove the Beszel Hub binary and data # Remove the Beszel Hub binary and data
echo "Removing the Beszel Hub binary and data..." echo "Removing the Beszel Hub binary and data..."
rm -rf /opt/beszel rm -rf "$HUB_DIR"
# Remove the dedicated user # Remove the dedicated user
echo "Removing the dedicated user..." echo "Removing the dedicated user..."
@@ -103,6 +267,7 @@ if [ "$UNINSTALL" = "true" ]; then
echo "The Beszel Hub has been uninstalled successfully!" echo "The Beszel Hub has been uninstalled successfully!"
exit 0 exit 0
fi
fi fi
# Function to check if a package is installed # Function to check if a package is installed
@@ -111,7 +276,12 @@ package_installed() {
} }
# 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 pkg && is_freebsd; then
if ! package_installed tar || ! package_installed curl; then
pkg update
pkg install -y gtar curl
fi
elif package_installed apt-get; then
if ! package_installed tar || ! package_installed curl; then if ! package_installed tar || ! package_installed curl; then
apt-get update apt-get update
apt-get install -y tar curl apt-get install -y tar curl
@@ -129,28 +299,91 @@ else
fi fi
# 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 echo "Creating a dedicated user for the Beszel Hub service..."
echo "Creating a dedicated user for the Beszel Hub service..." if is_freebsd; then
if ! id -u beszel >/dev/null 2>&1; then
pw user add beszel -d /nonexistent -s /usr/sbin/nologin -c "beszel user"
fi
else
if ! id -u beszel >/dev/null 2>&1; then
useradd -M -s /bin/false beszel useradd -M -s /bin/false beszel
fi
fi fi
# Create the directory for the Beszel Hub
echo "Creating the directory for the Beszel Hub..."
mkdir -p "$HUB_DIR/beszel_data"
chown -R beszel:beszel "$HUB_DIR"
chmod 755 "$HUB_DIR"
# Download and install the Beszel Hub # Download and install the Beszel Hub
echo "Downloading and installing the Beszel Hub..." echo "Downloading and installing the Beszel Hub..."
curl -sL "${GITHUB_PROXY_URL}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
# Create the systemd service OS=$(uname -s | tr '[:upper:]' '[:lower:]')
printf "Creating the systemd service for the Beszel Hub...\n\n" ARCH=$(detect_architecture)
tee /etc/systemd/system/beszel-hub.service <<EOF FILE_NAME="beszel_${OS}_${ARCH}.tar.gz"
curl -sL "${GITHUB_PROXY_URL}https://github.com/henrygd/beszel/releases/latest/download/$FILE_NAME" | tar -xz -O beszel | tee ./beszel >/dev/null
chmod +x ./beszel
mv ./beszel "$BIN_PATH"
chown beszel:beszel "$BIN_PATH"
if is_freebsd; then
echo "Creating FreeBSD rc service..."
# Create the rc service file
generate_freebsd_rc_service > /usr/local/etc/rc.d/beszel-hub
# Set proper permissions for the rc script
chmod 755 /usr/local/etc/rc.d/beszel-hub
# Configure the port
sysrc beszel_hub_port="$PORT"
# Enable and start the service
echo "Enabling and starting the Beszel Hub service..."
sysrc beszel_hub_enable="YES"
service beszel-hub restart
# Check if service started successfully
sleep 2
if ! service beszel-hub status | grep -q "is running"; then
echo "Error: The Beszel Hub service failed to start. Checking logs..."
tail -n 20 /var/log/beszel_hub.log
exit 1
fi
# Auto-update service for FreeBSD
if [ "$AUTO_UPDATE_FLAG" = "true" ]; then
echo "Setting up daily automatic updates for beszel-hub..."
# Create cron job in /etc/cron.d
cat >/etc/cron.d/beszel-hub <<EOF
# Beszel Hub daily update job
12 8 * * * root $BIN_PATH update >/dev/null 2>&1
EOF
chmod 644 /etc/cron.d/beszel-hub
printf "\nDaily updates have been enabled via /etc/cron.d.\n"
fi
# Check service status
if ! service beszel-hub status >/dev/null 2>&1; then
echo "Error: The Beszel Hub service is not running."
service beszel-hub status
exit 1
fi
else
# Original systemd service installation code
printf "Creating the systemd service for the Beszel Hub...\n\n"
tee /etc/systemd/system/beszel-hub.service <<EOF
[Unit] [Unit]
Description=Beszel Hub Service Description=Beszel Hub Service
After=network.target After=network.target
[Service] [Service]
ExecStart=/opt/beszel/beszel serve --http "0.0.0.0:$PORT" ExecStart=$BIN_PATH serve --http "0.0.0.0:$PORT"
WorkingDirectory=/opt/beszel WorkingDirectory=$HUB_DIR
User=beszel User=beszel
Restart=always Restart=always
RestartSec=5 RestartSec=5
@@ -159,24 +392,24 @@ RestartSec=5
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"
systemctl daemon-reload systemctl daemon-reload
systemctl enable beszel-hub.service systemctl enable beszel-hub.service
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 fi
# Enable auto-update if flag is set to true # Enable auto-update if flag is set to true
if [ "$AUTO_UPDATE_FLAG" = "true" ]; then if [ "$AUTO_UPDATE_FLAG" = "true" ]; then
echo "Setting up daily automatic updates for beszel-hub..." echo "Setting up daily automatic updates for beszel-hub..."
# Create systemd service for the daily update # Create systemd service for the daily update
@@ -187,7 +420,7 @@ Wants=beszel-hub.service
[Service] [Service]
Type=oneshot Type=oneshot
ExecStart=/opt/beszel/beszel update ExecStart=$BIN_PATH update
EOF EOF
# Create systemd timer for the daily update # Create systemd timer for the daily update
@@ -208,6 +441,7 @@ EOF
systemctl enable --now beszel-hub-update.timer systemctl enable --now beszel-hub-update.timer
printf "\nDaily updates have been enabled.\n" printf "\nDaily updates have been enabled.\n"
fi
fi fi
echo "The Beszel Hub has been installed and configured successfully! It is now accessible on port $PORT." echo "The Beszel Hub has been installed and configured successfully! It is now accessible on port $PORT."