class Airbrussh::Capistrano::Tasks

Encapsulates the rake behavior that integrates Airbrussh into Capistrano. This class allows us to easily test the behavior using a mock to stand in for the Capistrano DSL.

See airbrussh/capistrano.rb to see how this class is used.

Attributes

config[R]
dsl[R]
stderr[R]

Public Class Methods

new(dsl, stderr=$stderr, config=Airbrussh.configuration) click to toggle source
# File lib/airbrussh/capistrano/tasks.rb, line 22
def initialize(dsl, stderr=$stderr, config=Airbrussh.configuration)
  @dsl = dsl
  @stderr = stderr
  @config = config

  configure
  warn_if_missing_dsl
end

Public Instance Methods

deploy_failed() click to toggle source

Behavior for the rake deploy:failed task.

# File lib/airbrussh/capistrano/tasks.rb, line 38
def deploy_failed
  return unless airbrussh_is_being_used?
  return if log_file.nil?

  error_line
  error_line(red("** DEPLOY FAILED"))
  error_line(yellow("** Refer to #{log_file} for details. "                           "Here are the last 20 lines:"))
  error_line
  error_line(tail_log_file)
end
load_defaults() click to toggle source

Behavior for the rake load:defaults task.

# File lib/airbrussh/capistrano/tasks.rb, line 32
def load_defaults
  require "sshkit/formatter/airbrussh"
  set :format, :airbrussh
end

Private Instance Methods

airbrussh_is_being_used?() click to toggle source
# File lib/airbrussh/capistrano/tasks.rb, line 91
def airbrussh_is_being_used?
  # Obtain the current formatter from the SSHKit backend
  output = env.backend.config.output
  output.is_a?(Airbrussh::Formatter)
end
configure() click to toggle source

Change airbrussh's default configuration to be more appropriate for capistrano.

# File lib/airbrussh/capistrano/tasks.rb, line 56
def configure
  config.log_file = "log/capistrano.log"
  config.monkey_patch_rake = true
end
err_console() click to toggle source
# File lib/airbrussh/capistrano/tasks.rb, line 73
def err_console
  @err_console ||= Airbrussh::Console.new(stderr)
end
error_line(line="\n") click to toggle source
# File lib/airbrussh/capistrano/tasks.rb, line 77
def error_line(line="\n")
  line.each_line(&err_console.method(:print_line))
end
tail_log_file() click to toggle source

Returns a String containing the last 20 lines of the log file. Since this method uses a fixed-size buffer, fewer than 20 lines may be returned in cases where the lines are extremely long.

# File lib/airbrussh/capistrano/tasks.rb, line 84
def tail_log_file
  open(log_file) do |file|
    file.seek(-[8192, file.size].min, IO::SEEK_END)
    file.readlines.last(20).join
  end
end
warn_if_missing_dsl() click to toggle source

Verify that capistrano and rake DSLs are present

# File lib/airbrussh/capistrano/tasks.rb, line 62
def warn_if_missing_dsl
  return if %w(set namespace task).all? { |m| dsl.respond_to?(m, true) }

  error_line(
    red("WARNING: airbrussh/capistrano must be loaded by Capistrano in "               "order to work.\nRequire this gem within your application's "               "Capfile, as described here:\n"               "https://github.com/mattbrictson/airbrussh#installation")
  )
end