Module: ForemanMaintain::Concerns::SystemHelpers
- Includes:
- Finders, Logger
- Included in:
- ForemanMaintain::Check, ForemanMaintain::Cli::PrebuildBashCompletionCommand, Feature, Procedure, Scenario, Utils::Backup, Utils::Disk::Device, Utils::Disk::IODevice, Utils::Facter, Utils::MongoCoreInstalled, Utils::SystemHelpers
- Defined in:
- lib/foreman_maintain/concerns/system_helpers.rb
Defined Under Namespace
Classes: Version
Class Method Summary
collapse
Instance Method Summary
collapse
-
#check_max_version(name, maximal_version) ⇒ Object
-
#check_min_version(name, minimal_version) ⇒ Object
-
#clean_all_packages ⇒ Object
-
#create_lv_snapshot(name, block_size, path) ⇒ Object
-
#directory_empty?(dir) ⇒ Boolean
-
#downstream_installation? ⇒ Boolean
-
#execute(command, options = {}) ⇒ Object
-
#execute!(command, options = {}) ⇒ Object
-
#execute?(command, options = {}) ⇒ Boolean
-
#execute_with_status(command, options = {}) ⇒ Object
-
#file_exists?(filename) ⇒ Boolean
-
#find_dir_containing_file(directory, target) ⇒ Object
-
#find_package(name) ⇒ Object
-
#find_symlinks(dir_path) ⇒ Object
-
#format_shell_args(options = {}) ⇒ Object
-
#get_lv_info(dir) ⇒ Object
-
#get_lv_path(lv_name) ⇒ Object
-
#hostname ⇒ Object
-
#package_version(name) ⇒ Object
-
#packages_action(action, packages, options = {}) ⇒ Object
-
#parse_csv(data) ⇒ Object
-
#parse_json(json_string) ⇒ Object
-
#rpm_version(name) ⇒ Object
-
#server? ⇒ Boolean
-
#shellescape(string) ⇒ Object
-
#smart_proxy? ⇒ Boolean
-
#systemd_installed? ⇒ Boolean
-
#version(value) ⇒ Object
Methods included from Finders
#check, #detector, #feature, #find_all_scenarios, #find_checks, #find_procedures, #find_scenarios, #procedure
Methods included from Logger
#logger
Class Method Details
.included(klass) ⇒ Object
12
13
14
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 12
def self.included(klass)
klass.extend(self)
end
|
Instance Method Details
#check_max_version(name, maximal_version) ⇒ Object
41
42
43
44
45
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 41
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
35
36
37
38
39
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 35
def check_min_version(name, minimal_version)
check_version(name) do |current_version|
current_version >= version(minimal_version)
end
end
|
#clean_all_packages ⇒ Object
115
116
117
118
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 115
def clean_all_packages
execute!('dnf clean all') if find_package('dnf')
execute!('yum clean all') if find_package('yum')
end
|
#create_lv_snapshot(name, block_size, path) ⇒ Object
172
173
174
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 172
def create_lv_snapshot(name, block_size, path)
execute!("lvcreate -n#{name} -L#{block_size} -s #{path}")
end
|
#directory_empty?(dir) ⇒ Boolean
164
165
166
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 164
def directory_empty?(dir)
Dir.entries(dir).size <= 2
end
|
#downstream_installation? ⇒ Boolean
47
48
49
50
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 47
def downstream_installation?
execute?('rpm -q satellite') ||
(execute('rpm -q foreman') =~ /sat.noarch/)
end
|
#execute(command, options = {}) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 68
def execute(command, options = {})
command_runner = Utils::CommandRunner.new(logger, command, options)
execution.puts '' if command_runner.interactive? && respond_to?(:execution)
command_runner.run
command_runner.output
end
|
#execute!(command, options = {}) ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 57
def execute!(command, options = {})
command_runner = Utils::CommandRunner.new(logger, command, options)
execution.puts '' if command_runner.interactive? && respond_to?(:execution)
command_runner.run
if command_runner.success?
command_runner.output
else
raise command_runner.execution_error
end
end
|
#execute?(command, options = {}) ⇒ Boolean
52
53
54
55
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 52
def execute?(command, options = {})
execute(command, options)
$CHILD_STATUS.success?
end
|
#execute_with_status(command, options = {}) ⇒ Object
75
76
77
78
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 75
def execute_with_status(command, options = {})
result_msg = execute(command, options)
[$CHILD_STATUS.to_i, result_msg]
end
|
#file_exists?(filename) ⇒ Boolean
80
81
82
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 80
def file_exists?(filename)
File.exist?(filename)
end
|
#find_dir_containing_file(directory, target) ⇒ Object
180
181
182
183
184
185
186
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 180
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
84
85
86
87
88
89
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 84
def find_package(name)
result = execute(%(rpm -q '#{name}'))
if $CHILD_STATUS.success?
result
end
end
|
#find_symlinks(dir_path) ⇒ Object
158
159
160
161
162
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 158
def find_symlinks(dir_path)
cmd = "find '#{dir_path}' -maxdepth 1 -type l"
result = execute(cmd).strip
result.split(/\n/)
end
|
154
155
156
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 154
def format_shell_args(options = {})
options.map { |shell_optn, val| " #{shell_optn} '#{shellescape(val)}'" }.join
end
|
#get_lv_info(dir) ⇒ Object
168
169
170
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 168
def get_lv_info(dir)
execute("findmnt -n --target #{dir} -o SOURCE,FSTYPE").split
end
|
#get_lv_path(lv_name) ⇒ Object
176
177
178
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 176
def get_lv_path(lv_name)
execute("lvs --noheadings -o lv_path -S lv_name=#{lv_name}").strip
end
|
#hostname ⇒ Object
91
92
93
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 91
def hostname
execute('hostname -f')
end
|
#package_version(name) ⇒ Object
120
121
122
123
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 120
def package_version(name)
rpm_version(name)
end
|
#packages_action(action, packages, options = {}) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 103
def packages_action(action, packages, options = {})
expected_actions = [:install, :update]
unless expected_actions.include?(action)
raise ArgumentError, "Unexpected action #{action} expected #{expected_actions.inspect}"
end
options.validate_options!(:assumeyes)
yum_options = []
yum_options << '-y' if options[:assumeyes]
execute!("yum #{yum_options.join(' ')} #{action} #{packages.join(' ')}",
:interactive => true)
end
|
#parse_csv(data) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 125
def parse_csv(data)
parsed_data = CSV.parse(data)
= parsed_data.first
parsed_data[1..-1].map do |row|
Hash[*.zip(row).flatten(1)]
end
end
|
#parse_json(json_string) ⇒ Object
133
134
135
136
137
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 133
def parse_json(json_string)
JSON.parse(json_string)
rescue StandardError
nil
end
|
#rpm_version(name) ⇒ Object
139
140
141
142
143
144
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 139
def rpm_version(name)
rpm_version = execute(%(rpm -q '#{name}' --queryformat="%{VERSION}"))
if $CHILD_STATUS.success?
version(rpm_version)
end
end
|
#server? ⇒ Boolean
95
96
97
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 95
def server?
find_package('foreman')
end
|
#shellescape(string) ⇒ Object
146
147
148
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 146
def shellescape(string)
Shellwords.escape(string)
end
|
#smart_proxy? ⇒ Boolean
99
100
101
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 99
def smart_proxy?
!server? && find_package('foreman-proxy')
end
|
#systemd_installed? ⇒ Boolean
31
32
33
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 31
def systemd_installed?
File.exist?('/usr/bin/systemctl')
end
|
#version(value) ⇒ Object
150
151
152
|
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 150
def version(value)
Version.new(value)
end
|