Class: Chef::Provider::Package::Apt
- Inherits:
-
Chef::Provider::Package
- Object
- Chef::Provider
- Chef::Provider::Package
- Chef::Provider::Package::Apt
- Defined in:
- lib/chef/provider/package/apt.rb
Constant Summary
Constants included from Mixin::ShellOut
Mixin::ShellOut::DEPRECATED_OPTIONS
Instance Attribute Summary collapse
-
#is_virtual_package ⇒ Object
return [Hash] mapping of package name to Boolean value.
Attributes inherited from Chef::Provider::Package
Attributes inherited from Chef::Provider
#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context
Instance Method Summary collapse
- #check_all_packages_state(package) ⇒ Object
- #check_package_state(pkg) ⇒ Object
- #default_release_options ⇒ Object
- #define_resource_requirements ⇒ Object
-
#initialize(new_resource, run_context) ⇒ Apt
constructor
A new instance of Apt.
- #install_package(name, version) ⇒ Object
- #load_current_resource ⇒ Object
- #preseed_package(preseed_file) ⇒ Object
- #purge_package(name, version) ⇒ Object
- #reconfig_package(name, version) ⇒ Object
- #remove_package(name, version) ⇒ Object
- #upgrade_package(name, version) ⇒ Object
Methods inherited from Chef::Provider::Package
#action_install, #action_purge, #action_reconfig, #action_remove, #action_upgrade, #as_array, #check_resource_semantics!, #expand_options, #get_preseed_file, #have_any_matching_version?, #multipackage_api_adapter, #preseed_resource, #removing_package?, #target_version_already_installed?, #whyrun_supported?
Methods included from Mixin::SubclassDirective
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 Mixin::Command
#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale
Methods included from Mixin::Command::Windows
Methods included from Mixin::Command::Unix
Methods inherited from Chef::Provider
#action_nothing, #check_resource_semantics!, #cleanup_after_converge, #converge_by, #converge_if_changed, #events, include_resource_dsl, include_resource_dsl_module, #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 DeprecatedLWRPClass
#const_missing, #register_deprecated_lwrp_class
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
Constructor Details
#initialize(new_resource, run_context) ⇒ Apt
Returns a new instance of Apt.
34 35 36 37 |
# File 'lib/chef/provider/package/apt.rb', line 34 def initialize(new_resource, run_context) super @is_virtual_package = {} end |
Instance Attribute Details
#is_virtual_package ⇒ Object
return [Hash] mapping of package name to Boolean value
32 33 34 |
# File 'lib/chef/provider/package/apt.rb', line 32 def is_virtual_package @is_virtual_package end |
Instance Method Details
#check_all_packages_state(package) ⇒ Object
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 |
# File 'lib/chef/provider/package/apt.rb', line 112 def check_all_packages_state(package) installed_version = {} candidate_version = {} installed = {} [package].flatten.each do |pkg| ret = check_package_state(pkg) is_virtual_package[pkg] = ret[:is_virtual_package] installed[pkg] = ret[:installed] installed_version[pkg] = ret[:installed_version] candidate_version[pkg] = ret[:candidate_version] end if package.is_a?(Array) @candidate_version = [] final_installed_version = [] [package].flatten.each do |pkg| @candidate_version << candidate_version[pkg] final_installed_version << installed_version[pkg] end @current_resource.version(final_installed_version) else @candidate_version = candidate_version[package] @current_resource.version(installed_version[package]) end end |
#check_package_state(pkg) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 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 |
# File 'lib/chef/provider/package/apt.rb', line 60 def check_package_state(pkg) is_virtual_package = false installed = false installed_version = nil candidate_version = nil shell_out_with_timeout!("apt-cache#{()} policy #{pkg}").stdout.each_line do |line| case line when /^\s{2}Installed: (.+)$/ installed_version = $1 if installed_version == "(none)" Chef::Log.debug("#{@new_resource} current version is nil") installed_version = nil else Chef::Log.debug("#{@new_resource} current version is #{installed_version}") installed = true end when /^\s{2}Candidate: (.+)$/ candidate_version = $1 if candidate_version == "(none)" # This may not be an appropriate assumption, but it shouldn't break anything that already worked -- btm is_virtual_package = true showpkg = shell_out_with_timeout!("apt-cache showpkg #{pkg}").stdout providers = Hash.new showpkg.rpartition(/Reverse Provides: ?#{$/}/)[2].each_line do |line| provider, version = line.split providers[provider] = version end # Check if the package providing this virtual package is installed num_providers = providers.length raise Chef::Exceptions::Package, "#{@new_resource.package_name} has no candidate in the apt-cache" if num_providers == 0 # apt will only install a virtual package if there is a single providing package raise Chef::Exceptions::Package, "#{@new_resource.package_name} is a virtual package provided by #{num_providers} packages, you must explicitly select one to install" if num_providers > 1 # Check if the package providing this virtual package is installed Chef::Log.info("#{@new_resource} is a virtual package, actually acting on package[#{providers.keys.first}]") ret = check_package_state(providers.keys.first) installed = ret[:installed] installed_version = ret[:installed_version] else Chef::Log.debug("#{@new_resource} candidate version is #{$1}") end end end return { installed_version: installed_version, installed: installed, candidate_version: candidate_version, is_virtual_package: is_virtual_package, } end |
#default_release_options ⇒ Object
55 56 57 58 |
# File 'lib/chef/provider/package/apt.rb', line 55 def # Use apt::Default-Release option only if provider supports it "-o APT::Default-Release=#{@new_resource.default_release}" if @new_resource.respond_to?(:default_release) && @new_resource.default_release end |
#define_resource_requirements ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/chef/provider/package/apt.rb', line 46 def define_resource_requirements super requirements.assert(:all_actions) do |a| a.assertion { !@new_resource.source } a.(Chef::Exceptions::Package, "apt package provider cannot handle source attribute. Use dpkg provider instead") end end |
#install_package(name, version) ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/chef/provider/package/apt.rb', line 139 def install_package(name, version) name_array = [ name ].flatten version_array = [ version ].flatten package_name = name_array.zip(version_array).map do |n, v| is_virtual_package[n] ? n : "#{n}=#{v}" end.join(" ") run_noninteractive("apt-get -q -y#{()}#{(@new_resource.)} install #{package_name}") end |
#load_current_resource ⇒ Object
39 40 41 42 43 44 |
# File 'lib/chef/provider/package/apt.rb', line 39 def load_current_resource @current_resource = Chef::Resource::Package.new(@new_resource.name) @current_resource.package_name(@new_resource.package_name) check_all_packages_state(@new_resource.package_name) @current_resource end |
#preseed_package(preseed_file) ⇒ Object
162 163 164 165 |
# File 'lib/chef/provider/package/apt.rb', line 162 def preseed_package(preseed_file) Chef::Log.info("#{@new_resource} pre-seeding package installation instructions") run_noninteractive("debconf-set-selections #{preseed_file}") end |
#purge_package(name, version) ⇒ Object
157 158 159 160 |
# File 'lib/chef/provider/package/apt.rb', line 157 def purge_package(name, version) package_name = [ name ].flatten.join(" ") run_noninteractive("apt-get -q -y#{(@new_resource.)} purge #{package_name}") end |
#reconfig_package(name, version) ⇒ Object
167 168 169 170 171 |
# File 'lib/chef/provider/package/apt.rb', line 167 def reconfig_package(name, version) package_name = [ name ].flatten.join(" ") Chef::Log.info("#{@new_resource} reconfiguring") run_noninteractive("dpkg-reconfigure #{package_name}") end |
#remove_package(name, version) ⇒ Object
152 153 154 155 |
# File 'lib/chef/provider/package/apt.rb', line 152 def remove_package(name, version) package_name = [ name ].flatten.join(" ") run_noninteractive("apt-get -q -y#{(@new_resource.)} remove #{package_name}") end |
#upgrade_package(name, version) ⇒ Object
148 149 150 |
# File 'lib/chef/provider/package/apt.rb', line 148 def upgrade_package(name, version) install_package(name, version) end |