Class: Runner

Inherits:
Object
  • Object
show all
Extended by:
Delegation
Defined in:
lib/runner.rb

Defined Under Namespace

Classes: CommandFailed

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegation

delegate

Constructor Details

#initialize(logger = nil) ⇒ Runner

Returns a new instance of Runner.



9
10
11
# File 'lib/runner.rb', line 9

def initialize(logger=nil)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/runner.rb', line 13

def logger
  @logger
end

Instance Method Details

#shell(cmd, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/runner.rb', line 17

def shell cmd, opts={}
  cmd = cmd.strip
  
  # As a default, escape from the bundler jail:
  environment = opts[:environment] ||= {}
  environment['RUBYOPT'] = ''
  environment['BUNDLE_GEMFILE'] = ''
  environment['RAILS_ENV'] = 'production'
  environment['RACK_ENV'] = 'production'
  
  debug "Executing '#{cmd}'."
  command = shell_out(cmd, opts)
  
  command.run_command
  command.error!
  
  return command.stdout
rescue Mixlib::ShellOut::ShellCommandFailed => error
  warn "Failed command (exit #{command.exitstatus}): #{cmd} "
  
  lines = command.stderr.lines.to_a
  warn "STDERR: #{lines.size} lines of output:" unless lines.empty?
  lines.each_with_index do |line, idx|
    symbol = idx == lines.size-1 ? '`-' : '|-'
    debug " #{symbol} #{line.chomp}"
  end

  lines = command.stdout.lines.to_a
  warn "STDOUT: #{lines.size} lines of output:" unless lines.empty?
  lines.each_with_index do |line, idx|
    symbol = idx == lines.size-1 ? '`-' : '|-'
    debug " #{symbol} #{line.chomp}"
  end
  
  raise CommandFailed, "'#{cmd}' exited with #{command.exitstatus}. (see log for details)"
end

#shell_as(user, cmd, opts = {}) ⇒ Object



53
54
55
# File 'lib/runner.rb', line 53

def shell_as user, cmd, opts={}
  shell cmd, opts.merge(user: user)
end

#shell_out(*args) ⇒ Object



57
58
59
# File 'lib/runner.rb', line 57

def shell_out(*args)
  Mixlib::ShellOut.new(*args)
end