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

Inherits:
Chef::Provider::Package show all
Includes:
Mixin::Checksum, Mixin::Uris
Defined in:
lib/chef/provider/package/windows.rb,
lib/chef/provider/package/windows/exe.rb,
lib/chef/provider/package/windows/msi.rb,
lib/chef/provider/package/windows/registry_uninstall_entry.rb

Defined Under Namespace

Classes: Exe, MSI, RegistryUninstallEntry

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods included from Mixin::Checksum

#checksum, #short_cksum

Methods included from Mixin::Uris

#as_uri, #uri_scheme?

Methods inherited from Chef::Provider::Package

#action_lock, #action_unlock, #as_array, #check_resource_semantics!, #expand_options, #initialize, #lock_package, #multipackage_api_adapter, #options, #package_locked, #prepare_for_installation, #preseed_package, #purge_package, #reconfig_package, #removing_package?, #unlock_package, #upgrade_package, #version_requirement_satisfied?

Methods included from Mixin::SubclassDirective

#subclass_directive

Methods included from Mixin::ShellOut

apply_default_env, maybe_add_timeout, #shell_out, #shell_out!

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #description, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #introduced, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #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 Mixin::PowershellExec

#powershell_exec

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::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 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

Constructor Details

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

Instance Method Details

#action_installObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/provider/package/windows.rb', line 141

def action_install
  if uri_scheme?(new_resource.source)
    download_source_file
    load_current_resource
  else
    validate_content!
  end

  super
end

#candidate_versionString

Returns candidate_version.

Returns:

  • (String)

    candidate_version



169
170
171
# File 'lib/chef/provider/package/windows.rb', line 169

def candidate_version
  @candidate_version ||= (new_resource.version || "latest")
end

#current_version_arrayArray

this package provider does not support package arrays However, There may be multiple versions for a single package so the first element may be a nested array

FIXME: this breaks the semantics of the superclass and needs to get unwound. Since these package providers don’t support multipackage they can’t put multiple versions into this array. The windows package managers need this in order to uninstall multiple installed version, and they should track that in something like an ‘uninstall_version_array` of their own. The superclass does not implement this kind of feature. Doing this here breaks LSP and will create bugs since the superclass will not expect it at all. The `current_resource.version` also MUST NOT be an array if the package provider is not multipackage. The existing implementation of package_provider.installed_version should probably be what `uninstall_version_array` is, and then that list should be sorted and last/first’d into the current_resource.version. The current_version_array method was not intended to be overwritten by sublasses (but ruby provides no feature to block doing so – it is already marked as private).

Returns:

  • (Array)

    current_version(s) as an array



189
190
191
# File 'lib/chef/provider/package/windows.rb', line 189

def current_version_array
  [ current_resource.version ]
end

#define_resource_requirementsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chef/provider/package/windows.rb', line 37

def define_resource_requirements
  requirements.assert(:install) do |a|
    a.assertion { new_resource.source || msi? }
    a.failure_message Chef::Exceptions::NoWindowsPackageSource, "Source for package #{new_resource.package_name} must be specified in the resource's source property for package to be installed because the package_name property is used to test for the package installation state for this package type."
  end

  unless uri_scheme?(new_resource.source)
    requirements.assert(:install) do |a|
      a.assertion { ::File.exist?(new_resource.source) }
      a.failure_message Chef::Exceptions::Package, "Source for package #{new_resource.package_name} does not exist"
      a.whyrun "Assuming source file #{new_resource.source} would have been created."
    end
  end
end

#have_any_matching_version?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/chef/provider/package/windows.rb', line 210

def have_any_matching_version?
  target_version_already_installed?(current_resource.version, new_resource.version)
end

#install_package(name, version) ⇒ Object

Chef::Provider::Package action_install + action_remove call install_package + remove_package Pass those calls to the correct sub-provider



154
155
156
# File 'lib/chef/provider/package/windows.rb', line 154

def install_package(name, version)
  package_provider.install_package
end

#installer_typeObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chef/provider/package/windows.rb', line 91

def installer_type
  # Depending on the installer, we may need to examine installer_type or
  # source attributes, or search for text strings in the installer file
  # binary to determine the installer type for the user. Since the file
  # must be on disk to do so, we have to make this choice in the provider.
  @installer_type ||= begin
    return :msi if msi?

    if new_resource.installer_type
      new_resource.installer_type
    elsif source_location.nil?
      inferred_registry_type
    else
      basename = ::File.basename(source_location)
      file_extension = basename.split(".").last.downcase

      # search the binary file for installer type
      ::Kernel.open(::File.expand_path(source_location), "rb") do |io|
        filesize = io.size
        bufsize = 4096 # read 4K buffers
        overlap = 16 # bytes to overlap between buffer reads

        until io.eof
          contents = io.read(bufsize)

          case contents
          when /inno/i # Inno Setup
            return :inno
          when /wise/i # Wise InstallMaster
            return :wise
          when /nullsoft/i # Nullsoft Scriptable Install System
            return :nsis
          end

          if io.tell < filesize
            io.seek(io.tell - overlap)
          end
        end

        # if file is named 'setup.exe' assume installshield
        if basename == "setup.exe"
          :installshield
        else
          raise Chef::Exceptions::CannotDetermineWindowsInstallerType, "Installer type for Windows Package '#{new_resource.package_name}' not specified and cannot be determined from file extension '#{file_extension}'"
        end
      end
    end
  end
end

#load_current_resourceObject

load_current_resource is run in Chef::Provider#run_action when not in whyrun_mode?



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chef/provider/package/windows.rb', line 53

def load_current_resource
  @current_resource = Chef::Resource::WindowsPackage.new(new_resource.name)
  if downloadable_file_missing?
    logger.trace("We do not know the version of #{new_resource.source} because the file is not downloaded")
    # FIXME: this label should not be used.  It could be set to nil.  Probably what should happen is that
    # if the file hasn't been downloaded then load_current_resource must download the file here, and then
    # the else clause to set current_resource.version can always be run.  Relying on a side-effect here
    # produces at least less readable code, if not outright buggy...  (and I'm assuming that this isn't
    # wholly just a bug -- since if we only need the package_name to determine if its installed then we
    # need this, so I'm assuming we need to download the file to pull out the name in order to check
    # the registry -- which it still feels like we get wrong in the sense we're forcing always downloading
    # and then always installing(?) which violates idempotency -- and I'm having to think way too hard
    # about this and would need to go surfing around the code to determine what actually happens, probably
    # in every different package_provider...)
    current_resource.version(:unknown.to_s)
  else
    current_resource.version(package_provider.installed_version)
    new_resource.version(package_provider.package_version) if package_provider.package_version
  end

  current_resource
end

#new_version_arrayArray

Returns new_version(s) as an array.

Returns:

  • (Array)

    new_version(s) as an array



163
164
165
166
# File 'lib/chef/provider/package/windows.rb', line 163

def new_version_array
  # Because the one in the parent caches things
  [new_resource.version]
end

#package_providerObject



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

def package_provider
  @package_provider ||= begin
    case installer_type
    when :msi
      logger.trace("#{new_resource} is MSI")
      require_relative "windows/msi"
      Chef::Provider::Package::Windows::MSI.new(resource_for_provider, uninstall_registry_entries)
    else
      logger.trace("#{new_resource} is EXE with type '#{installer_type}'")
      require_relative "windows/exe"
      Chef::Provider::Package::Windows::Exe.new(resource_for_provider, installer_type, uninstall_registry_entries)
    end
  end
end

#remove_package(name, version) ⇒ Object



158
159
160
# File 'lib/chef/provider/package/windows.rb', line 158

def remove_package(name, version)
  package_provider.remove_package
end

#target_version_already_installed?(current_version, new_version) ⇒ Boolean

Returns true if new_version is equal to or included in current_version.

Parameters:

  • current_version (String)

    one or more versions currently installed

  • new_version (String)

    version of the new resource

Returns:

  • (Boolean)

    true if new_version is equal to or included in current_version



197
198
199
# File 'lib/chef/provider/package/windows.rb', line 197

def target_version_already_installed?(current_version, new_version)
  version_equals?(current_version, new_version)
end

#version_equals?(current_version, new_version) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
208
# File 'lib/chef/provider/package/windows.rb', line 201

def version_equals?(current_version, new_version)
  logger.trace("Checking if #{new_resource} version '#{new_version}' is already installed. #{current_version} is currently installed")
  if current_version.is_a?(Array)
    current_version.include?(new_version)
  else
    new_version == current_version
  end
end