Class: VagrantPlugins::Kubevirt::Action::WaitForState

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-kubevirt/action/wait_for_state.rb

Overview

This action will wait for a machine to reach a specific state or quit by timeout

Instance Method Summary collapse

Constructor Details

#initialize(app, env, state, timeout) ⇒ WaitForState

env will be false in case of timeout.

Parameters:

  • state (Symbol)

    Target machine state.

  • timeout (Number)

    Timeout in seconds.



12
13
14
15
16
17
# File 'lib/vagrant-kubevirt/action/wait_for_state.rb', line 12

def initialize(app, env, state, timeout)
  @app     = app
  @logger  = Log4r::Logger.new("vagrant_kubevirt::action::wait_for_state")
  @state   = state
  @timeout = timeout
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-kubevirt/action/wait_for_state.rb', line 20

def call(env)
  env[:result] = true
  vm_name = env[:domain_name]
  kubevirt = env[:kubevirt_compute]


  if @state.to_s == 'running'
    watch = kubevirt.watch_vminstances
    kind = 'VirtualMachineInstance'
    env[:ui].info(I18n.t("vagrant_kubevirt.wait_for_boot"))
  else
    watch = kubevirt.watch_vms
    kind = 'VirtualMachine'
    env[:ui].info(I18n.t("vagrant_kubevirt.wait_for_state", :state => @state))
  end

  vm = kubevirt.vms.get(vm_name)

  if vm.status == @state
    env[:ui].info(I18n.t("vagrant_kubevirt.already_status", :status => @state))
  else
    begin
      Timeout.timeout(@timeout) do
        watch.each do |notice|
          break if notice.kind == kind && notice.name == vm_name && !notice.status.nil? && notice.status.downcase == @state.to_s
        end
      end
    rescue Timeout::Error
      raise Errors::VMReadyTimeout, timeout: @timeout
    end
  end
  watch.finish

  # wait for sshd to be up
  if @state.to_s == 'running'
    wait = env[:machine].config.vm.boot_timeout
    env[:machine].communicate.wait_for_ready(wait)
  end

  @app.call(env)
end