module ForemanMaintain::Concerns::SystemHelpers
Public Class Methods
included(klass)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 13 def self.included(klass) klass.extend(self) end
Public Instance Methods
check_max_version(name, maximal_version)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 53 def command_present?(command_name) execute?("command -v #{command_name}") end
create_lv_snapshot(name, block_size, path)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 168 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 160 def directory_empty?(dir) Dir.entries(dir).size <= 2 end
execute(command, options = {})
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 73 def execute(command, options = {}) execute_runner(command, options).output end
execute!(command, options = {})
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 64 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 = {})
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 48 def execute?(command, options = {}) execute(command, options) $CHILD_STATUS.success? end
execute_runner(command, options = {})
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 57 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 = {})
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 77 def execute_with_status(command, options = {}) command_runner = execute_runner(command, options) [command_runner.exit_status, command_runner.output] end
file_exists?(filename)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 82 def file_exists?(filename) File.exist?(filename) end
file_nonzero?(filename)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 86 def file_nonzero?(filename) File.exist?(filename) && !File.zero?(filename) end
find_dir_containing_file(directory, target)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 176 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 90 def find_package(name) package_manager.find_installed_package(name) end
find_symlinks(dir_path)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 154 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)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 202 def foreman_plugin_name(plugin) plugin = plugin.tr('_', '-') if debian? ruby_prefix + plugin end
format_shell_args(options = {})
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 150 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 164 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 172 def get_lv_path(lv_name) execute("lvs --noheadings -o lv_path -S lv_name=#{lv_name}").strip end
hammer_package()
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 223 def hammer_package hammer_prefix = if debian? 'hammer-cli' else 'hammer_cli' end ruby_prefix + hammer_prefix end
hammer_plugin_name(plugin)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 218 def hammer_plugin_name(plugin) plugin = plugin.tr('_', '-') if debian? [hammer_package, plugin].join(debian? ? '-' : '_') end
hostname()
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 94 def hostname execute('hostname -f') end
package_manager()
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 184 def package_manager ForemanMaintain.package_manager end
package_version(name)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 116 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 102 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)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 121 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 129 def parse_json(json_string) JSON.parse(json_string) rescue StandardError nil end
proxy_plugin_name(plugin)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 207 def proxy_plugin_name(plugin) if debian? plugin = 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
repository_manager()
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 188 def repository_manager ForemanMaintain.repository_manager end
rpm_version(name)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 135 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)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 192 def ruby_prefix(scl = true) if debian? 'ruby-' elsif el7? && scl 'tfm-rubygem-' else 'rubygem-' end end
server?()
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 98 def server? find_package('foreman') end
shellescape(string)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 142 def shellescape(string) Shellwords.escape(string) end
systemd_installed?()
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 32 def systemd_installed? File.exist?('/usr/bin/systemctl') end
version(value)
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 146 def version(value) Version.new(value) end
Private Instance Methods
check_version(name) { |current_version| ... }
click to toggle source
# File lib/foreman_maintain/concerns/system_helpers.rb, line 234 def check_version(name) current_version = package_version(name) if current_version yield current_version end end