Provisioning now generates a DNA JSON file and uploads it to the /tmp directory for use. Fixed some issues with SCPing.

This commit is contained in:
Mitchell Hashimoto 2010-02-09 18:16:19 -08:00
parent b0574aa95c
commit 8abb4e1d6e
11 changed files with 76 additions and 8 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ Hobofile
.hobo
.bundle
*.lock
cookbooks/*

View File

@ -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

View File

@ -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

View File

@ -71,6 +71,7 @@ module Hobo
class ChefConfig < Base
attr_accessor :cookbooks_path
attr_accessor :provisioning_path
attr_accessor :json
end
class Top < Base

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -1,12 +1,46 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class ProvisioningTest < Test::Unit::TestCase
context "initializing" do
should "setup shared folder on VM" do
File.expects(:expand_path).with(Hobo.config.chef.cookbooks_path, Hobo::Env.root_path).returns("foo")
setup do
# Stub upload so nothing happens
Hobo::SSH.stubs(:upload!)
vm = mock("vm")
vm.expects(:share_folder).with("hobo-provisioning", "foo", Hobo.config.chef.provisioning_path)
vm.stubs(:share_folder)
@prov = Hobo::Provisioning.new(vm)
end
context "initializing" 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", 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

View File

@ -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

View File

@ -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)

View File

@ -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!