fix: Download agent installer to file before executing to prevent stdin pipe hang

This commit is contained in:
Muhammad Ibrahim
2025-10-02 13:37:55 +01:00
parent 55c8f74b73
commit bec09b9457

View File

@@ -4,7 +4,7 @@ set -euo pipefail # Exit on error, undefined vars, pipe failures
# Trap to catch any unexpected exits
trap 'echo "[ERROR] Script exited unexpectedly at line $LINENO with exit code $?"' ERR EXIT
SCRIPT_VERSION="1.0.0-debug.6"
SCRIPT_VERSION="1.0.0-debug.7"
echo "[DEBUG] Script Version: $SCRIPT_VERSION ($(date +%Y-%m-%d\ %H:%M:%S))"
# =============================================================================
@@ -194,16 +194,31 @@ while IFS= read -r line; do
# Install PatchMon agent in container
info " Installing PatchMon agent..."
install_output=$(timeout 120 pct exec "$vmid" -- bash -c "curl $CURL_FLAGS \
-H 'X-API-ID: $api_id' \
-H 'X-API-KEY: $api_key' \
'$PATCHMON_URL/api/v1/hosts/install' | bash" 2>&1 </dev/null)
# Download and execute in separate steps to avoid stdin issues with piping
install_output=$(timeout 180 pct exec "$vmid" -- bash -c "
export API_ID='$api_id'
export API_KEY='$api_key'
cd /tmp
curl $CURL_FLAGS \
-H 'X-API-ID: \$API_ID' \
-H 'X-API-KEY: \$API_KEY' \
-o patchmon-install.sh \
'$PATCHMON_URL/api/v1/hosts/install' && \
bash patchmon-install.sh && \
rm -f patchmon-install.sh
" 2>&1 </dev/null)
install_exit_code=$?
if [[ $? -eq 0 ]]; then
if [[ $install_exit_code -eq 0 ]]; then
info " ✓ Agent installed successfully in $friendly_name"
((enrolled_count++))
elif [[ $install_exit_code -eq 124 ]]; then
warn " ⏱ Agent installation timed out (>180s) in $friendly_name"
debug " Install output: $install_output"
((failed_count++))
else
error " ✗ Failed to install agent in $friendly_name"
warn " ✗ Failed to install agent in $friendly_name (exit: $install_exit_code)"
debug " Install output: $install_output"
((failed_count++))
fi