Class: VagrantPlugins::SoftLayer::Action::ReadState

Inherits:
Object
  • Object
show all
Includes:
Util::Warden
Defined in:
lib/vagrant-softlayer/action/read_state.rb

Overview

This action reads the state of the machine and puts it in the ‘:machine_state_id` key in the environment.

Instance Method Summary collapse

Methods included from Util::Warden

#sl_warden

Constructor Details

#initialize(app, env) ⇒ ReadState

Returns a new instance of ReadState.



11
12
13
14
# File 'lib/vagrant-softlayer/action/read_state.rb', line 11

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

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
# File 'lib/vagrant-softlayer/action/read_state.rb', line 16

def call(env)
  env[:machine_state_id] = read_state(env)

  # Carry on
  @app.call(env)
end

#read_state(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant-softlayer/action/read_state.rb', line 23

def read_state(env)
  return :not_created unless env[:sl_machine]

  wipe_id = lambda do
    @logger.info("Machine not found, assuming it got destroyed.")
    env[:machine].id = nil
    next :not_created
  end

  state = sl_warden(wipe_id) { env[:sl_machine].getPowerState }
  if state && ["Halted", "Paused", "Running"].include?(state["name"])
    return state["name"].downcase.to_sym
  else
    return :unknown
  end
end