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

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



66
67
68
69
70
71
72
73
74
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 66

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



94
95
96
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 94

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

#enable_module(name) ⇒ Object



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

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

#files_not_owned_by_package(directory) ⇒ Object



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

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



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

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



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

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)


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

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

#module_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#modules_supported?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 98

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

#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



111
112
113
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 111

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, dnf_options: []) ⇒ Object



62
63
64
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 62

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

#update_available?(package) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/foreman_maintain/package_manager/dnf.rb', line 76

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