diff options
author | Thom May <thom@may.lt> | 2018-02-27 15:19:03 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-27 15:19:03 +0000 |
commit | e2788b28b33f4fba47c13c7834fdc448e534f5ad (patch) | |
tree | 0ca9525720dc8ebd415ebe860f5a77d9d209bc86 | |
parent | 81d9c1b0fda19ed6da64569e141382c05cdda8aa (diff) | |
parent | dae3b6be0f30d7006ec3891018bd112fc4826a0c (diff) | |
download | chef-e2788b28b33f4fba47c13c7834fdc448e534f5ad.tar.gz |
Merge pull request #6781 from linyows/notice-duplicated-ip
Raise fatal error If FQDN duplicated
-rw-r--r-- | lib/chef/knife/ssh.rb | 16 | ||||
-rw-r--r-- | spec/unit/knife/ssh_spec.rb | 18 |
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/chef/knife/ssh.rb b/lib/chef/knife/ssh.rb index e01977a976..c0df6ecae8 100644 --- a/lib/chef/knife/ssh.rb +++ b/lib/chef/knife/ssh.rb @@ -123,6 +123,12 @@ class Chef :boolean => true, :default => false + option :duplicated_fqdns, + :long => "--duplicated-fqdns", + :description => "Behavior if FQDNs are duplicated, ignored by default", + :proc => Proc.new { |key| Chef::Config[:knife][:duplicated_fqdns] = key.strip.to_sym }, + :default => :ignore + option :tmux_split, :long => "--tmux-split", :description => "Split tmux window.", @@ -175,6 +181,16 @@ class Chef end exit 10 end + if %i{warn fatal}.include?(config[:duplicated_fqdns]) + fqdns = list.map { |v| v[0] } + if fqdns.count != fqdns.uniq.count + duplicated_fqdns = fqdns.uniq + ui.send(config[:duplicated_fqdns], + "SSH #{duplicated_fqdns.count > 1 ? 'nodes are' : 'node is'} " + + "duplicated: #{duplicated_fqdns.join(',')}") + exit 10 if config[:duplicated_fqdns] == :fatal + end + end session_from_list(list) end diff --git a/spec/unit/knife/ssh_spec.rb b/spec/unit/knife/ssh_spec.rb index af54115ac7..9d7fcb6f9e 100644 --- a/spec/unit/knife/ssh_spec.rb +++ b/spec/unit/knife/ssh_spec.rb @@ -137,6 +137,24 @@ describe Chef::Knife::Ssh do @knife.configure_session end end + + context "when there are some hosts found but IPs duplicated if duplicated_fqdns option sets :fatal" do + before do + @knife.config[:duplicated_fqdns] = :fatal + @node_foo["fqdn"] = "foo.example.org" + @node_bar["fqdn"] = "foo.example.org" + end + + it "should raise a specific error" do + expect(@knife.ui).to receive(:fatal).with(/^SSH node is duplicated: foo\.example\.org/) + expect(@knife).to receive(:exit).with(10) + expect(@knife).to receive(:session_from_list).with([ + ["foo.example.org", nil, nil], + ["foo.example.org", nil, nil], + ]) + @knife.configure_session + end + end end context "manual is set to true" do |