Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(MODULES-3657) Fix waiting on Windows during upgrade #137

Merged
merged 3 commits into from
Jul 30, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions templates/install_puppet.bat.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
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=%TEMP%\puppet_agent_upgrade.pid
set pid_path=%~dp0puppet_agent_upgrade.pid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... and we're certain this is always writable? And that there are no side effects of writing into this Puppet module template dir (consider PMT install vs pluginsync)?

I think it would be preferable to just set %TEMP% to %windir%\Temp if it's not already set:

IF "%TEMP%"=="" (SET TEMP=%windir%\Temp)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment withdrawn. I was mistakenly thinking that ~dp0 is the template directory. Its the location of the install_puppet.bat created from the ERB template, which is known to be the temp directory.

👍 to the change.


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
timeout /t 5 /nobreak > NUL
FOR /F "tokens=*" %%A IN ('tasklist /FI "PID eq %AGENT_PID%" /NH') DO set _task=%%A
echo %_task% | findstr "No tasks are running" >nul
IF NOT %errorlevel% == 0 ( GOTO 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,
Expand All @@ -28,7 +42,8 @@ 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%

:End
ENDLOCAL