Class: VagrantPlugins::ProviderZone::Action::Shutdown

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-zones/action/shutdown.rb

Overview

This is used to shutdown the zone

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ Shutdown

Returns a new instance of Shutdown.



13
14
15
16
# File 'lib/vagrant-zones/action/shutdown.rb', line 13

def initialize(app, _env)
  @logger = Log4r::Logger.new('vagrant_zones::action::shutdown')
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant-zones/action/shutdown.rb', line 18

def call(env)
  @machine = env[:machine]
  @driver  = @machine.provider.driver
  ui = env[:ui]
  ui.info(I18n.t('vagrant_zones.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
      state_id = @driver.state(@machine)
      ui.info(I18n.t('vagrant_zones.graceful_shutdown_complete')) unless state_id == :running
      sleep 1 if state_id == :running
      break unless state_id == :running
      break if env[:interrupted]
    end
  end
  @driver.halt(env[:ui])
  @app.call(env)
end