Class: PoolParty::Resource

Inherits:
Base show all
Defined in:
lib/poolparty/resource.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#base_name, #init_opts

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_ordered_resources_to_result, #after_loaded, #all_resources, #before_load, #clouds_dot_rb_dir, clouds_dot_rb_dir, #clouds_dot_rb_file, clouds_dot_rb_file, #compile_opts, #create_graph, #dependencies, #get_resource, #has_searchable_paths, #method_missing, #ordered_resources, #output_resources_graph, #resources, #resources_graph, #resources_with_dependencies, #resources_without_dependencies, #run_in_context, #run_with_callbacks, #to_s, #valid?, #validations

Methods included from Delayed

included

Methods included from Callbacks

included

Methods included from SearchablePaths

included

Constructor Details

#initialize(opts = {}, extra_opts = {}, exists = true, &block) ⇒ Resource

Returns a new instance of Resource.



15
16
17
18
19
20
# File 'lib/poolparty/resource.rb', line 15

def initialize(opts={}, extra_opts={}, exists=true, &block)
  @exists ||= exists
  super(opts, extra_opts, &block)
  after_loaded_requires_parent
  valid?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PoolParty::Base

Instance Attribute Details

#exists(n = nil) ⇒ Object Also known as: exists?

Should this resource exist on the remote systems which is a lookup of the instance variable on the instance of the resource The default is that the resource DOES exist



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

def exists
  @exists
end

#graph_indexObject

Returns the value of attribute graph_index.



6
7
8
# File 'lib/poolparty/resource.rb', line 6

def graph_index
  @graph_index
end

#meta_not_ifObject

Returns the value of attribute meta_not_if.



5
6
7
# File 'lib/poolparty/resource.rb', line 5

def meta_not_if
  @meta_not_if
end

#meta_notifiesObject

Returns the value of attribute meta_notifies.



5
6
7
# File 'lib/poolparty/resource.rb', line 5

def meta_notifies
  @meta_notifies
end

#meta_only_ifObject

Returns the value of attribute meta_only_if.



5
6
7
# File 'lib/poolparty/resource.rb', line 5

def meta_only_if
  @meta_only_if
end

#meta_subscribesObject

Returns the value of attribute meta_subscribes.



5
6
7
# File 'lib/poolparty/resource.rb', line 5

def meta_subscribes
  @meta_subscribes
end

Class Method Details

.define_resource(res, base_klass = Base) ⇒ Object

Define the resource on the base class so it’s available across all PoolParty classes that use Base



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/poolparty/resource.rb', line 186

def self.define_resource(res, base_klass=Base)
  base_klass.class_eval <<-EOE
    def has_#{res.has_method_name}(a={},b={},e=true, &block)
      obj = #{res}.new(a,b,e,&block)
      resources << obj
      obj
    end
    def does_not_have_#{res.has_method_name}(a={},b={},e=false,&block)
      obj = has_#{res.has_method_name}(a,b,e,&block)
      obj
    end
    def #{res.has_method_name}s
      all_resources.select {|q| q if q.class.to_s =~ /#{res.to_s.classify}/ }
    end
    alias :#{res.has_method_name} :has_#{res.has_method_name}
    
    def get_#{res.has_method_name}(nm)
      {:#{res.has_method_name} => nm}
    end
  EOE
end

.define_resource_methodsObject

Define the resource methods for all the resources sublcassed by Resource this creates the methods:

has_<resource_name>
does_not_have_<resource_name>
<resource_name>

on the Base class The has_ method calls exists! on the resource, then places the resource in the ordered_resources array



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/poolparty/resource.rb', line 168

def self.define_resource_methods
  defined_resources.each do |res|
    next if res.method_defined?
    ddputs "Defining resource: #{res} as #{res.has_method_name} on #{self}"
    define_resource(res, is_base_resource_class? ? Base : self)
    res.method_defined!
    unless res.defined_resources.empty?
      res.define_resource_methods
    end
  end
end

.definedObject



224
225
226
# File 'lib/poolparty/resource.rb', line 224

def self.defined
  @defined ||= false
end

.defined_resourcesObject

Storage of defined resources that are stored when the subclass’d resource is subclassed



230
231
232
# File 'lib/poolparty/resource.rb', line 230

def self.defined_resources
  @defined_resources ||= []
end

.has_method_nameObject

Singleton methods has_name The has_ and does_not_have methods names are considered, unless otherwise denoted to be the top level class name for instance

class Tengo < Resource
end

the has_ method will be

has_tengo


137
138
139
# File 'lib/poolparty/resource.rb', line 137

def self.has_method_name
  to_s.top_level_class
end

.inherited(subclass) ⇒ Object

When a new resource is created, the class gets stored as a defined resource in the defined_resources resources class variable



210
211
212
# File 'lib/poolparty/resource.rb', line 210

def self.inherited(subclass)
  defined_resources << subclass
end

.is_base_resource_class?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/poolparty/resource.rb', line 180

def self.is_base_resource_class?
  self.to_s == PoolParty::Resource.to_s
end

.method_defined!Object

Note that this resource has been defined already



215
216
217
# File 'lib/poolparty/resource.rb', line 215

def self.method_defined!
  @defined = true
end

.method_defined?Boolean

Query if this resource has been defined yet

Returns:

  • (Boolean)


220
221
222
# File 'lib/poolparty/resource.rb', line 220

def self.method_defined?
  defined
end

Instance Method Details

#after_compileObject



120
121
# File 'lib/poolparty/resource.rb', line 120

def after_compile
end

#after_loaded_requires_parentObject



123
124
125
# File 'lib/poolparty/resource.rb', line 123

def after_loaded_requires_parent
  requires parent if parent && !parent.is_a?(PoolParty::Cloud) && !parent.is_a?(PoolParty::Pool)
end

#before_compileObject

CALLBACKS



117
118
# File 'lib/poolparty/resource.rb', line 117

def before_compile
end

#case_of(var, &block) ⇒ Object



157
158
# File 'lib/poolparty/resource.rb', line 157

def case_of(var, &block)
end

#cloudObject

DSL METHODS Get access to the cloud that contains this resource



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

def cloud
  get_parent_of_class(PoolParty::Cloud)
end

#compile(compiler) ⇒ Object

Dependency resolver methods



23
24
25
26
# File 'lib/poolparty/resource.rb', line 23

def compile(compiler)
  @compiler ||= PoolParty.module_eval("DependencyResolvers::#{compiler.to_s.capitalize}")
  @compiler.compile(self)
end

#does_not_exist!Object

The resource should be removed or deleted from the remote system



111
112
113
114
# File 'lib/poolparty/resource.rb', line 111

def does_not_exist!
  @exists = false
  false
end

#exists!Object

The resource exists in the output and should be created on the remote systems.



105
106
107
# File 'lib/poolparty/resource.rb', line 105

def exists!
  @exists = true
end

#has_method_nameObject

has_method_name alias for the singleton method has_method_name so that there is access to the has_method_name on the instance



143
144
145
# File 'lib/poolparty/resource.rb', line 143

def has_method_name
  self.class.has_method_name
end

#not_if(code_str = nil, &block) ⇒ Object

Not if If a block is given with the not_if, we assume it is a proc object so we grab the proc source code on both not_if and only_if code



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

def not_if(code_str=nil, &block)
  @meta_not_if = block ? [block.code, :block] : [code_str, :string]
end

#notifies(other_resources_hash, action_to_take = :reload, at_time = :delayed) ⇒ Object

META FUNCTIONS ALL RESOURCES HAVE THESE METHODS AVAILABLE



43
44
45
46
47
48
49
50
51
# File 'lib/poolparty/resource.rb', line 43

def notifies(other_resources_hash, action_to_take=:reload, at_time=:delayed)
  @meta_notifies ||= {}
  other_resources_hash.each do |k,v|
    notifies_array = (@meta_notifies[k] ||= [])
    notifies_array << [v, action_to_take, at_time] unless notifies_array.include?([v, action_to_take, at_time])
    # Implicitly add a require
    # requires(k => v)
  end
end

#only_if(code_str = nil, &block) ⇒ Object

Run only if



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

def only_if(code_str=nil, &block)
  @meta_only_if = block ? [block.code, :block] : [code_str, :string]
end

#poolObject

Get access to the pool that contains this resource



153
154
155
# File 'lib/poolparty/resource.rb', line 153

def pool
  get_parent_of_class(PoolParty::Pool)
end

print_to_chef When the dependency resolver comes through and resolves this resource, it will come through and check if it resolves to chef by checking it it responds to the

print_to_chef

method. The contents of the method are considered an ERB template and will be rendered as an ERB template.



35
36
37
38
39
# File 'lib/poolparty/resource.rb', line 35

def print_to_chef
  <<-EOE
# <%= has_method_name %>
  EOE
end

HELPERS FOR RESOURCES Print objects This helper takes an object and prints them out with as expected Case of:

Number:
  Integer of the format \d\d\d      => 0644
  Else                              => 79
String
  String of the format \d\d\d\d     => 0655
  String of the format \d\d\d       => 0644
  Else                              => "String"
Proc object
  Calls the proc object
Array
  All                               => [ "a", "b" ]
Symbol
  All                               => :a
Hash
  All                               => :a => "a", :b => ["b"]
Object
  All                               => object


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/poolparty/resource.rb', line 255

def print_variable(obj)
  case obj
  when Fixnum
    case obj
    when /^\d{3}$/
      "0#{obj.to_i}"
    else
      "#{obj.to_i}"
    end        
  when String
    "\"#{obj}\""
  when Proc
    obj.call # eh
  when Array
    "[ #{obj.map {|e| print_variable(e) }.reject {|a| a.nil? || a.empty? }.join(", ")} ]"
  when nil
    nil
  when Symbol
    ":#{obj}"
  when Hash
    "#{obj.map {|k,v| ":#{k} => #{print_variable(v)}" unless v == obj }.compact.join(",\n")}"
  else
    "#{obj}"
  end
end

#requires(other_resources_obj) ⇒ Object

Requires



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/poolparty/resource.rb', line 62

def requires(other_resources_obj)
  case other_resources_obj
  when Hash
    other_resources_obj.each do |k,v|
      dependencies[k] ||= []
      dependencies[k] << v unless dependencies[k].include?(v)
    end
  when Array
    other_resources_obj.each do |obj|
      requires(obj)
    end
  else
    # When is an object
    # k = other_resources_obj.has_method_name
    # dependencies[k] ||= []
    # dependencies[k] << other_resources_obj.name
  end
end

#subscribes(other_resources_hash, action_to_take = :reload, at_time = :delayed) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/poolparty/resource.rb', line 53

def subscribes(other_resources_hash, action_to_take=:reload, at_time=:delayed)
  @meta_subscribes ||= {}
  other_resources_hash.each do |k,v|
    subscribes_array = (@meta_subscribes[k] ||= [])
    subscribes_array << [v, action_to_take, at_time] unless subscribes_array.include?([v, action_to_take, at_time])
  end
end