Class: Chef::Provider::Package::Apt

Inherits:
Chef::Provider::Package show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provider/package/apt.rb

Direct Known Subclasses

Dpkg

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider::Package

#candidate_version

Attributes inherited from Chef::Provider

#action, #current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!

Methods inherited from Chef::Provider::Package

#action_install, #action_purge, #action_reconfig, #action_remove, #action_upgrade, #define_resource_requirements, #expand_options, #get_preseed_file, #initialize, #preseed_resource, #removing_package?, #target_version_already_installed?, #whyrun_supported?

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, #cleanup_after_converge, #cookbook_name, #define_resource_requirements, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #set_updated_status, #whyrun_mode?, #whyrun_supported?

Methods included from DSL::Recipe

#method_missing

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::DSL::Recipe

Instance Attribute Details

#is_virtual_packageObject

Returns the value of attribute is_virtual_package.



31
32
33
# File 'lib/chef/provider/package/apt.rb', line 31

def is_virtual_package
  @is_virtual_package
end

Instance Method Details

#check_package_state(package) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'lib/chef/provider/package/apt.rb', line 45

def check_package_state(package)
  Chef::Log.debug("#{@new_resource} checking package status for #{package}")
  installed = false

  shell_out!("apt-cache#{expand_options(default_release_options)} policy #{package}").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")
        @current_resource.version(nil)
      else
        Chef::Log.debug("#{@new_resource} current version is #{installed_version}")
        @current_resource.version(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!("apt-cache showpkg #{package}").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}]")
        installed = check_package_state(providers.keys.first)
      else
        Chef::Log.debug("#{@new_resource} candidate version is #{$1}")
        @candidate_version = $1
      end
    end
  end

  return installed
end

#default_release_optionsObject



40
41
42
43
# File 'lib/chef/provider/package/apt.rb', line 40

def default_release_options
  # Use apt::Default-Release option only if provider was explicitly defined
  "-o APT::Default-Release=#{@new_resource.default_release}" if @new_resource.provider && @new_resource.default_release
end

#install_package(name, version) ⇒ Object



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

def install_package(name, version)
  package_name = "#{name}=#{version}"
  package_name = name if @is_virtual_package
  run_command_with_systems_locale(
    :command => "apt-get -q -y#{expand_options(default_release_options)}#{expand_options(@new_resource.options)} install #{package_name}",
    :environment => {
      "DEBIAN_FRONTEND" => "noninteractive"
    }
  )
end

#load_current_resourceObject



33
34
35
36
37
38
# File 'lib/chef/provider/package/apt.rb', line 33

def load_current_resource
  @current_resource = Chef::Resource::Package.new(@new_resource.name)
  @current_resource.package_name(@new_resource.package_name)
  check_package_state(@new_resource.package_name)
  @current_resource
end

#preseed_package(preseed_file) ⇒ Object



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

def preseed_package(preseed_file)
  Chef::Log.info("#{@new_resource} pre-seeding package installation instructions")
  run_command_with_systems_locale(
    :command => "debconf-set-selections #{preseed_file}",
    :environment => {
      "DEBIAN_FRONTEND" => "noninteractive"
    }
  )
end

#purge_package(name, version) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/chef/provider/package/apt.rb', line 115

def purge_package(name, version)
  run_command_with_systems_locale(
    :command => "apt-get -q -y#{expand_options(@new_resource.options)} purge #{@new_resource.package_name}",
    :environment => {
      "DEBIAN_FRONTEND" => "noninteractive"
    }
  )
end

#reconfig_package(name, version) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/chef/provider/package/apt.rb', line 134

def reconfig_package(name, version)
  Chef::Log.info("#{@new_resource} reconfiguring")
  run_command_with_systems_locale(
    :command => "dpkg-reconfigure #{name}",
    :environment => {
      "DEBIAN_FRONTEND" => "noninteractive"
    }
  )
end

#remove_package(name, version) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/chef/provider/package/apt.rb', line 105

def remove_package(name, version)
  package_name = "#{name}"
  run_command_with_systems_locale(
    :command => "apt-get -q -y#{expand_options(@new_resource.options)} remove #{package_name}",
    :environment => {
      "DEBIAN_FRONTEND" => "noninteractive"
    }
  )
end

#upgrade_package(name, version) ⇒ Object



101
102
103
# File 'lib/chef/provider/package/apt.rb', line 101

def upgrade_package(name, version)
  install_package(name, version)
end