Sha256: 11f3c1d174dc7181e9620f01ffd503acfb8c8e78b9071f93cf6b1fa83d64516a

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require 'log4r'

module VagrantPlugins
  module Packet
    module Action
      # This action reads the state of the machine and puts it in the
      # `:machine_state_id` key in the environment.
      class ReadState
        def initialize(app, _env)
          @app    = app
          @logger = Log4r::Logger.new('vagrant_packet::action::read_state')
        end

        def call(env)
          env[:machine_state_id] = read_state(env[:packet_compute], env[:machine])

          @app.call(env)
        end

        def read_state(packet, machine)
          return :not_created if machine.id.nil?

          # Find the machine
          server = packet.devices.get(machine.id)
          if server.nil? || %i[shutting-down terminated].include?(server.state.to_sym)
            # The machine can't be found
            @logger.info('Machine not found or terminated, assuming it got destroyed.')
            machine.id = nil
            return :not_created
          end

          # Return the state
          server.state.to_sym
        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/read_state.rb
vagrant-packet-0.1.5 lib/vagrant-packet/action/read_state.rb
vagrant-packet-0.1.4 lib/vagrant-packet/action/read_state.rb
vagrant-packet-0.1.3 lib/vagrant-packet/action/read_state.rb
vagrant-packet-0.1.2 lib/vagrant-packet/action/read_state.rb
vagrant-packet-0.1.1 lib/vagrant-packet/action/read_state.rb