lib/capistrano-chef-solo.rb in yyuu-capistrano-chef-solo-0.0.3 vs lib/capistrano-chef-solo.rb in yyuu-capistrano-chef-solo-0.0.4
- old
+ new
@@ -1,9 +1,10 @@
require 'capistrano-chef-solo/version'
require 'capistrano-rbenv'
require 'capistrano/configuration'
require 'capistrano/recipes/deploy/scm'
+require 'capistrano/transfer'
require 'json'
require 'tmpdir'
require 'uri'
module Capistrano
@@ -162,13 +163,30 @@
}
task(:update_config) {
put(chef_solo_config, File.join(chef_solo_path, 'config', 'solo.rb'))
}
+ # merge nested hashes
+ def _deep_merge(a, b)
+ f = lambda { |key, val1, val2| Hash === val1 && Hash === val2 ? val1.merge(val2, &f) : val2 }
+ a.merge(b, &f)
+ end
+
_cset(:chef_solo_attributes, {})
+ _cset(:chef_solo_host_attributes, {})
task(:update_attributes) {
- attributes = chef_solo_attributes.merge('run_list' => fetch(:chef_solo_run_list, []))
- put(attributes.to_json, File.join(chef_solo_path, 'config', 'solo.json'))
+ attributes = _deep_merge(chef_solo_attributes, {'run_list' => fetch(:chef_solo_run_list, [])})
+ to = File.join(chef_solo_path, 'config', 'solo.json')
+ if chef_solo_host_attributes.empty?
+ put(attributes.to_json, to)
+ else
+ execute_on_servers { |servers|
+ servers.each { |server|
+ host_attributes = _deep_merge(attributes, chef_solo_host_attributes.fetch(server.host, {}))
+ Capistrano::Transfer.process(:up, StringIO.new(host_attributes.to_json), to, [sessions[server]], :logger => logger)
+ }
+ }
+ end
}
task(:invoke) {
execute = []
execute << "cd #{chef_solo_path}"