116 lines
3.3 KiB
Bash
116 lines
3.3 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
set -u
|
|
|
|
AGENT_DIR="/OGP"
|
|
PIDFILE="${1:-/OGP/ogp_agent_run.pid}"
|
|
PREFS_FILE="$AGENT_DIR/Cfg/bash_prefs.cfg"
|
|
REPO_URL_DEFAULT="http://forge.runlevelsystems.com/dev/GSP.git"
|
|
REPO_BRANCH_DEFAULT="Panel-unstable"
|
|
|
|
warn() {
|
|
printf 'WARNING: %s\n' "$*" >&2
|
|
}
|
|
|
|
fail() {
|
|
printf 'ERROR: %s\n' "$*" >&2
|
|
exit 1
|
|
}
|
|
|
|
normalize_text_files() {
|
|
local root="$1"
|
|
[ -d "$root" ] || return 0
|
|
find "$root" -type f \( -name '*.pl' -o -name '*.pm' -o -name '*.sh' -o -name '*.cfg' \) -print0 2>/dev/null |
|
|
while IFS= read -r -d '' file; do
|
|
sed -i 's/\r$//' "$file" 2>/dev/null || warn "Could not normalize line endings for $file"
|
|
done
|
|
}
|
|
|
|
load_agent_preferences() {
|
|
agent_auto_update=0
|
|
agent_update_repo_url="$REPO_URL_DEFAULT"
|
|
agent_update_branch="$REPO_BRANCH_DEFAULT"
|
|
|
|
if [ -f "$PREFS_FILE" ]; then
|
|
# shellcheck disable=SC1090
|
|
. "$PREFS_FILE"
|
|
fi
|
|
|
|
agent_auto_update="${agent_auto_update:-0}"
|
|
agent_update_repo_url="${agent_update_repo_url:-$REPO_URL_DEFAULT}"
|
|
agent_update_branch="${agent_update_branch:-$REPO_BRANCH_DEFAULT}"
|
|
}
|
|
|
|
auto_update_windows_agent() {
|
|
[ "$agent_auto_update" = "1" ] || {
|
|
echo "Agent auto-update is disabled."
|
|
return 0
|
|
}
|
|
|
|
if ! command -v git >/dev/null 2>&1; then
|
|
warn "git is not installed; skipping agent auto-update and using the current agent."
|
|
return 0
|
|
fi
|
|
|
|
local tmp_dir repo_dir source_file target_file backup_file
|
|
tmp_dir="$(mktemp -d /tmp/gsp-agent-update.XXXXXX 2>/dev/null)" || {
|
|
warn "Could not create temporary update directory; skipping auto-update."
|
|
return 0
|
|
}
|
|
repo_dir="$tmp_dir/repo"
|
|
target_file="$AGENT_DIR/ogp_agent.pl"
|
|
backup_file="$AGENT_DIR/ogp_agent.pl.bak.$(date +%Y%m%d%H%M%S)"
|
|
|
|
echo "Checking for Windows agent update from $agent_update_repo_url ($agent_update_branch)..."
|
|
if ! git clone --depth 1 --branch "$agent_update_branch" "$agent_update_repo_url" "$repo_dir"; then
|
|
warn "Agent auto-update clone failed; using the current agent."
|
|
rm -rf "$tmp_dir"
|
|
return 0
|
|
fi
|
|
|
|
source_file="$repo_dir/Agent-Windows/ogp_agent.pl"
|
|
if [ ! -f "$source_file" ]; then
|
|
warn "Updated Windows agent source was not found at Agent-Windows/ogp_agent.pl; using the current agent."
|
|
rm -rf "$tmp_dir"
|
|
return 0
|
|
fi
|
|
|
|
cp "$target_file" "$backup_file" 2>/dev/null || {
|
|
warn "Could not backup $target_file; skipping auto-update."
|
|
rm -rf "$tmp_dir"
|
|
return 0
|
|
}
|
|
|
|
if ! cp "$source_file" "$target_file"; then
|
|
warn "Could not copy updated Windows agent; restoring backup."
|
|
cp "$backup_file" "$target_file" 2>/dev/null
|
|
rm -rf "$tmp_dir"
|
|
return 0
|
|
fi
|
|
|
|
sed -i 's/\r$//' "$target_file" 2>/dev/null || true
|
|
if ! perl -c "$target_file"; then
|
|
warn "Updated Windows agent failed perl syntax validation; restoring backup."
|
|
cp "$backup_file" "$target_file" 2>/dev/null
|
|
perl -c "$target_file" || true
|
|
rm -rf "$tmp_dir"
|
|
return 0
|
|
fi
|
|
|
|
echo "Windows agent auto-update completed."
|
|
rm -rf "$tmp_dir"
|
|
return 0
|
|
}
|
|
|
|
cd "$AGENT_DIR" || fail "Could not enter $AGENT_DIR. Is the Windows agent installed under Cygwin /OGP?"
|
|
|
|
normalize_text_files "$AGENT_DIR"
|
|
normalize_text_files "/Install"
|
|
load_agent_preferences
|
|
auto_update_windows_agent
|
|
|
|
echo "Validating $AGENT_DIR/ogp_agent.pl..."
|
|
perl -c "$AGENT_DIR/ogp_agent.pl" || fail "Perl syntax/dependency validation failed. Install missing Cygwin Perl packages or restore a valid Windows agent file."
|
|
|
|
echo "Launching GSP Windows Agent..."
|
|
exec perl "$AGENT_DIR/ogp_agent.pl" -pidfile "$PIDFILE"
|