diff --git a/lib/chef/knife/spork-promote.rb b/lib/chef/knife/spork-promote.rb index 40ae034..d124387 100644 --- a/lib/chef/knife/spork-promote.rb +++ b/lib/chef/knife/spork-promote.rb @@ -26,7 +26,7 @@ class SporkPromote < Chef::Knife if defined?(::Berkshelf) option :berksfile, :short => '-b', - :long => 'berksfile', + :long => '--berksfile BERKSFILE', :description => 'Path to a Berksfile to operate off of', :default => File.join(Dir.pwd, ::Berkshelf::DEFAULT_FILENAME) end diff --git a/lib/knife-spork/plugins/git.rb b/lib/knife-spork/plugins/git.rb index 179e7f3..70afb9a 100644 --- a/lib/knife-spork/plugins/git.rb +++ b/lib/knife-spork/plugins/git.rb @@ -11,8 +11,8 @@ def before_bump git_pull(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","") git_pull_submodules(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","") cookbooks.each do |cookbook| - git_pull(cookbook.root_dir) - git_pull_submodules(cookbook.root_dir) + git_pull(cookbook_path_for(cookbook)) + git_pull_submodules(cookbook_path_for(cookbook)) end end @@ -20,23 +20,23 @@ def before_upload git_pull(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","") git_pull_submodules(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","") cookbooks.each do |cookbook| - git_pull(cookbook.root_dir) - git_pull_submodules(cookbook.root_dir) + git_pull(cookbook_path_for(cookbook)) + git_pull_submodules(cookbook_path_for(cookbook)) end end def before_promote cookbooks.each do |cookbook| - git_pull(environment_path) unless cookbook.root_dir.include?(environment_path.gsub"/environments","") - git_pull_submodules(environment_path) unless cookbook.root_dir.include?(environment_path.gsub"/environments","") - git_pull(cookbook.root_dir) - git_pull_submodules(cookbook.root_dir) + git_pull(environment_path) unless cookbook_path_for(cookbook).include?(environment_path.gsub"/environments","") + git_pull_submodules(environment_path) unless cookbook_path_for(cookbook).include?(environment_path.gsub"/environments","") + git_pull(cookbook_path_for(cookbook)) + git_pull_submodules(cookbook_path_for(cookbook)) end end def after_bump cookbooks.each do |cookbook| - git_add(cookbook.root_dir,"metadata.rb") + git_add(cookbook_path_for(cookbook),"metadata.rb") end end @@ -92,7 +92,7 @@ def git_pull_submodules(path) end end end - + def git_add(filepath,filename) if is_repo?(filepath) ui.msg "Git add'ing #{filepath}/#{filename}" @@ -105,7 +105,7 @@ def git_add(filepath,filename) end end end - + # Commit changes, if any def git_commit begin @@ -184,6 +184,14 @@ def branch def tag_name cookbooks.collect{|c| "#{c.name}@#{c.version}"}.join('-') end + + def cookbook_path_for cookbook + if defined?(Berkshelf) and cookbook.is_a? Berkshelf::CachedCookbook + cookbook.path.to_s + else + cookbook.root_path + end + end end end end diff --git a/lib/knife-spork/runner.rb b/lib/knife-spork/runner.rb index ef75f53..cda2110 100644 --- a/lib/knife-spork/runner.rb +++ b/lib/knife-spork/runner.rb @@ -98,26 +98,40 @@ def all_cookbooks ::Chef::CookbookLoader.new(::Chef::Config.cookbook_path) end - def load_cookbook(cookbook_name) - return cookbook_name if cookbook_name.is_a?(::Chef::CookbookVersion) + def load_cookbook(name) + return name if name.is_a?(Chef::CookbookVersion) - # Search the local chef repo first - loader = ::Chef::CookbookLoader.new(Chef::Config.cookbook_path) - if loader.has_key?(cookbook_name) - return loader[cookbook_name] - end + cookbook = load_from_chef(name) || load_from_berkshelf(name) || load_from_librarian(name) - # We didn't find the cookbook in our local repo, so check Berkshelf - if defined?(::Berkshelf) - berksfile = ::Berkshelf::Berksfile.from_file(self.config[:berksfile]) - if cookbook = berksfile.sources.find{ |source| source.name == cookbook_name } - return cookbook - end - end + cookbook || raise(Chef::Exceptions::CookbookNotFound, + "Could not find cookbook '#{name}' in any of the sources!") + end + + def load_from_chef(name) + all_cookbooks[name] + rescue Chef::Exceptions::CookbookNotFound, + Chef::Exceptions::CookbookNotFoundInRepo + nil + end + + def load_from_berkshelf(name) + return unless defined?(::Berkshelf) + berksfile = ::Berkshelf::Berksfile.from_file(self.config[:berksfile]) + lockfile = ::Berkshelf::Lockfile.new(berksfile) + + raise Berkshelf::BerkshelfError, "LockFileNotFound" unless File.exists?(lockfile.filepath) - # TODO: add librarian support here + cookbook = Berkshelf.ui.mute { + berksfile.resolve(lockfile.find(name))[:solution].first + } + + cookbook + end - raise ::Chef::Exceptions::CookbookNotFound, "Could not find cookbook '#{cookbook_name}' in any of the sources!" + # @todo #opensource + def load_from_librarian(name) + # Your code here :) + nil end def load_cookbooks(cookbook_names)