Class: Dk::Config

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

Defined Under Namespace

Classes: DryTreeStub, LogslyLogger

Constant Summary collapse

UnknownTaskError =
Class.new(ArgumentError) do
  def initialize(task_name)
    super("No task named #{task_name}")
  end
end
DEFAULT_INIT_PROCS =
[].freeze
DEFAULT_PARAMS =
{}.freeze
DEFAULT_CALLBACKS =
Hash.new{ |h, k| h[k] = Dk::Task::CallbackSet.new }.freeze
DEFAULT_SSH_HOSTS =
{}.freeze
DEFAULT_SSH_ARGS =
''.freeze
DEFAULT_HOST_SSH_ARGS =
Hash.new{ |h, k| h[k] = DEFAULT_SSH_ARGS }
DEFAULT_TASKS =
Hash.new{ |h, k| raise UnknownTaskError.new("`#{k}`") }.freeze
DEFAULT_LOG_PATTERN =
"%m\n".freeze
DEFAULT_LOG_FILE_PATTERN =
'[%d %-5l] : %m\n'.freeze
DEFAULT_STDOUT_LOG_LEVEL =
'info'.freeze
DEFAULT_STUBS =
[].freeze
FILE_LOG_LEVEL =
'debug'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dk/config.rb', line 36

def initialize
  @init_procs               = DEFAULT_INIT_PROCS.dup
  @params                   = DEFAULT_PARAMS.dup
  @before_callbacks         = DEFAULT_CALLBACKS.dup
  @prepend_before_callbacks = DEFAULT_CALLBACKS.dup
  @after_callbacks          = DEFAULT_CALLBACKS.dup
  @prepend_after_callbacks  = DEFAULT_CALLBACKS.dup
  @ssh_hosts                = DEFAULT_SSH_HOSTS.dup
  @ssh_args                 = DEFAULT_SSH_ARGS.dup
  @host_ssh_args            = DEFAULT_HOST_SSH_ARGS.dup
  @tasks                    = DEFAULT_TASKS.dup
  @stdout_log_level         = DEFAULT_STDOUT_LOG_LEVEL
  @log_pattern              = DEFAULT_LOG_PATTERN
  @log_file                 = nil
  @log_file_pattern         = DEFAULT_LOG_FILE_PATTERN
  @dry_tree_cmd_stubs       = DEFAULT_STUBS.dup
  @dry_tree_ssh_stubs       = DEFAULT_STUBS.dup
end

Instance Attribute Details

#after_callbacksObject (readonly)

Returns the value of attribute after_callbacks.



33
34
35
# File 'lib/dk/config.rb', line 33

def after_callbacks
  @after_callbacks
end

#before_callbacksObject (readonly)

Returns the value of attribute before_callbacks.



32
33
34
# File 'lib/dk/config.rb', line 32

def before_callbacks
  @before_callbacks
end

#dry_tree_cmd_stubsObject (readonly)

Returns the value of attribute dry_tree_cmd_stubs.



34
35
36
# File 'lib/dk/config.rb', line 34

def dry_tree_cmd_stubs
  @dry_tree_cmd_stubs
end

#dry_tree_ssh_stubsObject (readonly)

Returns the value of attribute dry_tree_ssh_stubs.



34
35
36
# File 'lib/dk/config.rb', line 34

def dry_tree_ssh_stubs
  @dry_tree_ssh_stubs
end

#init_procsObject (readonly)

Returns the value of attribute init_procs.



31
32
33
# File 'lib/dk/config.rb', line 31

def init_procs
  @init_procs
end

#paramsObject (readonly)

Returns the value of attribute params.



31
32
33
# File 'lib/dk/config.rb', line 31

def params
  @params
end

#prepend_after_callbacksObject (readonly)

Returns the value of attribute prepend_after_callbacks.



33
34
35
# File 'lib/dk/config.rb', line 33

def prepend_after_callbacks
  @prepend_after_callbacks
end

#prepend_before_callbacksObject (readonly)

Returns the value of attribute prepend_before_callbacks.



32
33
34
# File 'lib/dk/config.rb', line 32

def prepend_before_callbacks
  @prepend_before_callbacks
end

#tasksObject (readonly)

Returns the value of attribute tasks.



34
35
36
# File 'lib/dk/config.rb', line 34

def tasks
  @tasks
end

Instance Method Details

#after(subject_task_class, callback_task_class, params = nil) ⇒ Object



77
78
79
80
81
82
# File 'lib/dk/config.rb', line 77

def after(subject_task_class, callback_task_class, params = nil)
  self.after_callbacks[subject_task_class] << Task::Callback.new(
    callback_task_class,
    params
  )
end

#after_callback_task_classes(for_task_class) ⇒ Object



99
100
101
# File 'lib/dk/config.rb', line 99

def after_callback_task_classes(for_task_class)
  self.after_callbacks[for_task_class].map(&:task_class)
end

#before(subject_task_class, callback_task_class, params = nil) ⇒ Object



63
64
65
66
67
68
# File 'lib/dk/config.rb', line 63

def before(subject_task_class, callback_task_class, params = nil)
  self.before_callbacks[subject_task_class] << Task::Callback.new(
    callback_task_class,
    params
  )
end

#before_callback_task_classes(for_task_class) ⇒ Object



91
92
93
# File 'lib/dk/config.rb', line 91

def before_callback_task_classes(for_task_class)
  self.before_callbacks[for_task_class].map(&:task_class)
end

#dk_loggerObject



173
174
175
# File 'lib/dk/config.rb', line 173

def dk_logger
  @dk_logger ||= LogslyLogger.new(self)
end

#dk_logger_file_output_nameObject



168
169
170
171
# File 'lib/dk/config.rb', line 168

def dk_logger_file_output_name
  # include the object id to ensure the output is unique to the instance
  @dk_logger_file_output_name ||= "dk-config-#{self.object_id}-file"
end

#dk_logger_stdout_output_nameObject

private - intended for internal use only



163
164
165
166
# File 'lib/dk/config.rb', line 163

def dk_logger_stdout_output_name
  # include the object id to ensure the output is unique to the instance
  @dk_logger_stdout_output_name ||= "dk-config-#{self.object_id}-stdout"
end

#initObject



55
56
57
# File 'lib/dk/config.rb', line 55

def init
  self.init_procs.each{ |block| self.instance_eval(&block) }
end

#log_file(value = nil) ⇒ Object



127
128
129
130
# File 'lib/dk/config.rb', line 127

def log_file(value = nil)
  @log_file = value if !value.nil?
  @log_file
end

#log_file_pattern(value = nil) ⇒ Object



132
133
134
135
# File 'lib/dk/config.rb', line 132

def log_file_pattern(value = nil)
  @log_file_pattern = value if !value.nil?
  @log_file_pattern
end

#log_pattern(value = nil) ⇒ Object



122
123
124
125
# File 'lib/dk/config.rb', line 122

def log_pattern(value = nil)
  @log_pattern = value if !value.nil?
  @log_pattern
end

#prepend_after(subject_task_class, callback_task_class, params = nil) ⇒ Object



84
85
86
87
88
89
# File 'lib/dk/config.rb', line 84

def prepend_after(subject_task_class, callback_task_class, params = nil)
  self.prepend_after_callbacks[subject_task_class].unshift(Task::Callback.new(
    callback_task_class,
    params
  ))
end

#prepend_after_callback_task_classes(for_task_class) ⇒ Object



103
104
105
# File 'lib/dk/config.rb', line 103

def prepend_after_callback_task_classes(for_task_class)
  self.prepend_after_callbacks[for_task_class].map(&:task_class)
end

#prepend_before(subject_task_class, callback_task_class, params = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/dk/config.rb', line 70

def prepend_before(subject_task_class, callback_task_class, params = nil)
  self.prepend_before_callbacks[subject_task_class].unshift(Task::Callback.new(
    callback_task_class,
    params
  ))
end

#prepend_before_callback_task_classes(for_task_class) ⇒ Object



95
96
97
# File 'lib/dk/config.rb', line 95

def prepend_before_callback_task_classes(for_task_class)
  self.prepend_before_callbacks[for_task_class].map(&:task_class)
end

#set_param(key, value) ⇒ Object



59
60
61
# File 'lib/dk/config.rb', line 59

def set_param(key, value)
  self.params.merge!(dk_normalize_params(key => value))
end

#stdout_log_level(value = nil) ⇒ Object



117
118
119
120
# File 'lib/dk/config.rb', line 117

def stdout_log_level(value = nil)
  @stdout_log_level = value if !value.nil?
  @stdout_log_level
end

#stub_dry_tree_cmd(cmd_str, args = nil, &block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dk/config.rb', line 137

def stub_dry_tree_cmd(cmd_str, args = nil, &block)
  args ||= {}

  cmd_str_proc    = get_cmd_ssh_proc(cmd_str)
  input_proc      = get_cmd_ssh_proc(args[:input])
  given_opts_proc = get_cmd_ssh_proc(args[:opts])

  @dry_tree_cmd_stubs.unshift(
    DryTreeStub.new(cmd_str_proc, input_proc, given_opts_proc, block)
  )
end

#stub_dry_tree_ssh(cmd_str, args = nil, &block) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dk/config.rb', line 149

def stub_dry_tree_ssh(cmd_str, args = nil, &block)
  args ||= {}

  cmd_str_proc    = get_cmd_ssh_proc(cmd_str)
  input_proc      = get_cmd_ssh_proc(args[:input])
  given_opts_proc = get_cmd_ssh_proc(args[:opts])

  @dry_tree_ssh_stubs.unshift(
    DryTreeStub.new(cmd_str_proc, input_proc, given_opts_proc, block)
  )
end

#task(name, task_class = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/dk/config.rb', line 107

def task(name, task_class = nil)
  if !task_class.nil?
    if !task_class.kind_of?(Class) || !task_class.include?(Dk::Task)
      raise ArgumentError, "#{task_class.inspect} is not a Dk::Task"
    end
    @tasks[name.to_s] = task_class
  end
  @tasks[name.to_s]
end