module Baptize::Plugins::Execution

Public Instance Methods

put(buffer, remote, options = {}) click to toggle source
# File lib/baptize/plugins/execution.rb, line 60
def put(buffer, remote, options = {})
  upload StringIO.new(buffer), remote, options
end
remote_assert(command) click to toggle source
# File lib/baptize/plugins/execution.rb, line 33
def remote_assert(command)
  command = Array(command).flatten.map {|c| "#{c} > /dev/null 2> /dev/null" }.join(" && ")
  call_current_ssh_connection :test, command
end
remote_capture(*args) click to toggle source
# File lib/baptize/plugins/execution.rb, line 29
def remote_capture(*args)
  call_current_ssh_connection :capture, *args
end
remote_execute(*args) click to toggle source
# File lib/baptize/plugins/execution.rb, line 25
def remote_execute(*args)
  call_current_ssh_connection :execute, *args
end
run_locally(cmd) click to toggle source

logs the command then executes it locally. returns the command output as a string

# File lib/baptize/plugins/execution.rb, line 9
def run_locally(cmd)
  if fetch(:dry_run)
    return logger.debug "executing locally: #{cmd.inspect}"
  end
  logger.trace "executing locally: #{cmd.inspect}" if logger
  output_on_stdout = nil
  elapsed = Benchmark.realtime do
    output_on_stdout = `#{cmd}`
  end
  if $?.to_i > 0 # $? is command exit code (posix style)
    raise ArgumentError, "Command #{cmd} returned status code #{$?}"
  end
  logger.trace "command finished in #{(elapsed * 1000).round}ms" if logger
  output_on_stdout
end
upload(local, remote, options = {}) click to toggle source
# File lib/baptize/plugins/execution.rb, line 38
def upload(local, remote, options = {})
  if fetch(:use_sudo)
    # perform the transfer in two steps if we're using sudo
    final = remote
    remote = "/tmp/baptize_#{File.basename(remote)}"
  end
  ssh = fetch(:current_ssh_connection)
  old_verbosity = nil
  if fetch(:ssh_verbose)
    old_verbosity = SSHKit.config.output_verbosity
    SSHKit.config.output_verbosity = Logger::DEBUG
  end
  begin
    ssh.upload! local, remote, options
  ensure
    SSHKit.config.output_verbosity = old_verbosity if old_verbosity
  end
  if fetch(:use_sudo)
    remote_execute "mv #{remote.shellescape} #{final.shellescape}"
  end
end

Private Instance Methods

call_current_ssh_connection(action, *args) click to toggle source
# File lib/baptize/plugins/execution.rb, line 65
def call_current_ssh_connection(action, *args)
  ssh = fetch(:current_ssh_connection)
  old_verbosity = nil
  if fetch(:ssh_verbose)
    old_verbosity = SSHKit.config.output_verbosity
    SSHKit.config.output_verbosity = Logger::DEBUG
  end
  begin
    if fetch(:use_bash)
      args = ["bash -c " + args.join(" ; ").dump]
    end
    args.unshift(:sudo) if fetch(:use_sudo)
    ssh.send(action, *args)
  ensure
    SSHKit.config.output_verbosity = old_verbosity if old_verbosity
  end
end