Sha256: 793af1c78c7c2a82252e93c12b7cfed6c4e0bb515c9ee9220752a2f1eb828403

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require 'log4r'
require 'vagrant-local/util/timer'
require 'vagrant/util/retryable'

module VagrantPlugins
  module ProviderLocal
    module Action
      # This is used to shutdown the instance
      class Shutdown
        include Vagrant::Util::Retryable
        def initialize(app, _env)
          @logger = Log4r::Logger.new('vagrant_local::action::shutdown')
          @app = app
        end

        def call(env)
          @machine = env[:machine]
          @driver  = @machine.provider.driver
          ui = env[:ui]
          ui.info(I18n.t('vagrant_local.graceful_shutdown_started'))
          @driver.control(ui, 'shutdown')
          env[:metrics] ||= {}
          env[:metrics]['instance_ssh_time'] = Util::Timer.time do
            retryable(on: Errors::TimeoutError, tries: 300) do
              # If we're interrupted don't worry about waiting
              break if env[:interrupted]
              break unless env[:machine].communicate.ready?
            end
          end
          env[:metrics]['instance_ssh_time'] = Util::Timer.time do
            300.times do
              ui.info(I18n.t('vagrant_local.graceful_shutdown_complete')) unless @driver.state == :running
              sleep 1 if @driver.state == :running
              break unless @driver.state == :running
              break if env[:interrupted]
            end
          end
          @driver.halt(env[:ui])
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vagrant-local-0.0.3 lib/vagrant-local/action/shutdown.rb
vagrant-local-0.0.2 lib/vagrant-local/action/shutdown.rb