Class: VagrantPlugins::Azure::Action::ReadState

Inherits:
Object
  • Object
show all
Includes:
Util::MachineIdHelper, Util::VMStatusTranslator
Defined in:
lib/vagrant-azure/action/read_state.rb

Constant Summary

Constants included from Util::VMStatusTranslator

Util::VMStatusTranslator::POWER_STATES, Util::VMStatusTranslator::PROVISIONING_STATES

Instance Method Summary collapse

Methods included from Util::MachineIdHelper

#parse_machine_id, #serialize_machine_id

Methods included from Util::VMStatusTranslator

#built?, #power_state, #running?, #stopped?, #stopping?, #tearing_down?, #vm_status_to_state

Constructor Details

#initialize(app, env) ⇒ ReadState

Returns a new instance of ReadState.



15
16
17
18
# File 'lib/vagrant-azure/action/read_state.rb', line 15

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new('vagrant_azure::action::read_state')
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
# File 'lib/vagrant-azure/action/read_state.rb', line 20

def call(env)
  env[:machine_state_id] = read_state(env[:azure_arm_service], env[:machine])
  @app.call(env)
end

#read_state(azure, machine) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-azure/action/read_state.rb', line 25

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

  # Find the machine
  parsed = parse_machine_id(machine.id)
  vm = nil
  begin
    vm = azure.compute.virtual_machines.get(parsed[:group], parsed[:name], 'instanceView')
  rescue MsRestAzure::AzureOperationError => ex
    if vm.nil? || tearing_down?(vm.instance_view.statuses)
      # The machine can't be found
      @logger.info('Machine not found or terminated, assuming it got destroyed.')
      machine.id = nil
      return :not_created
    end
  end

  # Return the state
  power_state(vm.instance_view.statuses)
end