Class: VagrantPlugins::SecuredCloud::Action::CheckState

Inherits:
Object
  • Object
show all
Defined in:
lib/secured-cloud-vagrant/actions/check_state.rb

Overview

This can be used with “Call” built-in to check if the machine is created and branch in the middleware.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CheckState

Returns a new instance of CheckState.



12
13
14
15
16
# File 'lib/secured-cloud-vagrant/actions/check_state.rb', line 12

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

Instance Method Details

#call(env) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
# File 'lib/secured-cloud-vagrant/actions/check_state.rb', line 18

def call(env)

	@logger.debug("Checking VM state ...")
	
	vm_resource_url = @machine.id
	
	if vm_resource_url.nil? || vm_resource_url.empty?
		env[:machine_state] = :not_created
	else
	
		begin
		  
		  # Create a Secured Cloud Connection instance to connect tot he SecuredCloud API
          authInfo = @machine.provider_config.auth
          sc_connection = SecuredCloudConnection.new(authInfo.url, authInfo.applicationKey, authInfo.sharedSecret)
		
			# Get the VM details
			virtualMachine = SecuredCloudRestClient.getVMDetails(sc_connection, vm_resource_url)
				
			# Set the VM name
			env[:vm_name] = virtualMachine.get_name
			@logger.debug("VM Name: '#{env[:vm_name]}'")
				
			# Get the VM power status
			if (virtualMachine.get_power_status == "POWERED_OFF")
				env[:machine_state] = :stopped
			elsif (virtualMachine.get_power_status == "POWERED_ON")
				env[:machine_state] = :active
			end
			
			@logger.debug("State for VM #{vm_resource_url} is #{env[:machine_state]}")
			
		rescue Errno::ETIMEDOUT
			env[:ui].error(I18n.t("secured_cloud_vagrant.errors.request_timed_out", :request => "get the VM details"))
		rescue Exception => e
		  env[:ui].error(I18n.t("secured_cloud_vagrant.errors.generic_error", :error_message => e.message))
		end
	end				
	
	@app.call(env)
end