make config reading a global thing

This commit is contained in:
John Bintz 2011-08-30 07:21:03 -04:00
parent 06387cb1e6
commit 0685b5436f
4 changed files with 30 additions and 15 deletions

21
lib/hydra/config.rb Normal file
View File

@ -0,0 +1,21 @@
module Hydra
class Config
class << self
def load(config_file)
begin
config_erb = ERB.new(IO.read(config_file)).result(binding)
rescue Exception => e
raise(YmlLoadError,"config file was found, but could not be parsed with ERB.\n#{$!.inspect}")
end
begin
config_yml = YAML::load(config_erb)
rescue StandardError => e
raise(YmlLoadError,"config file was found, but could not be parsed.\n#{$!.inspect}")
end
config_yml
end
end
end
end

View File

@ -1,4 +1,5 @@
require 'hydra/hash' require 'hydra/hash'
require 'hydra/config'
require 'open3' require 'open3'
require 'hydra/tmpdir' require 'hydra/tmpdir'
require 'erb' require 'erb'
@ -37,18 +38,7 @@ module Hydra #:nodoc:
opts.stringify_keys! opts.stringify_keys!
config_file = opts.delete('config') { nil } config_file = opts.delete('config') { nil }
if config_file if config_file
config_yml = Hydra::Config.load(config_file)
begin
config_erb = ERB.new(IO.read(config_file)).result(binding)
rescue Exception => e
raise(YmlLoadError,"config file was found, but could not be parsed with ERB.\n#{$!.inspect}")
end
begin
config_yml = YAML::load(config_erb)
rescue StandardError => e
raise(YmlLoadError,"config file was found, but could not be parsed.\n#{$!.inspect}")
end
opts.merge!(config_yml.stringify_keys!) opts.merge!(config_yml.stringify_keys!)
end end

View File

@ -1,4 +1,6 @@
require 'yaml' require 'yaml'
require 'hydra/config'
module Hydra #:nodoc: module Hydra #:nodoc:
# Hydra class responsible for delegate work down to workers. # Hydra class responsible for delegate work down to workers.
# #
@ -62,7 +64,8 @@ module Hydra #:nodoc:
opts.stringify_keys! opts.stringify_keys!
config_file = opts.delete('config') { nil } config_file = opts.delete('config') { nil }
if config_file if config_file
opts.merge!(YAML.load_file(config_file).stringify_keys!) config_yml = Hydra::Config.load(config_file)
opts.merge!(config_yml.stringify_keys!)
end end
@verbose = opts.fetch('verbose') { false } @verbose = opts.fetch('verbose') { false }
@sync = opts.fetch('sync') { {} } @sync = opts.fetch('sync') { {} }

View File

@ -1,4 +1,6 @@
require 'open3' require 'open3'
require 'hydra/config'
module Hydra #:nodoc: module Hydra #:nodoc:
# Hydra Task Common attributes and methods # Hydra Task Common attributes and methods
class Task class Task
@ -227,7 +229,6 @@ module Hydra #:nodoc:
# t.verbose = false # optionally set to true for lots of debug messages # t.verbose = false # optionally set to true for lots of debug messages
# end # end
class SyncTask < Hydra::Task class SyncTask < Hydra::Task
# Create a new SyncTestTask # Create a new SyncTestTask
def initialize(name = :sync) def initialize(name = :sync)
@name = name @name = name
@ -280,7 +281,7 @@ module Hydra #:nodoc:
def define def define
desc "Run #{@name} remotely on all workers" desc "Run #{@name} remotely on all workers"
task "hydra:remote:#{@name}" do task "hydra:remote:#{@name}" do
config = YAML.load_file(@config) config = Hydra::Config.load(@config)
environment = config.fetch('environment') { 'test' } environment = config.fetch('environment') { 'test' }
workers = config.fetch('workers') { [] } workers = config.fetch('workers') { [] }
workers = workers.select{|w| w['type'] == 'ssh'} workers = workers.select{|w| w['type'] == 'ssh'}