From b1ced46d7cd357c8c48add1d1184b7a6e48506a4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 6 Aug 2012 10:29:25 -0700 Subject: [PATCH] SSHExec raises proper exception if SSH is not yet ready --- lib/vagrant/action/builtin/ssh_exec.rb | 6 ++++-- lib/vagrant/errors.rb | 4 ++++ templates/locales/en.yml | 6 ++++++ test/unit/vagrant/action/builtin/ssh_exec_test.rb | 9 +++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/action/builtin/ssh_exec.rb b/lib/vagrant/action/builtin/ssh_exec.rb index 300a40b25..372707b07 100644 --- a/lib/vagrant/action/builtin/ssh_exec.rb +++ b/lib/vagrant/action/builtin/ssh_exec.rb @@ -21,8 +21,10 @@ module Vagrant def call(env) # Grab the SSH info from the machine info = env[:machine].ssh_info - # XXX: Raise an exception if info is nil, since that means that - # SSH is not ready. + + # If the result is nil, then the machine is telling us that it is + # not yet ready for SSH, so we raise this exception. + raise Errors::SSHNotReady if info.nil? # Check the SSH key permissions SSH.check_key_permissions(info[:private_key_path]) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 6097de946..9d2597d36 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -356,6 +356,10 @@ module Vagrant error_key(:ssh_key_type_not_supported) end + class SSHNotReady < VagrantError + error_key(:ssh_not_ready) + end + class SSHPortNotDetected < VagrantError status_code(50) error_key(:ssh_port_not_detected) diff --git a/templates/locales/en.yml b/templates/locales/en.yml index de3d55e31..eb9b04165 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -156,6 +156,12 @@ en: The private key you're attempting to use with this Vagrant box uses an unsupported encryption type. The SSH library Vagrant uses does not support this key type. Please use `ssh-rsa` or `ssh-dss` instead. + ssh_not_ready: |- + The provider for this Vagrant-managed machine is reporting that it + is not yet ready for SSH. Depending on your provider this can carry + different meanings. Make sure your machine is created and running and + try again. If you continue to get this error message, please view the + documentation for the provider you're using. ssh_port_not_detected: |- Vagrant couldn't determine the SSH port for your VM! Vagrant attempts to automatically find a forwarded port that matches your `config.ssh.guest_port` diff --git a/test/unit/vagrant/action/builtin/ssh_exec_test.rb b/test/unit/vagrant/action/builtin/ssh_exec_test.rb index f057cc393..a6ea69f47 100644 --- a/test/unit/vagrant/action/builtin/ssh_exec_test.rb +++ b/test/unit/vagrant/action/builtin/ssh_exec_test.rb @@ -20,6 +20,15 @@ describe Vagrant::Action::Builtin::SSHExec do ssh_klass.stub(:exec) end + it "should raise an exception if SSH is not ready" do + not_ready_machine = double("machine") + not_ready_machine.stub(:ssh_info).and_return(nil) + + env[:machine] = not_ready_machine + expect { described_class.new(app, env).call(env) }. + to raise_error(Vagrant::Errors::SSHNotReady) + end + it "should check key permissions then exec" do ssh_klass.should_receive(:check_key_permissions). with(machine_ssh_info[:private_key_path]).