diff --git a/README.md b/README.md index b8a4078..ebff6a5 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,9 @@ There is some monkey-patching that needs to happen to have the option of a clean C:\\.kill_chef on Windows, /.kill_chef on Linux - ['kill_switch']['when_reboot_pending'] + ['kill_switch']['when_reboot_scheduled'] Bool - Engage kill switch if there is a pending reboot + Engage kill switch if there is a scheduled reboot (only supported on Ubuntu 16+ presently) true diff --git a/attributes/default.rb b/attributes/default.rb index a3f80d4..523bd23 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + default['kill_switch']['engage'] = false default['kill_switch']['normal_exit'] = false default['kill_switch']['touch_dir'] = case node['os'] @@ -5,4 +7,4 @@ 'C:' end default['kill_switch']['touch_file'] = File.join(node['kill_switch']['touch_dir'].to_s, '.kill_chef') -default['kill_switch']['when_reboot_pending'] = true +default['kill_switch']['when_reboot_scheduled'] = true diff --git a/libraries/reboots.rb b/libraries/reboots.rb new file mode 100644 index 0000000..87f66a9 --- /dev/null +++ b/libraries/reboots.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +def reboot_scheduled? + case node['platform'] + when 'ubuntu' + if node['platform_version'].to_i >= 16 + cmd = Mixlib::ShellOut.new('busctl get-property org.freedesktop.login1 /org/freedesktop/login1 ' \ + 'org.freedesktop.login1.Manager ScheduledShutdown') + cmd.run_command + return cmd.exitstatus == 0 && (cmd.stdout.include?('"poweroff"') || cmd.stdout.include?('"reboot"')) + end + end + false +end diff --git a/metadata.rb b/metadata.rb index 08ca7c7..f1e7440 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,7 +4,7 @@ license 'MIT' description 'Kill switch to prevent Chef runs from occuring' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '1.1.0' +version '1.1.1' chef_version '>= 12.1' if respond_to?(:chef_version) %w[ubuntu debian fedora centos redhat oracle scientific amazon freebsd openbsd mac_os_x solaris2 opensuse opensuseleap diff --git a/recipes/default.rb b/recipes/default.rb index 316acad..1bb7ff8 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -10,8 +10,8 @@ reason = "node attribute ['kill_switch']['engage']" elsif File.exist?(node['kill_switch']['touch_file']) reason = "touch file #{node['kill_switch']['touch_file']}" -elsif node['kill_switch']['when_reboot_pending'] && reboot_pending? - reason = 'reboot pending' +elsif node['kill_switch']['when_reboot_scheduled'] && reboot_scheduled? + reason = 'reboot scheduled' else return end diff --git a/spec/unit/recipes/default_spec.rb b/spec/unit/recipes/default_spec.rb index cf5c357..f1a2461 100644 --- a/spec/unit/recipes/default_spec.rb +++ b/spec/unit/recipes/default_spec.rb @@ -67,7 +67,7 @@ class FakeException < RuntimeError; end chef_run.node.normal['kill_switch']['engage'] = false allow(File).to receive(:exist?).and_call_original allow(File).to receive(:exist?).with('/.kill_chef').and_return(false) - allow_any_instance_of(Chef::Recipe).to receive(:reboot_pending?).and_return(true) + allow_any_instance_of(Chef::Recipe).to receive(:reboot_scheduled?).and_return(true) end it 'exits noisily' do