diff --git a/templates/install_puppet.bat.erb b/templates/install_puppet.bat.erb index 4b82e376e..62ba4dfd2 100644 --- a/templates/install_puppet.bat.erb +++ b/templates/install_puppet.bat.erb @@ -1,49 +1,49 @@ -SET - -set AGENT_PID=%1 -set windowTitle=Puppet Agent Upgrade -title %windowTitle% - -set pid= -for /f "tokens=2" %%a in ('tasklist /v ^| findstr /c:"%windowTitle%"') do set pid=%%a -set pid_path=%~dp0puppet_agent_upgrade.pid - -set environment= -for /f "delims=" %%i in ('puppet config print --section agent environment') do set environment=%%i - -if exist %pid_path% del %pid_path% -@echo %pid%> %pid_path% - -SET /A pid_checks_performed=0 - -:wait_for_pid -REM Wait 5 seconds -ping 127.0.0.1 -n 6 > NUL -wmic path Win32_Process where handle=%AGENT_PID% get handle /format:textvaluelist | findstr /I "Handle=%AGENT_PID%" - -SET /A pid_checks_performed+=1 -REM Wait for a max of 120 seconds (or 24 iterations) before aborting the upgrade -IF %errorlevel% == 0 IF %pid_checks_performed% == 24 ( - REM Adding CustomSource allows EventCreate to work properly - reg add HKLM\SYSTEM\CurrentControlSet\services\eventlog\Application\Puppet /v CustomSource /t REG_DWORD /d 1 /f - EVENTCREATE /T ERROR /L APPLICATION /SO Puppet /ID 101 /D "Puppet agent upgrade failed while waiting for Puppet process [ID: %AGENT_PID%] to exit." - GOTO End -) - -IF %errorlevel% == 0 ( GOTO wait_for_pid ) - -REM This *must* occur after Puppet Agent has finished applying its -REM prior catalog which manages the pxp-agent service state. If not, -REM the catalog includes the PE module which starts the service and -REM sets its startup type, which prevents installs from proceeding. -REM This may fail on agents without pxp-agent, but since this is not -REM run interactively and the next command sets ERRORLEVEL, it's OK. -net stop pxp-agent - -start /wait msiexec.exe /qn /norestart /i "<%= @_msi_location %>" /l*vx "<%= @_logfile %>" PUPPET_MASTER_SERVER="<%= @_puppet_master %>" PUPPET_AGENT_ENVIRONMENT="%environment%" <% unless @install_dir.to_s.empty? -%>INSTALLDIR="<%= @install_dir %>"<% end -%> - -:End - -if exist %pid_path% del %pid_path% - -ENDLOCAL +SET + +set AGENT_PID=%1 +set windowTitle=Puppet Agent Upgrade +title %windowTitle% + +set pid= +for /f "tokens=2" %%a in ('tasklist /v ^| findstr /c:"%windowTitle%"') do set pid=%%a +set pid_path=%~dp0puppet_agent_upgrade.pid + +set environment= +for /f "delims=" %%i in ('puppet config print --section agent environment') do set environment=%%i + +if exist %pid_path% del %pid_path% +@echo %pid%> %pid_path% + +SET /A pid_checks_performed=0 + +:wait_for_pid +REM Wait 5 seconds +ping 127.0.0.1 -n 6 > NUL +wmic path Win32_Process where handle=%AGENT_PID% get handle /format:textvaluelist | findstr /I "Handle=%AGENT_PID%" + +SET /A pid_checks_performed+=1 +REM Wait for a max of 120 seconds (or 24 iterations) before aborting the upgrade +IF %errorlevel% == 0 IF %pid_checks_performed% == 24 ( + REM Adding CustomSource allows EventCreate to work properly + reg add HKLM\SYSTEM\CurrentControlSet\services\eventlog\Application\Puppet /v CustomSource /t REG_DWORD /d 1 /f + EVENTCREATE /T ERROR /L APPLICATION /SO Puppet /ID 101 /D "Puppet agent upgrade failed while waiting for Puppet process [ID: %AGENT_PID%] to exit." + GOTO End +) + +IF %errorlevel% == 0 ( GOTO wait_for_pid ) + +REM This *must* occur after Puppet Agent has finished applying its +REM prior catalog which manages the pxp-agent service state. If not, +REM the catalog includes the PE module which starts the service and +REM sets its startup type, which prevents installs from proceeding. +REM This may fail on agents without pxp-agent, but since this is not +REM run interactively and the next command sets ERRORLEVEL, it's OK. +net stop pxp-agent + +start /wait msiexec.exe /qn /norestart /i "<%= @_msi_location %>" /l*vx "<%= @_logfile %>" PUPPET_MASTER_SERVER="<%= @_puppet_master %>" PUPPET_AGENT_ENVIRONMENT="%environment%" <% unless @install_dir.to_s.empty? -%>INSTALLDIR="<%= @install_dir %>"<% end -%> + +:End + +if exist %pid_path% del %pid_path% + +ENDLOCAL