mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 17:59:28 +08:00
update agent install script arg parsing
This commit is contained in:
@@ -7,74 +7,74 @@ UNINSTALL=false
|
|||||||
CHINA_MAINLAND=false
|
CHINA_MAINLAND=false
|
||||||
GITHUB_URL="https://github.com"
|
GITHUB_URL="https://github.com"
|
||||||
GITHUB_API_URL="https://api.github.com"
|
GITHUB_API_URL="https://api.github.com"
|
||||||
|
KEY=""
|
||||||
|
|
||||||
# Read command line options
|
# Check for help flag first
|
||||||
TEMP=$(getopt -o 'k:p:uh' --long 'china-mirrors,help' -n 'install-agent.sh' -- "$@")
|
case "$1" in
|
||||||
|
-h | --help)
|
||||||
|
printf "Beszel Agent installation script\n\n"
|
||||||
|
printf "Usage: ./install-agent.sh [options]\n\n"
|
||||||
|
printf "Options: \n"
|
||||||
|
printf " -k : SSH key (required, or interactive if not provided)\n"
|
||||||
|
printf " -p : Port (default: $PORT)\n"
|
||||||
|
printf " -u : Uninstall Beszel Agent\n"
|
||||||
|
printf " --china-mirrors : Using GitHub mirror sources to resolve network timeout issues in mainland China\n"
|
||||||
|
printf " -h, --help : Display this help message\n"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
# Build sudo args by properly quoting everything
|
||||||
echo 'Failed to parse command line arguments' >&2
|
build_sudo_args() {
|
||||||
exit 1
|
QUOTED_ARGS=""
|
||||||
fi
|
while [ $# -gt 0 ]; do
|
||||||
|
if [ -n "$QUOTED_ARGS" ]; then
|
||||||
eval set -- "$TEMP"
|
QUOTED_ARGS="$QUOTED_ARGS "
|
||||||
unset TEMP
|
|
||||||
|
|
||||||
while true; do
|
|
||||||
case "$1" in
|
|
||||||
'-k')
|
|
||||||
KEY="$2"
|
|
||||||
shift 2
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
'-p')
|
|
||||||
PORT="$2"
|
|
||||||
shift 2
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
'-u')
|
|
||||||
UNINSTALL=true
|
|
||||||
shift
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
'--china-mirrors')
|
|
||||||
CHINA_MAINLAND=true
|
|
||||||
shift
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
'-h'|'--help')
|
|
||||||
printf "Beszel Agent installation script\n\n"
|
|
||||||
printf "Usage: ./install-agent.sh [options]\n\n"
|
|
||||||
printf "Options: \n"
|
|
||||||
printf " -k : SSH key (required, or interactive if not provided)\n"
|
|
||||||
printf " -p : Port (default: $PORT)\n"
|
|
||||||
printf " -u : Uninstall Beszel Agent\n"
|
|
||||||
printf " --china-mirrors : Using GitHub mirror sources to resolve network timeout issues in mainland China\n"
|
|
||||||
printf " -h, --help : Display this help message\n"
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
'--')
|
|
||||||
shift
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Invalid option: $1" >&2
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
# 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
|
||||||
|
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
|
fi
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
-k)
|
||||||
|
shift
|
||||||
|
KEY="$1"
|
||||||
|
;;
|
||||||
|
-p)
|
||||||
|
shift
|
||||||
|
PORT="$1"
|
||||||
|
;;
|
||||||
|
-u)
|
||||||
|
UNINSTALL=true
|
||||||
|
;;
|
||||||
|
--china-mirrors)
|
||||||
|
CHINA_MAINLAND=true
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Invalid option: $1" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
# Uninstall process
|
# Uninstall process
|
||||||
if [ "$UNINSTALL" = true ]; then
|
if [ "$UNINSTALL" = true ]; then
|
||||||
echo "Stopping and disabling the agent service..."
|
echo "Stopping and disabling the agent service..."
|
||||||
@@ -158,7 +158,7 @@ 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
|
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..."
|
||||||
useradd -M -s /bin/false beszel
|
useradd -M -s /bin/false beszel
|
||||||
fi
|
fi
|
||||||
@@ -223,7 +223,7 @@ rm -rf "$TEMP_DIR"
|
|||||||
|
|
||||||
# Create the systemd service
|
# Create the systemd service
|
||||||
echo "Creating the systemd service for the agent..."
|
echo "Creating the systemd service for the agent..."
|
||||||
cat > /etc/systemd/system/beszel-agent.service << EOF
|
cat >/etc/systemd/system/beszel-agent.service <<EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Beszel Agent Service
|
Description=Beszel Agent Service
|
||||||
After=network.target
|
After=network.target
|
||||||
@@ -250,11 +250,11 @@ systemctl start beszel-agent.service
|
|||||||
printf "\nWould you like to enable automatic daily updates for beszel-agent? (y/n): "
|
printf "\nWould you like to enable automatic daily updates for beszel-agent? (y/n): "
|
||||||
read AUTO_UPDATE
|
read AUTO_UPDATE
|
||||||
case "$AUTO_UPDATE" in
|
case "$AUTO_UPDATE" in
|
||||||
[Yy]*)
|
[Yy]*)
|
||||||
echo "Setting up daily automatic updates for beszel-agent..."
|
echo "Setting up daily automatic updates for beszel-agent..."
|
||||||
|
|
||||||
# Create systemd service for the daily update
|
# Create systemd service for the daily update
|
||||||
cat > /etc/systemd/system/beszel-agent-update.service << EOF
|
cat >/etc/systemd/system/beszel-agent-update.service <<EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Update beszel-agent if needed
|
Description=Update beszel-agent if needed
|
||||||
Wants=beszel-agent.service
|
Wants=beszel-agent.service
|
||||||
@@ -264,8 +264,8 @@ Type=oneshot
|
|||||||
ExecStart=/bin/sh -c '/opt/beszel-agent/beszel-agent update | grep -q "Successfully updated" && systemctl restart beszel-agent'
|
ExecStart=/bin/sh -c '/opt/beszel-agent/beszel-agent update | grep -q "Successfully updated" && systemctl restart beszel-agent'
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# Create systemd timer for the daily update
|
# Create systemd timer for the daily update
|
||||||
cat > /etc/systemd/system/beszel-agent-update.timer << EOF
|
cat >/etc/systemd/system/beszel-agent-update.timer <<EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Run beszel-agent update daily
|
Description=Run beszel-agent update daily
|
||||||
|
|
||||||
@@ -278,11 +278,11 @@ RandomizedDelaySec=4h
|
|||||||
WantedBy=timers.target
|
WantedBy=timers.target
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
systemctl enable --now beszel-agent-update.timer
|
systemctl enable --now beszel-agent-update.timer
|
||||||
|
|
||||||
printf "\nAutomatic daily updates have been enabled.\n"
|
printf "\nAutomatic daily updates have been enabled.\n"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Wait for the service to start or fail
|
# Wait for the service to start or fail
|
||||||
@@ -292,4 +292,4 @@ if [ "$(systemctl is-active beszel-agent.service)" != "active" ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "\n\033[32mBeszel Agent has been installed successfully! It is now running on port $PORT.\033[0m\n"
|
printf "\n\033[32mBeszel Agent has been installed successfully! It is now running on port $PORT.\033[0m\n"
|
||||||
|
Reference in New Issue
Block a user