Module: Kitchen::Yansible::Tools::Exec

Included in:
Provisioner::Yansible, Install
Defined in:
lib/kitchen-yansible/tools/exec.rb

Instance Method Summary collapse

Instance Method Details

#check_command(command, args: '') ⇒ Object



31
32
33
# File 'lib/kitchen-yansible/tools/exec.rb', line 31

def check_command(command, args: '')
  "command -v #{command}" + "#{(" #{args}" unless args.empty?)}"
end

#command_exists(command) ⇒ Object



35
36
37
# File 'lib/kitchen-yansible/tools/exec.rb', line 35

def command_exists(command)
  check_command(command, :args => '&>/dev/null')
end

#detect_platformObject



104
105
106
107
# File 'lib/kitchen-yansible/tools/exec.rb', line 104

def detect_platform
  @instance.driver.diagnose[:name] == 'docker' ?
    @instance.driver.diagnose[:platform].to_s : @instance.platform.name.to_s
end

#execute_local_command(command, env: {}, opts: {}, print_stdout: false, return_stdout: false) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kitchen-yansible/tools/exec.rb', line 74

def execute_local_command(command, env: {}, opts: {}, print_stdout: false, return_stdout: false)
  print_cmd_parameters(command, env)

  # noinspection RubyUnusedLocalVariable
  Open3.popen3(env, command, opts) { |stdin, stdout, stderr, thread|
    if print_stdout
      while (line = stdout.gets)
        puts line
      end
    end
    proc = thread.value

    print_cmd_error(stderr, proc)
    return_stdout ? stdout.read : proc.success?
  }
end

#local_command_exists(command) ⇒ Object



43
44
45
# File 'lib/kitchen-yansible/tools/exec.rb', line 43

def local_command_exists(command)
  "#{local_command_path(command, :args => '&>/dev/null')}"
end

#local_command_path(command, args: '') ⇒ Object



39
40
41
# File 'lib/kitchen-yansible/tools/exec.rb', line 39

def local_command_path(command, args: '')
  system(check_command(command, args))
end

Raises:

  • (UserError)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kitchen-yansible/tools/exec.rb', line 62

def print_cmd_error(stderr, proc)
  message = unindent(<<-MSG)

    ===============================================================================
     Command returned '#{proc.exitstatus}'.
     stderr: '#{stderr.read}'
    ===============================================================================
  MSG
  debug(message)
  raise UserError, message unless proc.success?
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kitchen-yansible/tools/exec.rb', line 47

def print_cmd_parameters(command, env = {})
  env_vars = []
  env.each { |k,v| env_vars.push("#{k}=#{v}") }
  message = unindent(<<-MSG)

    ===============================================================================
     Environment:
      #{env_vars.join("\n              ")}
     Command line:
      #{command}
    ===============================================================================
  MSG
  debug(message)
end

#sudo(script) ⇒ Object



100
101
102
# File 'lib/kitchen-yansible/tools/exec.rb', line 100

def sudo(script)
  "sudo -E #{script}"
end

#sudo_env(pm) ⇒ Object

Helpers



92
93
94
95
96
97
# File 'lib/kitchen-yansible/tools/exec.rb', line 92

def sudo_env(pm)
  s = @config[:https_proxy] ? "https_proxy=#{@config[:https_proxy]}" : nil
  p = @config[:http_proxy] ? "http_proxy=#{@config[:http_proxy]}" : nil
  n = @config[:no_proxy] ? "no_proxy=#{@config[:no_proxy]}" : nil
  p || s ? "#{sudo('env')} #{p} #{s} #{n} #{pm}" : "#{sudo(pm)}"
end

#unindent(s) ⇒ Object



27
28
29
# File 'lib/kitchen-yansible/tools/exec.rb', line 27

def unindent(s)
  s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, '')
end