diff --git a/.gitignore b/.gitignore index 2f5e4f1e4..ae58e64df 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ Hobofile .hobo .bundle *.lock +cookbooks/* \ No newline at end of file diff --git a/Gemfile b/Gemfile index f4b28f2d6..0a0bf59de 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,9 @@ source "http://gems.github.com" # Gems required for the lib to even run gem "virtualbox", ">= 0.4.3" gem "net-ssh", ">= 2.0.19" +gem "net-scp", ">= 1.0.2" gem "jashmenn-git-style-binaries", ">= 0.1.10" +gem "json", ">= 1.2.0" # Gems required for testing only. To install run # gem bundle test diff --git a/config/default.rb b/config/default.rb index 40da4f8cc..9a3a3aeb0 100644 --- a/config/default.rb +++ b/config/default.rb @@ -15,4 +15,7 @@ Hobo::Config.run do |config| config.chef.cookbooks_path = "cookbooks" config.chef.provisioning_path = "/tmp/hobo-chef" + config.chef.json = { + :recipes => ["hobo_main"] + } end \ No newline at end of file diff --git a/lib/hobo/config.rb b/lib/hobo/config.rb index 04e6b73ff..f84dad762 100644 --- a/lib/hobo/config.rb +++ b/lib/hobo/config.rb @@ -71,6 +71,7 @@ module Hobo class ChefConfig < Base attr_accessor :cookbooks_path attr_accessor :provisioning_path + attr_accessor :json end class Top < Base diff --git a/lib/hobo/provisioning.rb b/lib/hobo/provisioning.rb index 15a050a10..8de3048c2 100644 --- a/lib/hobo/provisioning.rb +++ b/lib/hobo/provisioning.rb @@ -4,11 +4,28 @@ module Hobo def initialize(vm) @vm = vm - @vm.share_folder("hobo-provisioning", File.expand_path(Hobo.config.chef.cookbooks_path, Env.root_path), Hobo.config.chef.provisioning_path) + + # Share the cookbook folder. We'll use the provisioning path exclusively for + # chef stuff. + cookbooks_path = File.join(Hobo.config.chef.provisioning_path, "cookbooks") + @vm.share_folder("hobo-provisioning", File.expand_path(Hobo.config.chef.cookbooks_path, Env.root_path), cookbooks_path) end def run + chown_provisioning_folder + setup_json + end + def chown_provisioning_folder + logger.info "Setting permissions on deployment folder..." + SSH.execute do |ssh| + ssh.exec!("sudo chown #{Hobo.config.ssh.username} #{Hobo.config.chef.provisioning_path}") + end + end + + def setup_json + logger.info "Generating JSON and uploading..." + SSH.upload!(StringIO.new(Hobo.config.chef.json.to_json), File.join(Hobo.config.chef.provisioning_path, "dna.json")) end end end \ No newline at end of file diff --git a/lib/hobo/ssh.rb b/lib/hobo/ssh.rb index d252a8262..37151da6d 100644 --- a/lib/hobo/ssh.rb +++ b/lib/hobo/ssh.rb @@ -23,9 +23,10 @@ module Hobo end def upload!(from, to) - Net::SCP.upload!(Hobo.config.ssh.host, Hobo.config.ssh.username, - from, to, - :password => Hobo.config.ssh.password) + execute do |ssh| + scp = Net::SCP.new(ssh) + scp.upload!(from, to) + end end def up? diff --git a/lib/hobo/vm.rb b/lib/hobo/vm.rb index 7b0a264ef..7b801c22a 100644 --- a/lib/hobo/vm.rb +++ b/lib/hobo/vm.rb @@ -162,6 +162,7 @@ error logger.info "-- #{name}: #{guestpath}" ssh.exec!("sudo mkdir -p #{guestpath}") ssh.exec!("sudo mount -t vboxsf #{name} #{guestpath}") + ssh.exec!("sudo chown #{Hobo.config.ssh.username} #{guestpath}") end end end diff --git a/test/hobo/provisioning_test.rb b/test/hobo/provisioning_test.rb index 896355e32..a53c8a529 100644 --- a/test/hobo/provisioning_test.rb +++ b/test/hobo/provisioning_test.rb @@ -1,12 +1,46 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper') class ProvisioningTest < Test::Unit::TestCase + setup do + # Stub upload so nothing happens + Hobo::SSH.stubs(:upload!) + + vm = mock("vm") + vm.stubs(:share_folder) + @prov = Hobo::Provisioning.new(vm) + end + context "initializing" do - should "setup shared folder on VM" do + should "setup shared folder on VM for the cookbooks" do File.expects(:expand_path).with(Hobo.config.chef.cookbooks_path, Hobo::Env.root_path).returns("foo") + cookbooks_path = File.join(Hobo.config.chef.provisioning_path, "cookbooks") vm = mock("vm") - vm.expects(:share_folder).with("hobo-provisioning", "foo", Hobo.config.chef.provisioning_path) + vm.expects(:share_folder).with("hobo-provisioning", "foo", cookbooks_path) Hobo::Provisioning.new(vm) end end + + context "permissions on provisioning folder" do + should "chown the folder to the ssh user" do + ssh = mock("ssh") + ssh.expects(:exec!).with("sudo chown #{Hobo.config.ssh.username} #{Hobo.config.chef.provisioning_path}") + Hobo::SSH.expects(:execute).yields(ssh) + @prov.chown_provisioning_folder + end + end + + context "generating and uploading json" do + should "convert the JSON config to JSON" do + Hobo.config.chef.json.expects(:to_json).once.returns("foo") + @prov.setup_json + end + + should "upload a StringIO to dna.json" do + Hobo.config.chef.json.expects(:to_json).once.returns("foo") + StringIO.expects(:new).with("foo").returns("bar") + File.expects(:join).with(Hobo.config.chef.provisioning_path, "dna.json").once.returns("baz") + Hobo::SSH.expects(:upload!).with("bar", "baz").once + @prov.setup_json + end + end end diff --git a/test/hobo/ssh_test.rb b/test/hobo/ssh_test.rb index c73c38714..d6c551327 100644 --- a/test/hobo/ssh_test.rb +++ b/test/hobo/ssh_test.rb @@ -38,8 +38,12 @@ class SshTest < Test::Unit::TestCase end context "SCPing files to the remote host" do - should "use the SSH information to SCP files" do - Net::SCP.expects(:upload!).with(Hobo.config.ssh.host, Hobo.config.ssh.username, "foo", "bar", :password => Hobo.config.ssh.password) + should "use Hobo::SSH execute to setup an SCP connection and upload" do + scp = mock("scp") + ssh = mock("ssh") + scp.expects(:upload!).with("foo", "bar").once + Net::SCP.expects(:new).with(ssh).returns(scp).once + Hobo::SSH.expects(:execute).yields(ssh).once Hobo::SSH.upload!("foo", "bar") end end diff --git a/test/hobo/vm_test.rb b/test/hobo/vm_test.rb index bcdb963d7..bccc13ac5 100644 --- a/test/hobo/vm_test.rb +++ b/test/hobo/vm_test.rb @@ -260,6 +260,7 @@ class VMTest < Test::Unit::TestCase @vm.shared_folders.each do |name, hostpath, guestpath| ssh.expects(:exec!).with("sudo mkdir -p #{guestpath}").in_sequence(mount_seq) ssh.expects(:exec!).with("sudo mount -t vboxsf #{name} #{guestpath}").in_sequence(mount_seq) + ssh.expects(:exec!).with("sudo chown #{Hobo.config.ssh.username} #{guestpath}").in_sequence(mount_seq) end Hobo::SSH.expects(:execute).yields(ssh) diff --git a/test/test_helper.rb b/test/test_helper.rb index a2efcd59e..ea344c800 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -41,6 +41,9 @@ class Test::Unit::TestCase config.chef.cookbooks_path = "cookbooks" config.chef.provisioning_path = "/tmp/hobo-chef" + config.chef.json = { + :recipes => ["hobo_main"] + } end Hobo::Config.execute!