Class: CORL::Provisioner::Puppetnode

Inherits:
Object
  • Object
show all
Defined in:
lib/CORL/provisioner/puppetnode.rb

Constant Summary collapse

@@puppet_lock =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#add_search_path(type, resource_name) ⇒ Object




198
199
200
# File 'lib/CORL/provisioner/puppetnode.rb', line 198

def add_search_path(type, resource_name)
  Config.set_options([ :all, type ], { :search => [ resource_name.to_s ] })  
end

#build(options = {}) ⇒ Object


Provisioner interface operations



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/CORL/provisioner/puppetnode.rb', line 119

def build(options = {})
  super do |locations, package_info|
    success = true
    
    # Build modules
    locations[:module] = {}
    
    init_profile = lambda do |package_name, profile_name, profile_info|
      package_id      = id(package_name)
      base_directory  = File.join(locations[:build], 'modules', package_id.to_s, profile_name.to_s)
      profile_success = true
      
      ui.info("Building CORL profile #{blue(profile_name)} modules into #{green(base_directory)}")
      
      if profile_info.has_key?(:modules)
        profile_info[:modules].each do |module_name, module_reference|
          module_directory = File.join(base_directory, module_name.to_s)
          
          ui.info("Building Puppet module #{blue(module_name)} at #{purple(module_reference)} into #{green(module_directory)}")
          
          full_module_directory = File.join(build_directory, module_directory)
              
          module_project = CORL.project(extended_config(:puppet_module, {
            :directory => full_module_directory,
            :url       => module_reference,
            :create    => File.directory?(full_module_directory) ? false : true,
            :pull      => true
          }))
          unless module_project
            ui.warn("Puppet module #{cyan(module_name)} failed to initialize")
            profile_success = false
            break
          end             
        end
        locations[:module][profile_id(package_name, profile_name)] = base_directory if profile_success
        profile_success
      end
    end     
    
    hash(package_info.get([ :provisioners, plugin_provider ])).each do |package_name, info|
      if info.has_key?(:profiles)
        info[:profiles].each do |profile_name, profile_info|
          unless init_profile.call(package_name, profile_name, profile_info)
            success = false
            break
          end
        end
      end
    end
    profiles.each do |profile_name, profile_info|
      unless init_profile.call(plugin_name, profile_name, profile_info)
        success = false
        break  
      end
    end      
    success
  end     
end

#compilerObject


Property accessor / modifiers



51
52
53
# File 'lib/CORL/provisioner/puppetnode.rb', line 51

def compiler
  @compiler
end

#concatenate(components, capitalize = false) ⇒ Object




307
308
309
# File 'lib/CORL/provisioner/puppetnode.rb', line 307

def concatenate(components, capitalize = false)
  super(components, capitalize, '::')
end

#import(files, options = {}) ⇒ Object



189
190
191
192
193
194
# File 'lib/CORL/provisioner/puppetnode.rb', line 189

def import(files, options = {})
  Util::Puppet.import(files, Config.ensure(options).defaults({ 
    :puppet_scope       => scope, 
    :puppet_import_base => network.directory 
  }))
end

#lookup(property, default = nil, options = {}) ⇒ Object




180
181
182
183
184
185
# File 'lib/CORL/provisioner/puppetnode.rb', line 180

def lookup(property, default = nil, options = {})
  Util::Puppet.lookup(property, default, Config.ensure(options).defaults({ 
    :provisioner  => :puppetnode,
    :puppet_scope => scope      
  }))
end

#normalize(reload) ⇒ Object


Provisioner plugin interface



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/CORL/provisioner/puppetnode.rb', line 11

def normalize(reload)
  super do
    if CORL.log_level == :debug
      Puppet.debug = true
    end
    unless reload
      Puppet::Util::Log.newdesttype id do
        def handle(msg)
          levels = {
            :emerg   => { :name => 'emergency', :send => :error },
            :alert   => { :name => 'alert', :send => :error },
            :crit    => { :name => 'critical', :send => :error },
            :err     => { :name => 'error', :send => :error },
            :warning => { :name => 'warning', :send => :warn },
            :notice  => { :name => 'notice', :send => :success },
            :info    => { :name => 'info', :send => :info },
            :debug   => { :name => 'debug', :send => :info }
          }
          str   = msg.respond_to?(:multiline) ? msg.multiline : msg.to_s
          str   = msg.source == "Puppet" ? str : "#{CORL.blue(msg.source)}: #{str}"
          level = levels[msg.level]
      
          CORL.ui_group("puppetnode::#{name}(#{CORL.yellow(level[:name])})", :cyan) do |ui|        
            ui.send(level[:send], str)
          end
        end
      end
    end
  end  
end

#profile_id(package_name, profile_name) ⇒ Object


Utilities



301
302
303
# File 'lib/CORL/provisioner/puppetnode.rb', line 301

def profile_id(package_name, profile_name)
  concatenate([ package_name, 'profile', profile_name ], false)
end

#provision(profiles, options = {}) ⇒ Object




204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/CORL/provisioner/puppetnode.rb', line 204

def provision(profiles, options = {})
  super do |config|
    locations = build_locations
    success   = true
  
    include_location = lambda do |type, parameters = {}, add_search_path = false|
      classes = {}
          
      locations[:package].each do |name, package_directory|
        type_gateway  = File.join(build_directory, package_directory, "#{type}.pp")
        resource_name = concatenate([ name, type ])
      
        add_search_path(type, resource_name) if add_search_path
      
        if File.exists?(type_gateway)
          import(type_gateway)
          classes[resource_name] = parameters                 
        end
      
        type_directory = File.join(build_directory, package_directory, type.to_s)
        Dir.glob(File.join(type_directory, '*.pp')).each do |file|
          resource_name = concatenate([ name, type, File.basename(file).gsub('.pp', '') ])
          import(file)
          classes[resource_name] = parameters
        end        
      end
  
      type_gateway  = File.join(directory, "#{type}.pp")
      resource_name = concatenate([ plugin_name, type ])
    
      add_search_path(type, resource_name) if add_search_path
   
      if File.exists?(type_gateway)
        import(type_gateway)
        classes[resource_name] = parameters                
      end
  
      type_directory = File.join(directory, type.to_s)
      
      if File.directory?(type_directory)
        Dir.glob(File.join(type_directory, '*.pp')).each do |file|
          resource_name = concatenate([ plugin_name, type, File.basename(file).gsub('.pp', '') ])
          import(file)
          classes[resource_name] = parameters
        end
      end
      classes
    end
  
    @@puppet_lock.synchronize do
      begin          
        ui.info("Starting catalog generation")
        
        start_time = Time.now        
        node       = init_puppet(profiles)
      
        # Include defaults
        classes = include_location.call(:default, {}, true)
    
        # Import needed profiles
        include_location.call(:profiles, {}, false)
    
        profiles.each do |profile|
          classes[profile.to_s] = { :require => 'Anchor[profile_start]' }
        end
        
        # Compile catalog
        node.classes = classes
        compiler.compile
      
        # Start system configuration
        catalog = compiler.catalog.to_ral
        catalog.finalize
        catalog.retrieval_duration = Time.now - start_time
        
        unless config.get(:dry_run, false)
          ui.info("\n", { :prefix => false })
          ui.info("Starting configuration run")
                      
          configurer = Puppet::Configurer.new
          if ! configurer.run(:catalog => catalog, :pluginsync => false)
            success = false
          end
        end
      
      rescue Exception => error
        raise error
        Puppet.log_exception(error)
      end
    end    
    success
  end
end

#register(options = {}) ⇒ Object




44
45
46
# File 'lib/CORL/provisioner/puppetnode.rb', line 44

def register(options = {})
  Util::Puppet.register_plugins(Config.ensure(options).defaults({ :puppet_scope => scope }))
end

#scopeObject




57
58
59
60
# File 'lib/CORL/provisioner/puppetnode.rb', line 57

def scope
  return compiler.topscope if compiler
  nil
end