Class: PoolParty::Pool

Inherits:
DslBase show all
Defined in:
lib/poolparty/pool.rb

Instance Attribute Summary

Attributes inherited from Base

#base_name, #init_opts

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DslBase

#instances

Methods inherited from Base

#add_ordered_resources_to_result, #after_loaded, #all_resources, #before_load, #clouds_dot_rb_dir, #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(n, &block) ⇒ Pool

Freeze the pool_name so we can’t modify it at all call and run instance_eval on the block and then call the after_create callback



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/poolparty/pool.rb', line 12

def initialize(n, &block)
  PoolParty::Pool.init
  
  context_stack.clear
  
  @pool_name = n.to_s
  @pool_name.freeze
  
  super do
    instance_eval &block
    form_clouds
    clouds.each do |name, cld|
      cld.after_all_loaded
    end
    
  end
end

Dynamic Method Handling

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

Class Method Details

.after_file_load(filepath) ⇒ Object

CALLBACKS After the entire cloud is loaded



179
180
# File 'lib/poolparty/pool.rb', line 179

def self.after_file_load(filepath)
end

.before_file_load(filepath) ⇒ Object

Before the specfile is loaded this method is called It…

+ loads the plugin paths local to the clouds_dot_rb_file into the load_path
+ calls the resource define_resource_methods to define the resource methods
+ sets up the log


153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/poolparty/pool.rb', line 153

def self.before_file_load(filepath)
  $:.unshift(File.dirname(filepath))
  $:.unshift("#{File.dirname(filepath)}/lib")
  $:.unshift("#{File.dirname(filepath)}/plugins")
  
  Dir["#{File.dirname(filepath)}/lib/*.rb"].each {|lib_path| require lib_path }
  Dir["#{File.dirname(filepath)}/plugins/*"].each do |plugin_path|
    if File.directory?(plugin_path)
      $:.unshift(plugin_path)
    else
      require plugin_path if File.file?(plugin_path) && plugin_path.match(/.rb$/)
    end
  end
end

.clouds_dot_rb_dir(n = nil) ⇒ Object



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

def self.clouds_dot_rb_dir(n=nil)
  self.clouds_dot_rb_file ? File.dirname(self.clouds_dot_rb_file) : "./"
end

.clouds_dot_rb_file(n = nil) ⇒ Object

Store the clouds_dot_rb_file location



91
92
93
94
95
96
97
# File 'lib/poolparty/pool.rb', line 91

def self.clouds_dot_rb_file(n=nil)
  if n
    @clouds_dot_rb_file = n
  else
    @clouds_dot_rb_file
  end
end

.default_clouds_dot_rb_locationsObject

Default clouds_dot_rb_file locations

+ CWD/clouds.rb
+ ENV["CLOUDS_DOT_RB"]
+ ENV["HOME"]/clouds.rb
+ /etc/poolparty/clouds.rb
+ /var/poolparty/clouds.rb


138
139
140
141
142
143
144
145
146
# File 'lib/poolparty/pool.rb', line 138

def self.default_clouds_dot_rb_locations
  @default_clouds_dot_rb_locations ||= [
    Dir.pwd,
    ENV["CLOUDS_DOT_RB"],
    PoolParty::Default.poolparty_home_path,
    PoolParty::Default.base_config_directory,
    PoolParty::Default.remote_storage_path
  ].flatten.reject {|a| a.nil?}
end

.find_and_load_default_clouds_dot_rb(filename = "clouds.rb") ⇒ Object

Load the default clouds.rb file If a full filepath is given, then load the given path if it is given, but not found or is not given entirely, then search the following locations, in preferential order for the clouds_dot_rb_file and load the first one found

+ CWD/clouds.rb
+ ENV["CLOUDS_DOT_RB"]
+ ENV["HOME"]/clouds.rb
+ /etc/poolparty/clouds.rb
+ /var/poolparty/clouds.rb


113
114
115
116
117
118
119
120
121
# File 'lib/poolparty/pool.rb', line 113

def self.find_and_load_default_clouds_dot_rb(filename="clouds.rb")
  f = if File.file?(filename) 
      filename
    else
      find_default_clouds_dot_rb(filename)
    end
  ENV["CLOUDS_DOT_RB"] = f
  load_from_file(f)
end

.find_default_clouds_dot_rb(filename) ⇒ Object

Look for the default clouds_dot_rb_file



124
125
126
127
128
129
130
# File 'lib/poolparty/pool.rb', line 124

def self.find_default_clouds_dot_rb(filename)      
  path = default_clouds_dot_rb_locations.detect do |dir|
    File.file?(File.expand_path(dir / filename))
  end
  raise PoolPartyError.create("CloudsConfigFile", "Cannot find your config file") unless path && filename
  File.expand_path(File.join(path, filename))
end

.initObject

Call init to the resource methods and init the log It pulls the requires for chef TODO: Pull require_chef_only_resources out



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

def self.init
  DependencyResolvers::Chef.require_chef_only_resources
  PoolParty::Resource.define_resource_methods
  PoolParty::PoolPartyLog.init
end

.load_from_file(filename = nil) ⇒ Object

Load a clouds.rb Call the prerequisites (before_file_load) and then instance_eval the contents finally, call after_file_load callback after the clouds.rb is loaded Arguments:

+ file on the filesystem
+ open-uri url (http)


80
81
82
83
84
85
86
87
88
# File 'lib/poolparty/pool.rb', line 80

def self.load_from_file(filename=nil)
  raise PoolPartyError.create("CloudsDotRbLoadError", "Cannot load the specified clouds.rb: #{filename}. Check to make sure it exists") unless filename && File.file?(filename)
  ddputs "Loading #{filename} from file in Pool"
  @clouds_dot_rb_file = filename
  before_file_load(clouds_dot_rb_file)
  o = instance_eval open(clouds_dot_rb_file).read, clouds_dot_rb_file
  after_file_load(clouds_dot_rb_file)
  o
end

Instance Method Details

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

cloud Define a cloud by a name and a block



32
33
34
35
36
37
38
# File 'lib/poolparty/pool.rb', line 32

def cloud(name, o={}, &block)
  if block
    clouds[name.to_s] ||= PoolParty::Cloud.new(name, soakable_options.merge(o), &block)
  else
    raise PoolPartyError.new("CloudError", "You must pass a block when defining a cloud")
  end
end

#form_cloudsObject

Run twice to catch the errors on the first run TODO: CHANGE ME!



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

def form_clouds
  failed_clouds = []
  clouds.each do |name, cld|
    begin
      clouds[name].form_clouds
    rescue Exception => e
      failed_clouds << [name, cld]
      next
    end
  end
  failed_clouds.each do |name, cld|
    clouds[name].form_clouds
  end
end

#soakable_optionsObject

Soaked options These are options that can be set on the pool that will be grabbed from the pool that can be set to the cloud. Soakable options:

minimum_instances
maximum_instances


46
47
48
49
50
51
# File 'lib/poolparty/pool.rb', line 46

def soakable_options
  soaked_options = {}
  soaked_options.merge!(:minimum_instances => minimum_instances) if minimum_instances
  soaked_options.merge!(:maximum_instances => maximum_instances) if maximum_instances
  soaked_options
end