Tuesday, August 19, 2014

Chef and Windows Reboots - My personal experience #getchef

I have been seeing a few posts on issues with Windows reboots and Chef. I actually had this issue last year while trying to decide between Puppet and Chef. I posted on the issue on here back in either Nov or October 2013.

Alex Vinyar, from then Opscode, now Chef, provided me with a custom library file that I have used since up until it was added to chef on, I believe, in version 11.12 of the Chef Client.

All I had to do was make sure you include windows::reboot_handler first in the nodes run list or role.

 Then include a not_if {reboot_pending?} on your recipes and on the windows_reboot section add a only_if {reboot_pending?}.

Here are couple of examples

Both of these are for deploying RDSH for some Citrix XenApp servers. These are different recipes from the same cookbook.

I do have the custom library file in this cookbook

library/reboot_pending.rb

class Chef
  class Resource
    # include Chef::Mixin::ShellOut

    def reboot_pending?
      # Any files listed here means reboot needed
      (Registry.key_exists?('HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations') &&
        Registry.get_value('HKLM\SYSTEM\CurrentControlSet\Control\Session Manager','PendingFileRenameOperations').any?) ||
      # 1 for any value means reboot pending
      # "9306cdfc-c4a1-4a22-9996-848cb67eddc3"=1
      (Registry.key_exists?('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired') &&
        Registry.get_values('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired').select{|v| v[2] == 1 }.any?) ||
      # 1 or 2 for 'Flags' value means reboot pending
      (Registry.key_exists?('HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile') &&
        [1,2].include?(Registry::get_value('HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile','Flags'))) ||
      # added by Alex
      Registry.key_exists?('HKLM\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending')
    end

  end
end



w2k8r2.rb - Using powershell code I have found its easier to split them up.

# install RDS
powershell node['w2k8']['rds'] do
  code <<-eoh p="">  Import-Module ServerManager
  Add-WindowsFeature RDS-RD-Server
  EOH
  not_if {reboot_pending?}
end


# Install desktop experience
powershell node['w2k8']['deskexp'] do
  code <<-eoh p="">  Import-Module ServerManager
  Add-WindowsFeature Desktop-Experience
  EOH
  not_if {reboot_pending?}
end

powershell node['w2k8']['xps'] do
  code <<-eoh p="">  Import-Module ServerManager
  Add-WindowsFeature XPS-Viewer
  EOH
  not_if {reboot_pending?}
end

windows_reboot 30 do
  reason 'Chef said to'
  only_if {reboot_pending?}
end

w2012r2.rb

%w{ Xps-Foundation-Xps-Viewer Remote-Desktop-Services ServerMediaFoundation AppServer DesktopExperience }.each do |feature|
  windows_feature feature do
    action :install
    not_if {reboot_pending?}
  end
end

windows_reboot 30 do
  reason 'Chef said to'
  only_if {reboot_pending?}
end

Per the latest windows cookbook :


Attributes

  • node['windows']['allow_pending_reboots'] - used to configure the WindowsRebootHandler (via the windows::reboot_handler recipe) to act on pending reboots. default is true (ie act on pending reboots). The value of this attribute only has an effect if the windows::reboot_handler is in a node's run list.
I have performed some testes on w2012/w2012r2 without the custom library file, and it worked great. I have not tried it on w2k8r2 though....Hope this helps.

No comments:

Post a Comment