Module: Psychic::Runner::BaseRunner

Includes:
Logger, Shell
Included in:
Psychic::Runner, Cold::ShellScriptRunner, HotRunner
Defined in:
lib/psychic/runner/base_runner.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_PARAMS_FILE =
'psychic-parameters.yaml'

Constants included from Shell

Shell::AVAILABLE_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log_level=, #logger, #new_logger

Methods included from Shell

#shell

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(task, *args, &block) ⇒ Object



50
51
52
53
54
# File 'lib/psychic/runner/base_runner.rb', line 50

def method_missing(task, *args, &block)
  execute_task(task, *args)
rescue Psychic::Runner::TaskNotImplementedError
  super
end

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



10
11
12
# File 'lib/psychic/runner/base_runner.rb', line 10

def cwd
  @cwd
end

#envObject (readonly)

Returns the value of attribute env.



11
12
13
# File 'lib/psychic/runner/base_runner.rb', line 11

def env
  @env
end

#hintsObject (readonly)

Returns the value of attribute hints.



12
13
14
# File 'lib/psychic/runner/base_runner.rb', line 12

def hints
  @hints
end

#known_tasksObject (readonly)

Returns the value of attribute known_tasks.



9
10
11
# File 'lib/psychic/runner/base_runner.rb', line 9

def known_tasks
  @known_tasks
end

Class Method Details

.included(base) ⇒ Object



26
27
28
# File 'lib/psychic/runner/base_runner.rb', line 26

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/psychic/runner/base_runner.rb', line 75

def active?
  self.class.magic_file_pattern ? false : Dir["#{@cwd}/#{self.class.magic_file_pattern}"]
end

#command_for_task(task, *_args) ⇒ Object



64
65
66
67
# File 'lib/psychic/runner/base_runner.rb', line 64

def command_for_task(task, *_args)
  task_name = task.to_s
  self[task_name]
end

#dry_run?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/psychic/runner/base_runner.rb', line 79

def dry_run?
  @dry_run == true
end

#execute(command, *args) ⇒ Object

Reserved words



58
59
60
61
62
# File 'lib/psychic/runner/base_runner.rb', line 58

def execute(command, *args)
  full_cmd = [command, *args].join(' ')
  logger.info("Executing #{full_cmd}")
  shell.execute(full_cmd, @shell_opts) unless dry_run?
end

#execute_task(task, *args) ⇒ Object



69
70
71
72
73
# File 'lib/psychic/runner/base_runner.rb', line 69

def execute_task(task, *args)
  command = command_for_task(task, *args)
  fail Psychic::Runner::TaskNotImplementedError if command.nil?
  execute(command, *args)
end

#initialize(opts = {}, _hints = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/psychic/runner/base_runner.rb', line 30

def initialize(opts = {}, _hints = {})
  @cwd = opts[:cwd] ||= Dir.pwd
  @hints = Psychic::Util.stringified_hash(opts[:hints] || load_hints || {})
  if @hints['options']
    opts.merge! Psychic::Util.symbolized_hash(@hints['options'])
  end
  @logger = opts[:logger] || new_logger
  @env = opts[:env] || ENV.to_hash
  @parameters = load_parameters(opts[:parameters])
  @cli, @interactive_mode, @parameter_mode, @restore_mode, @dry_run = opts.values_at(
    :cli, :interactive, :parameter_mode, :restore_mode, :dry_run)
  # Make sure to delete any option that isn't a MixLib::ShellOut option
  @shell_opts = opts.select { |key, _| Psychic::Shell::AVAILABLE_OPTIONS.include? key }
end

#respond_to_missing?(task, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/psychic/runner/base_runner.rb', line 45

def respond_to_missing?(task, include_all = false)
  return true if known_tasks.include?(task.to_s)
  super
end