From b13cbe4730a986a3b60c4c70ce2b5f16da8a4feb Mon Sep 17 00:00:00 2001 From: elijah Date: Thu, 15 Sep 2016 22:21:38 -0700 Subject: leap vm: grab ssh host key when adding a new vm --- lib/leap_cli/cloud/cloud.rb | 53 +++++++++++++++++++++++++++++++++++++++++++-- lib/leap_cli/commands/vm.rb | 10 +++++++++ 2 files changed, 61 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 # diff --git a/lib/leap_cli/commands/vm.rb b/lib/leap_cli/commands/vm.rb index b1911596..790774f1 100644 --- a/lib/leap_cli/commands/vm.rb +++ b/lib/leap_cli/commands/vm.rb @@ -253,6 +253,16 @@ module LeapCli; module Commands if server cloud.bind_server_to_node(server) + ssh_host_key = cloud.wait_for_ssh_host_key(server) + if ssh_host_key.nil? + log :warning, "We could not get a SSH host key." do + log "Try running `leap vm add #{node.name}` again later." + end + else + log :saving, "SSH host key for #{node.name}" + write_file! [:node_ssh_pub_key, node.name], ssh_host_key.to_s + end + log "done", :color => :green, :style => :bold end end -- cgit v1.2.3