Class: Dk::Runner

Inherits:
Object
  • Object
show all
Includes:
HasSSHOpts, HasSetParam
Defined in:
lib/dk/runner.rb

Direct Known Subclasses

ConfigRunner, TestRunner

Constant Summary collapse

TASK_START_LOG_PREFIX =
' >>>  '.freeze
TASK_END_LOG_PREFIX =
' <<<  '.freeze
INDENT_LOG_PREFIX =
'      '.freeze
CMD_LOG_PREFIX =
'[CMD] '.freeze
SSH_LOG_PREFIX =
'[SSH] '.freeze
CMD_SSH_OUT_LOG_PREFIX =
"> ".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ Runner

Returns a new instance of Runner.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



25
26
27
# File 'lib/dk/runner.rb', line 25

def logger
  @logger
end

#paramsObject (readonly)

Returns the value of attribute params.



25
26
27
# File 'lib/dk/runner.rb', line 25

def params
  @params
end

Instance Method Details

#add_task_callback(named, subject_task_class, callback_task_class, params) ⇒ Object



57
58
59
60
61
62
# 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(task, cmd_str, input, given_opts) ⇒ Object



112
113
114
# File 'lib/dk/runner.rb', line 112

def cmd(task, cmd_str, input, given_opts)
  build_and_run_local_cmd(task, cmd_str, input, given_opts)
end

#has_run_task?(task_class) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/dk/runner.rb', line 120

def has_run_task?(task_class)
  @has_run_task_classes.include?(task_class)
end

#log_cli_run(cli_argv, &run_block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object



92
93
94
95
96
97
98
# 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) ⇒ Object



78
79
80
# 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) ⇒ Object



82
83
84
# 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) ⇒ Object



74
75
76
# 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) ⇒ Object



86
87
88
89
90
# 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) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/dk/runner.rb', line 124

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) ⇒ Object

called by CLI on top-level tasks



65
66
67
# 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) ⇒ Object

called by other tasks on sub-tasks



70
71
72
# 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(task, cmd_str, input, given_opts, ssh_opts) ⇒ Object



116
117
118
# File 'lib/dk/runner.rb', line 116

def ssh(task, cmd_str, input, given_opts, ssh_opts)
  build_and_run_remote_cmd(task, cmd_str, input, given_opts, ssh_opts)
end

#task_callback_task_classes(named, task_class) ⇒ Object



53
54
55
# 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) ⇒ Object



49
50
51
# File 'lib/dk/runner.rb', line 49

def task_callbacks(named, task_class)
  @task_callbacks[named][task_class] || []
end