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?, #sys, #unlock_versions, #versions_locked?

Instance Method Details

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

rubocop:disable Layout/LineLength



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/foreman_maintain/package_manager/apt.rb', line 72

def apt_action(action, packages, with_status: false, assumeyes: false, valid_exit_statuses: [0], download_only: false)
  apt_options = []
  packages = [packages].flatten(1)
  apt_options << '-y' if assumeyes
  apt_options << '--download-only' if download_only
  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



44
45
46
# File 'lib/foreman_maintain/package_manager/apt.rb', line 44

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

#clean_cache(assumeyes: false) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



29
30
31
# File 'lib/foreman_maintain/package_manager/apt.rb', line 29

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

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



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

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



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

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

#reboot_required?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/foreman_maintain/package_manager/apt.rb', line 65

def reboot_required?
  status = File.exist?('/var/run/reboot-required') ? 1 : 0
  output = ''
  [status, output]
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, download_only: false, enabled_repos: []) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

#update_available?(package) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/foreman_maintain/package_manager/apt.rb', line 48

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)


61
62
63
# File 'lib/foreman_maintain/package_manager/apt.rb', line 61

def version_locking_supported?
  false
end