Class: Conf

Inherits:
Object
  • Object
show all
Defined in:
lib/rake/config.rb

Constant Summary collapse

@@data =
Construct.new()
@@encryptedData =
Construct.new()
@@passPhrase =
nil
@@configFileNames =
Array.new()
@@recipePaths =
Array.new()
@@globalCookbookDirectories =
Array.new()

Class Method Summary collapse

Class Method Details

.add_config_file_name(configFileName) ⇒ Object



333
334
335
336
337
# File 'lib/rake/config.rb', line 333

def self.add_config_file_name(configFileName)
  configFileName.sub!(/\.[^\.]*$/,'');
  Rake::Application.mesg "Adding config name: [#{configFileName}]";
  @@configFileNames.push(configFileName);
end

.add_cookbook(aCookbookPath) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/rake/config.rb', line 413

def self.add_cookbook(aCookbookPath)
  Rake::Application.mesg "Loading cookbook: [#{aCookbookPath}]";
  $LOAD_PATH.unshift(aCookbookPath+'/lib');
  Conf.add_recipes_path(aCookbookPath+'/');
  Conf.add_recipes_path(aCookbookPath+'/recipes/');
#    Dir.chdir(aCookbookPath) do
    #
    # load the cookbook configuration file first incase they are used 
    # in the rakefile
    confFile = aCookbookPath + '/' + 'cookbook.conf';
    Conf.load_file(confFile) if File.exists?(confFile);
    encConfFile = aCookbookPath + '/' + 'cookbook.enc';
    Conf.load_encrypted_file(encConfFile) if File.exists?(encConfFile);

    # now load the corresponding cookbook rake file
    rakeFile = aCookbookPath + '/' + 'cookbook.rake';
    Rake.load_rakefile(rakeFile) if File.exists?(rakeFile);
#    end
end

.add_global_cookbook_directory(globalCookbookDirectory) ⇒ Object



392
393
394
395
# File 'lib/rake/config.rb', line 392

def self.add_global_cookbook_directory(globalCookbookDirectory)
  Rake::Application.mesg "Adding global cookbook: [#{globalCookbookDirectory}]";
  @@globalCookbookDirectories.push(globalCookbookDirectory)
end

.add_global_cookbooksObject



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/rake/config.rb', line 397

def self.add_global_cookbooks()
  if @@globalCookbookDirectories.empty?() then
    if ENV.has_key?('HOME') then
      if not ENV['HOME'].empty?() then
        @@globalCookbookDirectories.push(ENV['HOME']+'/.cookbook');
      end
    end
  end
  @@globalCookbookDirectories.each() do | aGlobalCookbookDir |
    if Dir.exists?(aGlobalCookbookDir) then
      Rake::Application.mesg "Adding global cookbook #{aGlobalCookbookDir}";
      Conf.add_cookbook(aGlobalCookbookDir);
    end
  end
end

.add_recipes_path(aRecipesPath) ⇒ Object



361
362
363
364
365
366
367
368
369
370
# File 'lib/rake/config.rb', line 361

def self.add_recipes_path(aRecipesPath)
  #
  # Make sure the recipeDir is in the load path
  #
  $LOAD_PATH.unshift(aRecipesPath);
  #
  # Add this path to the collection of recipe paths to search
  #
  @@recipePaths.push(aRecipesPath);
end

.dataObject



253
254
255
256
257
# File 'lib/rake/config.rb', line 253

def self.data
  Thread.current[:confStack] = ConfStack.new(self, :Conf);
  Thread.current[:confStack].calling(@@data, :data, :data);
  return @@data;
end

.each_resource(aPartialResourcePath, &aBlock) ⇒ Object



384
385
386
387
388
389
390
# File 'lib/rake/config.rb', line 384

def self.each_resource(aPartialResourcePath, &aBlock)
  @@recipePaths.each do | aRecipePath |
    aResourcePath = aRecipePath+aPartialResourcePath;
    next unless File.exists?(aResourcePath);
    aBlock.call(aResourcePath);
  end
end

.encryptedDataObject



259
260
261
262
263
# File 'lib/rake/config.rb', line 259

def self.encryptedData
  Thread.current[:confStack] = ConfStack.new(self, :Conf);
  Thread.current[:confStack].calling(@@encryptedData, :encryptedData, :encryptedData);
  return @@encryptedData;
end

.find_resource(aPartialResourcePath) ⇒ Object



376
377
378
379
380
381
382
# File 'lib/rake/config.rb', line 376

def self.find_resource(aPartialResourcePath)
  @@recipePaths.each do | aRecipePath |
    aResourcePath = aRecipePath+aPartialResourcePath;
    return aResourcePath if File.exists?(aResourcePath);
  end
  raise ResourceNotFoundError, "Resource file #{aPartialResourcePath} could not be found";
end

.get_pass_phrase(check = false) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rake/config.rb', line 236

def self.get_pass_phrase(check = false)
  if @@passPhrase.nil? then
    require 'highline';
    @@passPhrase = HighLine.new.ask("Pass phrase for encrypted configuration:") { | q | q.echo='*'; }
    while check do
      secondPassPhrase = HighLine.new.ask("Pass phrase (again):") { | q | q.echo='*'; }
      if @@passPhrase != secondPassPhrase then
        puts "The pass phrases do not match... please try again\n\n";
        @@passPhrase = HighLine.new.ask("Pass phrase for encrypted configuration:") { | q | q.echo='*'; }
      else
        check = false;
      end
    end
  end
  @@passPhrase;
end

.get_recipe_pathsObject



372
373
374
# File 'lib/rake/config.rb', line 372

def self.get_recipe_paths()
  @@recipePaths;
end

.has_encrypted_key?(aKey) ⇒ Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/rake/config.rb', line 269

def self.has_encrypted_key?(aKey)
  return @@encryptedData.has_key?(aKey);
end

.has_key?(aKey) ⇒ Boolean

Returns:

  • (Boolean)


265
266
267
# File 'lib/rake/config.rb', line 265

def self.has_key?(aKey)
  return @@data.has_key?(aKey);
end

.load(yaml) ⇒ Object



273
274
275
276
277
278
# File 'lib/rake/config.rb', line 273

def self.load(yaml)
  loadedHash = YAML::load(yaml);
  if loadedHash then
    @@data.merge(loadedHash);
  end
end

.load_central_config_filesObject



482
483
484
485
486
# File 'lib/rake/config.rb', line 482

def self.load_central_config_files()
  @@recipePaths.reverse.each do | aRecipePath |
    load_config_files(aRecipePath.sub(/\/$/,''));
  end
end

.load_config_files(recipeDir) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
# File 'lib/rake/config.rb', line 339

def self.load_config_files(recipeDir)
  @@configFileNames.each() do  | aConfigFileName |
    #
    # load the yaml configuration file
    #
    confFile = recipeDir + '/' + aConfigFileName + '.conf';
    self.load_file(confFile) if File.exists?(confFile);
    encConfFile = recipeDir + '/' + aConfigFileName + '.enc';
    self.load_encrypted_file(encConfFile) if File.exists?(encConfFile);
  end
end

.load_encrypted_file(yamlFileName) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rake/config.rb', line 288

def self.load_encrypted_file(yamlFileName)
  Rake::Application.mesg "loading encrypted configuration file [#{yamlFileName}]\n  in #{Dir.getwd}" if Rake.application.options.trace;
  yamlPlainText = "";
  begin 
    yamlPlainText = gpgDecryptFile2Data(yamlFileName);
  rescue
    yamlPlainText = openSslDecryptFile2Data(yamlFileName);
  end
  loadedHash = YAML::load(yamlPlainText);
  if loadedHash then
    @@encryptedData.merge(loadedHash);
  end
end

.load_file(yamlFileName) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/rake/config.rb', line 280

def self.load_file(yamlFileName)
  Rake::Application.mesg "loading configuration file [#{yamlFileName}]\n  in #{Dir.getwd}" if Rake.application.options.trace;
  loadedHash = YAML::load_file(yamlFileName);
  if loadedHash then
    @@data.merge(loadedHash);
  end
end

.load_rake_files(recipeDir) ⇒ Object



351
352
353
354
355
356
357
358
359
# File 'lib/rake/config.rb', line 351

def self.load_rake_files(recipeDir)
  @@configFileNames.each() do | aConfigFileName |
    #
    # load the corresponding rake file
    #
    rakeFile = recipeDir + '/' + aConfigFileName + '.rake';
    Rake.load_rakefile(rakeFile) if File.exists?(rakeFile);
  end
end

.load_recipe(recipeName) ⇒ Object



474
475
476
477
478
479
480
# File 'lib/rake/config.rb', line 474

def self.load_recipe(recipeName)
  @@recipePaths.reverse.each do | aRecipePath |
    recipeDir = aRecipePath+recipeName;
    next unless File.directory?(recipeDir);
    load_recipe_dir(recipeDir);
  end
end

.load_recipe_dir(recipeDir) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/rake/config.rb', line 438

def self.load_recipe_dir(recipeDir)
  #
  # extract the last directory name in the path to use as the 
  # recipeName
  #
  recipeName = recipeDir.split(/\//).pop();
  #
  # search for sub-directories and recursively calling 
  # load_recipe_dir to load any *.conf/*.enc or *.rake fragments 
  # found.
  #
  Dir.glob(recipeDir+'/*').each do | aFileName |
    next unless aFileName.dup.force_encoding("UTF-8").valid_encoding?
    next if aFileName =~ /\.$/;
    next unless File.directory?(aFileName);
    load_recipe_dir(aFileName);
  end
  #
  # load the configurtion files first in case they are used in the 
  # rakefile
  # ... generic first ...
  confFile = recipeDir + '/' + recipeName + '.conf';
  Conf.load_file(confFile) if File.exists?(confFile);
  encConfFile = recipeDir + '/' + recipeName + '.enc';
  Conf.load_encrypted_file(encConfFile) if File.exists?(encConfFile);
  # ... then more specific ...
  Conf.load_config_files(recipeDir);

  # now load the corresponding rake files
  # ... generic first...
  rakeFile = recipeDir + '/' + recipeName + '.rake';
  Rake.load_rakefile(rakeFile) if File.exists?(rakeFile);
  # ... then more specific ...
  Conf.load_rake_files(recipeDir);
end

.log_file_nameObject



433
434
435
436
# File 'lib/rake/config.rb', line 433

def self.log_file_name()
  return @@configFileNames.join('-') unless @@configFileNames.empty?;
  return "noConfig";
end

.method_missing(meth, *args) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/rake/config.rb', line 318

def self.method_missing(meth, *args)
  Thread.current[:confStack] = ConfStack.new(self, :Conf, args);
  Thread.current[:confStack].calling(self, meth, :method_missing, args);
  meth_s = meth.to_s
  if @@data.respond_to?(meth) ||
    @@data.data.has_key?(meth) ||
    @@data.schema.has_key?(meth) ||
    meth_s[-1..-1] == '='then
    @@data.method_missing(meth, *args);
  else 
    Thread.current[:confStack].reportNoMethodError(
      NoMethodError.new("no such key #{meth} in construct"))
  end
end

.prettyPrintObject



488
489
490
491
492
# File 'lib/rake/config.rb', line 488

def self.prettyPrint
  result = StringIO.new;
  @@data.prettyPrint(result, "Conf");
  result.string;
end

.save_file(yamlFileName, branch = []) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/rake/config.rb', line 302

def self.save_file(yamlFileName, branch = [])
  branchData = @@data;
  branch.each do | aKey |
    branchData = branchData[aKey] if branchData.has_key?(aKey);
  end
  branchData = branchData.to_stringHash if branchData.is_a?(Construct);
  branch.reverse.each do | aKey |
    tmpHash = Hash.new();
    tmpHash[aKey.to_s] = branchData;
    branchData = tmpHash;
  end
  yamlFile = File.open(yamlFileName, "w");
  YAML::dump(branchData, yamlFile);
  yamlFile.close();
end