Class: ForemanMaintain::PackageManager::Dnf

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

Constant Summary collapse

PROTECTOR_CONFIG_FILE =
'/etc/dnf/plugins/foreman-protector.conf'.freeze
PROTECTOR_WHITELIST_FILE =
'/etc/dnf/plugins/foreman-protector.whitelist'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#sys

Class Method Details

.parse_envra(envra) ⇒ Object



6
7
8
9
10
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 6

def self.parse_envra(envra)
  # envra format: 0:foreman-1.20.1.10-1.el7sat.noarch
  parsed = envra.match(/\d*:?(?<name>.*)-[^-]+-[^-]+\.[^.]+/)
  parsed ? Hash[parsed.names.map(&:to_sym).zip(parsed.captures)].merge(:envra => envra) : nil
end

Instance Method Details

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



72
73
74
75
76
77
78
79
80
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 72

def check_update(packages: nil, with_status: false)
  dnf_action(
    'check-update',
    packages,
    assumeyes: true,
    valid_exit_statuses: [0, 100],
    with_status: with_status
  )
end

#clean_cache(assumeyes: false) ⇒ Object



100
101
102
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 100

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

#enable_module(name) ⇒ Object



113
114
115
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 113

def enable_module(name)
  dnf_action('module enable', name, assumeyes: true)
end

#files_not_owned_by_package(directory) ⇒ Object



87
88
89
90
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 87

def files_not_owned_by_package(directory)
  find_cmd = "find #{directory} -exec /bin/sh -c 'rpm -qf {} &> /dev/null || echo {}' \\;"
  sys.execute(find_cmd).split("\n")
end

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 39

def find_installed_package(name, queryformat = '')
  rpm_cmd = "rpm -q '#{name}'"
  unless queryformat.empty?
    rpm_cmd += " --qf '#{queryformat}'"
  end
  status, result = sys.execute_with_status(rpm_cmd, interactive: false)
  if status == 0
    result
  end
end

#info(name) ⇒ Object



126
127
128
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 126

def info(name)
  dnf_action('module info', name, with_status: true, assumeyes: true)
end

#install(packages, assumeyes: false) ⇒ Object



50
51
52
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 50

def install(packages, assumeyes: false)
  dnf_action('install', packages, assumeyes: assumeyes)
end

#installed?(packages) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 34

def installed?(packages)
  packages_list = [packages].flatten(1).map { |pkg| "'#{pkg}'" }.join(' ')
  sys.execute?(%(rpm -q #{packages_list}))
end

#list_installed_packages(queryformat = '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n') ⇒ Object



92
93
94
95
96
97
98
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 92

def list_installed_packages(queryformat = '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n')
  # The queryformat should only include valid tag(s) as per `rpm --querytags` list.
  # If any special formatting is required with querytag then it should be provided with tag i.e,
  # "--%{VENDOR}"
  # The queryformat string must end with '\n'
  sys.execute!("rpm -qa --qf '#{queryformat}'").split("\n")
end

#lock_versionsObject



12
13
14
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 12

def lock_versions
  enable_protector
end

#module_enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 108

def module_enabled?(name)
  _status, result = info(name)
  result.match?(/Stream.+\[e\].+/)
end

#module_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 121

def module_exists?(name)
  status, _result = info(name)
  status == 0
end

#modules_supported?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 104

def modules_supported?
  true
end

#protector_whitelist_file_nonzero?Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 25

def protector_whitelist_file_nonzero?
  File.exist?(PROTECTOR_WHITELIST_FILE) &&
    !File.zero?(PROTECTOR_WHITELIST_FILE)
end

#reboot_required?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 130

def reboot_required?
  sys.execute_with_status('dnf needs-restarting --reboothint', :interactive => false)
end

#reinstall(packages, assumeyes: false) ⇒ Object



54
55
56
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 54

def reinstall(packages, assumeyes: false)
  dnf_action('reinstall', packages, assumeyes: assumeyes)
end

#remove(packages, assumeyes: false) ⇒ Object



58
59
60
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 58

def remove(packages, assumeyes: false)
  dnf_action('remove', packages, assumeyes: assumeyes)
end

#switch_module(name) ⇒ Object



117
118
119
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 117

def switch_module(name)
  dnf_action('module switch-to', name, assumeyes: true)
end

#unlock_versionsObject



16
17
18
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 16

def unlock_versions
  disable_protector
end

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



62
63
64
65
66
67
68
69
70
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 62

def update(packages = [], assumeyes: false, download_only: false, enabled_repos: [])
  dnf_action(
    'update',
    packages,
    assumeyes: assumeyes,
    download_only: download_only,
    enabled_repos: enabled_repos
  )
end

#update_available?(package) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 82

def update_available?(package)
  cmd_output = dnf_action('check-update -q', package, with_status: true, assumeyes: false)
  cmd_output[0] == 100
end

#version_locking_supported?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 30

def version_locking_supported?
  true
end

#versions_locked?Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 20

def versions_locked?
  !!(protector_config =~ /^\s*enabled\s*=\s*1/) &&
    protector_whitelist_file_nonzero?
end