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

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider::Package

#candidate_version

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#shell_out, #shell_out!

Methods inherited from Chef::Provider::Package

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

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, build_from_file, #cookbook_name, #initialize, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

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

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



40
41
42
43
44
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
# File 'lib/chef/provider/package/apt.rb', line 40

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

  shell_out!("apt-cache 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

#install_package(name, version) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/chef/provider/package/apt.rb', line 85

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(@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(name, version) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chef/provider/package/apt.rb', line 119

def preseed_package(name, version)
  preseed_file = get_preseed_file(name, version)
  if 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
end

#purge_package(name, version) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/chef/provider/package/apt.rb', line 110

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



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

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



100
101
102
103
104
105
106
107
108
# File 'lib/chef/provider/package/apt.rb', line 100

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



96
97
98
# File 'lib/chef/provider/package/apt.rb', line 96

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