Class: Itamae::Resource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/itamae/resource/base.rb

Defined Under Namespace

Classes: EvalContext

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe, resource_name, &block) ⇒ Base

Returns a new instance of Base.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/itamae/resource/base.rb', line 92

def initialize(recipe, resource_name, &block)
  clear_current_attributes
  @recipe = recipe
  @resource_name = resource_name
  @updated = false

  EvalContext.new(self).tap do |context|
    context.instance_eval(&block) if block
    @attributes = context.attributes
    @notifications = context.notifications
    @subscriptions = context.subscriptions
    @only_if_command = context.only_if_command
    @not_if_command = context.not_if_command
  end

  process_attributes
end

Class Attribute Details

.defined_attributesObject (readonly)

Returns the value of attribute defined_attributes.



65
66
67
# File 'lib/itamae/resource/base.rb', line 65

def defined_attributes
  @defined_attributes
end

.supported_osesObject (readonly)

Returns the value of attribute supported_oses.



66
67
68
# File 'lib/itamae/resource/base.rb', line 66

def supported_oses
  @supported_oses
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



86
87
88
# File 'lib/itamae/resource/base.rb', line 86

def attributes
  @attributes
end

#current_attributesObject (readonly) Also known as: current

Returns the value of attribute current_attributes.



87
88
89
# File 'lib/itamae/resource/base.rb', line 87

def current_attributes
  @current_attributes
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



89
90
91
# File 'lib/itamae/resource/base.rb', line 89

def notifications
  @notifications
end

#recipeObject (readonly)

Returns the value of attribute recipe.



84
85
86
# File 'lib/itamae/resource/base.rb', line 84

def recipe
  @recipe
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



85
86
87
# File 'lib/itamae/resource/base.rb', line 85

def resource_name
  @resource_name
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



88
89
90
# File 'lib/itamae/resource/base.rb', line 88

def subscriptions
  @subscriptions
end

#updatedObject (readonly)

Returns the value of attribute updated.



90
91
92
# File 'lib/itamae/resource/base.rb', line 90

def updated
  @updated
end

Class Method Details

.define_attribute(name, options) ⇒ Object



75
76
77
78
# File 'lib/itamae/resource/base.rb', line 75

def define_attribute(name, options)
  current = @defined_attributes[name.to_sym] || {}
  @defined_attributes[name.to_sym] = current.merge(options)
end

.inherited(subclass) ⇒ Object



68
69
70
71
72
73
# File 'lib/itamae/resource/base.rb', line 68

def inherited(subclass)
  subclass.instance_variable_set(
    :@defined_attributes,
    self.defined_attributes.dup
  )
end

Instance Method Details

#action_nothing(options) ⇒ Object



133
134
135
# File 'lib/itamae/resource/base.rb', line 133

def action_nothing(options)
  # do nothing
end

#resource_typeObject



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

def resource_type
  humps = []
  self.class.name.split("::").last.each_char do |c|
    if "A" <= c && c <= "Z"
      humps << c.downcase
    else
      humps.last << c
    end
  end
  humps.join('_')
end

#run(specific_action = nil, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/itamae/resource/base.rb', line 110

def run(specific_action = nil, options = {})
  Logger.info "#{resource_type}[#{resource_name}]"

  Logger.formatter.indent do
    if do_not_run_because_of_only_if?
      Logger.info "Execution skipped because of only_if attribute"
      return
    elsif do_not_run_because_of_not_if?
      Logger.info "Execution skipped because of not_if attribute"
      return
    end

    [specific_action || attributes.action].flatten.each do |action|
      run_action(action, options)
    end

    notify(options) if updated?
  end
rescue Backend::CommandExecutionError
  Logger.error "Failed."
  exit 2
end