Skip to content

Commit

Permalink
Provisioning now generates a DNA JSON file and uploads it to the /tmp…
Browse files Browse the repository at this point in the history
… directory for use. Fixed some issues with SCPing.
  • Loading branch information
mitchellh committed Feb 10, 2010
1 parent b0574aa commit 8abb4e1
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ Hobofile
.hobo
.bundle
*.lock
cookbooks/*
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/default.rb
Expand Up @@ -15,4 +15,7 @@

config.chef.cookbooks_path = "cookbooks"
config.chef.provisioning_path = "/tmp/hobo-chef"
config.chef.json = {
:recipes => ["hobo_main"]
}
end
1 change: 1 addition & 0 deletions lib/hobo/config.rb
Expand Up @@ -71,6 +71,7 @@ def hd_location=(val)
class ChefConfig < Base
attr_accessor :cookbooks_path
attr_accessor :provisioning_path
attr_accessor :json
end

class Top < Base
Expand Down
19 changes: 18 additions & 1 deletion lib/hobo/provisioning.rb
Expand Up @@ -4,11 +4,28 @@ class Provisioning

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
7 changes: 4 additions & 3 deletions lib/hobo/ssh.rb
Expand Up @@ -23,9 +23,10 @@ def execute
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?
Expand Down
1 change: 1 addition & 0 deletions lib/hobo/vm.rb
Expand Up @@ -162,6 +162,7 @@ def mount_shared_folders
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
Expand Down
38 changes: 36 additions & 2 deletions 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
8 changes: 6 additions & 2 deletions test/hobo/ssh_test.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/hobo/vm_test.rb
Expand Up @@ -260,6 +260,7 @@ def start_expectation
@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)

Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
Expand Up @@ -41,6 +41,9 @@ def hobo_mock_config

config.chef.cookbooks_path = "cookbooks"
config.chef.provisioning_path = "/tmp/hobo-chef"
config.chef.json = {
:recipes => ["hobo_main"]
}
end

Hobo::Config.execute!
Expand Down

0 comments on commit 8abb4e1

Please sign in to comment.