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, #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, #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
# File 'lib/poolparty/resource.rb', line 15

def initialize(opts={}, extra_opts={}, exists=true, &block)
  @exists ||= exists
  super(opts, extra_opts, &block)
  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



92
93
94
# File 'lib/poolparty/resource.rb', line 92

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) ⇒ Object

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/poolparty/resource.rb', line 176

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

.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



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/poolparty/resource.rb', line 162

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

.definedObject



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

def self.defined
  @defined ||= false
end

.defined_resourcesObject

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



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

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


131
132
133
# File 'lib/poolparty/resource.rb', line 131

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



200
201
202
# File 'lib/poolparty/resource.rb', line 200

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

.method_defined!Object

Note that this resource has been defined already



205
206
207
# File 'lib/poolparty/resource.rb', line 205

def self.method_defined!
  @defined = true
end

.method_defined?Boolean

Query if this resource has been defined yet

Returns:

  • (Boolean)


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

def self.method_defined?
  defined
end

Instance Method Details

#after_compileObject



114
115
# File 'lib/poolparty/resource.rb', line 114

def after_compile
end

#after_loadedObject



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

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

#before_compileObject

CALLBACKS



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

def before_compile
end

#case_of(var, &block) ⇒ Object



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

def case_of(var, &block)
end

#cloudObject

DSL METHODS Get access to the cloud that contains this resource



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

def cloud
  get_parent_of_class(PoolParty::Cloud)
end

#compile(compiler) ⇒ Object

Dependency resolver methods



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

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



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

def does_not_exist!
  @exists = false
  false
end

#exists!Object

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



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

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



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

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



79
80
81
# File 'lib/poolparty/resource.rb', line 79

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) ⇒ Object

META FUNCTIONS ALL RESOURCES HAVE THESE METHODS AVAILABLE



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

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

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

Run only if



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

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



147
148
149
# File 'lib/poolparty/resource.rb', line 147

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.



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

def print_to_chef
  "# <%= has_method_name %>\n  EOE\nend\n"

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


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/poolparty/resource.rb', line 245

def print_variable(obj)
  case obj
  when Fixnum
    case obj
    when /^\d{3}$/
      "0#{obj.to_i}"
    else
      "#{obj.to_i}"
    end        
  when String
    case obj
    when /^\d{4}$/
      "#{obj}"
    when /^\d{3}$/
      "0#{obj}"
    else
      "\"#{obj}\""
    end
  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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/poolparty/resource.rb', line 61

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
  end
end

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



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

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