From ec6798bc7f9f6f9c89d68b4aa90344821c9579d7 Mon Sep 17 00:00:00 2001 From: Trey Dockendorf Date: Tue, 27 Feb 2024 12:56:56 -0500 Subject: [PATCH] (#64) Allow service to be configured for non-Windows. Check the service state rather than PID for non-Windows This allows for the continued checking of 'puppet' service but also allows checking other service names like 'puppet-run.service' used by The Foreman module for Puppet. This change is backwards incompatible due to the change in configuration key Fixes #64 --- README.md | 7 +++++++ agent/puppet.rb | 3 ++- spec/agent/puppet_agent_spec.rb | 10 +++++++++- spec/util/puppet_agent_mgr/mgr_v3_spec.rb | 17 +++++++++++------ util/puppet_agent_mgr/mgr_v3.rb | 7 +++++++ 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 05c3a31..aee612c 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,13 @@ mcollective_agent_puppet::config: windows_service: puppet ``` +The service name can also be set using the `service` config. The `service` setting will be used instead of `windows_service` if `service` is defined. + +```yaml +mcollective_agent_puppet::config: + service: puppet +``` + The agent will by default invoke `command` to initiate a run, passing through any applicable flags to adjust behavior. On POSIX-compliant platforms where Puppet is already running in diff --git a/agent/puppet.rb b/agent/puppet.rb index 1380902..1ac7ee2 100644 --- a/agent/puppet.rb +++ b/agent/puppet.rb @@ -10,7 +10,8 @@ def startup_hook configfile = @config.pluginconf.fetch("puppet.config", nil) @puppet_command = @config.pluginconf.fetch("puppet.command", default_agent_command) - @puppet_service = @config.pluginconf.fetch("puppet.windows_service", "puppet") + @windows_service = @config.pluginconf.fetch("puppet.windows_service", "puppet") + @puppet_service = @config.pluginconf.fetch("puppet.service", @windows_service) @puppet_splaylimit = Integer(@config.pluginconf.fetch("puppet.splaylimit", 30)) @puppet_splay = Util.str_to_bool(@config.pluginconf.fetch("puppet.splay", "true")) @puppet_agent = Util::PuppetAgentMgr.manager(configfile, @puppet_service) diff --git a/spec/agent/puppet_agent_spec.rb b/spec/agent/puppet_agent_spec.rb index 7b78d5c..99686dc 100644 --- a/spec/agent/puppet_agent_spec.rb +++ b/spec/agent/puppet_agent_spec.rb @@ -25,13 +25,21 @@ @agent.startup_hook end - it "should set the service name based on the config" do + it "should set the windows service name based on the config" do MCollective::Config.instance.stubs(:pluginconf).returns( {"puppet.windows_service" => "not-puppet"}) MCollective::Util::PuppetAgentMgr.expects(:manager).with(nil, "not-puppet") @agent.startup_hook end + + it "should set the service name based on the config" do + MCollective::Config.instance.stubs(:pluginconf).returns( + {"puppet.service" => "not-puppet"}) + MCollective::Util::PuppetAgentMgr.expects(:manager).with(nil, "not-puppet") + + @agent.startup_hook + end end describe "#default_agent_command" do diff --git a/spec/util/puppet_agent_mgr/mgr_v3_spec.rb b/spec/util/puppet_agent_mgr/mgr_v3_spec.rb index 20bc6a4..6a721a1 100644 --- a/spec/util/puppet_agent_mgr/mgr_v3_spec.rb +++ b/spec/util/puppet_agent_mgr/mgr_v3_spec.rb @@ -15,15 +15,20 @@ module MCollective::Util end describe "#daemon_present?" do - it "should return false if the pidfile does not exist" do - File.expects(:exist?).with("pidfile").returns(false) + before :each do + @type = mock; @service = mock; @provider = mock + @service.stubs(:provider).returns(@provider) + @type.stubs(:new).returns(@service) + Puppet::Type.stubs(:type).returns(@type) + end + + it "should return false if the service is not running" do + @provider.stubs(:status).returns(:stopped) @manager.daemon_present?.should == false end - it "should check the pid if the pidfile exist" do - File.expects(:exist?).with("pidfile").returns(true) - File.expects(:read).with("pidfile").returns(1) - @manager.expects(:has_process_for_pid?).with(1).returns(true) + it "should return true if the service is running" do + @provider.stubs(:status).returns(:running) @manager.daemon_present?.should == true end end diff --git a/util/puppet_agent_mgr/mgr_v3.rb b/util/puppet_agent_mgr/mgr_v3.rb index d453c31..c112e4a 100644 --- a/util/puppet_agent_mgr/mgr_v3.rb +++ b/util/puppet_agent_mgr/mgr_v3.rb @@ -58,6 +58,13 @@ def disabled? File.exist?(Puppet[:agent_disabled_lockfile]) end + # is the agent daemon currently running? + def daemon_present? + service = ::Puppet::Type.type(:service).new(name: @puppet_service) + status = service.provider.status + status.to_s == 'running' + end + private def platform_applying?