Class: VagrantPlugins::SecuredCloud::Action::Create

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Create

Returns a new instance of Create.



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

def initialize(app, env)

	@app = app
	@machine = env[:machine]
	@logger = Log4r::Logger.new('vagrant::secured_cloud::action::create')
	
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/secured-cloud-vagrant/actions/create.rb', line 19

def call(env)

	@logger.debug("Creating VM with name #{@machine.provider_config.vm.name}")
	env[:ui].info(I18n.t("secured_cloud_vagrant.info.creating"))
	
	# 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)

	# Extract the parameters to pass to the createVM method
	vmName = @machine.provider_config.vm.name
	vmDescription = @machine.provider_config.vm.description
	vmStorageGB = @machine.provider_config.vm.storageGB
	vmMemoryMB = @machine.provider_config.vm.memoryMB
	vmVcpus = @machine.provider_config.vm.vcpus
	vmPowerStatus = "POWERED_ON"
	vmOsPassword = @machine.provider_config.vm.newOsPassword
	
	orgResource = @machine.provider_config.vm.orgResourceUrl
	nodeResource = @machine.provider_config.vm.nodeResourceUrl
	osTemplateResource = @machine.provider_config.vm.osTemplateUrl
	imageResource = @machine.provider_config.vm.imageResourceUrl
						
	begin
	
		# Call Rest Client to create a VM
		response = SecuredCloudRestClient::createVM(sc_connection, orgResource, nodeResource, vmName, vmDescription, vmStorageGB, vmMemoryMB, vmVcpus, vmPowerStatus, imageResource, osTemplateResource, vmOsPassword)
		
		#Monitor the transaction.
		if (response[0] == "202")

			#Task successful.
			taskResource = response[1]
			taskStatus = SecuredCloudRestClient::getTaskStatus(sc_connection, taskResource)
			
			@logger.info("Task Status:\n#{taskStatus.get_details()}")
			  
			while ((taskStatus.instance_variable_get(:@requestStateEnum) == nil) || (taskStatus.instance_variable_get(:@requestStateEnum) == "OPEN")) do
				sleep(20)
				taskStatus = SecuredCloudRestClient.getTaskStatus(sc_connection, taskResource)
            env[:ui].info(I18n.t("secured_cloud_vagrant.info.task_status", :percentage => taskStatus.get_percentage_completed, 
              :task_desc => taskStatus.get_latest_task_description))
				@logger.info("Task Status:\n#{taskStatus.get_details()}")
			end
			 
			if(taskStatus.get_result.nil?) 
					
					#Task unsuccessful.
            @logger.debug("VM Creation failed with the following error:\n#{taskStatus.get_error_code}: #{taskStatus.get_error_message}")
          
            error_code = (taskStatus.get_error_code.nil?) ? "" : "#{taskStatus.get_error_code} "
            error_message = (taskStatus.get_error_message.nil?) ? I18n.t('secured_cloud_vagrant.errors.internal_server_error') : 
              error_code + taskStatus.get_error_message
                
            env[:ui].error(I18n.t("secured_cloud_vagrant.errors.creating_vm", :vm_name => @machine.provider_config.vm.name,
               :error_message => error_message))
					
				else
				  
				  # Put the resource URL of the newly created VM in the environment
            env[:machine].id = taskStatus.get_result 
            env[:vm_name] = vmName
            
            @logger.debug("VM #{@machine.provider_config.vm.name} has been created with resource URL: #{env[:machine].id}")
            env[:ui].info(I18n.t("secured_cloud_vagrant.info.success.create_vm", :vm_name => @machine.provider_config.vm.name, 
              :resource_url => env[:machine].id))
            
				end
			
			 
		else
			
			#Task unsuccessful.
          @logger.debug("VM Creation failed with the following error:\n#{response}")
          
			error_message = (response[2].nil?) ? I18n.t('secured_cloud_vagrant.errors.internal_server_error') : response[2]
			env[:ui].error(I18n.t("secured_cloud_vagrant.errors.creating_vm", :vm_name => @machine.provider_config.vm.name, 
				:error_message => error_message))
		end
		
	rescue Errno::ETIMEDOUT
		env[:ui].error(I18n.t("secured_cloud_vagrant.errors.request_timed_out", :request => "create VM '#{vmName}'"))
      rescue Exception => e
        env[:ui].error(I18n.t("secured_cloud_vagrant.errors.generic_error", :error_message => e.message))
	end

	@app.call(env)
end