Class: PoolParty::Resources::Resource

Inherits:
Object
  • Object
show all
Extended by:
PoolParty::Resources
Includes:
CloudResourcer, Configurable, PoolParty::Resources
Defined in:
lib/poolparty/pool/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PoolParty::Resources

add_has_and_does_not_have_methods_for, add_resource, call_function, classpackage_with_self, custom_file, gem, get_resource, reset_resources!, resource, resources, resources_string, resources_string_from_resources

Methods included from Configurable

included

Methods included from CloudResourcer

#full_keypair_path, #instances, #keypair_path, #keypair_paths, #new_keypair_path, #number_of_resources, #parent, #possible_keypair_basenames, #set_parent

Constructor Details

#initialize(opts = {}, parent = self, &block) ⇒ Resource

This is set in order of descending precedence The options are overwritten from the bottom up and the resource will use those as the values Then it takes the value of the block and sets whatever is sent there as the options Finally, it uses the parent's options as the lowest priority



84
85
86
87
88
89
90
# File 'lib/poolparty/pool/resource.rb', line 84

def initialize(opts={}, parent=self, &block)
  # Take the options of the parents        
  set_parent(parent) if parent
  set_vars_from_options(opts) unless opts.empty?
  self.instance_eval &block if block
  loaded
end

Class Method Details

.available_resource_methods ⇒ Object



74
75
76
# File 'lib/poolparty/pool/resource.rb', line 74

def self.available_resource_methods
  available_resources.map {|a| a.my_methods }
end

.available_resources ⇒ Object



70
71
72
# File 'lib/poolparty/pool/resource.rb', line 70

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

.custom_function(str) ⇒ Object



141
142
143
# File 'lib/poolparty/pool/resource.rb', line 141

def self.custom_function(str)
  custom_functions << str
end

.custom_functions ⇒ Object



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

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

.custom_functions_to_string(prev = "") ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/poolparty/pool/resource.rb', line 155

def self.custom_functions_to_string(prev="")
  returning Array.new do |output|
    PoolParty::Resources.available_custom_resources.each do |resource|
      resource.custom_functions.each do |func|
        output << "#{prev*2}#{func}"
      end
    end
  end.join("\n")
end

.inherited(subclass) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/poolparty/pool/resource.rb', line 52

def self.inherited(subclass)
  subclass = subclass.to_s.split("::")[-1] if subclass.to_s.index("::")
  lowercase_class_name = subclass.to_s.downcase
  
  # Add add resource method to the Resources module
  unless PoolParty::Resources.respond_to?(lowercase_class_name.to_sym)
    method =<<-EOE
      def #{lowercase_class_name}(opts={}, &blk)
        add_resource(:#{lowercase_class_name}, opts, &blk)
      end            
    EOE
    PoolParty::Resources.module_eval method
    PoolParty::Resources.add_has_and_does_not_have_methods_for(lowercase_class_name.to_sym)
    
    available_resources << subclass
  end
end

Instance Method Details

#absent ⇒ Object



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

def absent
  "absent"
end

#class_type_name ⇒ Object

This way we can subclass resources without worry



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

def class_type_name
  self.class.to_s.top_level_class
end

#custom_function(str) ⇒ Object



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

def custom_function(str)
  self.class.custom_functions << str
end

#disallowed_options ⇒ Object

Some things in puppet aren't allowed, so let's override them here



165
166
167
# File 'lib/poolparty/pool/resource.rb', line 165

def disallowed_options
  []
end

#ensures(str = "running") ⇒ Object



102
103
104
# File 'lib/poolparty/pool/resource.rb', line 102

def ensures(str="running")
  str == "absent" ? is_absent : is_present
end

#get_modified_options ⇒ Object

We want to gather the options, but if the option sent is nil then we want to override the option value by sending the key as a method so that we can override this if necessary. Only runs on objects that have options defined, otherwise it returns an empty hash



179
180
181
182
183
184
185
186
187
188
# File 'lib/poolparty/pool/resource.rb', line 179

def get_modified_options
  if options
    opts = options.inject({}) do |sum,h| 
      sum.merge!({h[0].to_sym => ((h[1].nil?) ? self.send(h[0].to_sym) : h[1]) })
    end
  else
    opts = {}
  end
  opts.reject {|k,v| disallowed_options.include?(k) }
end

#ifnot(str = "") ⇒ Object

Alias for unless



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

def ifnot(str="")
  options.merge!(:unless => str)
end

#is_absent(*args) ⇒ Object

Ensures that what we are sending is absent



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

def is_absent(*args)
  options.merge!(:ensure => absent)
end

#is_present(*args) ⇒ Object

Allows us to send an ensure to ensure the presence of a resource



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

def is_present(*args)
  options.merge!(:ensure => present)
end

#key ⇒ Object



168
169
170
# File 'lib/poolparty/pool/resource.rb', line 168

def key
  name
end

#loaded ⇒ Object

Stub, so you can create virtual resources



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

def loaded
end

#present ⇒ Object



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

def present
  "present"
end

#requires(str = "") ⇒ Object

DSL Overriders Overrides for syntax Allows us to send require to require a resource



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

def requires(str="")
  options.merge!(:require => str)
end

#template(file, opts = {}) ⇒ Object

Give us a template to work with on the resource Make sure this template is moved to the tmp directory as well

Raises:



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/poolparty/pool/resource.rb', line 126

def template(file, opts={})
  raise TemplateNotFound.new("no template given") unless file
  raise TemplateNotFound.new("template cannot be found #{file}") unless ::File.file?(file)
  unless opts[:just_copy]          
    options.merge!({:content => "template(\"#{::File.basename(file)}\")"})
    options.delete(:source) if options.has_key?(:source)
    copy_template_to_storage_directory(file)
  else
    copy_file_to_storage_directory(file)
  end
end

#to_string(prev = "") ⇒ Object

Generic to_s Most Resources won't need to extend this



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/poolparty/pool/resource.rb', line 192

def to_string(prev="")
  opts = get_modified_options
  returning Array.new do |output|
    
    if resources && !resources.empty?
      @cp = classpackage_with_self(self)
      output << @cp.to_string
      output << "include #{@cp.name.sanitize}"
    end
    
    unless virtual_resource?
      output << "#{prev}#{class_type_name} {"
      output << "#{prev}\"#{self.key}\":"
      output << opts.flush_out("#{prev*2}").join(",\n")
      output << "#{prev}}"            
    end
              
  end.join("\n")
end

#virtual_resource? ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/poolparty/pool/resource.rb', line 171

def virtual_resource?
  false
end