diff options
-rw-r--r-- | lib/chef/knife/ssh.rb | 49 | ||||
-rw-r--r-- | spec/functional/knife/ssh_spec.rb | 12 | ||||
-rw-r--r-- | spec/unit/knife/ssh_spec.rb | 36 |
3 files changed, 37 insertions, 60 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 diff --git a/spec/functional/knife/ssh_spec.rb b/spec/functional/knife/ssh_spec.rb index fe1db2df20..3872d34322 100644 --- a/spec/functional/knife/ssh_spec.rb +++ b/spec/functional/knife/ssh_spec.rb @@ -181,7 +181,7 @@ describe Chef::Knife::Ssh do it "uses the ssh_attribute" do @knife.run - expect(@knife.get_ssh_attribute({ "knife_config" => "ec2.public_hostname" })).to eq("ec2.public_hostname") + expect(@knife.get_ssh_attribute({ "target" => "ec2.public_hostname" })).to eq("ec2.public_hostname") end end @@ -199,22 +199,22 @@ describe Chef::Knife::Ssh do context "when -a ec2.public_public_hostname is provided" do before do - setup_knife(["-a ec2.public_hostname", "*:*", "uptime"]) + setup_knife(["-a", "ec2.public_hostname", "*:*", "uptime"]) Chef::Config[:knife][:ssh_attribute] = nil end it "should use the value on the command line" do @knife.run - expect(@knife.config[:attribute]).to eq("ec2.public_hostname") + expect(@knife.config[:ssh_attribute]).to eq("ec2.public_hostname") end it "should override what is set in knife.rb" do # This is the setting imported from knife.rb Chef::Config[:knife][:ssh_attribute] = "fqdn" # Then we run knife with the -a flag, which sets the above variable - setup_knife(["-a ec2.public_hostname", "*:*", "uptime"]) + setup_knife(["-a", "ec2.public_hostname", "*:*", "uptime"]) @knife.run - expect(@knife.config[:attribute]).to eq("ec2.public_hostname") + expect(@knife.config[:ssh_attribute]).to eq("ec2.public_hostname") end end end @@ -305,7 +305,7 @@ describe Chef::Knife::Ssh do Chef::Config[:chef_server_url] = "http://localhost:9000" @api.post("/search/node?q=*:*&start=0&rows=1000", 200) do - %({"total":1, "start":0, "rows":[{"data": {"fqdn":"the.fqdn", "config": "the_public_hostname", "knife_config": "the_public_hostname" }}]}) + %({"total":1, "start":0, "rows":[{"data": {"fqdn":"the.fqdn", "target": "the_public_hostname"}}]}) end end diff --git a/spec/unit/knife/ssh_spec.rb b/spec/unit/knife/ssh_spec.rb index e786de5431..e3cdf2c2f7 100644 --- a/spec/unit/knife/ssh_spec.rb +++ b/spec/unit/knife/ssh_spec.rb @@ -49,19 +49,19 @@ describe Chef::Knife::Ssh do def self.should_return_specified_attributes it "returns an array of the attributes specified on the command line OR config file, if only one is set" do - @node_bar["config"] = "10.0.0.2" - @node_foo["config"] = "10.0.0.1" - @knife.config[:attribute] = "ipaddress" + @node_bar["target"] = "10.0.0.2" + @node_foo["target"] = "10.0.0.1" + @knife.config[:ssh_attribute] = "ipaddress" Chef::Config[:knife][:ssh_attribute] = "ipaddress" # this value will be in the config file expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil], ["10.0.0.2", nil]]) @knife.configure_session end it "returns an array of the attributes specified on the command line even when a config value is set" do - @node_bar["config"] = "10.0.0.2" - @node_foo["config"] = "10.0.0.1" + @node_bar["target"] = "10.0.0.2" + @node_foo["target"] = "10.0.0.1" Chef::Config[:knife][:ssh_attribute] = "config_file" # this value will be in the config file - @knife.config[:attribute] = "ipaddress" # this is the value of the command line via #configure_attribute + @knife.config[:ssh_attribute] = "ipaddress" # this is the value of the command line via #configure_attribute expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil], ["10.0.0.2", nil]]) @knife.configure_session end @@ -146,13 +146,12 @@ describe Chef::Knife::Ssh do describe "#get_ssh_attribute" do # Order of precedence for ssh target - # 1) command line attribute - # 2) configuration file - # 3) cloud attribute - # 4) fqdn + # 1) config value (cli or knife config) + # 2) cloud attribute + # 3) fqdn before do Chef::Config[:knife][:ssh_attribute] = nil - @knife.config[:attribute] = nil + @knife.config[:ssh_attribute] = nil @node_foo["cloud"]["public_hostname"] = "ec2-10-0-0-1.compute-1.amazonaws.com" @node_bar["cloud"]["public_hostname"] = "" end @@ -165,18 +164,9 @@ describe Chef::Knife::Ssh do expect(@knife.get_ssh_attribute(@node_foo)).to eq("ec2-10-0-0-1.compute-1.amazonaws.com") end - it "should favor to attribute_from_cli over config file and cloud" do - @knife.config[:attribute] = "command_line" - Chef::Config[:knife][:ssh_attribute] = "config_file" - @node_foo["config"] = "command_line" - @node_foo["knife_config"] = "config_file" - expect( @knife.get_ssh_attribute(@node_foo)).to eq("command_line") - end - - it "should favor config file over cloud and default" do - Chef::Config[:knife][:ssh_attribute] = "config_file" - @node_foo["knife_config"] = "config_file" - expect( @knife.get_ssh_attribute(@node_foo)).to eq("config_file") + it "should favor config over cloud and default" do + @node_foo["target"] = "config" + expect( @knife.get_ssh_attribute(@node_foo)).to eq("config") end it "should return fqdn if cloud.hostname is empty" do |