Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate wsus_client_update LWRP to custom resource #33

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 0 additions & 94 deletions providers/update.rb

This file was deleted.

73 changes: 71 additions & 2 deletions resources/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,74 @@
# limitations under the License.
#

actions :download, :install
default_action [:download, :install]
default_action %i[download install]

action :download do
updates_to_download = updates.reject(&:IsDownloaded)
if updates_to_download.count != 0
Chef::Log.info "Windows Auto Update: #{updates_to_download.count} update(s) to download."
converge_by "downloading #{updates_to_download.count} update(s)" do
# Transforms to a new update collection
update_collection = ::WIN32OLE.new('Microsoft.Update.UpdateColl')
updates_to_download.each { |update| update_collection.Add update }
# Performs download
downloader = session.CreateUpdateDownloader
downloader.Updates = update_collection
# Verifies operation result
assert_result 'Download', downloader.Download
end
end
end

action :install do
downloaded_updates = updates.select(&:IsDownloaded)
if downloaded_updates.count != 0
Chef::Log.info "Windows Auto Update: #{downloaded_updates.count} update(s) to install."
converge_by "installing #{downloaded_updates.count} update(s)" do
# Transforms to a new update collection
update_collection = ::WIN32OLE.new('Microsoft.Update.UpdateColl')
downloaded_updates.each do |update|
unless update.EulaAccepted
converge_by "accepting EULA for #{update.Title}" do
update.AcceptEula
end
end
update_collection.Add update
end
# Performs install
installer = session.CreateUpdateInstaller
installer.ForceQuiet = true
installer.Updates = update_collection
# Verifies operation result
assert_result 'Installation', installer.Install
end
end
end

action_class do
def whyrun_supported?
true
end

# ResultCode values: http://msdn.microsoft.com/aa387095
SUCCESS_CODE = 2
def assert_result(action, result)
return unless result.HResult != 0 || result.ResultCode != SUCCESS_CODE
raise "#{action} failed. (Error code #{result.HResult})"
end

def updates
node.run_state['wsus_client_updates'] ||= [].tap do |updates|
# Searches non installed updates
search_result = session.CreateUpdateSearcher.Search 'IsInstalled=0'
# Transforms to ruby array for future use
search_result.Updates.each { |update| updates << update }
end
end

def session
require 'win32ole'
# API documentation: http://msdn.microsoft.com/aa387099
@session ||= ::WIN32OLE.new('Microsoft.Update.Session')
end
end
File renamed without changes.