class Bosh::Workspace::Shell

Attributes

stdout[R]

Public Class Methods

new(stdout = $stdout) click to toggle source
# File lib/bosh/workspace/shell.rb, line 4
def initialize(stdout = $stdout)
  @stdout = stdout
end

Public Instance Methods

run(command, options = {}) click to toggle source
# File lib/bosh/workspace/shell.rb, line 8
def run(command, options = {})
  output_lines = run_command(command, options)
  output_lines = tail(output_lines, options)

  command_output = output_lines.join("\n")
  report(command, command_output, options)
  command_output
end

Private Instance Methods

command_exited_successfully?() click to toggle source
# File lib/bosh/workspace/shell.rb, line 56
def command_exited_successfully?
  $?.success?
end
pwd() click to toggle source
# File lib/bosh/workspace/shell.rb, line 60
def pwd
  Dir.pwd
rescue Errno::ENOENT
  'a deleted directory'
end
report(cmd, command_output, options) click to toggle source
# File lib/bosh/workspace/shell.rb, line 49
def report(cmd, command_output, options)
  return if command_exited_successfully?

  err_msg = "Failed: '#{cmd}' from #{pwd}, with exit status #{$?.to_i}\n\n #{command_output}"
  options[:ignore_failures] ? stdout.puts("#{err_msg}, continuing anyway") : raise(err_msg)
end
run_command(command, options) click to toggle source
# File lib/bosh/workspace/shell.rb, line 21
def run_command(command, options)
  stdout.puts command if options[:output_command]
  lines = []

  if options[:env]
    # Wrap in a shell because existing api to Shell#run takes a string
    # which makes it really hard to pass it to popen with custom environment.
    popen_args = [options[:env], ENV['SHELL'] || 'bash', '-c', command]
  else
    popen_args = command
  end

  IO.popen(popen_args) do |io|
    io.each do |line|
      stdout.puts line.chomp
      stdout.flush
      lines << line.chomp
    end
  end

  lines
end
tail(lines, options) click to toggle source
# File lib/bosh/workspace/shell.rb, line 44
def tail(lines, options)
  line_number = options[:last_number]
  line_number ? lines.last(line_number) : lines
end