Class: VagrantPlugins::Rimu::Actions::ReadState

Inherits:
AbstractAction show all
Defined in:
lib/vagrant-rimu/actions/read_state.rb

Instance Method Summary collapse

Methods inherited from AbstractAction

#call

Constructor Details

#initialize(app, env) ⇒ ReadState

Returns a new instance of ReadState.



9
10
11
12
13
# File 'lib/vagrant-rimu/actions/read_state.rb', line 9

def initialize(app, env)
  @app = app
  @machine = env[:machine]
  @logger = Log4r::Logger.new('vagrant_rimu::action::read_state')
end

Instance Method Details

#execute(env) ⇒ Object



15
16
17
18
19
20
# File 'lib/vagrant-rimu/actions/read_state.rb', line 15

def execute(env)
  client = env[:rimu_api]
  env[:machine_state] = read_state(client, @machine)
  @logger.info I18n.t('vagrant_rimu.states.current_state', {:state => env[:machine_state]})
  @app.call(env)
end

#read_state(client, machine) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vagrant-rimu/actions/read_state.rb', line 22

def read_state(client, machine)
  return :not_created if machine.id.nil?
  begin
    server = client.servers.status(machine.id.to_i)
  rescue ::Rimu::RimuAPI::RimuRequestError, ::Rimu::RimuAPI::RimuResponseError => e
    @logger.debug(e)
    return :destroyed
  end
  return :not_created if server.nil?
  status = server.running_state
  return :not_created if status.nil?
  states = {
    'RUNNING' => :active,
    'NOTRUNNING' => :off,
    'RESTARTING' => :shutting_down,
    'POWERCYCLING' => :shutting_down,
    'DESTROYED' => :destroyed
  }
  states[status.to_s]
end