Skip to content

Commit f754cf7

Browse files
joshcoopercthorn42
authored andcommitted
Error when trying to ensure non-existent services are stopped
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
1 parent 03b3df3 commit f754cf7

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

lib/puppet/application/resource.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,10 @@ def find_or_save_resources(type, name, params)
234234
resource = Puppet::Resource.new( type, name, :parameters => params )
235235

236236
# save returns [resource that was saved, transaction log from applying the resource]
237-
save_result = Puppet::Resource.indirection.save(resource, key)
238-
[ save_result.first ]
237+
save_result, report = Puppet::Resource.indirection.save(resource, key)
238+
status = report.resource_statuses[resource.ref]
239+
raise "Failed to manage resource #{resource.ref}" if status&.failed?
240+
[ save_result ]
239241
end
240242
else
241243
if type == "file"

lib/puppet/provider/service/systemd.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ def daemon_reload?
159159
end
160160
end
161161

162+
# override base#status
163+
def status
164+
if exist?
165+
status = service_command(:status, false)
166+
if status.exitstatus == 0
167+
return :running
168+
else
169+
return :stopped
170+
end
171+
else
172+
return :absent
173+
end
174+
end
175+
162176
def enable
163177
self.unmask
164178
systemctl_change_enable(:enable)

lib/puppet/type/service.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def insync?(current)
110110
provider.start
111111
end
112112

113+
newvalue(:absent)
114+
113115
aliasvalue(:false, :stopped)
114116
aliasvalue(:true, :running)
115117

0 commit comments

Comments
 (0)