diff --git a/supplemental/scripts/install-agent.sh b/supplemental/scripts/install-agent.sh index 1bbb705..27959cd 100755 --- a/supplemental/scripts/install-agent.sh +++ b/supplemental/scripts/install-agent.sh @@ -7,74 +7,74 @@ UNINSTALL=false CHINA_MAINLAND=false GITHUB_URL="https://github.com" GITHUB_API_URL="https://api.github.com" +KEY="" -# Read command line options -TEMP=$(getopt -o 'k:p:uh' --long 'china-mirrors,help' -n 'install-agent.sh' -- "$@") +# Check for help flag first +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 - echo 'Failed to parse command line arguments' >&2 - exit 1 -fi - -eval set -- "$TEMP" -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 +# 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 +# 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 if [ "$UNINSTALL" = true ]; then echo "Stopping and disabling the agent service..." @@ -158,7 +158,7 @@ else fi # 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..." useradd -M -s /bin/false beszel fi @@ -223,7 +223,7 @@ rm -rf "$TEMP_DIR" # Create the systemd service echo "Creating the systemd service for the agent..." -cat > /etc/systemd/system/beszel-agent.service << EOF +cat >/etc/systemd/system/beszel-agent.service < /etc/systemd/system/beszel-agent-update.service << EOF + # Create systemd service for the daily update + cat >/etc/systemd/system/beszel-agent-update.service < /etc/systemd/system/beszel-agent-update.timer << EOF + # Create systemd timer for the daily update + cat >/etc/systemd/system/beszel-agent-update.timer <