From 0685b5436fa72fa9c922929069e835f92623c43f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 30 Aug 2011 07:21:03 -0400 Subject: [PATCH] make config reading a global thing --- lib/hydra/config.rb | 21 +++++++++++++++++++++ lib/hydra/master.rb | 14 ++------------ lib/hydra/sync.rb | 5 ++++- lib/hydra/tasks.rb | 5 +++-- 4 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 lib/hydra/config.rb diff --git a/lib/hydra/config.rb b/lib/hydra/config.rb new file mode 100644 index 0000000..92d70a8 --- /dev/null +++ b/lib/hydra/config.rb @@ -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 diff --git a/lib/hydra/master.rb b/lib/hydra/master.rb index 9820f78..ddb844a 100644 --- a/lib/hydra/master.rb +++ b/lib/hydra/master.rb @@ -1,4 +1,5 @@ require 'hydra/hash' +require 'hydra/config' require 'open3' require 'hydra/tmpdir' require 'erb' @@ -37,18 +38,7 @@ module Hydra #:nodoc: opts.stringify_keys! config_file = opts.delete('config') { nil } if 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 = Hydra::Config.load(config_file) opts.merge!(config_yml.stringify_keys!) end diff --git a/lib/hydra/sync.rb b/lib/hydra/sync.rb index c4777f9..4f5f8d7 100644 --- a/lib/hydra/sync.rb +++ b/lib/hydra/sync.rb @@ -1,4 +1,6 @@ require 'yaml' +require 'hydra/config' + module Hydra #:nodoc: # Hydra class responsible for delegate work down to workers. # @@ -62,7 +64,8 @@ module Hydra #:nodoc: opts.stringify_keys! config_file = opts.delete('config') { nil } 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 @verbose = opts.fetch('verbose') { false } @sync = opts.fetch('sync') { {} } diff --git a/lib/hydra/tasks.rb b/lib/hydra/tasks.rb index 39fb8d4..542ecc4 100644 --- a/lib/hydra/tasks.rb +++ b/lib/hydra/tasks.rb @@ -1,4 +1,6 @@ require 'open3' +require 'hydra/config' + module Hydra #:nodoc: # Hydra Task Common attributes and methods class Task @@ -227,7 +229,6 @@ module Hydra #:nodoc: # t.verbose = false # optionally set to true for lots of debug messages # end class SyncTask < Hydra::Task - # Create a new SyncTestTask def initialize(name = :sync) @name = name @@ -280,7 +281,7 @@ module Hydra #:nodoc: def define desc "Run #{@name} remotely on all workers" task "hydra:remote:#{@name}" do - config = YAML.load_file(@config) + config = Hydra::Config.load(@config) environment = config.fetch('environment') { 'test' } workers = config.fetch('workers') { [] } workers = workers.select{|w| w['type'] == 'ssh'}