Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Berkshelf integration #74

Merged
merged 9 commits into from
Aug 6, 2013
2 changes: 1 addition & 1 deletion lib/chef/knife/spork-promote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions lib/knife-spork/plugins/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ 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

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

Expand Down Expand Up @@ -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}"
Expand All @@ -105,7 +105,7 @@ def git_add(filepath,filename)
end
end
end

# Commit changes, if any
def git_commit
begin
Expand Down Expand Up @@ -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
46 changes: 30 additions & 16 deletions lib/knife-spork/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down