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, #cookbook_name, #current_resource, #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, #get_preseed_file, #initialize, #lock_package, #multipackage_api_adapter, #options, #package_locked, #preseed_package, #preseed_resource, #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

#a_to_s, #clean_array, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_compact_timeout, #shell_out_compact_timeout!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #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::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!, #with_run_context

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

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

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

Instance Method Details

#action_installObject


123
124
125
126
127
128
129
130
131
132
# File 'lib/chef/provider/package/windows.rb', line 123

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


151
152
153
# File 'lib/chef/provider/package/windows.rb', line 151

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

Returns:

  • (Array)

    current_version(s) as an array


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

def current_version_array
  [ current_resource.version ]
end

#define_resource_requirementsObject


37
38
39
40
41
42
# 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.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
end

#have_any_matching_version?Boolean

Returns:

  • (Boolean)

176
177
178
# File 'lib/chef/provider/package/windows.rb', line 176

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


136
137
138
# File 'lib/chef/provider/package/windows.rb', line 136

def install_package(name, version)
  package_provider.install_package
end

#installer_typeObject


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
# File 'lib/chef/provider/package/windows.rb', line 73

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.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?


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

def load_current_resource
  @current_resource = Chef::Resource::WindowsPackage.new(new_resource.name)
  if downloadable_file_missing?
    Chef::Log.debug("We do not know the version of #{new_resource.source} because the file is not downloaded")
    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


145
146
147
148
# File 'lib/chef/provider/package/windows.rb', line 145

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

#package_providerObject


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/provider/package/windows.rb', line 58

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

#remove_package(name, version) ⇒ Object


140
141
142
# File 'lib/chef/provider/package/windows.rb', line 140

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


167
168
169
170
171
172
173
174
# File 'lib/chef/provider/package/windows.rb', line 167

def target_version_already_installed?(current_version, new_version)
  Chef::Log.debug("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