Hi Glenn,

On Sun, Oct 13, 2013 at 6:28 PM, Glenn Sarti <glenn.sar...@gmail.com> wrote:

> Why does the Puppet-Agent on Windows use a batch file?
>
Because the service and command line invocations, e.g. puppet apply, both
need to setup their environment to run ruby, e.g. RUBYLIB, etc. The easiest
way to do that was to create daemon.bat that calls environment.bat, and
then invokes ruby.

> I posted a question in the Ask PuppetLabs section and was directed to
> create post here as well;
>
> https://ask.puppetlabs.com/question/3506/why-does-the-puppet-agent-on-windows-use-a-batch-file/
>
> ------------------------ Original question
> The Puppet Enterprise for Windows Agent runs as a windows service,
> basically a deamonised version of Puppet, which is all fine. However the
> Windows Service calls a batch file which seems extremely strange. While it
> does work, i.e. the Service starts and runs, using CMD.EXE as a service
> executable is generally considered a really bad idea.
>
> It does not respond to the usual SCM (Service Control Manager) calls and
> in it's current state is misconfigured e.g. The service says that it can
> respond to Pause and Continue events but CMD.EXE can't fulfill those
> requests.
>

The service is hosted in the ruby process that cmd.exe executes, not
cmd.exe itself. Puppet does respond to SCM events like start & stop. The
puppet service does claim to support pause & continue (due to the
win32-service gem[1]), but the service does not implement the necessary
handler logic. Please file a bug on that.

> Also CMD.EXE does not monitor the ruby process (except for the basic
> operation of is it running) and vice versa. I can kill the cmd.exe process
> and the service manager will report that the Puppet Agent has stopped
> however the ruby process is still quite happily running.
>
> Either I'm missing something and CMD.EXE is an appropriate service
> executable or perhaps the community or puppet labs could create a better
> native wrapper to the ruby based puppet process.
>
> ------------------------
>
> So I did a few tests;
>
>    - You can send pause and continue messages to the service but they're
>    just ignored even though the Services says it's Paused.
>
> Puppet's daemon.rb[2] needs to override the default service_pause and
service_continue. The pause method should set a flag that the main loop
checks each time.

>
>    - You can kill CMD.EXE service process but the Puppet Agent is still
>    running (It becomes an orphaned process).  You can then start the service
>    again, and you'll end up with two Puppet Agents running daemonised at the
>    same time.
>
> cmd.exe is not essential to the puppet service once it's running.

>
>    -   I'm not sure what will happen if they both try to do a catalog run
>    at the same time, but nothing good can come of it.
>
> Puppet uses a lock file to ensure two processes don't apply a catalog at
the same time. This scenario can come up in the normal case when puppet is
running as a service, and you run `puppet apply` on the command line.

>
>    - CMD.EXE doesn't respond to power events e.g. going into
>    Standby/Hibernate; but I have no idea how any service wrapper could raise
>    that kind of event in Puppet so that it could deal with it.  Does it even
>    matter if the host goes into Standby in the middle of Puppet run?
>    Admittedly this would be very unlikely scenario as Puppet seems to be more
>    always-on server orientated rather than for laptop configuration 
> management.
>    - If you start Puppet and then quickly attempt to stop it, it get's
>    stuck in the Stopping state and you have to kill Ruby manually.  Thtat's
>    because the service control manager raises events in a multithreaded
>    manner, however CMD is running a single thread.  If CMD.EXE is too busy to
>    process the event then things get into a funny state.  This would probably
>    occur during a catalog run too but I haven't confirmed it.
>
> The service control manager calls back into ruby.exe, not cmd.exe. Our
daemon.rb is what calls StartServiceCtrlDispatcher...

>
>    - From a troubleshooting perspective, there are no logs or diagnostic
>    information created by CMD.EXE
>
> The daemon.rb code creates a log file (with different verbosity levels). I
have been meaning to switch it over to using the application event log, but
haven't yet, patches welcome!

Josh

[1]
https://github.com/djberg96/win32-service/blob/ffi/lib/win32/daemon.rb#L86-87
[2]
https://github.com/puppetlabs/puppet/blob/master/ext/windows/service/daemon.rb

-- 
Josh Cooper
Developer, Puppet Labs

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to