1module MCollective
2  module Util
3    class PuppetAgentMgr
4      class MgrWindows < MgrV3
5
6        # is the agent daemon currently running?
7        # this will require win32/service
8        def daemon_present?
9          require 'win32/service'
10          case Win32::Service.status(@puppet_service).current_state
11          when "running", "continue pending", "start pending"
12            true
13          else
14            false
15          end
16        rescue Win32::Service::Error
17          false
18        end
19
20        # is the agent currently applying a catalog
21        def applying?
22          return false if disabled?
23          begin
24            pid = File.read(Puppet[:agent_catalog_run_lockfile])
25            return has_process_for_pid?(pid)
26          rescue Errno::ENOENT
27            return false
28          end
29        rescue => e
30          Log.warn("Could not determine if Puppet is applying a catalog: " \
31                   "%s: %s: %s" % [e.backtrace.first, e.class, e.to_s])
32          return false
33        end
34
35        def signal_running_daemon
36          raise "Signalling the puppet daemon is not supported on Windows"
37        end
38
39        def has_process_for_pid?(pid)
40          return false if pid.nil? or pid.empty?
41          !!::Process.kill(0, Integer(pid))
42        rescue Errno::EPERM
43          true
44        rescue Errno::ESRCH
45          false
46        end
47
48        # the daemon doesn't interfere with background runs,
49        # so they're always allowed
50        def background_run_allowed?
51          true
52        end
53
54        # this will require win32/process
55        def run_in_background(clioptions, execute=true)
56          require 'win32/process'
57          options =["--onetime", "--color=false"].concat(clioptions)
58          return options unless execute
59          command = "puppet.bat agent #{options.join(' ')}"
60          ::Process.create(:command_line   => command,
61                           :creation_flags => ::Process::CREATE_NO_WINDOW)
62        end
63
64        # this will require win32/process
65        def run_in_foreground(clioptions, execute=true)
66          run_in_background(clioptions, execute)
67        end
68      end
69    end
70  end
71end
72