From 300f9a756912859909a352cc15d2f3f12c7dbdba Mon Sep 17 00:00:00 2001 From: Charlie Sharpsteen Date: Wed, 13 Apr 2016 05:56:13 -0700 Subject: [PATCH] Switch to WaitForCommunicator The OpenStack provider's action_up used a custom WaitForServerToBeAccessible action to poll instances via SSH in order to determine when the VM is up and running. One large shortcoming of this custom implementation is that it does not support communicators other than SSH, notably WinRM. This patch replaces the WaitForServerToBeAccessible with the built-in WaitForCommunicator action which has been part of Vagrant since v1.3 and is able to handle multiple communicators. Fixes ggiamarchi/vagrant-openstack-provider#264. Closes ggiamarchi/vagrant-openstack-provider#227. --- .../lib/vagrant-openstack-provider/action.rb | 3 +- .../action/wait_accessible.rb | 61 ----------------- .../action/wait_accessible_spec.rb | 67 ------------------- 3 files changed, 1 insertion(+), 130 deletions(-) delete mode 100644 source/lib/vagrant-openstack-provider/action/wait_accessible.rb delete mode 100644 source/spec/vagrant-openstack-provider/action/wait_accessible_spec.rb diff --git a/source/lib/vagrant-openstack-provider/action.rb b/source/lib/vagrant-openstack-provider/action.rb index f4d20bf..b675e05 100644 --- a/source/lib/vagrant-openstack-provider/action.rb +++ b/source/lib/vagrant-openstack-provider/action.rb @@ -105,7 +105,7 @@ def self.action_up b2.use CreateStack b2.use CreateServer b2.use Message, I18n.t('vagrant_openstack.ssh_disabled_provisioning') if ssh_disabled - b2.use WaitForServerToBeAccessible unless ssh_disabled + b2.use WaitForCommunicator unless ssh_disabled when :shutoff b2.use StartServer when :suspended @@ -216,7 +216,6 @@ def self.action_reload autoload :ProvisionWrapper, action_root.join('provision') autoload :WaitForServerToStop, action_root.join('wait_stop') autoload :WaitForServerToBeActive, action_root.join('wait_active') - autoload :WaitForServerToBeAccessible, action_root.join('wait_accessible') private diff --git a/source/lib/vagrant-openstack-provider/action/wait_accessible.rb b/source/lib/vagrant-openstack-provider/action/wait_accessible.rb deleted file mode 100644 index 074c714..0000000 --- a/source/lib/vagrant-openstack-provider/action/wait_accessible.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'log4r' - -module VagrantPlugins - module Openstack - module Action - class WaitForServerToBeAccessible < AbstractAction - def initialize(app, env, resolver = nil, ssh = nil) - @logger = Log4r::Logger.new('vagrant_openstack::action::wait_accessible') - @app = app - @ssh = ssh || Vagrant::Action::Builtin::SSHRun.new(app, env) - @resolver = resolver || VagrantPlugins::Openstack::ConfigResolver.new - end - - def execute(env) - waiting_for_server_to_be_reachable(env) - @logger.info 'The server is ready' - env[:ui].info(I18n.t('vagrant_openstack.ready')) - @app.call(env) - end - - private - - def waiting_for_server_to_be_reachable(env) - return if env[:interrupted] - ssh_timeout = env[:machine].provider_config.ssh_timeout - return if server_is_reachable?(env, ssh_timeout) - env[:ui].error(I18n.t('vagrant_openstack.timeout')) - fail Errors::SshUnavailable, host: @resolver.resolve_floating_ip(env), timeout: ssh_timeout - end - - def server_is_reachable?(env, timeout) - start_time = Time.now - current_time = start_time - nb_retry = 0 - - while (current_time - start_time) <= timeout - @logger.debug "Checking if SSH port is open... Attempt number #{nb_retry}" - if nb_retry % 5 == 0 - @logger.info 'Waiting for SSH to become available...' - env[:ui].info(I18n.t('vagrant_openstack.waiting_for_ssh')) - end - - env[:ssh_run_command] = 'exit 0' - env[:ssh_opts] = { - extra_args: ['-o', 'BatchMode=yes'] - } - @ssh.call(env) - return true if env[:ssh_run_exit_status] == 0 - - @logger.debug 'SSH not yet available... new retry in in 1 second' - nb_retry += 1 - sleep 1 - current_time = Time.now - end - - false - end - end - end - end -end diff --git a/source/spec/vagrant-openstack-provider/action/wait_accessible_spec.rb b/source/spec/vagrant-openstack-provider/action/wait_accessible_spec.rb deleted file mode 100644 index fe5a418..0000000 --- a/source/spec/vagrant-openstack-provider/action/wait_accessible_spec.rb +++ /dev/null @@ -1,67 +0,0 @@ -require 'vagrant-openstack-provider/spec_helper' - -describe VagrantPlugins::Openstack::Action::WaitForServerToBeAccessible do - let(:config) do - double('config') - end - - let(:env) do - {}.tap do |env| - env[:ui] = double('ui') - env[:ui].stub(:info).with(anything) - env[:ui].stub(:error).with(anything) - env[:machine] = double('machine').tap do |m| - m.stub(:provider_config) { config } - end - end - end - - let(:resolver) do - double('resolver').tap do |r| - r.stub(:resolve_floating_ip).with(anything) { '1.2.3.4' } - end - end - - let(:ssh) do - double('shh') - end - - let(:app) do - double('app').tap do |app| - app.stub(:call).with(anything) - end - end - - class SSHMock - def initialize(*exit_codes) - @times = 0 - @exit_codes = exit_codes - end - - def call(env) - env[:ssh_run_exit_status] = @exit_codes[@times] - @times += 1 - end - end - - describe 'call' do - context 'when server is not yet reachable' do - it 'retry until server is reachable' do - config.stub(:ssh_timeout) { 2 } - expect(app).to receive(:call) - - @action = WaitForServerToBeAccessible.new(app, nil, resolver, SSHMock.new(1, 0)) - @action.call(env) - end - end - context 'when server is not yet reachable after timeout' do - it 'raise an error' do - config.stub(:ssh_timeout) { 1 } - expect(app).should_not_receive(:call) - - @action = WaitForServerToBeAccessible.new(app, nil, resolver, SSHMock.new(1, 1)) - expect { @action.call(env) }.to raise_error Errors::SshUnavailable - end - end - end -end