diff options
author | Joshua Miller <joshmiller@fb.com> | 2021-02-02 20:22:09 -0500 |
---|---|---|
committer | Joshua Miller <joshmiller@fb.com> | 2021-02-02 20:34:30 -0500 |
commit | e89693875cef26ba22941fd8b042dcbe758e70e3 (patch) | |
tree | 3c9ab667b6a4357acfe09898495abd601f7b6519 | |
parent | 2a84adbe13744e4fda11a10c9cb8ef2691acb692 (diff) | |
download | chef-e89693875cef26ba22941fd8b042dcbe758e70e3.tar.gz |
Handle sysv compat mode when checking enabled status for systemd service
Signed-off-by: Joshua Miller <joshmiller@fb.com>
-rw-r--r-- | lib/chef/provider/service/systemd.rb | 5 | ||||
-rw-r--r-- | spec/unit/provider/service/systemd_service_spec.rb | 22 |
2 files changed, 27 insertions, 0 deletions
diff --git a/lib/chef/provider/service/systemd.rb b/lib/chef/provider/service/systemd.rb index 9b0cf3abd8..f8fac18bc3 100644 --- a/lib/chef/provider/service/systemd.rb +++ b/lib/chef/provider/service/systemd.rb @@ -202,6 +202,11 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple end def is_enabled? + # if the service is in sysv compat mode, shellout to determine if enabled + if systemd_service_status["UnitFileState"] == "bad" + options, args = get_systemctl_options_args + return shell_out(systemctl_path, args, "is-enabled", new_resource.service_name, "--quiet", **options).exitstatus == 0 + end # See https://github.com/systemd/systemd/blob/master/src/systemctl/systemctl-is-enabled.c # Note: enabled-runtime is excluded because this is volatile, and the state of enabled-runtime # specifically means that the service is not enabled diff --git a/spec/unit/provider/service/systemd_service_spec.rb b/spec/unit/provider/service/systemd_service_spec.rb index 34b8094974..ed6e3f989e 100644 --- a/spec/unit/provider/service/systemd_service_spec.rb +++ b/spec/unit/provider/service/systemd_service_spec.rb @@ -412,6 +412,28 @@ describe Chef::Provider::Service::Systemd do with_systemctl_show(systemctl_path, enabled_runtime_and_active) expect(provider.is_enabled?).to be false end + + it "should shellout to 'is-enabled' and return false if unit file is bad and sysv compat isn't enabled" do + bad_and_inactive = <<-STDOUT + ActiveState=inactive + UnitFileState=bad + STDOUT + with_systemctl_show(systemctl_path, bad_and_inactive) + systemctl_isenabled = [systemctl_path, "--system", "is-enabled", service_name, "--quiet"] + expect(provider).to receive(:shell_out).with(*systemctl_isenabled).and_return(shell_out_failure) + expect(provider.is_enabled?).to be false + end + + it "should shellout to 'is-enabled' and return true if unit file is bad and sysv compat is enabled" do + bad_and_inactive = <<-STDOUT + ActiveState=inactive + UnitFileState=bad + STDOUT + with_systemctl_show(systemctl_path, bad_and_inactive) + systemctl_isenabled = [systemctl_path, "--system", "is-enabled", service_name, "--quiet"] + expect(provider).to receive(:shell_out).with(*systemctl_isenabled).and_return(shell_out_success) + expect(provider.is_enabled?).to be true + end end describe "is_masked?" do |