refactor install-agent.sh for freebsd

- Updated paths for FreeBSD installation, changing default directories from /opt/beszel-agent to /usr/local/etc/beszel-agent and /usr/local/sbin.
- Introduced conditional path setting based on the operating system.
- Updated cron job creation to use /etc/cron.d
This commit is contained in:
henrygd
2025-09-08 19:46:39 -04:00
parent a284dd74dd
commit 86ea23fe39

View File

@@ -12,6 +12,7 @@ is_freebsd() {
[ "$(uname -s)" = "FreeBSD" ] [ "$(uname -s)" = "FreeBSD" ]
} }
# If SELinux is enabled, set the context of the binary # If SELinux is enabled, set the context of the binary
set_selinux_context() { set_selinux_context() {
# Check if SELinux is enabled and in enforcing or permissive mode # Check if SELinux is enabled and in enforcing or permissive mode
@@ -23,8 +24,8 @@ set_selinux_context() {
# First try to set persistent context if semanage is available # First try to set persistent context if semanage is available
if command -v semanage >/dev/null 2>&1; then if command -v semanage >/dev/null 2>&1; then
echo "Attempting to set persistent SELinux context..." echo "Attempting to set persistent SELinux context..."
if semanage fcontext -a -t bin_t "/opt/beszel-agent/beszel-agent" >/dev/null 2>&1; then if semanage fcontext -a -t bin_t "$BIN_PATH" >/dev/null 2>&1; then
restorecon -v /opt/beszel-agent/beszel-agent >/dev/null 2>&1 restorecon -v "$BIN_PATH" >/dev/null 2>&1
else else
echo "Warning: Failed to set persistent context, falling back to temporary context." echo "Warning: Failed to set persistent context, falling back to temporary context."
fi fi
@@ -33,8 +34,8 @@ set_selinux_context() {
# Fall back to chcon if semanage failed or isn't available # Fall back to chcon if semanage failed or isn't available
if command -v chcon >/dev/null 2>&1; then if command -v chcon >/dev/null 2>&1; then
# Set context for both the directory and binary # Set context for both the directory and binary
chcon -t bin_t /opt/beszel-agent/beszel-agent || echo "Warning: Failed to set SELinux context for binary." chcon -t bin_t "$BIN_PATH" || echo "Warning: Failed to set SELinux context for binary."
chcon -R -t bin_t /opt/beszel-agent || echo "Warning: Failed to set SELinux context for directory." chcon -R -t bin_t "$AGENT_DIR" || echo "Warning: Failed to set SELinux context for directory."
else else
if [ "$SELINUX_MODE" = "Enforcing" ]; then if [ "$SELINUX_MODE" = "Enforcing" ]; then
echo "Warning: SELinux is in enforcing mode but chcon command not found. The service may fail to start." echo "Warning: SELinux is in enforcing mode but chcon command not found. The service may fail to start."
@@ -53,7 +54,7 @@ cleanup_selinux_context() {
echo "Cleaning up SELinux contexts..." echo "Cleaning up SELinux contexts..."
# Remove persistent context if semanage is available # Remove persistent context if semanage is available
if command -v semanage >/dev/null 2>&1; then if command -v semanage >/dev/null 2>&1; then
semanage fcontext -d "/opt/beszel-agent/beszel-agent" 2>/dev/null || true semanage fcontext -d "$BIN_PATH" 2>/dev/null || true
fi fi
fi fi
} }
@@ -85,11 +86,11 @@ generate_freebsd_rc_service() {
# beszel_agent_enable (bool): Set to YES to enable Beszel Agent # beszel_agent_enable (bool): Set to YES to enable Beszel Agent
# Default: YES # Default: YES
# beszel_agent_env_file (str): Beszel Agent env configuration file # beszel_agent_env_file (str): Beszel Agent env configuration file
# Default: /opt/beszel-agent/env # Default: /usr/local/etc/beszel-agent/env
# beszel_agent_user (str): Beszel Agent daemon user # beszel_agent_user (str): Beszel Agent daemon user
# Default: beszel # Default: beszel
# beszel_agent_bin (str): Path to the beszel-agent binary # beszel_agent_bin (str): Path to the beszel-agent binary
# Default: /opt/beszel-agent/beszel-agent # Default: /usr/local/sbin/beszel-agent
# beszel_agent_flags (str): Extra flags passed to beszel-agent command invocation # beszel_agent_flags (str): Extra flags passed to beszel-agent command invocation
# Default: # Default:
@@ -102,8 +103,8 @@ load_rc_config $name
: ${beszel_agent_enable:="YES"} : ${beszel_agent_enable:="YES"}
: ${beszel_agent_user:="beszel"} : ${beszel_agent_user:="beszel"}
: ${beszel_agent_flags:=""} : ${beszel_agent_flags:=""}
: ${beszel_agent_env_file:="/opt/beszel-agent/env"} : ${beszel_agent_env_file:="/usr/local/etc/beszel-agent/env"}
: ${beszel_agent_bin:="/opt/beszel-agent/beszel-agent"} : ${beszel_agent_bin:="/usr/local/sbin/beszel-agent"}
logfile="/var/log/${name}.log" logfile="/var/log/${name}.log"
pidfile="/var/run/${name}.pid" pidfile="/var/run/${name}.pid"
@@ -297,6 +298,17 @@ while [ $# -gt 0 ]; do
shift shift
done done
# Set paths based on operating system
if is_freebsd; then
AGENT_DIR="/usr/local/etc/beszel-agent"
BIN_DIR="/usr/local/sbin"
BIN_PATH="/usr/local/sbin/beszel-agent"
else
AGENT_DIR="/opt/beszel-agent"
BIN_DIR="/opt/beszel-agent"
BIN_PATH="/opt/beszel-agent/beszel-agent"
fi
# Uninstall process # Uninstall process
if [ "$UNINSTALL" = true ]; then if [ "$UNINSTALL" = true ]; then
# Clean up SELinux contexts before removing files # Clean up SELinux contexts before removing files
@@ -346,17 +358,17 @@ if [ "$UNINSTALL" = true ]; then
# Remove the daily update cron job if it exists # Remove the daily update cron job if it exists
echo "Removing the daily update cron job..." echo "Removing the daily update cron job..."
if crontab -u root -l 2>/dev/null | grep -q "beszel-agent.*update"; then rm -f /etc/cron.d/beszel-agent
crontab -u root -l 2>/dev/null | grep -v "beszel-agent.*update" | crontab -u root -
fi
# Remove log files # Remove log files
echo "Removing log files..." echo "Removing log files..."
rm -f /var/log/beszel-agent.log rm -f /var/log/beszel-agent.log
# Remove env file # Remove env file and directories
echo "Removing environment configuration file..." echo "Removing environment configuration file..."
rm -f /opt/beszel-agent/env rm -f "$AGENT_DIR/env"
rm -f "$BIN_PATH"
rmdir "$AGENT_DIR" 2>/dev/null || true
else else
echo "Stopping and disabling the agent service..." echo "Stopping and disabling the agent service..."
@@ -377,7 +389,7 @@ if [ "$UNINSTALL" = true ]; then
fi fi
echo "Removing the Beszel Agent directory..." echo "Removing the Beszel Agent directory..."
rm -rf /opt/beszel-agent rm -rf "$AGENT_DIR"
echo "Removing the dedicated user for the agent service..." echo "Removing the dedicated user for the agent service..."
killall beszel-agent 2>/dev/null killall beszel-agent 2>/dev/null
@@ -508,7 +520,7 @@ elif is_openwrt; then
elif is_freebsd; then elif is_freebsd; then
if ! id -u beszel >/dev/null 2>&1; then if ! id -u beszel >/dev/null 2>&1; then
pw user add beszel -u 2000 -d /nonexistent -s /usr/sbin/nologin -c "beszel user" pw user add beszel -d /nonexistent -s /usr/sbin/nologin -c "beszel user"
fi fi
# Add the user to the wheel group to allow self-updates # Add the user to the wheel group to allow self-updates
if pw group show wheel >/dev/null 2>&1; then if pw group show wheel >/dev/null 2>&1; then
@@ -528,11 +540,16 @@ else
fi fi
# Create the directory for the Beszel Agent # Create the directory for the Beszel Agent
if [ ! -d "/opt/beszel-agent" ]; then
if [ ! -d "$AGENT_DIR" ]; then
echo "Creating the directory for the Beszel Agent..." echo "Creating the directory for the Beszel Agent..."
mkdir -p /opt/beszel-agent mkdir -p "$AGENT_DIR"
chown beszel:beszel /opt/beszel-agent chown beszel:beszel "$AGENT_DIR"
chmod 755 /opt/beszel-agent chmod 755 "$AGENT_DIR"
fi
if [ ! -d "$BIN_DIR" ]; then
mkdir -p "$BIN_DIR"
fi fi
# Download and install the Beszel Agent # Download and install the Beszel Agent
@@ -584,9 +601,9 @@ if ! tar -xzf "$FILE_NAME" beszel-agent; then
exit 1 exit 1
fi fi
mv beszel-agent /opt/beszel-agent/beszel-agent mv beszel-agent "$BIN_PATH"
chown beszel:beszel /opt/beszel-agent/beszel-agent chown beszel:beszel "$BIN_PATH"
chmod 755 /opt/beszel-agent/beszel-agent chmod 755 "$BIN_PATH"
# Set SELinux context if needed # Set SELinux context if needed
set_selinux_context set_selinux_context
@@ -618,7 +635,7 @@ if is_alpine; then
name="beszel-agent" name="beszel-agent"
description="Beszel Agent Service" description="Beszel Agent Service"
command="/opt/beszel-agent/beszel-agent" command="$BIN_PATH"
command_user="beszel" command_user="beszel"
command_background="yes" command_background="yes"
pidfile="/run/\${RC_SVCNAME}.pid" pidfile="/run/\${RC_SVCNAME}.pid"
@@ -673,7 +690,7 @@ EOF
# Create cron job to run beszel-agent update command daily at midnight # Create cron job to run beszel-agent update command daily at midnight
if ! crontab -u root -l 2>/dev/null | grep -q "beszel-agent.*update"; then if ! crontab -u root -l 2>/dev/null | grep -q "beszel-agent.*update"; then
(crontab -u root -l 2>/dev/null; echo "12 0 * * * /opt/beszel-agent/beszel-agent update >/dev/null 2>&1") | crontab -u root - (crontab -u root -l 2>/dev/null; echo "12 0 * * * $BIN_PATH update >/dev/null 2>&1") | crontab -u root -
fi fi
printf "\nDaily updates have been enabled via cron job.\n" printf "\nDaily updates have been enabled via cron job.\n"
@@ -697,7 +714,7 @@ START=99
start_service() { start_service() {
procd_open_instance procd_open_instance
procd_set_param command /opt/beszel-agent/beszel-agent procd_set_param command $BIN_PATH
procd_set_param user beszel procd_set_param user beszel
procd_set_param pidfile /var/run/beszel-agent.pid procd_set_param pidfile /var/run/beszel-agent.pid
procd_set_param env PORT="$PORT" KEY="$KEY" TOKEN="$TOKEN" HUB_URL="$HUB_URL" procd_set_param env PORT="$PORT" KEY="$KEY" TOKEN="$TOKEN" HUB_URL="$HUB_URL"
@@ -721,7 +738,7 @@ EXTRA_HELP=" update Update the Beszel agent
restart Restart the Beszel agent" restart Restart the Beszel agent"
update() { update() {
if /opt/beszel-agent/beszel-agent update | grep -q "Update completed successfully"; then if $BIN_PATH update | grep -q "Update completed successfully"; then
/etc/init.d/beszel-agent restart /etc/init.d/beszel-agent restart
fi fi
} }
@@ -772,14 +789,14 @@ elif is_freebsd; then
# Create environment configuration file with proper permissions # Create environment configuration file with proper permissions
echo "Creating environment configuration file..." echo "Creating environment configuration file..."
cat >/opt/beszel-agent/env <<EOF cat >"$AGENT_DIR/env" <<EOF
LISTEN=$PORT LISTEN=$PORT
KEY="$KEY" KEY="$KEY"
TOKEN=$TOKEN TOKEN=$TOKEN
HUB_URL=$HUB_URL HUB_URL=$HUB_URL
EOF EOF
chmod 640 /opt/beszel-agent/env chmod 640 "$AGENT_DIR/env"
chown root:beszel /opt/beszel-agent/env chown root:beszel "$AGENT_DIR/env"
# Create the rc service file # Create the rc service file
generate_freebsd_rc_service > /usr/local/etc/rc.d/beszel-agent generate_freebsd_rc_service > /usr/local/etc/rc.d/beszel-agent
@@ -813,12 +830,13 @@ EOF
[Yy]*) [Yy]*)
echo "Setting up daily automatic updates for beszel-agent..." echo "Setting up daily automatic updates for beszel-agent..."
# Create cron job to run beszel-agent update command daily # Create cron job in /etc/cron.d
if ! crontab -u root -l 2>/dev/null | grep -q "beszel-agent.*update"; then cat >/etc/cron.d/beszel-agent <<EOF
(crontab -u root -l 2>/dev/null; echo "12 0 * * * /opt/beszel-agent/beszel-agent update >/dev/null 2>&1") | crontab -u root - # Beszel Agent daily update job
fi 12 0 * * * root $BIN_PATH update >/dev/null 2>&1
EOF
printf "\nDaily updates have been enabled via cron job.\n" chmod 644 /etc/cron.d/beszel-agent
printf "\nDaily updates have been enabled via /etc/cron.d.\n"
;; ;;
esac esac
@@ -848,7 +866,7 @@ Environment="KEY=$KEY"
Environment="TOKEN=$TOKEN" Environment="TOKEN=$TOKEN"
Environment="HUB_URL=$HUB_URL" Environment="HUB_URL=$HUB_URL"
# Environment="EXTRA_FILESYSTEMS=sdb" # Environment="EXTRA_FILESYSTEMS=sdb"
ExecStart=/opt/beszel-agent/beszel-agent ExecStart=$BIN_PATH
User=beszel User=beszel
Restart=on-failure Restart=on-failure
RestartSec=5 RestartSec=5
@@ -903,7 +921,7 @@ Wants=beszel-agent.service
[Service] [Service]
Type=oneshot Type=oneshot
ExecStart=/opt/beszel-agent/beszel-agent update ExecStart=$BIN_PATH update
EOF EOF
# Create systemd timer for the daily update # Create systemd timer for the daily update