lib/soloist/chef_config_generator.rb in soloist-0.0.8 vs lib/soloist/chef_config_generator.rb in soloist-0.9.0
- old
+ new
@@ -1,60 +1,71 @@
require 'yaml'
-class ChefConfigGenerator
- def initialize(yaml_string, relative_path_to_soloistrc)
- @hash = YAML.load(yaml_string)
- @relative_path_to_soloistrc = relative_path_to_soloistrc
- merge_env_variable_switches
- end
+module Soloist
+ class Soloist::ChefConfigGenerator
+ def initialize(config, relative_path_to_soloistrc)
+ @recipes = []
+ @cookbook_paths = []
+ @preserved_environment_variables = %w{PATH BUNDLE_PATH GEM_HOME GEM_PATH RAILS_ENV RACK_ENV}
+ merge_config(config, relative_path_to_soloistrc)
+ end
+
+ attr_reader :preserved_environment_variables, :cookbook_paths
+ attr_accessor :recipes
+
+ def support_old_format(hash)
+ hash['recipes'] ||= hash.delete('Recipes')
+ hash['cookbook_paths'] ||= hash.delete('Cookbook_Paths')
+ hash
+ end
+
+ def append_path(paths, relative_path_to_soloistrc)
+ paths.map do |path|
+ path.slice(0,1) == '/' ? path : "#{FileUtils.pwd}/#{relative_path_to_soloistrc}/#{path}"
+ end
+ end
- def merge_env_variable_switches
- return unless @hash["env_variable_switches"]
- @hash["env_variable_switches"].keys.each do |variable|
- ENV[variable] && ENV[variable].split(',').each do |env_variable_value|
- sub_hash = @hash["env_variable_switches"][variable][env_variable_value]
- if sub_hash && sub_hash["recipes"]
- @hash["recipes"] ||= []
- @hash["recipes"] = (@hash["recipes"] + sub_hash["recipes"]).uniq
+ def merge_config(sub_hash, relative_path_to_soloistrc)
+ sub_hash = support_old_format(sub_hash)
+ if sub_hash["recipes"]
+ @recipes = (@recipes + sub_hash["recipes"]).uniq
+ end
+ if sub_hash["cookbook_paths"]
+ @cookbook_paths = (@cookbook_paths + append_path(sub_hash["cookbook_paths"], relative_path_to_soloistrc)).uniq
+ end
+ if sub_hash["env_variable_switches"]
+ merge_env_variable_switches(sub_hash["env_variable_switches"], relative_path_to_soloistrc)
+ end
+ end
+
+ def merge_env_variable_switches(hash_to_merge, relative_path_to_soloistrc)
+ hash_to_merge.keys.each do |variable|
+ @preserved_environment_variables << variable
+ ENV[variable] && ENV[variable].split(',').each do |env_variable_value|
+ sub_hash = hash_to_merge[variable] && hash_to_merge[variable][env_variable_value]
+ merge_config(sub_hash, relative_path_to_soloistrc) if sub_hash
end
- if sub_hash && sub_hash["cookbook_paths"]
- @hash["cookbook_paths"] ||= []
- @hash["cookbook_paths"] = (@hash["cookbook_paths"] + sub_hash["cookbook_paths"]).uniq
- end
end
end
- end
- def cookbook_paths
- (@hash["cookbook_paths"] || @hash["Cookbook_Paths"]).map do |v|
- (v =~ /\//) == 0 ? v : "#{FileUtils.pwd}/#{@relative_path_to_soloistrc}/#{v}"
+ def solo_rb
+ "cookbook_path #{cookbook_paths.inspect}"
end
- end
- def solo_rb
- "cookbook_path #{cookbook_paths.inspect}"
- end
+ def json_hash
+ {
+ "recipes" => @recipes
+ }
+ end
- def json_hash
- recipes = @hash["Recipes"] || @hash["recipes"]
- {
- "recipes" => recipes
- }
- end
+ def json_file
+ json_hash.to_json
+ end
- def json_file
- json_hash.to_json
- end
-
- def preserved_environment_variables
- always_passed = %w{PATH BUNDLE_PATH GEM_HOME GEM_PATH RAILS_ENV RACK_ENV}
- always_passed += @hash["env_variable_switches"].keys if @hash["env_variable_switches"]
- always_passed
- end
-
- def preserved_environment_variables_string
- variable_array = []
- preserved_environment_variables.map do |env_variable|
- "#{env_variable}=#{ENV[env_variable]}" unless ENV[env_variable].nil?
- end.compact.join(" ")
+ def preserved_environment_variables_string
+ variable_array = []
+ preserved_environment_variables.map do |env_variable|
+ "#{env_variable}=#{ENV[env_variable]}" unless ENV[env_variable].nil?
+ end.compact.join(" ")
+ end
end
end
\ No newline at end of file