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.



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

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.



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

def defined_attributes
  @defined_attributes
end

.supported_osesObject (readonly)

Returns the value of attribute supported_oses.



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

def supported_oses
  @supported_oses
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#current_attributesObject (readonly) Also known as: current

Returns the value of attribute current_attributes.



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

def current_attributes
  @current_attributes
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



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

def notifications
  @notifications
end

#recipeObject (readonly)

Returns the value of attribute recipe.



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

def recipe
  @recipe
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



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

def resource_name
  @resource_name
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



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

def subscriptions
  @subscriptions
end

#updatedObject (readonly)

Returns the value of attribute updated.



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

def updated
  @updated
end

Class Method Details

.define_attribute(name, options) ⇒ Object



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

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

.inherited(subclass) ⇒ Object



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

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

Instance Method Details

#action_nothingObject



154
155
156
# File 'lib/itamae/resource/base.rb', line 154

def action_nothing
  # do nothing
end

#do_not_run_because_of_not_if?Boolean

Returns:

  • (Boolean)


167
168
169
170
# File 'lib/itamae/resource/base.rb', line 167

def do_not_run_because_of_not_if?
  @not_if_command &&
    run_command(@not_if_command, error: false).exit_status == 0
end

#do_not_run_because_of_only_if?Boolean

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/itamae/resource/base.rb', line 162

def do_not_run_because_of_only_if?
  @only_if_command &&
    run_command(@only_if_command, error: false).exit_status != 0
end

#resource_typeObject



158
159
160
# File 'lib/itamae/resource/base.rb', line 158

def resource_type
  self.class.name.split("::").last.scan(/[A-Z][^A-Z]+/).map(&:downcase).join('_')
end

#run(specific_action = nil) ⇒ Object



122
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
148
149
150
151
152
# File 'lib/itamae/resource/base.rb', line 122

def run(specific_action = nil)
  runner.handler.event(:resource, resource_type: resource_type, resource_name: resource_name) do
    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)
      end

      verify unless runner.dry_run?
      if updated?
        runner.diff_found!
        notify
        runner.handler.event(:resource_updated)
      end
    end

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