diff options
author | elijah <elijah@riseup.net> | 2016-09-15 22:21:38 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2016-09-15 22:21:38 -0700 |
commit | b13cbe4730a986a3b60c4c70ce2b5f16da8a4feb (patch) | |
tree | 1b166072fa972933c601dad6c1a45f7fb6ff0b25 /lib/leap_cli/cloud/cloud.rb | |
parent | 5cf0ff60c2038773bd9c5546d3930f5434670578 (diff) |
leap vm: grab ssh host key when adding a new vm
Diffstat (limited to 'lib/leap_cli/cloud/cloud.rb')
-rw-r--r-- | lib/leap_cli/cloud/cloud.rb | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/lib/leap_cli/cloud/cloud.rb b/lib/leap_cli/cloud/cloud.rb index 753041f6..2c06e7ed 100644 --- a/lib/leap_cli/cloud/cloud.rb +++ b/lib/leap_cli/cloud/cloud.rb @@ -155,7 +155,6 @@ module LeapCli "ip_address" => server.public_ip_address, "vm"=> {"id"=>server.id} }) - log "done", :color => :green, :style => :bold end # @@ -188,7 +187,7 @@ module LeapCli require 'leap_cli/ssh' key_pair, local_key = match_ssh_key(:user_only => true) if key_pair - log :using, "SSH key #{local_key.filename}" do + log :using, "user SSH key #{local_key.filename}" do log 'AWS MD5 fingerprint: ' + local_key.fingerprint(:digest => :md5, :type => :der, :encoding => :hex) log 'SSH MD5 fingerprint: ' + local_key.fingerprint(:digest => :md5, :type => :ssh, :encoding => :hex) log 'SSH SHA256 fingerprint: ' + local_key.fingerprint(:digest => :sha256, :type => :ssh, :encoding => :base64) @@ -232,6 +231,56 @@ module LeapCli end end + def wait_for_ssh_host_key(server) + require 'leap_cli/ssh' + return nil if Fog.mock? + tries = 0 + host_key = nil + cloud = self + server.wait_for { + if tries > 0 + LeapCli.log :waiting, "for SSH host key..." + elsif tries > 20 + return nil + end + tries += 1 + ssh_host_keys = cloud.ssh_host_keys(server) + if ssh_host_keys.nil? + false + else + host_key = SSH::Key.pick_best_key(ssh_host_keys) + true + end + } + return host_key + end + + # + # checks the console of the server for the ssh host keys + # + # returns nil if they cannot be found. + # + def ssh_host_keys(server) + require 'leap_cli/ssh' + return nil if Fog.mock? + response = @compute.get_console_output(server.id) + output = response.body["output"] + if output.nil? + return nil + end + keys = output.match( + /-----BEGIN SSH HOST KEY KEYS-----(.*)-----END SSH HOST KEY KEYS-----/m + ) + if keys.nil? + return nil + else + ssh_key_list = keys[1].strip.split("\r\n").map {|key_str| + SSH::Key.load(key_str) + } + return ssh_key_list.compact + end + end + private # |