diff options
author | Mal Graty <mal.graty@googlemail.com> | 2017-11-28 11:06:16 +0000 |
---|---|---|
committer | Mal Graty <mal.graty@googlemail.com> | 2017-11-28 11:37:22 +0000 |
commit | edd8a50fed5899129dff538be0e92f125c5c12fd (patch) | |
tree | f994aa9b2bfd84805418b77a4a914203cec88fdc /lib/chef/knife/ssh.rb | |
parent | 4b52680a30d43c4c41595379b78fce09ec8e70d8 (diff) | |
download | chef-edd8a50fed5899129dff538be0e92f125c5c12fd.tar.gz |
Refactor SSH attribute
Signed-off-by: Mal Graty <mal.graty@googlemail.com>
Diffstat (limited to 'lib/chef/knife/ssh.rb')
-rw-r--r-- | lib/chef/knife/ssh.rb | 49 |
1 files changed, 18 insertions, 31 deletions
diff --git a/lib/chef/knife/ssh.rb b/lib/chef/knife/ssh.rb index f4a025dba3..af5b15db0d 100644 --- a/lib/chef/knife/ssh.rb +++ b/lib/chef/knife/ssh.rb @@ -46,11 +46,10 @@ class Chef :default => nil, :proc => lambda { |o| o.to_i } - option :attribute, + option :ssh_attribute, :short => "-a ATTR", :long => "--attribute ATTR", - :description => "The attribute to use for opening the connection - default depends on the context", - :proc => Proc.new { |key| Chef::Config[:knife][:ssh_attribute] = key.strip } + :description => "The attribute to use for opening the connection - default depends on the context" option :manual, :short => "-m", @@ -181,27 +180,21 @@ class Chef session_from_list(list) end - def get_ssh_attribute(node) + def get_ssh_attribute(item) # Order of precedence for ssh target - # 1) command line attribute - # 2) configuration file - # 3) cloud attribute - # 4) fqdn - if node["config"] - Chef::Log.debug("Using node attribute '#{config[:attribute]}' as the ssh target: #{node["config"]}") - node["config"] - elsif Chef::Config[:knife][:ssh_attribute] - Chef::Log.debug("Using node attribute #{Chef::Config[:knife][:ssh_attribute]}: #{node["knife_config"]}") - node["knife_config"] - elsif node["cloud"] && - node["cloud"]["public_hostname"] && - !node["cloud"]["public_hostname"].empty? - Chef::Log.debug("Using node attribute 'cloud[:public_hostname]' automatically as the ssh target: #{node["cloud"]["public_hostname"]}") - node["cloud"]["public_hostname"] + # 1) config value (cli or knife config) + # 2) cloud attribute + # 3) fqdn + msg = "Using node attribute '%s' as the ssh target: %s" + if item["target"] + Chef::Log.debug(sprintf(msg, config[:ssh_attribute], item["target"])) + item["target"] + elsif !item.dig("cloud", "public_hostname").to_s.empty? + Chef::Log.debug(sprintf(msg, "cloud.public_hostname", item["cloud"]["public_hostname"])) + item["cloud"]["public_hostname"] else - # falling back to default of fqdn - Chef::Log.debug("Using node attribute 'fqdn' as the ssh target: #{node["fqdn"]}") - node["fqdn"] + Chef::Log.debug(sprintf(msg, "fqdn", item["fqdn"])) + item["fqdn"] end end @@ -212,14 +205,8 @@ class Chef separator = ui.presenter.attribute_field_separator - # if we've set an attribute to use on the command line - if config[:attribute] - required_attributes[:config] = config[:attribute].split(separator) - end - - # if we've configured an attribute in our config - if Chef::Config[:knife][:ssh_attribute] - required_attributes[:knife_config] = Chef::Config[:knife][:ssh_attribute].split(separator) + if config[:ssh_attribute] + required_attributes[:target] = config[:ssh_attribute].split(separator) end @search_count = 0 @@ -232,7 +219,7 @@ class Chef # returned node object host = get_ssh_attribute(item) next if host.nil? - ssh_port = item[:cloud].nil? ? nil : item[:cloud][:public_ssh_port] + ssh_port = item.dig("cloud", "public_ssh_port") srv = [host, ssh_port] list.push(srv) end |