Class: ForemanMaintain::PackageManager::Apt

Inherits:
Base
  • Object
show all
Defined in:
lib/foreman_maintain/package_manager/apt.rb

Instance Method Summary collapse

Methods inherited from Base

#files_not_owned_by_package, #lock_versions, #modules_supported?, #unlock_versions, #versions_locked?

Instance Method Details

#apt_action(action, packages, with_status: false, assumeyes: false, valid_exit_statuses: [0]) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/foreman_maintain/package_manager/apt.rb', line 63

def apt_action(action, packages, with_status: false, assumeyes: false, valid_exit_statuses: [0])
  apt_options = []
  packages = [packages].flatten(1)
  apt_options << '-y' if assumeyes
  apt_options_s = apt_options.empty? ? '' : ' ' + apt_options.join(' ')
  packages_s = packages.empty? ? '' : ' ' + packages.join(' ')
  if with_status
    sys.execute_with_status("apt-get#{apt_options_s} #{action}#{packages_s}",
      :interactive => !assumeyes)
  else
    sys.execute!("apt-get#{apt_options_s} #{action}#{packages_s}",
      :interactive => !assumeyes, :valid_exit_statuses => valid_exit_statuses)
  end
end

#check_update(packages: nil, with_status: false) ⇒ Object



42
43
44
# File 'lib/foreman_maintain/package_manager/apt.rb', line 42

def check_update(packages: nil, with_status: false)
  apt_action('upgrade --dry-run', packages, :with_status => with_status)
end

#clean_cache(assumeyes: false) ⇒ Object



27
28
29
# File 'lib/foreman_maintain/package_manager/apt.rb', line 27

def clean_cache(assumeyes: false)
  apt_action('clean', :assumeyes => assumeyes)
end

#find_installed_package(name, queryfm = '') ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/foreman_maintain/package_manager/apt.rb', line 31

def find_installed_package(name, queryfm = '')
  return unless installed?(name)

  dpkg_cmd = "dpkg-query --show #{name}"
  unless queryfm.empty?
    dpkg_cmd = "dpkg-query --showformat='#{queryfm}' --show #{name}"
  end
  _, result = sys.execute_with_status(dpkg_cmd)
  result
end

#install(packages, assumeyes: false) ⇒ Object



14
15
16
# File 'lib/foreman_maintain/package_manager/apt.rb', line 14

def install(packages, assumeyes: false)
  apt_action('install', packages, :assumeyes => assumeyes)
end

#installed?(packages) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
# File 'lib/foreman_maintain/package_manager/apt.rb', line 5

def installed?(packages)
  packages_list = [packages].flatten(1).map { |pkg| "'#{pkg}'" }.join(' ')
  status, output = sys.execute_with_status(%(dpkg --status #{packages_list}))
  return false if status != 0

  status_of_pkg = output.split("\n").grep(/^Status:/).first
  status_of_pkg.include?('installed')
end

#list_installed_packages(queryfm = '${binary:Package}-${VERSION}\n') ⇒ Object



51
52
53
54
55
56
57
# File 'lib/foreman_maintain/package_manager/apt.rb', line 51

def list_installed_packages(queryfm = '${binary:Package}-${VERSION}\n')
  # The queryfm should only include valid tag(s) as per `dpkg-query` man page.
  # If any special formatting is required with querytag then it should be provided with tag i.e,
  # querytag = "--%{VERSION}"
  # The queryfm string must end with '\n'
  sys.execute!("dpkg-query --showformat='#{queryfm}' -W").split("\n")
end

#remove(packages, assumeyes: false) ⇒ Object



18
19
20
# File 'lib/foreman_maintain/package_manager/apt.rb', line 18

def remove(packages, assumeyes: false)
  apt_action('remove', packages, :assumeyes => assumeyes)
end

#update(packages = [], assumeyes: false) ⇒ Object



22
23
24
25
# File 'lib/foreman_maintain/package_manager/apt.rb', line 22

def update(packages = [], assumeyes: false)
  action = packages.any? ? '--only-upgrade install' : 'upgrade'
  apt_action(action, packages, :assumeyes => assumeyes)
end

#update_available?(package) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/foreman_maintain/package_manager/apt.rb', line 46

def update_available?(package)
  output, status = Open3.capture2("apt-get install #{package} --dry-run")
  status.success? && output.include?("Inst #{package}")
end

#version_locking_supported?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/foreman_maintain/package_manager/apt.rb', line 59

def version_locking_supported?
  false
end