Class: Wright::Provider::Package::Apt

Inherits:
Wright::Provider::Package show all
Defined in:
lib/wright/provider/package/apt.rb

Overview

Apt package provider. Used as a provider for Resource::Package on Debian-based systems.

Constant Summary

Constants inherited from Wright::Provider

Wright::Provider::PROVIDER_DIR

Instance Method Summary collapse

Methods inherited from Wright::Provider::Package

#install, #installed?, #package_name, #package_options, #package_version, #remove, #uptodate?

Methods inherited from Wright::Provider

#exec_or_fail, #initialize, #unless_dry_run, #unless_uptodate, #updated?

Constructor Details

This class inherits a constructor from Wright::Provider

Instance Method Details

#apt_get(action, options, package, version = nil) ⇒ Object (private)


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

def apt_get(action, options, package, version = nil)
  package_version = version.nil? ? '' : "=#{version}"
  cmd = 'apt-get'
  args = [*options, action.to_s, '-qy', package + package_version]
  exec_or_fail(cmd, args, "cannot #{action} package '#{package}'")
end

#envObject (private)


63
64
65
# File 'lib/wright/provider/package/apt.rb', line 63

def env
  { 'DEBIAN_FRONTEND' => 'noninteractive' }
end

#install_packageObject (private)


26
27
28
# File 'lib/wright/provider/package/apt.rb', line 26

def install_package
  apt_get(:install, package_options, package_name, package_version)
end

#installed_versionsArray<String>

Returns the installed package versions.

Returns:

  • (Array<String>)

    the installed package versions


13
14
15
16
17
18
19
20
21
22
# File 'lib/wright/provider/package/apt.rb', line 13

def installed_versions
  package = "package '#{package_name}'"
  err = "Cannot determine installed versions for #{package}"
  apt_policy = exec_or_fail('apt-cache', ['policy', package_name], err)

  version_re = /(?!\(none\)).*/
  installed_re = /^  Installed: (?<version>#{version_re})$/
  match = installed_re.match(apt_policy)
  match ? [match['version']] : virtual_package_versions
end

#remove_packageObject (private)


30
31
32
# File 'lib/wright/provider/package/apt.rb', line 30

def remove_package
  apt_get(:remove, package_options, package_name)
end

#reverse_providesObject (private)


56
57
58
59
60
61
# File 'lib/wright/provider/package/apt.rb', line 56

def reverse_provides
  err = 'Error executing apt-cache'
  showpkg = exec_or_fail('apt-cache', ['showpkg', package_name], err)
  packages = showpkg.partition("Reverse Provides: \n").last.split("\n")
  Hash[packages.map { |p| p.split(' ', 2) }]
end

#virtual_package_installed?Boolean (private)

TODO:

replace the OpenStruct hack below by a direct instantiation of Resource::Package as soon as the resource-provider mapping can be changed more easily

Returns:

  • (Boolean)

48
49
50
51
52
53
54
# File 'lib/wright/provider/package/apt.rb', line 48

def virtual_package_installed?
  reverse_provides.any? do |name, version|
    resource = OpenStruct.new(name: name, version: version)
    package = Wright::Provider::Package::Apt.new(resource)
    package.installed?
  end
end

#virtual_package_versionsObject (private)


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

def virtual_package_versions
  virtual_package_installed? ? ['virtual'] : []
end