Class: VagrantPlugins::SecuredCloud::Action::ReadSshInfo
- Inherits:
-
Object
- Object
- VagrantPlugins::SecuredCloud::Action::ReadSshInfo
- Defined in:
- lib/secured-cloud-vagrant/actions/read_ssh_info.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
- #call(env) ⇒ Object
-
#get_public_ip(virtualMachine) ⇒ Object
Returns a public IP which is assigned to the VM.
-
#get_username(virtualMachine) ⇒ Object
Returns the username to SSH to the VM.
-
#initialize(app, env) ⇒ ReadSshInfo
constructor
A new instance of ReadSshInfo.
- #read_ssh_info(env) ⇒ Object
Constructor Details
#initialize(app, env) ⇒ ReadSshInfo
Returns a new instance of ReadSshInfo.
12 13 14 15 16 |
# File 'lib/secured-cloud-vagrant/actions/read_ssh_info.rb', line 12 def initialize(app, env) @app = app @machine = env[:machine] @logger = Log4r::Logger.new('vagrant::secured_cloud::action::read_ssh_info') end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/secured-cloud-vagrant/actions/read_ssh_info.rb', line 18 def call(env) @logger.debug("Reading SSH info for VM #{env[:vm_name]} ...") env[:vm_conn_info] = read_ssh_info(env) @app.call(env) end |
#get_public_ip(virtualMachine) ⇒ Object
Returns a public IP which is assigned to the VM
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/secured-cloud-vagrant/actions/read_ssh_info.rb', line 97 def get_public_ip(virtualMachine) publicIp = nil # Process the IP mappings of the VM if !virtualMachine.get_ip_mappings.nil? virtualMachine.get_ip_mappings.each do |ipMapping| if !ipMapping.get_public_ips.nil? publicIp = ipMapping.get_public_ips[0] @logger.debug("Public IP to SSH to VM: #{publicIp}") break end end end return publicIp end |
#get_username(virtualMachine) ⇒ Object
Returns the username to SSH to the VM
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/secured-cloud-vagrant/actions/read_ssh_info.rb', line 120 def get_username(virtualMachine) username = nil # Get username of VM osTemplateUrl = virtualMachine.get_os_template_resource_url if !osTemplateUrl.nil? osTemplate = SecuredCloudRestClient.getOsTemplateDetails(@sc_connection, osTemplateUrl) if osTemplate.nil? @logger.error("OsTemplate '#{osTemplateUrl}' not found") return nil end username = osTemplate.get_administrator_username @logger.debug("Username to connect to VM: #{username}") end return username end |
#read_ssh_info(env) ⇒ Object
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 |
# File 'lib/secured-cloud-vagrant/actions/read_ssh_info.rb', line 26 def read_ssh_info(env) # If the VM ID is not in the environment return null if @machine.id.nil? || @machine.id.empty? @logger.error("VM has not yet been created") return nil end # Initialize the public IP, port and username to those defined in the Vagrantfile publicIp = @machine.config.ssh.host port = @machine.config.ssh.port username = @machine.config.ssh.username # If they are all defined return those values if(!publicIp.nil? && !port.nil? && !username.nil?) return { :host => publicIp, :port => port, :username => username, :private_key_path => nil } end 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, @machine.id) # If the VM is not found return null if virtualMachine.nil? @logger.error("VM '#{@machine.id}' not found") return nil end # Get a public IP assigned to the VM if it is not nil if publicIp.nil? publicIp = get_public_ip(virtualMachine) # If no public IP has been found yet return nil and show an error message if publicIp.nil? @logger.error("Cannot connect to a private VM") env[:ui].warn(I18n.t('secured_cloud_vagrant.warnings.no_public_ips', :vm_name => virtualMachine.get_name)) return nil end end # Get the username to connect to the VM if username.nil? username = get_username(virtualMachine) if(username.nil?) @logger.warn("No username could be determined to SSH to the VM.") end end # If the port is not defined set it to 22 port = 22 if port.nil? return { :host => publicIp, :port => port, :username => username, :private_key_path => nil } rescue Errno::ETIMEDOUT env[:ui].error(I18n.t('secured_cloud_vagrant.errors.request_timed_out', :request => "get the SSH information for VM '#{virtualMachine.get_name}'")) rescue Exception => e env[:ui].error(I18n.t("secured_cloud_vagrant.errors.generic_error", :error_message => e.)) end end |