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-3636) Upgrade on non-English Windows #135

Conversation

Iristyle
Copy link
Contributor

@Iristyle Iristyle commented Jul 25, 2016

  • The previous checks for PID running uses a combination of tasklist,
    findstr and some more cryptic batch-isms to detect if a puppet
    agent is currently executing (by PID). This check also relies on
    the English string "No tasks are running", because tasklist.exe
    doesn't set exitcodes as desired when the query is not a match.

    C:\Users\Administrator>tasklist.exe /FI "PID eq 1234" /NH
    INFO: No tasks are running which match the specified criteria.

    C:\Users\Administrator>echo %ERRORLEVEL%
    0

    There are a couple of alternative methods for achieving the
    desired functionality. The simplest is qprocess.exe:

    C:\Users\Administrator>qprocess 1234
    No Process exists for 1234
    C:\Users\Administrator>echo %ERRORLEVEL%
    1

    C:\Users\Administrator>qprocess 32936
    USERNAME SESSIONNAME ID PID IMAGE

    administrator console 1 32936 mmc.exe
    C:\Users\Administrator>echo %ERRORLEVEL%
    0

    Unfortunately qprocess is no longer available on Nano Server, so
    the next best option is wmic.exe, which exists everywhere.

    C:>wmic PATH Win32_Process where handle=1111 get handle /format:textvaluelist |
    findstr /I "Handle=1111"
    No Instance(s) Available.

    C:>echo %ERRORLEVEL%
    1

    The string 'Handle' appears everywhere, so no locale specific
    string parsing is required on non-English versions of Windows, such
    as German:

    C:\Users\moses>tasklist /FI "PID eq 1234" /NH
    INFORMATION: Es werden keine Aufgaben mit den angegebenen Kriterien ausgeführt.

    C:\Users\moses>echo %ERRORLEVEL%
    0

@@ -14,8 +14,7 @@ if exist %pid_path% del %pid_path%

: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
qprocess %AGENT_PID%
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good!

@Iristyle
Copy link
Contributor Author

@ferventcoder asked about Nano... Nano doesn't have qprocess.exe sadly, but it does have wmic.exe - updating accordingly.

@Iristyle Iristyle force-pushed the ticket/master/MODULES-3636-wait-for-pid-properly-on-non-English-Windows branch from 5b4a159 to e15d9aa Compare July 25, 2016 22:50
@@ -14,8 +14,7 @@ if exist %pid_path% del %pid_path%

: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
wmic path Win32_Process where handle=%AGENT_PID% get handle /format:textvaluelist | findstr /I "Handle=%AGENT_PID%"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We tested that "Handle=PID" appears on English and German... not sure about French or other languages.

Since it's a WMI property, I doubt that it's localized - but we might make the code a little more resilient if we just look for =PID instead of Handle=PID.

Thoughts @puppetlabs/windows

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought you said findstr.exe also wasn't on nano?

Copy link
Contributor Author

@Iristyle Iristyle Jul 26, 2016

Choose a reason for hiding this comment

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

find.exe is not available, but findstr.exe is fortunately

@aharden
Copy link

aharden commented Jul 27, 2016

The current version of this template isn't working for me on WS2012 R2/Japanese w/ PE 3.8.3 agent. Symptoms are that the Puppet Agent service stops after the first agent run after puppet_agent is classified. The new agent installer is downloaded, but installation doesn't happen. I haven't looked into this further. The "chcp 437" method we were talking about in #134 was working for me.

UPDATE: after changing the %ERRORLEVEL% check (which was now backwards), @aharden has confirmed this works now on Japanese Windows.

 - The previous checks for PID running uses a combination of tasklist,
   findstr and some more cryptic batch-isms to detect if a puppet
   agent is currently executing (by PID).  This check also relies on
   the English string "No tasks are running", because tasklist.exe
   doesn't set exitcodes as desired when the query is not a match.

   C:\Users\Administrator>tasklist.exe /FI "PID eq 1234" /NH
   INFO: No tasks are running which match the specified criteria.

   C:\Users\Administrator>echo %ERRORLEVEL%
   0

   There are a couple of alternative methods for achieving the
   desired functionality.  The simplest is qprocess.exe:

   C:\Users\Administrator>qprocess 1234
   No Process exists for 1234
   C:\Users\Administrator>echo %ERRORLEVEL%
   1

   C:\Users\Administrator>qprocess 32936
   USERNAME              SESSIONNAME         ID    PID  IMAGE
   >administrator         console              1  32936  mmc.exe
   C:\Users\Administrator>echo %ERRORLEVEL%
   0

   Unfortunately qprocess is no longer available on Nano Server, so
   the next best option is wmic.exe, which exists everywhere.

   C:\>wmic PATH Win32_Process where handle=1111 get handle /format:textvaluelist |
     findstr /I "Handle=1111"
   No Instance(s) Available.

   C:\>echo %ERRORLEVEL%
   1

   The string 'Handle' appears everywhere, so no locale specific
   string parsing is required on non-English versions of Windows, such
   as German:

   C:\Users\moses>tasklist /FI "PID eq 1234" /NH
   INFORMATION: Es werden keine Aufgaben mit den angegebenen Kriterien ausgeführt.

   C:\Users\moses>echo %ERRORLEVEL%
   0
@Iristyle Iristyle force-pushed the ticket/master/MODULES-3636-wait-for-pid-properly-on-non-English-Windows branch from e15d9aa to 5da7b32 Compare July 27, 2016 16:03
@glennsarti glennsarti merged commit aef2d83 into puppetlabs:master Jul 29, 2016
@Iristyle Iristyle deleted the ticket/master/MODULES-3636-wait-for-pid-properly-on-non-English-Windows branch July 30, 2016 01:40
@MikaelSmith MikaelSmith mentioned this pull request Aug 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants