Tuesday, December 10, 2013

Using #getchef to deploy W2k8r2 Patches

I have come up with a way to deploy patches via Chef using 'batch' to call the cmd file that I use for how-to-script-citrix-and-ms-hot-fixes for RDSH and Citrix XA servers page.

My question that I am hoping to get answered is the following : Can windows_package call wusa.exe?

Here is what I am doing, is anyone doing similar or better process? I am really new to #getchef and ruby so  I am just pulling from some of the stuff that I have done on Altiris to get it working then add in attributes after it works correctly. If anyone has some better suggestions on how to do it or coding best practices, feel free to comment.  It is primitive, but it works.

w2k8_post_sp1_hotfixes

recipes::default.rb
#
# Cookbook Name:: w2k8_post_sp1_hotfixes
# Recipe:: default
#
# Copyright (C) 2013 Todd Pigram
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# make dir
batch node[:w2k8hf][:make] do
  code <<-EOH
  mkdir c:\\PostSP1
  EOH
  not_if {::File.exists?(node[:w2k8hf][:sleep])}
  not_if {reboot_pending?}
end

# download patches to server
# unzip patches to c:\
windows_zipfile node[:w2k8hf][:zip] do
  source node[:w2k8hf][:url]
  action :unzip
  not_if {::File.exists?(node[:w2k8hf][:sleep])}
  not_if {reboot_pending?}
end

# Install patches
batch node[:w2k8hf][:install] do
  code <<-EOH
  cd c:\\PostSp1
  c:\\postsp1\\installpostsp1.cmd
  EOH
  not_if {::File.exists?(node[:w2k8hf][:log])}
  not_if {reboot_pending?}
end

batch node[:w2k8hf][:remove] do
  code <<-EOH
  rmdir /s /q c:\\PostSp1
  EOH
  only_if {::File.exists?(node[:w2k8hf][:log])}
  not_if {reboot_pending?}
end

# if feature installs, schedule a reboot at end of chef run
windows_reboot 60 do
  reason 'cause chef said so'
  only_if {reboot_pending?}
end

attributes::default.rb
#
# Cookbook Name:: w2k8_post_sp1_hotfixes
# Recipe:: default
#
# Copyright (C) 2013 Todd Pigram
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
default[:w2k8hf][:make] = "make dir"
default[:w2k8hf][:remove] = "remove c:\\PostSp1"
default[:w2k8hf][:url] = "http://yourdomain.com/PostSP1.zip"
default[:w2k8hf][:install] = "install"
default[:w2k8hf][:log] = "C:/PostSp1_patches.log"
default[:w2k8hf][:sleep] = "C:/PostSp1/sleep.exe"
default[:w2k8hf][:zip] = "c:/PostSP1"

libraries::reboot_pending.rb - This is needed as I run this as part of the run list that takes a server from template all the way to having the XD 7.1 VDA installed.

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

No comments:

Post a Comment