Module: ForemanMaintain::Concerns::SystemHelpers
- Includes:
- Finders, Logger, OsFacts
- Included in:
- ForemanMaintain::Check, ForemanMaintain::Cli::PrebuildBashCompletionCommand, Feature, Procedure, Report, RepositoryManager::El, Scenario, Utils::Backup, Utils::Disk::Device, Utils::Disk::IODevice, Utils::SystemHelpers
- Defined in:
- lib/foreman_maintain/concerns/system_helpers.rb
Defined Under Namespace
Classes: Version
Constant Summary
Constants included
from OsFacts
OsFacts::FALLBACK_OS_RELEASE_FILE, OsFacts::OS_RELEASE_FILE
Class Method Summary
collapse
Instance Method Summary
collapse
-
#check_max_version(name, maximal_version) ⇒ Object
-
#check_min_version(name, minimal_version) ⇒ Object
-
#command_present?(command_name) ⇒ Boolean
-
#directory_empty?(dir) ⇒ Boolean
-
#execute(command, options = {}) ⇒ Object
-
#execute!(command, options = {}) ⇒ Object
-
#execute?(command, options = {}) ⇒ Boolean
-
#execute_runner(command, options = {}) ⇒ Object
-
#execute_with_status(command, options = {}) ⇒ Object
-
#file_exists?(filename) ⇒ Boolean
-
#file_nonzero?(filename) ⇒ Boolean
-
#find_dir_containing_file(directory, target) ⇒ Object
-
#find_package(name) ⇒ Object
-
#find_symlinks(dir_path) ⇒ Object
-
#foreman_plugin_name(plugin) ⇒ Object
-
#format_shell_args(options = {}) ⇒ Object
-
#hammer_package ⇒ Object
-
#hammer_plugin_name(plugin) ⇒ Object
-
#hostname ⇒ Object
-
#package_manager ⇒ Object
-
#package_version(name) ⇒ Object
-
#packages_action(action, packages, options = {}) ⇒ Object
-
#parse_csv(data) ⇒ Object
-
#parse_json(json_string) ⇒ Object
-
#proxy_plugin_name(plugin) ⇒ Object
-
#repository_manager ⇒ Object
-
#ruby_prefix ⇒ Object
-
#server? ⇒ Boolean
-
#shellescape(string) ⇒ Object
-
#systemd_installed? ⇒ Boolean
-
#version(value) ⇒ Object
Methods included from OsFacts
#centos?, #cpu_cores, #deb_major_version, #debian?, #debian_or_ubuntu?, #el8?, #el?, #el_major_version, #el_short_name, #facts, #memory, #os_id, #os_id_like_list, #os_name, #os_release_file, #os_version, #os_version_codename, #os_version_id, #rhel?, #ubuntu?, #ubuntu_major_version
Methods included from Finders
#check, #detector, #feature, #find_all_scenarios, #find_procedures, #find_scenarios, #procedure
Methods included from Logger
#logger
Class Method Details
.included(klass) ⇒ Object
13
14
15
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 13
def self.included(klass)
klass.extend(self)
end
|
Instance Method Details
#check_max_version(name, maximal_version) ⇒ Object
42
43
44
45
46
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 42
def check_max_version(name, maximal_version)
check_version(name) do |current_version|
version(maximal_version) >= current_version
end
end
|
#check_min_version(name, minimal_version) ⇒ Object
36
37
38
39
40
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 36
def check_min_version(name, minimal_version)
check_version(name) do |current_version|
current_version >= version(minimal_version)
end
end
|
#command_present?(command_name) ⇒ Boolean
52
53
54
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 52
def command_present?(command_name)
execute?("command -v #{command_name}")
end
|
#directory_empty?(dir) ⇒ Boolean
165
166
167
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 165
def directory_empty?(dir)
Dir.entries(dir).size <= 2
end
|
#execute(command, options = {}) ⇒ Object
72
73
74
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 72
def execute(command, options = {})
execute_runner(command, options).output
end
|
#execute!(command, options = {}) ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 63
def execute!(command, options = {})
command_runner = execute_runner(command, options)
if command_runner.success?
command_runner.output
else
raise command_runner.execution_error
end
end
|
#execute?(command, options = {}) ⇒ Boolean
48
49
50
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 48
def execute?(command, options = {})
execute_runner(command, options).success?
end
|
#execute_runner(command, options = {}) ⇒ Object
56
57
58
59
60
61
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 56
def execute_runner(command, options = {})
command_runner = Utils::CommandRunner.new(logger, command, options)
execution.puts '' if command_runner.interactive? && respond_to?(:execution)
command_runner.run
command_runner
end
|
#execute_with_status(command, options = {}) ⇒ Object
76
77
78
79
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 76
def execute_with_status(command, options = {})
command_runner = execute_runner(command, options)
[command_runner.exit_status, command_runner.output]
end
|
#file_exists?(filename) ⇒ Boolean
81
82
83
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 81
def file_exists?(filename)
File.exist?(filename)
end
|
#file_nonzero?(filename) ⇒ Boolean
85
86
87
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 85
def file_nonzero?(filename)
File.exist?(filename) && !File.zero?(filename)
end
|
#find_dir_containing_file(directory, target) ⇒ Object
169
170
171
172
173
174
175
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 169
def find_dir_containing_file(directory, target)
result = nil
Find.find(directory) do |path|
result = File.dirname(path) if File.basename(path) == target
end
result
end
|
#find_package(name) ⇒ Object
89
90
91
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 89
def find_package(name)
package_manager.find_installed_package(name)
end
|
#find_symlinks(dir_path) ⇒ Object
159
160
161
162
163
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 159
def find_symlinks(dir_path)
cmd = "find '#{dir_path}' -maxdepth 1 -type l"
result = execute(cmd).strip
result.split(/\n/)
end
|
#foreman_plugin_name(plugin) ⇒ Object
193
194
195
196
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 193
def foreman_plugin_name(plugin)
plugin = plugin.tr('_', '-') if debian_or_ubuntu?
ruby_prefix + plugin
end
|
155
156
157
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 155
def format_shell_args(options = {})
options.map { |shell_optn, val| " #{shell_optn} #{shellescape(val)}" }.join
end
|
#hammer_package ⇒ Object
213
214
215
216
217
218
219
220
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 213
def hammer_package
hammer_prefix = if debian_or_ubuntu?
'hammer-cli'
else
'hammer_cli'
end
ruby_prefix + hammer_prefix
end
|
#hammer_plugin_name(plugin) ⇒ Object
208
209
210
211
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 208
def hammer_plugin_name(plugin)
plugin = plugin.tr('_', '-') if debian_or_ubuntu?
[hammer_package, plugin].join(debian_or_ubuntu? ? '-' : '_')
end
|
#hostname ⇒ Object
93
94
95
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 93
def hostname
execute('hostname -f')
end
|
#package_manager ⇒ Object
#package_version(name) ⇒ Object
120
121
122
123
124
125
126
127
128
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 120
def package_version(name)
ver = if el?
'%{VERSION}'
elsif debian_or_ubuntu?
'${VERSION}'
end
pkg = package_manager.find_installed_package(name, ver)
version(pkg) unless pkg.nil?
end
|
#packages_action(action, packages, options = {}) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 101
def packages_action(action, packages, options = {})
options.validate_options!(:assumeyes, :options, :download_only, :enabled_repos)
case action
when :install
package_manager.install(packages, :assumeyes => options[:assumeyes])
when :update
package_manager.update(
packages,
:assumeyes => options[:assumeyes],
:download_only => options[:download_only],
:enabled_repos => options[:enabled_repos]
)
when :remove
package_manager.remove(packages, :assumeyes => options[:assumeyes])
else
raise ArgumentError, "Unexpected action #{action} expected #{expected_actions.inspect}"
end
end
|
#parse_csv(data) ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 130
def parse_csv(data)
parsed_data = CSV.parse(data)
= parsed_data.first
parsed_data[1..].map do |row|
Hash[*.zip(row).flatten(1)]
end
end
|
#parse_json(json_string) ⇒ Object
138
139
140
141
142
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 138
def parse_json(json_string)
JSON.parse(json_string)
rescue StandardError
nil
end
|
#proxy_plugin_name(plugin) ⇒ Object
198
199
200
201
202
203
204
205
206
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 198
def proxy_plugin_name(plugin)
if debian_or_ubuntu?
plugin = plugin.tr('_', '-')
proxy_plugin_prefix = 'smart-proxy-'
else
proxy_plugin_prefix = 'smart_proxy_'
end
ruby_prefix + proxy_plugin_prefix + plugin
end
|
#repository_manager ⇒ Object
#ruby_prefix ⇒ Object
185
186
187
188
189
190
191
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 185
def ruby_prefix
if debian_or_ubuntu?
'ruby-'
else
'rubygem-'
end
end
|
#server? ⇒ Boolean
97
98
99
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 97
def server?
find_package('foreman')
end
|
#shellescape(string) ⇒ Object
144
145
146
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 144
def shellescape(string)
Shellwords.escape(string)
end
|
#systemd_installed? ⇒ Boolean
32
33
34
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 32
def systemd_installed?
File.exist?('/run/systemd/system/')
end
|
#version(value) ⇒ Object
148
149
150
151
152
153
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 148
def version(value)
value.gsub!(/[+~]/, '-')
Version.new(value)
end
|