Chef runs (solo and client) check exit status and error if anything occurs.

This commit is contained in:
Mitchell Hashimoto 2010-05-28 23:18:32 -07:00
parent 8ea9816552
commit 2af2a81eb8
6 changed files with 70 additions and 13 deletions

View File

@ -49,12 +49,18 @@ module Vagrant
end
def run_chef_client
command = "cd #{env.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json"
logger.info "Running chef-client..."
vm.ssh.execute do |ssh|
ssh.exec!("cd #{env.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json") do |channel, data, stream|
ssh.exec!(command) do |channel, type, data|
# TODO: Very verbose. It would be easier to save the data and only show it during
# an error, or when verbosity level is set high
logger.info("#{stream}: #{data}")
if type == :exit_status
ssh.check_exit_status(data, command)
else
logger.info("#{data}: #{type}")
end
end
end
end

View File

@ -35,12 +35,15 @@ module Vagrant
end
def run_chef_solo
command = "cd #{env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json"
logger.info "Running chef-solo..."
vm.ssh.execute do |ssh|
ssh.exec!("cd #{env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json") do |channel, data, stream|
ssh.exec!(command) do |channel, type, data|
# TODO: Very verbose. It would be easier to save the data and only show it during
# an error, or when verbosity level is set high
logger.info("#{stream}: #{data}")
ssh.check_exit_status(data, command) if type == :exit_status
logger.info("#{data}: #{type}") if type != :exit_status
end
end
end

View File

@ -150,9 +150,7 @@ module Vagrant
# implementation also reports `:exit_status` to the block if given.
def exec!(command, &block)
block ||= Proc.new do |ch, type, data|
if type == :exit_status && data != 0
raise ActionException.new(:ssh_bad_exit_status, :command => command)
end
check_exit_status(data, command) if type == :exit_status
ch[:result] ||= ""
ch[:result] << data if [:stdout, :stderr].include?(type)
@ -182,6 +180,14 @@ module Vagrant
metach.wait
metach[:result]
end
# Checks for an erroroneous exit status and raises an exception
# if so.
def check_exit_status(exit_status, command)
if exit_status != 0
raise Actions::ActionException.new(:ssh_bad_exit_status, :command => command)
end
end
end
end
end

View File

@ -156,10 +156,19 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
end
context "running chef client" do
setup do
@ssh = mock("ssh")
@vm.ssh.stubs(:execute).yields(@ssh)
end
should "cd into the provisioning directory and run chef client" do
ssh = mock("ssh")
ssh.expects(:exec!).with("cd #{@env.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json").once
@vm.ssh.expects(:execute).yields(ssh)
@ssh.expects(:exec!).with("cd #{@env.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json").once
@action.run_chef_client
end
should "check the exit status if that is given" do
@ssh.stubs(:exec!).yields(nil, :exit_status, :foo)
@ssh.expects(:check_exit_status).with(:foo, anything).once
@action.run_chef_client
end
end

View File

@ -164,10 +164,19 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
end
context "running chef solo" do
setup do
@ssh = mock("ssh")
@vm.ssh.stubs(:execute).yields(@ssh)
end
should "cd into the provisioning directory and run chef solo" do
ssh = mock("ssh")
ssh.expects(:exec!).with("cd #{@env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json").once
@vm.ssh.expects(:execute).yields(ssh)
@ssh.expects(:exec!).with("cd #{@env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json").once
@action.run_chef_solo
end
should "check the exit status if that is given" do
@ssh.stubs(:exec!).yields(nil, :exit_status, :foo)
@ssh.expects(:check_exit_status).with(:foo, anything).once
@action.run_chef_solo
end
end

View File

@ -0,0 +1,24 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class SshTest < Test::Unit::TestCase
setup do
@session = mock("session")
@klass = Vagrant::SSH::Session
@instance = @klass.new(@session)
end
context "checking exit status" do
should "raise an ActionException if its non-zero" do
assert_raises(Vagrant::Actions::ActionException) {
@instance.check_exit_status(1, "foo")
}
end
should "raise nothing if its zero" do
assert_nothing_raised {
@instance.check_exit_status(0, "foo")
}
end
end
end