Module: ForemanMaintain::Concerns::SystemHelpers

Defined Under Namespace

Classes: Version

Class Method Summary collapse

Instance Method Summary collapse

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

#command_present?(command_name) ⇒ Boolean

Returns:

  • (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

#create_lv_snapshot(name, block_size, path) ⇒ Object



167
168
169
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 167

def create_lv_snapshot(name, block_size, path)
  execute!("lvcreate -n#{name} -L#{block_size} -s #{path}")
end

#debian?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 196

def debian?
  os_facts['os']['family'] == 'Debian'
end

#directory_empty?(dir) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 159

def directory_empty?(dir)
  Dir.entries(dir).size <= 2
end

#el7?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 200

def el7?
  os_facts['os']['release']['major'] == '7' && el?
end

#el8?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 204

def el8?
  os_facts['os']['release']['major'] == '8' && el?
end

#el?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 192

def el?
  os_facts['os']['family'] == 'RedHat'
end

#el_major_versionObject



208
209
210
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 208

def el_major_version
  return os_facts['os']['release']['major'] if el?
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

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 47

def execute?(command, options = {})
  execute(command, options)
  $CHILD_STATUS.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

Returns:

  • (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

Returns:

  • (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



175
176
177
178
179
180
181
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 175

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


153
154
155
156
157
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 153

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



222
223
224
225
226
227
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 222

def foreman_plugin_name(plugin)
  if debian?
    plugin.tr!('_', '-')
  end
  ruby_prefix + plugin
end

#format_shell_args(options = {}) ⇒ Object



149
150
151
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 149

def format_shell_args(options = {})
  options.map { |shell_optn, val| " #{shell_optn} '#{shellescape(val)}'" }.join
end

#get_lv_info(dir) ⇒ Object



163
164
165
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 163

def get_lv_info(dir)
  execute("findmnt -n --target #{dir} -o SOURCE,FSTYPE").split
end

#get_lv_path(lv_name) ⇒ Object



171
172
173
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 171

def get_lv_path(lv_name)
  execute("lvs --noheadings -o lv_path -S lv_name=#{lv_name}").strip
end

#hammer_packageObject



240
241
242
243
244
245
246
247
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 240

def hammer_package
  hammer_prefix = if debian?
                    'hammer-cli'
                  else
                    'hammer_cli'
                  end
  ruby_prefix + hammer_prefix
end

#hostnameObject



93
94
95
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 93

def hostname
  execute('hostname -f')
end

#os_factsObject



187
188
189
190
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 187

def os_facts
  facter = ForemanMaintain::Utils::Facter.path
  @os_facts ||= JSON.parse(execute("#{facter} -j os"))
end

#package_managerObject



183
184
185
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 183

def package_manager
  ForemanMaintain.package_manager
end

#package_version(name) ⇒ Object



115
116
117
118
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 115

def package_version(name)
  # space for extension to support non-rpm distributions
  rpm_version(name)
end

#packages_action(action, packages, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 101

def packages_action(action, packages, options = {})
  options.validate_options!(:assumeyes)
  case action
  when :install
    package_manager.install(packages, :assumeyes => options[:assumeyes])
  when :update
    package_manager.update(packages, :assumeyes => options[:assumeyes])
  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



120
121
122
123
124
125
126
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 120

def parse_csv(data)
  parsed_data = CSV.parse(data)
  header = parsed_data.first
  parsed_data[1..-1].map do |row|
    Hash[*header.zip(row).flatten(1)]
  end
end

#parse_json(json_string) ⇒ Object



128
129
130
131
132
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 128

def parse_json(json_string)
  JSON.parse(json_string)
rescue StandardError
  nil
end

#proxy_plugin_name(plugin) ⇒ Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 229

def proxy_plugin_name(plugin)
  if debian?
    plugin.tr!('_', '-')
    proxy_plugin_prefix = 'smart-proxy-'
  else
    proxy_plugin_prefix = 'smart_proxy_'
  end
  scl = check_min_version('foreman-proxy', '2.0')
  ruby_prefix(scl) + proxy_plugin_prefix + plugin
end

#rpm_version(name) ⇒ Object



134
135
136
137
138
139
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 134

def rpm_version(name)
  rpm_version = execute(%(rpm -q '#{name}' --queryformat="%{VERSION}"))
  if $CHILD_STATUS.success?
    version(rpm_version)
  end
end

#ruby_prefix(scl = true) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 212

def ruby_prefix(scl = true)
  if el7? && scl
    'tfm-rubygem-'
  elsif el7? || el8?
    'rubygem-'
  elsif debian?
    'ruby-'
  end
end

#server?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 97

def server?
  find_package('foreman')
end

#shellescape(string) ⇒ Object



141
142
143
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 141

def shellescape(string)
  Shellwords.escape(string)
end

#systemd_installed?Boolean

Returns:

  • (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



145
146
147
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 145

def version(value)
  Version.new(value)
end