Class: PoolParty::Pool

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

Class Attribute Summary collapse

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, #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(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 Attribute Details

.commandObject

Returns the value of attribute command.



30
31
32
# File 'lib/poolparty/pool.rb', line 30

def command
  @command
end

Class Method Details

.after_file_load(filepath) ⇒ Object

CALLBACKS After the entire cloud is loaded



184
185
# File 'lib/poolparty/pool.rb', line 184

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


158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/poolparty/pool.rb', line 158

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



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

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



96
97
98
99
100
101
102
# File 'lib/poolparty/pool.rb', line 96

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


143
144
145
146
147
148
149
150
151
# File 'lib/poolparty/pool.rb', line 143

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


118
119
120
121
122
123
124
125
126
# File 'lib/poolparty/pool.rb', line 118

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



129
130
131
132
133
134
135
# File 'lib/poolparty/pool.rb', line 129

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



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

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)


85
86
87
88
89
90
91
92
93
# File 'lib/poolparty/pool.rb', line 85

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



37
38
39
40
41
42
43
# File 'lib/poolparty/pool.rb', line 37

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

#commandObject



31
32
33
# File 'lib/poolparty/pool.rb', line 31

def command
  self.class.command
end

#form_cloudsObject

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



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

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


51
52
53
54
55
56
# File 'lib/poolparty/pool.rb', line 51

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