Class: PoolParty::PoolPartyBaseClass

Inherits:
Object
  • Object
show all
Includes:
Dslify, Parenting, DependencyResolverCloudExtensions
Defined in:
lib/poolparty/poolparty/poolparty_base_class.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DependencyResolverCloudExtensions

#to_properties_hash

Constructor Details

#initialize(opts = {}, extra_opts = {}, &block) ⇒ PoolPartyBaseClass

attr_accessor :depth



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 15

def initialize(opts={}, extra_opts={}, &block)
  add_to_parent_if_parent_exists_and_is_a_service
  
  @init_block = block
  @base_name = get_name_from_options_and_extra_options(opts, extra_opts)
  
  o = (opts.is_a?(Hash) ? extra_opts.merge(opts) : extra_opts).merge(:name => @base_name)
  
  run_in_context(o, &block)
  super(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 142

def method_missing(m,*a,&block)
  if this_context && this_context != self && this_context.respond_to?(m)# && !self.is_a?(PoolParty::Resources::Resource)
    this_context.send m, *a, &block      
  else
    # if dsl_options.has_key?(m)
    #          dsl_options[m]
    #        elsif parent && parent.respond_to?(:dsl_options) && parent.dsl_options.has_key?(m)
    #          parent.dsl_options[m]
    #        elsif self.class.default_options.has_key?(m)
    #          self.class.default_options[m]
    #        elsif parent && parent.respond_to?(:dsl_options) && parent.default_options.has_key?(m)
    #          parent.default_options[m]
    #        else
      super
    # end
  end
end

Class Method Details

.add_has_and_does_not_have_methods_for(typ = :file) ⇒ Object

Adds two methods to the module Adds the method type:

has_

and

does_not_have_

for the type passed for instance add_has_and_does_not_have_methods_for(:file) gives you the methods has_file and does_not_have_file TODO: Refactor nicely to include other types that don’t accept ensure



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 170

def self.add_has_and_does_not_have_methods_for(typ=:file)
  method_name = "__#{typ}"
  PoolParty::PoolPartyBaseClass.module_eval <<-EOE
    def has_#{typ}(opts={}, extra={}, &block)
      #{method_name}({:ensures => :present}.merge(handle_option_values(opts).merge(extra)), &block)
    end
    def does_not_have_#{typ}(opts={}, extra={}, &block)
      #{method_name}({:ensures => :absent}.merge(handle_option_values(opts).merge(extra)), &block)
    end
  EOE
end

Instance Method Details

#add_resource(ty, opts = {}, extra_opts = {}, &block) ⇒ Object

Add resource When we are looking to add a resource, we want to make sure the resources isn’t already added. This way we prevent duplicates as puppet can be finicky about duplicate resource definitions. We’ll look for the resource in either a local or global store If the resource appears in either, return that resource, we’ll just append to the resource config, otherwise instantiate a new resource of the type and store it into the global and local resource stores

A word about stores, the global store stores the entire list of stored resources. The local resource store is available on all clouds and plugins which stores the instance variable’s local resources.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 81

def add_resource(ty, opts={}, extra_opts={}, &block)    
  temp_name = get_name_from_options_and_extra_options(opts, extra_opts)
  
  if res = get_resource(ty, temp_name, opts)        
    res
  else
    opts = (opts.is_a?(Hash) ? extra_opts.merge(opts) : extra_opts).merge(:name => temp_name)
    
    # opts.merge!(:name => temp_name) unless opts.has_key?(:name)
    res = if PoolParty::Resources::Resource.available_resources.include?(ty.to_s.camelize)
      "PoolParty::Resources::#{ty.to_s.camelize}".camelize.constantize.new(opts, &block)
    else
      "#{ty.to_s.camelize}".camelize.constantize.new(opts.merge(:name), &block)
    end
    res.after_create
    store_in_local_resources(ty, res)
    ordered_resources << res
    res
  end
end

#add_service(serv, extra_name = "") ⇒ Object

Add to the services pool for the manifest listing



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

def add_service(serv, extra_name="")
  subclass = "#{serv.class.to_s.top_level_class.underscore.downcase}#{extra_name}"
  lowercase_class_name = subclass.to_s.underscore.downcase || subclass.downcase
  
  (services[lowercase_class_name.to_sym] ||= []) << serv if serv && !serv.empty?
  # services.merge!({lowercase_class_name.to_sym => serv})
end

#add_to_parent_if_parent_exists_and_is_a_serviceObject

Add the parent’s options to my own and add myself as a service if I am not a resource



40
41
42
43
44
45
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 40

def add_to_parent_if_parent_exists_and_is_a_service      
  if parent && !parent.is_a?(PoolParty::Resources::Resource)
    dsl_options.merge!(parent.dsl_options) if parent.is_a?(PoolParty::Pool::Pool)
    parent.add_service(self) if parent.respond_to?(:add_service) && !is_a_resource?
  end
end

#get_local_resource(ty, k) ⇒ Object



107
108
109
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 107

def get_local_resource(ty, k)
  resource(ty).select {|r| r.name == k }.first
end

#get_name_from_options_and_extra_options(opts = {}, extra_opts = {}) ⇒ Object

Try to extract the name from the options



48
49
50
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 48

def get_name_from_options_and_extra_options(opts={}, extra_opts={})
  opts.is_a?(Hash) ? (opts.has_key?(:name) ? opts.delete(:name) : nil) : dsl_options[:name] = opts
end

#get_resource(ty, n, opts = {}, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 111

def get_resource(ty, n, opts={}, &block)
  if in_local_resources?(ty, n)
    get_local_resource(ty, n)
  elsif parent && parent != self
    parent.get_resource(ty, n)
  else
    nil
  end
end

#handle_option_values(o) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 182

def handle_option_values(o)
  case o.class.to_s
  when "String"
    {:name => o}
  else
    o
  end
end

#in_local_resources?(ty, k) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 104

def in_local_resources?(ty, k)
  !resource(ty).select {|r| r.name == k }.empty? rescue false
end

#is_a_resource?Boolean

Returns:

  • (Boolean)


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

def is_a_resource?
  false
end

#is_plugin?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 134

def is_plugin?
  false
end

#ordered_resourcesObject



130
131
132
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 130

def ordered_resources
  @ordered_resources ||= []
end

#plugin_storeObject

Store the call and use of plugins into an array



122
123
124
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 122

def plugin_store
  @plugin_store ||= []
end

#resource(type = :file) ⇒ Object



126
127
128
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 126

def resource(type=:file)
  resources[type.to_sym] ||= []
end

#resourcesObject



65
66
67
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 65

def resources
  @resources ||= OrderedHash.new
end

#run_in_context(o = {}, &block) ⇒ Object

Overloading the parent run_in_context



28
29
30
31
32
33
34
35
36
37
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 28

def run_in_context(o={}, &block)
  if o
    context_stack.push self
    set_vars_from_options(o)
    instance_eval &block if block
    context_stack.pop
  else
    super
  end
end

#servicesObject

Container for the services



61
62
63
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 61

def services
  @services ||= OrderedHash.new
end

#store_in_local_resources(ty, obj) ⇒ Object



101
102
103
# File 'lib/poolparty/poolparty/poolparty_base_class.rb', line 101

def store_in_local_resources(ty, obj)
  resource(ty) << obj
end