class HammerCLIForemanVirtWhoConfigure::SystemCaller

Constants

WHITELISTED_VARS

Public Class Methods

new(tempfile = nil) click to toggle source
# File lib/hammer_cli_foreman_virt_who_configure/system_caller.rb, line 5
def initialize(tempfile = nil)
  @tempfile = tempfile || Tempfile.new('virt_who')
end

Public Instance Methods

clean_env_vars() click to toggle source
# File lib/hammer_cli_foreman_virt_who_configure/system_caller.rb, line 9
def clean_env_vars
  # Cleaning ENV vars and keeping required vars only because,
  # When using SCL it adds GEM_HOME and GEM_PATH ENV vars.
  # These vars break foreman-maintain tool.
  # This way we use system ruby to execute the bash script.
  cleaned_env = ENV.select { |var| WHITELISTED_VARS.include?(var) || var.start_with?('LC_') }
  cleaned_env['PATH'] = '/sbin:/bin:/usr/sbin:/usr/bin'
  cleaned_env
end
system(command) click to toggle source
# File lib/hammer_cli_foreman_virt_who_configure/system_caller.rb, line 19
def system(command)
  result = nil
  begin
    @tempfile.write(command)
    @tempfile.flush # to make sure the command is complete
    result = Kernel.system(clean_env_vars, "/usr/bin/bash #{@tempfile.path}", unsetenv_others: true)
  ensure
    @tempfile.close
    @tempfile.unlink
  end
  result
end