lib/cuken/api/chef.rb in cuken-0.1.13 vs lib/cuken/api/chef.rb in cuken-0.1.15

- old
+ new

@@ -12,13 +12,29 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # -require 'aruba/api' unless defined? ::Aruba::Api +#require 'aruba/api' unless defined? ::Aruba::Api require 'chef' unless defined? ::Chef require 'grit' unless defined? ::Grit +# +# Author:: Hedgehog (<hedgehogshiatus@gmail.com>) +# Copyright:: Copyright (c) 2011 Hedgehog. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# require 'vagrant' unless defined? ::Vagrant require 'cuken/api/common' require 'cuken/api/chef/common' require 'cuken/api/chef/cookbook' require 'cuken/api/chef/knife' @@ -28,10 +44,11 @@ module ::Cuken module Api module Chef include ::Cuken::Api::Chef::Common + include ::Cuken::Api::Aruba::Api 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 @@ -68,52 +85,124 @@ if repo = res[/ERROR: (.*) doesn't exist. Did you enter it correctly?/,1] raise RuntimeError, "ERROR: #{repo} doesn't exist. Did you enter it correctly? #{repo}", caller end end - def chef_clone_repo(ckbk_path, cookbook = false, repo = chef.remote_chef_repo, type = {'branch' => 'master'}) - in_current_dir do + def chef_clone_repo(ckbk_path, cookbook = false, repo = chef.remote_chef_repo, type = {'branch' => 'master'}, deep_clone = true) + in_dir do pth = Pathname(ckbk_path).expand_path - gritty = ::Grit::Git.new(current_dir) + gritty = ::Grit::Git.new((pth + '.git').to_s) announce_or_puts gritty.inspect - clone_opts = {:quiet => false, :verbose => true, :progress => true} + clone_opts = {:depth => 1, :quiet => false, :verbose => true, :progress => true} type['branch'] = type['branch'].nil? ? '' : type['branch'] type['tag'] = type['tag'].nil? ? '' : type['tag'] type['ref'] = type['ref'].nil? ? '' : type['ref'] - clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch'] + clone_opts[:depth] = 1 unless deep_clone if pth.directory? announce_or_puts "Pulling: #{repo} into #{pth}" - res = gritty.pull(clone_opts, repo, pth.to_s) + FileUtils.cd(pth.to_s) do + res = gritty.pull(clone_opts, repo) + end + clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch'] else + clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch'] announce_or_puts "Cloning: #{repo} into #{pth}" res = gritty.clone(clone_opts, repo, pth.to_s) + Dir.chdir(pth.to_s) do + if ::File.exist?('.gitmodules') + cmd = "git submodule init" + announce_or_puts `#{cmd}` + cmd = "git submodule update" + announce_or_puts `#{cmd}` + end + end end clone_pull_error_check(res) if res update_cookbook_paths(pth, cookbook) unless chef.cookbooks_paths.empty? # is empty after cloning default chef-repo grotty = ::Grit::Git.new((pth + '.git').to_s) - grotty.checkout( { :b => true }, clone_opts[:branch] ) + grotty.checkout( { :B => clone_opts[:branch]||'master' } ) if !type['tag'].empty? || !type['ref'].empty? grotty.checkout( { :B => true }, 'cuken', type['tag']||type['ref'] ) else grotty.checkout( { :B => true }, 'cuken' ) end end pth end end + def chef_submodule_repo(ckbk_path, cookbook = false, repo = chef.remote_chef_repo, type = {'branch' => 'master'}, deep_clone = true) + in_dir do + pth = (Pathname(chef.root_dir).expand_path)+ckbk_path + gritty = ::Grit::Git.new((pth + '.git').to_s) + announce_or_puts gritty.inspect + clone_opts = {:quiet => false, :verbose => true, :progress => true} + clone_opts[:depth] = 1 unless deep_clone + checkout = type['ref']||type['tag']||type['branch']||'master' + type['branch'] = type['branch'].nil? ? '' : type['branch'] + type['tag'] = type['tag'].nil? ? '' : type['tag'] + type['ref'] = type['ref'].nil? ? '' : type['ref'] + res = nil + if pth.directory? + announce_or_puts "Updating submodule: #{repo} at #{pth}" + FileUtils.cd(pth.to_s) do + cmd = "git fetch" + res = `#{cmd}` + announce_or_puts res + end + clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch'] + else + clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch'] + announce_or_puts "Adding submodule: #{repo} at #{pth}" + FileUtils.cd(chef.root_dir) do + cmd = "git submodule add #{repo} #{pth.to_s}" + announce_or_puts `#{cmd}` + end + FileUtils.cd(pth.to_s) do + cmd = "git submodule init" + res = `#{cmd}` + announce_or_puts res + cmd = "git submodule update" + res = `#{cmd}` + announce_or_puts res + end + end + clone_pull_error_check(res) if res + update_cookbook_paths(pth, cookbook) + unless chef.cookbooks_paths.empty? # is empty after cloning default chef-repo + grotty = ::Grit::Git.new((pth + '.git').to_s) + cmd = "git submodule add #{repo} #{pth.to_s}" + announce_or_puts `#{cmd}` + FileUtils.cd(pth.to_s) do + cmd = "git submodule init" + res = `#{cmd}` + announce_or_puts res + cmd = "git submodule update" + res = `#{cmd}` + announce_or_puts res + if !type['tag'].empty? || !type['ref'].empty? + grotty.checkout( { :B => true }, 'cuken', type['tag']||type['ref'] ) + else + grotty.checkout( { :B => true }, 'cuken' ) + end + end + end + pth + end + end + def run_knife_command(cmd, interactive=false) no_ckbk_pth_opt = ['cookbook delete', 'role from file'].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 + in_dir do unless chef.knife_config_file chef.knife_config_file = Pathname(chef.local_chef_repo).ascend { |d| h=d+'.chef'+'knife.rb'; break h if h.file? } end raise(RuntimeError, "chef.knife_config_file is required", caller) unless chef.knife_config_file end