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)


57
58
59
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 57

def command_present?(command_name)
  execute?("command -v #{command_name}")
end

#create_lv_snapshot(name, block_size, path) ⇒ Object



174
175
176
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 174

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

#directory_empty?(dir) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#downstream_installation?Boolean

Returns:

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



72
73
74
75
76
77
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 72

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



61
62
63
64
65
66
67
68
69
70
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 61

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

Returns:

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



79
80
81
82
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 79

def execute_with_status(command, options = {})
  result_msg = execute(command, options)
  [$CHILD_STATUS.to_i, result_msg]
end

#file_exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 84

def file_exists?(filename)
  File.exist?(filename)
end

#file_nonzero?(filename) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 88

def file_nonzero?(filename)
  File.exist?(filename) && !File.zero?(filename)
end

#find_dir_containing_file(directory, target) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 182

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



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

def find_package(name)
  feature(:package_manager).find_installed_package(name)
end


160
161
162
163
164
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 160

def find_symlinks(dir_path)
  cmd = "find '#{dir_path}' -maxdepth 1 -type l"
  result = execute(cmd).strip
  result.split(/\n/)
end

#format_shell_args(options = {}) ⇒ Object



156
157
158
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 156

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

#get_lv_info(dir) ⇒ Object



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

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

#get_lv_path(lv_name) ⇒ Object



178
179
180
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 178

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

#hostnameObject



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

def hostname
  execute('hostname -f')
end

#package_version(name) ⇒ Object



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

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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 108

def packages_action(action, packages, options = {})
  options.validate_options!(:assumeyes)
  case action
  when :install
    feature(:package_manager).install(packages, :assumeyes => options[:assumeyes])
  when :update
    feature(:package_manager).update(packages, :assumeyes => options[:assumeyes])
  when :remove
    feature(:package_manager).remove(packages, :assumeyes => options[:assumeyes])
  else
    raise ArgumentError, "Unexpected action #{action} expected #{expected_actions.inspect}"
  end
end

#parse_csv(data) ⇒ Object



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

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



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

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

#rpm_version(name) ⇒ Object



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

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

#server?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 100

def server?
  find_package('foreman')
end

#shellescape(string) ⇒ Object



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

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

#smart_proxy?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 104

def smart_proxy?
  !server? && find_package('foreman-proxy')
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



152
153
154
# File 'lib/foreman_maintain/concerns/system_helpers.rb', line 152

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