Skip to content

Commit

Permalink
Error when trying to ensure non-existent services are stopped
Browse files Browse the repository at this point in the history
When managing the `ensure` state for a service, the provider queries the
service status to determine if it's running or not. However, if the
service doesn't exist, then it returns that the service is `stopped`.

When trying to ensure a non-existent service is stopped, puppet thinks
the service is "insync", so returns exit code 0.

This commit updates the systemd provider's enable method to return
either :running, :stopped or :absent. To differentiate between `stopped`
vs `absent`, the provider first checks if the service exists.

This adds `absent` as a valid value for the `ensure` property, however,
it can't be set on a provider, since we don't want to remove services:

    puppet resource service ufw ensure=absent
    Error: The systemd provider can not handle attribute ensure
  • Loading branch information
joshcooper authored and cthorn42 committed Nov 17, 2023
1 parent 03b3df3 commit f754cf7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/puppet/application/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,10 @@ def find_or_save_resources(type, name, params)
resource = Puppet::Resource.new( type, name, :parameters => params )

# save returns [resource that was saved, transaction log from applying the resource]
save_result = Puppet::Resource.indirection.save(resource, key)
[ save_result.first ]
save_result, report = Puppet::Resource.indirection.save(resource, key)
status = report.resource_statuses[resource.ref]
raise "Failed to manage resource #{resource.ref}" if status&.failed?
[ save_result ]
end
else
if type == "file"
Expand Down
14 changes: 14 additions & 0 deletions lib/puppet/provider/service/systemd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ def daemon_reload?
end
end

# override base#status
def status
if exist?
status = service_command(:status, false)
if status.exitstatus == 0
return :running
else
return :stopped
end
else
return :absent
end
end

def enable
self.unmask
systemctl_change_enable(:enable)
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/type/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def insync?(current)
provider.start
end

newvalue(:absent)

aliasvalue(:false, :stopped)
aliasvalue(:true, :running)

Expand Down

0 comments on commit f754cf7

Please sign in to comment.