Sha256: 0b6e86d01ba343edab1f0396e50716338b5c4d9556957643d43626b9fc469334

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

module VagrantPlugins
  module VCloudAir
    module Action
      class ReadState
        def initialize(app, env)
          @app = app
          @logger = Log4r::Logger.new('vagrant_vcloudair::action::read_state')
        end

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

          @app.call env
        end

        def read_state(env)
          # FIXME: this part needs some cleanup
          begin
            cfg = env[:machine].provider_config
            cnx = cfg.vcloudair_cnx.driver
            vapp_id = env[:machine].get_vapp_id
            vm_name = 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 Errors::VCloudAirGenericError, :message => e.message
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vagrant-vcloudair-0.5.3 lib/vagrant-vcloudair/action/read_state.rb
vagrant-vcloudair-0.5.2 lib/vagrant-vcloudair/action/read_state.rb
vagrant-vcloudair-0.5.1 lib/vagrant-vcloudair/action/read_state.rb
vagrant-vcloudair-0.5.0 lib/vagrant-vcloudair/action/read_state.rb