Skip to content

Commit

Permalink
WIP Currently 'work for me' integration of berkshelf to master
Browse files Browse the repository at this point in the history
Tests are still outstanding, but sufficient for time sensitive work
  • Loading branch information
jasherai committed Mar 19, 2013
1 parent 1d18e0a commit 6cfd9e9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions knife-solo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'fog'
s.add_development_dependency 'minitest'
s.add_development_dependency 'parallel'
s.add_development_dependency 'berkshelf'

s.add_dependency 'chef', chef_version
s.add_dependency 'net-ssh', '>= 2.2.2', '< 3.0'
Expand Down
36 changes: 36 additions & 0 deletions lib/chef/knife/solo_cook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class SoloCook < Knife
:long => '--sync-only',
:description => 'Only sync the cookbook - do not run Chef'

option :berksfile,
:long => '--berksfile',
:description => 'Berkshelf file to use for configuration',
:default => 'Berksfile'

option :berkshelf,
:long => '--no-berkshelf',
:description => 'Skip berkshelf-chef install',
:default => true

option :librarian,
:long => '--no-librarian',
:description => 'Skip librarian-chef install',
Expand Down Expand Up @@ -72,6 +82,7 @@ def run

check_chef_version if config[:chef_check]
generate_node_config
berkshelf_install if config[:berkshelf]
librarian_install if config[:librarian]
rsync_kitchen
add_patches
Expand Down Expand Up @@ -195,6 +206,31 @@ def cook
result = stream_command cmd
raise "chef-solo failed. See output above." unless result.success?
end

def load_berkshelf
begin
require 'berkshelf'
rescue LoadError
false
else
true
end
end

def berkshelf_install
return unless File.exist?(File.expand_path(config[:berksfile]|| 'Berksfile'))
ui.msg 'Berksfile found! Using Berkshelf to install cookbooks...'
unless load_berkshelf
ui.warn [ "Berkshelf could not be loaded",
"Please add the berkshelf gem to your Gemfile or",
"install it manually with `gem install berkshelf`."
].join(' ')
else
ui.msg 'Installing Berkshelf cookbooks...'
::Berkshelf::Berksfile.from_file(File.expand_path(config[:berksfile])).install(:path => File.expand_path('cookbooks'))
end
end

end
end
end
35 changes: 35 additions & 0 deletions test/solo_cook_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'chef/cookbook/chefignore'
require 'chef/knife/solo_cook'
require 'librarian/action/install'
require 'berkshelf'

class SuccessfulResult
def success?
Expand Down Expand Up @@ -52,6 +53,40 @@ def test_does_not_run_librarian_if_denied_by_option
end
end

def test_does_not_load_berkshelf_if_no_berksfile
in_kitchen do
FileUtils.rm_f("Berksfile")
cmd = command("somehost")
cmd.expects(:load_berkshelf).never
cmd.ui.expects(:err).never
::Berkshelf::Berksfile.any_instance.expects(:from_file).never
cmd.run
end
end

def test_runs_berkshelf_if_berksfile_found
in_kitchen do
File.open("Berksfile", 'w') { |f| f.write "site: opscode\ncookbook 'ntp'" }
b = File.expand_path("Berksfile")
cmd = command("somehost")
#cmd.ui.expects(:msg).with(regexp_matches(/Berksfile found/))
cmd.expects(:berkshelf_install)
cmd.expects(:load_berkshelf)
#cmd.ui.expects(:err).never
#cmd.ui.expects(:msg).with(regexp_matches(/Installing Berkshelf cookbooks/))
::Berkshelf::Berksfile.any_instance.expects(:from_file).with(b) #.expects(:install).with('cookbooks') #.expects(:install)
cmd.run
end
end

#def test_runs_berkshelf_if_custom_berksfile_specified
#in_kitchen do
#f = File.open("Berksfile.local", 'w') {}
#::Berkshelf::Berksfile.from_file(f).expects(:run)
#command("somehost", "--berkshelf_file Berksfile.local").run
#end
#end

def test_validates_chef_version
in_kitchen do
cmd = command("somehost")
Expand Down

0 comments on commit 6cfd9e9

Please sign in to comment.