lib/cuken/api/chef.rb in cuken-0.1.2 vs lib/cuken/api/chef.rb in cuken-0.1.4

- old
+ new

@@ -6,39 +6,71 @@ module Api module Chef include ::Cuken::Api::Chef::Common - def update_cookbook_paths(ckbk_path, cookbook) - lp = Pathname(ckbk_path).expand_path.realdirpath - lrp = lp + '.git' + def append_cookbook_path(cookbook, lp, lrp) if lrp.exist? + announce_or_puts(%{# Adding cookbook path: #{lp}}) if @announce_env && cookbook chef.cookbook_paths << lp if cookbook + announce_or_puts(%{# Adding cookbooks path: #{lp.parent}}) if @announce_env && cookbook chef.cookbooks_paths << lp.parent if cookbook lrp + else + announce_or_puts(%{# WARNING: cookbook(s) path: #{lp} is not a Git repository.}) if @announce_env && cookbook end end + def update_cookbook_paths(ckbk_path, cookbook) + lp = Pathname(ckbk_path).expand_path.realdirpath + lrp = lp + '.git' + if cookbook + append_cookbook_path(cookbook, lp, lrp) + else + lp.each_child do |pth| + rpth = pth + '.git' + append_cookbook_path(true, pth, rpth) if rpth.directory? + end + chef.cookbooks_paths.uniq! + if chef.cookbooks_paths.empty? + announce_or_puts(%{# WARNING: cookbooks path: #{lp} does not contain any Git repositories.}) if @announce_env + end + end + end + def chef_clone_repo(ckbk_path, cookbook = false, repo = chef.remote_chef_repo, brnch = 'master') in_current_dir do + gritty = ::Grit::Git.new(current_dir) + clone_opts = {:quiet => false, :verbose => true, :progress => true, :branch => brnch} if Dir.exists?(ckbk_path) - update_cookbook_paths(ckbk_path, cookbook) + $stdout.puts "Pulling: #{repo} into #{ckbk_path}" + gritty.pull(clone_opts, repo, ckbk_path) else - clone_opts = {:quiet => false, :verbose => true, :progress => true, :branch => brnch} - gritty = ::Grit::Git.new(current_dir) + $stdout.puts "Cloning: #{repo} into #{ckbk_path}" gritty.clone(clone_opts, repo, ckbk_path) - update_cookbook_paths(ckbk_path, cookbook) end + update_cookbook_paths(ckbk_path, cookbook) + Pathname(ckbk_path) end end def run_knife_command(cmd, interactive=false) + no_ckbk_pth_opt = ['cookbook delete'].each{|str| break true if cmd[/#{str}/]} + no_ckbk_pth = chef.cookbooks_paths.empty? + if no_ckbk_pth + ckbk_pth_opt = false + else + ckbk_pth = (chef.cookbooks_paths.collect { |pn| pn.expand_path.to_s }).join(':') + ckbk_pth_opt = true + end + ckbk_pth_opt = false if no_ckbk_pth_opt.is_a? TrueClass in_current_dir do unless chef.client_knife_path chef.client_knife_path = Pathname(chef.local_chef_repo).ascend { |d| h=d+'.chef'+'knife.rb'; break h if h.file? } end + raise(RuntimeError, "chef.client_knife_path is required", caller) unless chef.client_knife_path cmd += " -c #{chef.client_knife_path.expand_path.to_s}" if chef.client_knife_path.expand_path.exist? - cmd += " -o #{(chef.cookbooks_paths.collect { |pn| pn.expand_path.to_s }).join(':')}" unless chef.cookbooks_paths.empty? + cmd += " -o #{ckbk_pth}" if ckbk_pth_opt cmd += " --log_level debug" if chef.knife_debug if interactive run_interactive(unescape("#{chef.knife}" + cmd)) else run_simple(unescape("#{chef.knife}" + cmd))