class Dk::Runner

Constants

CMD_LOG_PREFIX
CMD_SSH_OUT_LOG_PREFIX
INDENT_LOG_PREFIX
SSH_LOG_PREFIX
TASK_END_LOG_PREFIX
TASK_START_LOG_PREFIX

Attributes

logger[R]
params[R]

Public Class Methods

new(opts = nil) click to toggle source
# File lib/dk/runner.rb, line 27
def initialize(opts = nil)
  opts ||= {}
  @params = Hash.new{ |h, k| raise Dk::NoParamError, "no param named `#{k}`" }
  @params.merge!(dk_normalize_params(opts[:params]))

  d = Config::DEFAULT_CALLBACKS
  @task_callbacks = {
    'before'         => opts[:before_callbacks]         || d.dup,
    'prepend_before' => opts[:prepend_before_callbacks] || d.dup,
    'after'          => opts[:after_callbacks]          || d.dup,
    'prepend_after'  => opts[:prepend_after_callbacks]  || d.dup
  }

  @ssh_hosts     = opts[:ssh_hosts]     || Config::DEFAULT_SSH_HOSTS.dup
  @ssh_args      = opts[:ssh_args]      || Config::DEFAULT_SSH_ARGS.dup
  @host_ssh_args = opts[:host_ssh_args] || Config::DEFAULT_HOST_SSH_ARGS.dup

  @logger = opts[:logger] || NullLogger.new

  @has_run_task_classes = Set.new
end

Public Instance Methods

add_task_callback(named, subject_task_class, callback_task_class, params) click to toggle source
# File lib/dk/runner.rb, line 57
def add_task_callback(named, subject_task_class, callback_task_class, params)
  @task_callbacks[named][subject_task_class] << Task::Callback.new(
    callback_task_class,
    params
  )
end
cmd(*args) click to toggle source
# File lib/dk/runner.rb, line 116
def cmd(*args)
  build_and_run_local_cmd(*args)
end
has_run_task?(task_class) click to toggle source
# File lib/dk/runner.rb, line 124
def has_run_task?(task_class)
  @has_run_task_classes.include?(task_class)
end
log_cli_run(cli_argv, &run_block) click to toggle source
# File lib/dk/runner.rb, line 100
def log_cli_run(cli_argv, &run_block)
  15.times{ self.logger.debug "" }
  self.logger.debug "===================================="
  self.logger.debug ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> `#{cli_argv}`"
  self.logger.debug "===================================="
  time = Benchmark.realtime(&run_block)
  self.logger.info "(#{self.pretty_run_time(time)})"
  self.logger.debug "===================================="
  self.logger.debug "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< `#{cli_argv}`"
  self.logger.debug "===================================="
end
log_cli_task_run(task_name, &run_block) click to toggle source
# File lib/dk/runner.rb, line 92
def log_cli_task_run(task_name, &run_block)
  self.logger.info "Starting `#{task_name}`."
  time = Benchmark.realtime(&run_block)
  self.logger.info "`#{task_name}` finished in #{self.pretty_run_time(time)}."
  self.logger.info ""
  self.logger.info ""
end
log_debug(msg, *ansi_styles) click to toggle source
# File lib/dk/runner.rb, line 78
def log_debug(msg, *ansi_styles)
  self.logger.debug("#{INDENT_LOG_PREFIX}#{Ansi.styled_msg(msg, *ansi_styles)}")
end
log_error(msg, *ansi_styles) click to toggle source
# File lib/dk/runner.rb, line 82
def log_error(msg, *ansi_styles)
  self.logger.error("#{INDENT_LOG_PREFIX}#{Ansi.styled_msg(msg, *ansi_styles)}")
end
log_info(msg, *ansi_styles) click to toggle source
# File lib/dk/runner.rb, line 74
def log_info(msg, *ansi_styles)
  self.logger.info("#{INDENT_LOG_PREFIX}#{Ansi.styled_msg(msg, *ansi_styles)}")
end
log_task_run(task_class, &run_block) click to toggle source
# File lib/dk/runner.rb, line 86
def log_task_run(task_class, &run_block)
  self.logger.debug "#{TASK_START_LOG_PREFIX}#{task_class}"
  time = Benchmark.realtime(&run_block)
  self.logger.debug "#{TASK_END_LOG_PREFIX}#{task_class} (#{self.pretty_run_time(time)})"
end
pretty_run_time(raw_run_time) click to toggle source
# File lib/dk/runner.rb, line 128
def pretty_run_time(raw_run_time)
  if raw_run_time > 1.5 # seconds
    "#{raw_run_time.to_i / 60}:#{(raw_run_time.round % 60).to_i.to_s.rjust(2, '0')}s"
  else
    "#{(raw_run_time * 1000 * 10.0).round / 10.0}ms"
  end
end
run(task_class, params = nil) click to toggle source

called by CLI on top-level tasks

# File lib/dk/runner.rb, line 65
def run(task_class, params = nil)
  check_run_once_and_build_and_run_task(task_class, params)
end
run_task(task_class, params = nil) click to toggle source

called by other tasks on sub-tasks

# File lib/dk/runner.rb, line 70
def run_task(task_class, params = nil)
  check_run_once_and_build_and_run_task(task_class, params)
end
ssh(*args) click to toggle source
# File lib/dk/runner.rb, line 120
def ssh(*args)
  build_and_run_remote_cmd(*args)
end
start(*args) click to toggle source
# File lib/dk/runner.rb, line 112
def start(*args)
  build_and_start_local_cmd(*args)
end
task_callback_task_classes(named, task_class) click to toggle source
# File lib/dk/runner.rb, line 53
def task_callback_task_classes(named, task_class)
  task_callbacks(named, task_class).map(&:task_class)
end
task_callbacks(named, task_class) click to toggle source
# File lib/dk/runner.rb, line 49
def task_callbacks(named, task_class)
  @task_callbacks[named][task_class] || []
end

Private Instance Methods

build_and_log_local_cmd(task, cmd_str, input, given_opts, &block) click to toggle source
# File lib/dk/runner.rb, line 165
def build_and_log_local_cmd(task, cmd_str, input, given_opts, &block)
  local_cmd = build_local_cmd(task, cmd_str, input, given_opts)
  log_local_cmd(local_cmd){ |cmd| block.call(cmd, input) }
end
build_and_run_local_cmd(*args) click to toggle source
# File lib/dk/runner.rb, line 161
def build_and_run_local_cmd(*args)
  build_and_log_local_cmd(*args){ |cmd, input| cmd.run(input) }
end
build_and_run_remote_cmd(task, cmd_str, input, given_opts, ssh_opts) click to toggle source
# File lib/dk/runner.rb, line 186
def build_and_run_remote_cmd(task, cmd_str, input, given_opts, ssh_opts)
  remote_cmd = build_remote_cmd(task, cmd_str, input, given_opts, ssh_opts)
  log_remote_cmd(remote_cmd){ |cmd| cmd.run(input) }
end
build_and_run_task(task_class, params = nil) click to toggle source
# File lib/dk/runner.rb, line 146
def build_and_run_task(task_class, params = nil)
  build_task(task_class, params).tap do |task|
    task.dk_run
    @has_run_task_classes << task_class
  end
end
build_and_start_local_cmd(*args) click to toggle source
# File lib/dk/runner.rb, line 157
def build_and_start_local_cmd(*args)
  build_and_log_local_cmd(*args){ |cmd, input| cmd.start(input) }
end
build_local_cmd(task, cmd_str, input, given_opts) click to toggle source

input is needed for the `TestRunner` so it can use it with stubbing otherwise it is ignored when building a local cmd

# File lib/dk/runner.rb, line 172
def build_local_cmd(task, cmd_str, input, given_opts)
  Local::Cmd.new(cmd_str, given_opts)
end
build_remote_cmd(task, cmd_str, input, given_opts, ssh_opts) click to toggle source

input and given opts are needed for the `TestRunner` so it can use it with stubbing otherwise they are ignored when building a remote cmd

# File lib/dk/runner.rb, line 193
def build_remote_cmd(task, cmd_str, input, given_opts, ssh_opts)
  Remote::Cmd.new(cmd_str, ssh_opts)
end
build_task(task_class, params = nil) click to toggle source
# File lib/dk/runner.rb, line 153
def build_task(task_class, params = nil)
  task_class.new(self, params)
end
check_run_once_and_build_and_run_task(task_class, params = nil) click to toggle source
# File lib/dk/runner.rb, line 138
def check_run_once_and_build_and_run_task(task_class, params = nil)
  if task_class.run_only_once && self.has_run_task?(task_class)
    build_task(task_class, params)
  else
    build_and_run_task(task_class, params)
  end
end
log_local_cmd(cmd, &block) click to toggle source
# File lib/dk/runner.rb, line 176
def log_local_cmd(cmd, &block)
  self.logger.info("#{CMD_LOG_PREFIX}#{cmd.cmd_str}")
  time = Benchmark.realtime{ block.call(cmd) }
  self.logger.info("#{INDENT_LOG_PREFIX}(#{self.pretty_run_time(time)})")
  cmd.output_lines.each do |output_line|
    self.logger.debug("#{INDENT_LOG_PREFIX}#{CMD_SSH_OUT_LOG_PREFIX}#{output_line.line}")
  end
  cmd
end
log_remote_cmd(cmd, &block) click to toggle source
# File lib/dk/runner.rb, line 197
def log_remote_cmd(cmd, &block)
  self.logger.info("#{SSH_LOG_PREFIX}#{cmd.cmd_str}")
  self.logger.debug("#{INDENT_LOG_PREFIX}#{cmd.ssh_cmd_str('<host>')}")
  cmd.hosts.each do |host|
    self.logger.info("#{INDENT_LOG_PREFIX}[#{host}]")
  end
  time = Benchmark.realtime{ block.call(cmd) }
  self.logger.info("#{INDENT_LOG_PREFIX}(#{self.pretty_run_time(time)})")
  cmd.output_lines.each do |ol|
    self.logger.debug "#{INDENT_LOG_PREFIX}[#{ol.host}] " \
                      "#{CMD_SSH_OUT_LOG_PREFIX}#{ol.line}"
  end
  cmd
end