lib/capistrano-chef-solo.rb in yyuu-capistrano-chef-solo-0.1.2 vs lib/capistrano-chef-solo.rb in yyuu-capistrano-chef-solo-0.1.3

- old
+ new

@@ -32,13 +32,15 @@ } namespace(:chef_solo) { _cset(:chef_solo_version, "11.4.0") _cset(:chef_solo_path) { capture("echo $HOME/chef").strip } - _cset(:chef_solo_path_children, %w(bundle cache config cookbooks)) - _cset(:chef_solo_config_file) { File.join(chef_solo_path, "config", "solo.rb") } - _cset(:chef_solo_attributes_file) { File.join(chef_solo_path, "config", "solo.json") } + _cset(:chef_solo_cache_path) { File.join(chef_solo_path, "cache") } + _cset(:chef_solo_config_path) { File.join(chef_solo_path, "config") } + _cset(:chef_solo_cookbooks_path) { File.join(chef_solo_path, "cookbooks") } + _cset(:chef_solo_config_file) { File.join(chef_solo_config_path, "solo.rb") } + _cset(:chef_solo_attributes_file) { File.join(chef_solo_config_path, "solo.json") } _cset(:chef_solo_bootstrap_user) { if variables.key?(:chef_solo_user) logger.info(":chef_solo_user has been deprecated. use :chef_solo_bootstrap_user instead.") fetch(:chef_solo_user, user) @@ -107,10 +109,13 @@ servers = find_servers if block_given? begin activated = _activate_settings(servers) yield + rescue => error + logger.info("could not connect with bootstrap settings: #{error}") + raise ensure _deactivate_settings(servers) if activated end else _activate_settings(servers) @@ -198,11 +203,11 @@ installed = Regexp.new(Regexp.escape(chef_solo_version)) =~ version rescue installed = false end unless installed - dirs = chef_solo_path_children.map { |dir| File.join(chef_solo_path, dir) } + dirs = [ chef_solo_path, chef_solo_cache_path, chef_solo_config_path, chef_solo_cookbooks_path ].uniq run("mkdir -p #{dirs.map { |x| x.dump }.join(" ")}") top.put(chef_solo_gemfile, File.join(chef_solo_path, "Gemfile")) args = fetch(:chef_solo_bundle_options, []) args << "--path=#{File.join(chef_solo_path, "bundle").dump}" args << "--quiet" @@ -210,32 +215,31 @@ end } def update(options={}) update_cookbooks(options) - update_config(options) update_attributes(options) + update_config(options) end def update_cookbooks(options={}) _normalize_cookbooks(chef_solo_cookbooks).each do |name, variables| begin tmpdir = capture("mktemp -d /tmp/cookbooks.XXXXXXXXXX", options).strip run("rm -rf #{tmpdir.dump} && mkdir -p #{tmpdir.dump}", options) deploy_cookbooks(name, tmpdir, variables, options) - install_cookbooks(name, tmpdir, File.join(chef_solo_path, "cookbooks"), options) + install_cookbooks(name, tmpdir, chef_solo_cookbooks_path, options) ensure run("rm -rf #{tmpdir.dump}", options) end end end # # The definition of cookbooks. # By default, load cookbooks from local path of "config/cookbooks". # - _cset(:chef_solo_cookbooks_name) { application } _cset(:chef_solo_cookbooks_exclude, %w(.hg .git .svn)) _cset(:chef_solo_cookbooks_default_variables) {{ :scm => :none, :deploy_via => :copy_subdir, :deploy_subdir => nil, @@ -247,11 +251,19 @@ variables = chef_solo_cookbooks_default_variables.dup variables[:scm] = fetch(:chef_solo_cookbooks_scm) if exists?(:chef_solo_cookbooks_scm) variables[:deploy_subdir] = fetch(:chef_solo_cookbooks_subdir, "config/cookbooks") variables[:repository] = fetch(:chef_solo_cookbooks_repository) if exists?("chef_solo_cookbooks_repository") variables[:revision] = fetch(:chef_solo_cookbooks_revision) if exists?(:chef_solo_cookbooks_revision) - { chef_solo_cookbooks_name => variables } + if exists?(:chef_solo_cookbook_name) + # deploy as single cookbook + name = fetch(:chef_solo_cookbook_name) + { name => variables.merge(:cookbook_name => name) } + else + # deploy as multiple cookbooks + name = fetch(:chef_solo_cookbooks_name, application) + { name => variables } + end } _cset(:chef_solo_repository_cache) { File.expand_path("tmp/cookbooks-cache") } def _normalize_cookbooks(cookbooks) xs = cookbooks.map { |name, variables| @@ -285,11 +297,18 @@ variables.each do |key, val| set(key, val) end strategy.deploy! end - run("rsync -lrpt #{(release_path + "/").dump} #{destination.dump}", options) + if variables.key?(:cookbook_name) + # deploy as single cookbook + final_destination = File.join(destination, variables[:cookbook_name]) + else + # deploy as multiple cookbooks + final_destination = destination + end + run("rsync -lrpt #{(release_path + "/").dump} #{final_destination.dump}", options) ensure run("rm -rf #{releases_path.dump}", options) end end @@ -308,15 +327,15 @@ run("rsync -lrpt #{(source + "/").dump} #{destination.dump}", options) end _cset(:chef_solo_config) { (<<-EOS).gsub(/^\s*/, "") - file_cache_path #{File.join(chef_solo_path, "cache").dump} - cookbook_path #{File.join(chef_solo_path, "cookbooks").dump} + file_cache_path #{chef_solo_cache_path.dump} + cookbook_path #{chef_solo_cookbooks_path.dump} EOS } def update_config(options={}) - top.put(chef_solo_config, chef_solo_config_file) + top.put(chef_solo_config, chef_solo_config_file, options) end # merge nested hashes def _merge_attributes!(a, b) f = lambda { |key, val1, val2|