class Bosh::Core::Shell
Attributes
stdout[R]
Public Class Methods
new(stdout = $stdout)
click to toggle source
# File lib/bosh/core/shell.rb, line 3 def initialize(stdout = $stdout) @stdout = stdout end
Public Instance Methods
run(command, options = {})
click to toggle source
# File lib/bosh/core/shell.rb, line 7 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/core/shell.rb, line 55 def command_exited_successfully? $?.success? end
pwd()
click to toggle source
# File lib/bosh/core/shell.rb, line 59 def pwd Dir.pwd rescue Errno::ENOENT 'a deleted directory' end
report(cmd, command_output, options)
click to toggle source
# File lib/bosh/core/shell.rb, line 48 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/core/shell.rb, line 20 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 = IO.popen(popen_args) io.each do |line| stdout.puts line.chomp lines << line.chomp end io.close lines end
tail(lines, options)
click to toggle source
# File lib/bosh/core/shell.rb, line 43 def tail(lines, options) line_number = options[:last_number] line_number ? lines.last(line_number) : lines end