Class: Vagrant::Action::Builtin::WaitForCommunicator

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/builtin/wait_for_communicator.rb

Overview

This waits for the communicator to be ready for a set amount of time.

Instance Method Summary collapse

Constructor Details

#initialize(app, env, states = nil) ⇒ WaitForCommunicator

Returns a new instance of WaitForCommunicator.



7
8
9
10
# File 'lib/vagrant/action/builtin/wait_for_communicator.rb', line 7

def initialize(app, env, states=nil)
  @app    = app
  @states = states
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vagrant/action/builtin/wait_for_communicator.rb', line 12

def call(env)
  # Wait for ready in a thread so that we can continually check
  # for interrupts.
  ready_thr = Thread.new do
    Thread.current[:result] = env[:machine].communicate.wait_for_ready(
      env[:machine].config.vm.boot_timeout)
  end

  # Start a thread that verifies the VM stays in a good state.
  states_thr = Thread.new do
    Thread.current[:result] = true

    # If we aren't caring about states, just basically put this
    # thread to sleep because it'll get killed later.
    if !@states
      while true
        sleep 300
      end

      next
    end

    # Otherwise, periodically verify the VM isn't in a bad state.
    while true
      state = env[:machine].provider.state.id

      # Used to report invalid states
      Thread.current[:last_known_state] = state

      # Check if we have the proper state so we can break out
      if !@states.include?(state)
        Thread.current[:result] = false
        break
      end

      # Sleep a bit so we don't hit 100% CPU constantly.
      sleep 1
    end
  end

  # Wait for a result or an interrupt
  env[:ui].info I18n.t("vagrant.boot_waiting")
  while ready_thr.alive? && states_thr.alive?
    return if env[:interrupted]
  end

  # If it went into a bad state, then raise an error
  if !states_thr[:result]
    raise Errors::VMBootBadState,
      valid: @states.join(", "),
      invalid: states_thr[:last_known_state]
  end

  # If it didn't boot, raise an error
  if !ready_thr[:result]
    raise Errors::VMBootTimeout
  end

  env[:ui].info I18n.t("vagrant.boot_completed")

  # Make sure our threads are all killed
  ready_thr.kill
  states_thr.kill

  @app.call(env)
ensure
  ready_thr.kill
  states_thr.kill
end