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.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/itamae/resource/base.rb', line 104

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
    @verify_commands = context.verify_commands
  end

  process_attributes
end

Class Attribute Details

.defined_attributesObject (readonly)

Returns the value of attribute defined_attributes.



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

def defined_attributes
  @defined_attributes
end

.supported_osesObject (readonly)

Returns the value of attribute supported_oses.



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

def supported_oses
  @supported_oses
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



98
99
100
# File 'lib/itamae/resource/base.rb', line 98

def attributes
  @attributes
end

#current_attributesObject (readonly) Also known as: current

Returns the value of attribute current_attributes.



99
100
101
# File 'lib/itamae/resource/base.rb', line 99

def current_attributes
  @current_attributes
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



101
102
103
# File 'lib/itamae/resource/base.rb', line 101

def notifications
  @notifications
end

#recipeObject (readonly)

Returns the value of attribute recipe.



96
97
98
# File 'lib/itamae/resource/base.rb', line 96

def recipe
  @recipe
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



97
98
99
# File 'lib/itamae/resource/base.rb', line 97

def resource_name
  @resource_name
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



100
101
102
# File 'lib/itamae/resource/base.rb', line 100

def subscriptions
  @subscriptions
end

#updatedObject (readonly)

Returns the value of attribute updated.



102
103
104
# File 'lib/itamae/resource/base.rb', line 102

def updated
  @updated
end

Class Method Details

.define_attribute(name, options) ⇒ Object



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

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

.inherited(subclass) ⇒ Object



79
80
81
82
83
84
# File 'lib/itamae/resource/base.rb', line 79

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

Instance Method Details

#action_nothing(options) ⇒ Object



149
150
151
# File 'lib/itamae/resource/base.rb', line 149

def action_nothing(options)
  # do nothing
end

#resource_typeObject



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/itamae/resource/base.rb', line 153

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/itamae/resource/base.rb', line 123

def run(specific_action = nil, options = {})
  Itamae.logger.debug "#{resource_type}[#{resource_name}]"

  Itamae.logger.with_indent_if(Itamae.logger.debug?) do
    if do_not_run_because_of_only_if?
      Itamae.logger.debug "#{resource_type}[#{resource_name}] Execution skipped because of only_if attribute"
      return
    elsif do_not_run_because_of_not_if?
      Itamae.logger.debug "#{resource_type}[#{resource_name}] Execution skipped because of not_if attribute"
      return
    end

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

    verify unless options[:dry_run]
    notify(options) if updated?
  end

  @updated = false
rescue Backend::CommandExecutionError
  Itamae.logger.error "#{resource_type}[#{resource_name}] Failed."
  exit 2
end