Sha256: 7dc92cc4c8e1ed07cf0aa07efae0b5635eb82e31c32a64f5f880029e8ff08931

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'log4r'
require 'timeout'

module VagrantPlugins
  module Packet
    module Action
      # This action will wait for a machine to reach a specific state or quit by timeout
      class WaitForState
        # env[:result] will be false in case of timeout.
        # @param [Symbol] state Target machine state.
        # @param [Number] timeout Timeout in seconds.
        def initialize(app, _env, state, timeout)
          @app     = app
          @logger  = Log4r::Logger.new('vagrant_packet::action::wait_for_state')
          @state   = state
          @timeout = timeout
        end

        def call(env)
          env[:result] = true
          if env[:machine].state.id == @state
            @logger.info(I18n.t('vagrant_packet.already_status', status: @state))
          else
            @logger.info("Waiting for machine to reach state #{@state}")
            begin
              Timeout.timeout(@timeout) do
                sleep 2 until env[:machine].state.id == @state
              end
            rescue Timeout::Error
              env[:result] = false # couldn't reach state in time
            end
          end

          @app.call(env)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vagrant-packet-0.1.6 lib/vagrant-packet/action/wait_for_state.rb
vagrant-packet-0.1.5 lib/vagrant-packet/action/wait_for_state.rb
vagrant-packet-0.1.4 lib/vagrant-packet/action/wait_for_state.rb
vagrant-packet-0.1.3 lib/vagrant-packet/action/wait_for_state.rb
vagrant-packet-0.1.2 lib/vagrant-packet/action/wait_for_state.rb
vagrant-packet-0.1.1 lib/vagrant-packet/action/wait_for_state.rb