Class: Nodectl::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/nodectl/recipe.rb

Overview

TODO: param validation

Defined Under Namespace

Modules: ObjectMethods Classes: DSL

Constant Summary collapse

SLOT_NAMES =
[:install, :uninstall, :start, :version, :version_list, :version_set]
UnsupportedAction =
Class.new(StandardError)
UnsupportedSlot =
Class.new(StandardError)
LogNotFound =
Class.new(Nodectl::NotFound)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Recipe

Returns a new instance of Recipe.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nodectl/recipe.rb', line 14

def initialize(name, options = {})
  @name     = name
  @actions  = options[:actions] || {}
  @slots    = options[:slots] || {}
  @triggers = options[:triggers] || {}
  @events   = options[:events] || {}
  @logs     = options[:logs] || {}
  @params   = options[:params] || {}

  self.class[@name.to_s] = self
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/nodectl/recipe.rb', line 12

def name
  @name
end

Class Method Details

.declare(*args, &blk) ⇒ Object



193
194
195
# File 'lib/nodectl/recipe.rb', line 193

def declare(*args, &blk)
  DSL.new(*args, &blk).create
end

Instance Method Details

#actionsObject



96
97
98
# File 'lib/nodectl/recipe.rb', line 96

def actions
  @actions.keys
end

#bind(service) ⇒ Object



26
27
28
# File 'lib/nodectl/recipe.rb', line 26

def bind(service)
  Nodectl::Binding.new(service, self)
end

#get_logfile(service, log_name) ⇒ Object



85
86
87
88
89
90
# File 'lib/nodectl/recipe.rb', line 85

def get_logfile(service, log_name)
  log = @logs.fetch(log_name)
  log.instance_path(service)
rescue KeyError
  raise LogNotFound, "log '#{log_name}' for recipe '#{name}' not found (service '#{service.name}')"
end

#logsObject



92
93
94
# File 'lib/nodectl/recipe.rb', line 92

def logs
  @logs.keys
end

#paramsObject



112
113
114
# File 'lib/nodectl/recipe.rb', line 112

def params
  @params
end

#run_action(service, action_name, run_params = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nodectl/recipe.rb', line 58

def run_action(service, action_name, run_params = {})
  unless @actions.key?(action_name)
    raise UnsupportedAction, "action '#{action_name}' in not supported by '#{name}' recipe"
  end

  @params.each do |key, value|
    if !run_params.key?(key) && value.key?(:default)
      run_params[key] = value[:default]
    end
  end

  Nodectl::Action.run(service, action_name) do
    run_trigger(service, action_name, :pre)
    result = context(service).instance_exec run_params, &@actions[action_name]
    run_trigger(service, action_name, :post)
  end
end

#run_slot(service, slot_name, run_params = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nodectl/recipe.rb', line 30

def run_slot(service, slot_name, run_params = nil)
  run_params ||= {}
  
  unless @slots.key?(slot_name)
    raise UnsupportedSlot, "slot '#{slot_name}' in not supported by '#{name}' recipe"
  end

  if slot_name == :install
    install_deps(service)
  end

  run_trigger(service, slot_name, :pre)

  @params.each do |key, value|
    if !run_params.key?(key) && value.key?(:default)
      run_params[key] = value[:default]
    end
  end

  run_params.keys.each do |key|
    run_params.delete(key) unless @params.key?(key)
  end

  result = context(service).instance_exec run_params, &@slots[slot_name]
  run_trigger(service, slot_name, :post)
  result
end

#run_trigger(service, action_name, trigger_type) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/nodectl/recipe.rb', line 76

def run_trigger(service, action_name, trigger_type)
  unless [:pre, :post].include? trigger_type
    raise "incorrect trigger type: expected 'post' or 'pre'"
  end

  trigger = @triggers[:"#{trigger_type}_#{action_name}"]
  context(service).instance_eval &trigger if trigger
end

#supported_actions(service) ⇒ Object



104
105
106
# File 'lib/nodectl/recipe.rb', line 104

def supported_actions(service)
  @actions.keys
end

#supported_slots(service) ⇒ Object



108
109
110
# File 'lib/nodectl/recipe.rb', line 108

def supported_slots(service)
  @slots.keys
end

#triggersObject



100
101
102
# File 'lib/nodectl/recipe.rb', line 100

def triggers
  @triggers.keys
end