module ForemanMaintain::Concerns::SystemHelpers

Public Class Methods

included(klass) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 12
def self.included(klass)
  klass.extend(self)
end

Public Instance Methods

check_min_version(name, minimal_version) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 35
def check_min_version(name, minimal_version)
  current_version = package_version(name)
  if current_version
    return current_version >= version(minimal_version)
  end
end
clean_all_packages() click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 110
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) click to toggle source
# 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
directory_empty?(dir) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 159
def directory_empty?(dir)
  Dir.entries(dir).size <= 2
end
downstream_installation?() click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 42
def downstream_installation?
  execute?('rpm -q satellite') ||
    (execute('rpm -q foreman') =~ /sat.noarch/)
end
execute(command, options = {}) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 63
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 = {}) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 52
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 = {}) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 47
def execute?(command, options = {})
  execute(command, options)
  $CHILD_STATUS.success?
end
execute_with_status(command, options = {}) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 70
def execute_with_status(command, options = {})
  result_msg = execute(command, options)
  [$CHILD_STATUS.to_i, result_msg]
end
file_exists?(filename) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 75
def file_exists?(filename)
  File.exist?(filename)
end
find_dir_containing_file(directory, target) click to toggle source
# 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) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 79
def find_package(name)
  result = execute(%Q(rpm -q '#{name}'))
  if $CHILD_STATUS.success?
    result
  end
end
format_shell_args(options = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
hostname() click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 86
def hostname
  execute('hostname -f')
end
package_version(name) click to toggle source
# 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 = {}) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 98
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) click to toggle source
# 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) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 128
def parse_json(json_string)
  JSON.parse(json_string)
rescue StandardError
  nil
end
rpm_version(name) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 134
def rpm_version(name)
  rpm_version = execute(%Q(rpm -q '#{name}' --queryformat="%{VERSION}"))
  if $CHILD_STATUS.success?
    version(rpm_version)
  end
end
server?() click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 90
def server?
  find_package('foreman')
end
shellescape(string) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 141
def shellescape(string)
  Shellwords.escape(string)
end
smart_proxy?() click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 94
def smart_proxy?
  !server? && find_package('foreman-proxy')
end
systemd_installed?() click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 31
def systemd_installed?
  File.exist?('/usr/bin/systemctl')
end
version(value) click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 145
def version(value)
  Version.new(value)
end