Class: VagrantPlugins::VCloud::Action::ReadState

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vcloud/action/read_state.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ReadState

Returns a new instance of ReadState.


5
6
7
8
# File 'lib/vagrant-vcloud/action/read_state.rb', line 5

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

Instance Method Details

#call(env) ⇒ Object


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

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

  @app.call env
end

#read_state(env) ⇒ Object


16
17
18
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
# File 'lib/vagrant-vcloud/action/read_state.rb', line 16

def read_state(env)
  # FIXME: this part needs some cleanup
  begin
    cfg = env[:machine].provider_config
    cnx = cfg.vcloud_cnx.driver
    vapp_id = env[:machine].get_vapp_id
    vm_name = cfg.name ? cfg.name.to_sym : env[:machine].name

    if env[:machine].id.nil?
      @logger.info("VM [#{vm_name}] is not created yet")
      return :not_created
    end

    vapp = cnx.get_vapp(vapp_id)
    vm_status = vapp[:vms_hash][vm_name][:status]

    if vm_status == 'stopped'
      @logger.info("VM [#{vm_name}] is stopped")
      return :stopped
    elsif vm_status == 'running'
      @logger.info("VM [#{vm_name}] is running")
      return :running
    elsif vm_status == 'paused'
      @logger.info("VM [#{vm_name}] is suspended")
      return :suspended
    end
  rescue Exception => e
    ### When bad credentials, we get here.
    @logger.debug("Couldn't Read VM State: #{e.message}")
    raise VagrantPlugins::VCloud::Errors::VCloudError,
          :message => e.message
  end
end