Class: Chef::Provider::Package::Msu

Inherits:
Chef::Provider::Package show all
Includes:
Mixin::Checksum, Mixin::ShellOut, Mixin::Uris
Defined in:
lib/chef/provider/package/msu.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #after_resource, #current_resource, #logger, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::Checksum

#checksum, #checksum_match?, #short_cksum

Methods included from Mixin::Uris

#as_uri, #uri_scheme?

Methods inherited from Chef::Provider::Package

#as_array, #check_resource_semantics!, #define_resource_requirements, #expand_options, #have_any_matching_version?, #initialize, #lock_package, #multipackage_api_adapter, #options, #package_locked, #prepare_for_installation, #preseed_package, #purge_package, #reconfig_package, #removing_package?, #target_version_already_installed?, #unlock_package, #upgrade_package, #version_compare, #version_equals?, #version_requirement_satisfied?

Methods included from Mixin::SubclassDirective

#subclass_directive

Methods inherited from Chef::Provider

action, action_description, action_descriptions, #action_nothing, #check_resource_semantics!, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #define_resource_requirements, #description, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #introduced, #load_after_resource, #node, #process_resource_requirements, provides, provides?, #recipe_name, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use, use_inline_resources, #validate_required_properties!, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from DSL::Secret

#default_secret_config, #default_secret_service, #secret, #with_secret_config, #with_secret_service

Methods included from DSL::RenderHelpers

#render_json, #render_toml, #render_yaml

Methods included from DSL::ReaderHelpers

#parse_file, #parse_json, #parse_toml, #parse_yaml

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::ChefVault

#chef_vault, #chef_vault_item, #chef_vault_item_for_environment

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from DSL::Recipe

#exec, #have_resource_class_for?, #resource_class_for

Methods included from DSL::Definitions

add_definition, #evaluate_resource_definition, #has_resource_definition?

Methods included from DSL::Resources

add_resource_dsl, remove_resource_dsl

Methods included from DSL::Cheffish

load_cheffish

Methods included from DSL::RebootPending

#reboot_pending?

Methods included from DSL::IncludeRecipe

#include_recipe, #load_recipe

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context

Methods included from DSL::Compliance

#include_input, #include_profile, #include_waiver

Constructor Details

This class inherits a constructor from Chef::Provider::Package

Instance Method Details

#candidate_versionObject



76
77
78
# File 'lib/chef/provider/package/msu.rb', line 76

def candidate_version
  @candidate_version ||= get_candidate_versions
end

#cleanup_after_convergeObject



159
160
161
162
# File 'lib/chef/provider/package/msu.rb', line 159

def cleanup_after_converge
  # delete the temp directory where the contents of msu file are extracted
  FileUtils.rm_rf(@temp_directory) if Dir.exist?(@temp_directory)
end

#default_download_cache_pathObject



101
102
103
104
105
106
# File 'lib/chef/provider/package/msu.rb', line 101

def default_download_cache_path
  uri = ::URI.parse(new_resource.source)
  filename = ::File.basename(::CGI.unescape(uri.path))
  file_cache_dir = Chef::FileCache.create_cache_path("package/")
  Chef::Util::PathHelper.cleanpath("#{file_cache_dir}/#{filename}")
end

#download_source_fileObject



86
87
88
89
90
# File 'lib/chef/provider/package/msu.rb', line 86

def download_source_file
  source_resource.run_action(:create)
  logger.trace("#{new_resource} fetched source file to #{source_resource.path}")
  source_resource.path
end

#extract_msu_contents(msu_file, destination) ⇒ Object



130
131
132
133
134
# File 'lib/chef/provider/package/msu.rb', line 130

def extract_msu_contents(msu_file, destination)
  with_os_architecture(nil) do
    shell_out!("#{ENV["SYSTEMROOT"]}\\system32\\expand.exe -f:* #{msu_file} #{destination}")
  end
end

#get_cab_package(cab_file) ⇒ Object



80
81
82
83
84
# File 'lib/chef/provider/package/msu.rb', line 80

def get_cab_package(cab_file)
  cab_resource = new_resource
  cab_resource.source = cab_file
  Chef::Provider::Package::Cab.new(cab_resource, nil)
end

#get_candidate_versionsObject



69
70
71
72
73
74
# File 'lib/chef/provider/package/msu.rb', line 69

def get_candidate_versions
  @cab_files.map do |cabfile|
    cab_pkg = get_cab_package(cabfile)
    cab_pkg.package_version
  end
end

#get_current_versionsObject



62
63
64
65
66
67
# File 'lib/chef/provider/package/msu.rb', line 62

def get_current_versions
  @cab_files.map do |cabfile|
    cab_pkg = get_cab_package(cabfile)
    cab_pkg.installed_version
  end
end

#install_package(name, version) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/chef/provider/package/msu.rb', line 108

def install_package(name, version)
  # use cab_package resource to install the extracted cab packages
  @cab_files.each do |cab_file|
    declare_resource(:cab_package, new_resource.name) do
      source cab_file
      timeout new_resource.timeout
      action :install
    end
  end
end

#load_current_resourceObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/provider/package/msu.rb', line 42

def load_current_resource
  @current_resource = Chef::Resource::MsuPackage.new(new_resource.name)

  # download file if source is a url
  msu_file = uri_scheme?(new_resource.source) ? download_source_file : new_resource.source

  # temp directory where the contents of msu file get extracted
  @temp_directory = Dir.mktmpdir("chef")
  extract_msu_contents(msu_file, @temp_directory)
  @cab_files = read_cab_files_from_xml(@temp_directory)

  if @cab_files.empty?
    raise Chef::Exceptions::Package, "Corrupt MSU package: MSU package XML does not contain any cab file"
  else
    current_resource.version(get_current_versions)
  end

  current_resource
end

#read_cab_files_from_xml(msu_dir) ⇒ Object

msu package can contain multiple cab files Reading cab files from xml to ensure the order of installation in case of multiple cab files



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/chef/provider/package/msu.rb', line 138

def read_cab_files_from_xml(msu_dir)
  # get the file with .xml extension
  xml_files = Dir.glob("#{msu_dir}/*.xml")
  cab_files = []

  if xml_files.empty?
    raise Chef::Exceptions::Package, "Corrupt MSU package: MSU package doesn't contain any xml file"
  else
    # msu package contains only single xml file. So using xml_files.first is sufficient
    doc = ::File.open(xml_files.first.to_s) { |f| REXML::Document.new f }
    locations = doc.elements.each("unattend/servicing/package/source") { |element| element.attributes["location"] }
    locations.each do |loc|
      cab_files << msu_dir + "/" + loc.attribute("location").value.split("\\")[1]
    end

    cab_files
  end

  cab_files
end

#remove_package(name, version) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/chef/provider/package/msu.rb', line 119

def remove_package(name, version)
  # use cab_package provider to remove the extracted cab packages
  @cab_files.each do |cab_file|
    declare_resource(:cab_package, new_resource.name) do
      source cab_file
      timeout new_resource.timeout
      action :remove
    end
  end
end

#source_resourceObject



92
93
94
95
96
97
98
99
# File 'lib/chef/provider/package/msu.rb', line 92

def source_resource
  @source_resource ||= declare_resource(:remote_file, new_resource.name) do
    path default_download_cache_path
    source new_resource.source
    checksum new_resource.checksum
    backup false
  end
end