Class: Chef::Provider::Package::Windows::MSI

Inherits:
Object
  • Object
show all
Includes:
Mixin::ShellOut, ReservedNames::Win32::API::Installer
Defined in:
lib/chef/provider/package/windows/msi.rb

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from ReservedNames::Win32::API::Installer

#get_installed_version, #get_product_property, #open_package

Constructor Details

#initialize(resource, uninstall_entries) ⇒ MSI

Returns a new instance of MSI.



32
33
34
35
# File 'lib/chef/provider/package/windows/msi.rb', line 32

def initialize(resource, uninstall_entries)
  @new_resource = resource
  @uninstall_entries = uninstall_entries
end

Instance Attribute Details

#new_resourceObject (readonly)

Returns the value of attribute new_resource.



37
38
39
# File 'lib/chef/provider/package/windows/msi.rb', line 37

def new_resource
  @new_resource
end

#uninstall_entriesObject (readonly)

Returns the value of attribute uninstall_entries.



38
39
40
# File 'lib/chef/provider/package/windows/msi.rb', line 38

def uninstall_entries
  @uninstall_entries
end

Instance Method Details

#expand_options(options) ⇒ Object

From Chef::Provider::Package



41
42
43
# File 'lib/chef/provider/package/windows/msi.rb', line 41

def expand_options(options)
  options ? " #{options}" : ""
end

#install_packageObject



67
68
69
70
71
# File 'lib/chef/provider/package/windows/msi.rb', line 67

def install_package
  # We could use MsiConfigureProduct here, but we'll start off with msiexec
  Chef::Log.debug("#{new_resource} installing MSI package '#{new_resource.source}'")
  shell_out!("msiexec /qn /i \"#{new_resource.source}\" #{expand_options(new_resource.options)}", { :timeout => new_resource.timeout, :returns => new_resource.returns })
end

#installed_versionObject

Returns a version if the package is installed or nil if it is not.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/provider/package/windows/msi.rb', line 46

def installed_version
  if !new_resource.source.nil? && ::File.exist?(new_resource.source)
    Chef::Log.debug("#{new_resource} getting product code for package at #{new_resource.source}")
    product_code = get_product_property(new_resource.source, "ProductCode")
    Chef::Log.debug("#{new_resource} checking package status and version for #{product_code}")
    get_installed_version(product_code)
  else
    uninstall_entries.count == 0 ? nil : begin
      uninstall_entries.map { |entry| entry.display_version }.uniq
    end
  end
end

#package_versionObject



59
60
61
62
63
64
65
# File 'lib/chef/provider/package/windows/msi.rb', line 59

def package_version
  return new_resource.version if new_resource.version
  if !new_resource.source.nil? && ::File.exist?(new_resource.source)
    Chef::Log.debug("#{new_resource} getting product version for package at #{new_resource.source}")
    get_product_property(new_resource.source, "ProductVersion")
  end
end

#remove_packageObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/provider/package/windows/msi.rb', line 73

def remove_package
  # We could use MsiConfigureProduct here, but we'll start off with msiexec
  if !new_resource.source.nil? && ::File.exist?(new_resource.source)
    Chef::Log.debug("#{new_resource} removing MSI package '#{new_resource.source}'")
    shell_out!("msiexec /qn /x \"#{new_resource.source}\" #{expand_options(new_resource.options)}", { :timeout => new_resource.timeout, :returns => new_resource.returns })
  else
    uninstall_version = new_resource.version || installed_version
    uninstall_entries.select { |entry| [uninstall_version].flatten.include?(entry.display_version) }
      .map { |version| version.uninstall_string }.uniq.each do |uninstall_string|
      Chef::Log.debug("#{new_resource} removing MSI package version using '#{uninstall_string}'")
      uninstall_string += expand_options(new_resource.options)
      uninstall_string += " /Q" unless uninstall_string =~ / \/Q\b/
      shell_out!(uninstall_string, { :timeout => new_resource.timeout, :returns => new_resource.returns })
    end
  end
end