Skip to content

Commit

Permalink
Merge pull request #4 from mattlqx/reboot
Browse files Browse the repository at this point in the history
look for scheduled reboot instead of pending
  • Loading branch information
mattlqx authored Nov 12, 2019
2 parents 6a8f202 + 100ccf3 commit ffeb085
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ There is some monkey-patching that needs to happen to have the option of a clean
<td><tt>C:\\.kill_chef</tt> on Windows, <tt>/.kill_chef</tt> on Linux</td>
</tr>
<tr>
<td><tt>['kill_switch']['when_reboot_pending']</tt></td>
<td><tt>['kill_switch']['when_reboot_scheduled']</tt></td>
<td>Bool</td>
<td>Engage kill switch if there is a pending reboot</td>
<td>Engage kill switch if there is a scheduled reboot (only supported on Ubuntu 16+ presently)</td>
<td><tt>true</tt></td>
</tr>
</table>
Expand Down
4 changes: 3 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

default['kill_switch']['engage'] = false
default['kill_switch']['normal_exit'] = false
default['kill_switch']['touch_dir'] = case node['os']
when 'windows'
'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
14 changes: 14 additions & 0 deletions libraries/reboots.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/recipes/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ffeb085

Please sign in to comment.