Class: Wright::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/wright/resource.rb,
lib/wright/resource/file.rb,
lib/wright/resource/user.rb,
lib/wright/resource/group.rb,
lib/wright/resource/package.rb,
lib/wright/resource/symlink.rb,
lib/wright/resource/directory.rb

Overview

Resource base class.

Direct Known Subclasses

Directory, File, Group, Package, Symlink, User

Defined Under Namespace

Classes: Directory, File, Group, Package, Symlink, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Resource

Initializes a Resource.


12
13
14
15
16
17
18
19
# File 'lib/wright/resource.rb', line 12

def initialize(name = nil)
  @name = name
  @resource_name = Util.class_to_resource_name(self.class).to_sym
  @provider = provider_for_resource
  @action = nil
  @on_update = nil
  @ignore_failure = false
end

Instance Attribute Details

#actionSymbol


22
23
24
# File 'lib/wright/resource.rb', line 22

def action
  @action
end

#ignore_failureBool


25
26
27
# File 'lib/wright/resource.rb', line 25

def ignore_failure
  @ignore_failure
end

#nameString

Returns the resource's name attribute

Examples:

foo = Wright::Resource::Symlink.new('/tmp/fstab')
foo.name
# => "/tmp/fstab"

bar = Wright::Resource::Symlink.new
bar.name = '/tmp/passwd'
bar.name
# => "/tmp/passwd"

38
39
40
# File 'lib/wright/resource.rb', line 38

def name
  @name
end

#resource_nameSymbol (readonly)

Returns a compact resource name

Examples:

foo = Wright::Resource::Symlink.new
foo.resource_name
# => :symlink

46
47
48
# File 'lib/wright/resource.rb', line 46

def resource_name
  @resource_name
end

Instance Method Details

#log_error(exception) ⇒ Object (private)


117
118
119
120
121
# File 'lib/wright/resource.rb', line 117

def log_error(exception)
  resource = "#{@resource_name}"
  resource << " '#{@name}'" if @name
  Wright.log.error "#{resource}: #{exception}"
end

#might_update_resourceBool (private)

Marks a code block that might update a resource.

Usually this method is called in the definition of a new resource class in order to mark those methods that should be able to trigger update actions. Runs the current update action if the provider was updated by the block method.

Examples:

class BalloonAnimal < Wright::Provider
  def inflate
    puts "It's a giraffe!"
    @updated = true
  end
end

class Balloon < Wright::Resource
  def inflate
    might_update_resource { @provider.inflate }
  end
end
Wright::Config[:resources] = { balloon: { provider: 'BalloonAnimal' } }

balloon = Balloon.new.inflate
# => true

105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wright/resource.rb', line 105

def might_update_resource
  begin
    yield
  rescue => e
    log_error(e)
    raise e unless @ignore_failure
  end
  updated = @provider.updated?
  run_update_action if updated
  updated
end

#on_update=(on_update) ⇒ void

This method returns an undefined value.

Sets an update action for a resource.

Raises:

  • (ArgumentError)

    if on_update is not callable


55
56
57
58
59
60
61
# File 'lib/wright/resource.rb', line 55

def on_update=(on_update)
  if on_update.respond_to?(:call) || on_update.nil?
    @on_update = on_update
  else
    fail ArgumentError, "#{on_update} is not callable"
  end
end

#provider_for_resourceObject (private)


148
149
150
151
152
153
154
155
156
157
# File 'lib/wright/resource.rb', line 148

def provider_for_resource
  klass = Util::ActiveSupport.safe_constantize(provider_name)
  if klass
    klass.new(self)
  else
    warning = "Could not find a provider for resource #{resource_class}"
    Wright.log.warn warning
    nil
  end
end

#provider_nameObject (private)


140
141
142
143
144
145
146
# File 'lib/wright/resource.rb', line 140

def provider_name
  if Wright::Config.nested_key?(:resources, @resource_name, :provider)
    Wright::Config[:resources][@resource_name][:provider]
  else
    "Wright::Provider::#{resource_class}"
  end
end

#resource_classObject (private)


136
137
138
# File 'lib/wright/resource.rb', line 136

def resource_class
  Util::ActiveSupport.camelize(@resource_name)
end

#run_actionObject

Runs the resource's current action.

Examples:

fstab = Wright::Resource::Symlink.new('/tmp/fstab')
fstab.action = :remove
fstab.run_action

71
72
73
# File 'lib/wright/resource.rb', line 71

def run_action
  send @action if @action
end

#run_update_actionObject (private)


123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/wright/resource.rb', line 123

def run_update_action
  return if @on_update.nil?

  resource = "#{@resource_name} '#{@name}'"
  notification = "run update action for #{resource}"
  if Wright.dry_run?
    Wright.log.info "(would) #{notification}"
  else
    Wright.log.info notification
    @on_update.call
  end
end