Class: VagrantPlugins::TiktalikVagrant::Action::ReadState

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-tiktalik/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

Constructor Details

#initialize(app, env) ⇒ ReadState

Returns a new instance of ReadState.



13
14
15
16
17
# File 'lib/vagrant-tiktalik/action/read_state.rb', line 13

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_tiktalik::action::read_state")
  @config = env[:machine].provider_config
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vagrant-tiktalik/action/read_state.rb', line 19

def call(env)
  t = Tiktalik
  t.api_key = @config.api_key
  t.api_secret_key = @config.api_secret
  t.ca_file = @config.ca_file

  env[:machine_state_id] = :not_created

  hostname = env[:machine].config.vm.hostname || env[:machine].name

  i = Tiktalik::Computing::Instance
  instances = i.all
  instances.each do |instance|
    if instance.hostname == hostname.to_s
      env[:machine].id = instance.uuid if env[:machine].id.nil?
      case instance.state
        when 12
          if instance.running
            env[:machine_state_id] = :running
          else
            env[:machine_state_id] = :stopped
          end
        else
          env[:machine_state_id] = :pending
      end
      break
    end
  end

  @app.call(env)
end