Class: HammerCLIForemanVirtWhoConfigure::SystemCaller

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli_foreman_virt_who_configure/system_caller.rb

Constant Summary collapse

WHITELISTED_VARS =
%w(HOME USER LANG).freeze

Instance Method Summary collapse

Constructor Details

#initialize(tempfile = nil) ⇒ SystemCaller

Returns a new instance of SystemCaller.



7
8
9
# File 'lib/hammer_cli_foreman_virt_who_configure/system_caller.rb', line 7

def initialize(tempfile = nil)
  @tempfile = tempfile || Tempfile.new('virt_who')
end

Instance Method Details

#clean_env_varsObject



11
12
13
14
15
16
17
18
19
# File 'lib/hammer_cli_foreman_virt_who_configure/system_caller.rb', line 11

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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hammer_cli_foreman_virt_who_configure/system_caller.rb', line 21

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