summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Walberg <sean@ertw.com>2015-04-05 00:10:57 -0500
committerSean Walberg <sean@ertw.com>2015-04-05 00:10:57 -0500
commit4cdce72a39b6f2e9ad0eb3029d861c99494738f0 (patch)
tree7da84564e60bee3eef0f0266d818d6230cf98219
parent361ba778a36b9da6e29ece85c124babcbfaabdd8 (diff)
downloadchef-4cdce72a39b6f2e9ad0eb3029d861c99494738f0.tar.gz
Allow tags to be set on a node during bootstrap
I can set attributes, environment, and run_list, but why not tags? Some of our recipes are driven off of tags, such as for sudo access or membership in a load balancing pool. If this patch is accepted then I will add the corresponding feature to knife-vsphere. This patch only works with the new vault method, not the validator key method. If someone has some pointers on where to add it in the validator method I'd be happy to amend.
-rw-r--r--lib/chef/knife/bootstrap.rb6
-rw-r--r--lib/chef/knife/bootstrap/client_builder.rb3
-rw-r--r--spec/unit/knife/bootstrap/client_builder_spec.rb16
3 files changed, 25 insertions, 0 deletions
diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb
index a4095e8402..157c815a98 100644
--- a/lib/chef/knife/bootstrap.rb
+++ b/lib/chef/knife/bootstrap.rb
@@ -143,6 +143,12 @@ class Chef
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []
+ option :tags,
+ :long => "--tags TAGS",
+ :description => "Comma separated list of tags to apply to the node",
+ :proc => lambda { |o| o.split(/[\s,]+/) },
+ :default => []
+
option :first_boot_attributes,
:short => "-j JSON_ATTRIBS",
:long => "--json-attributes",
diff --git a/lib/chef/knife/bootstrap/client_builder.rb b/lib/chef/knife/bootstrap/client_builder.rb
index b9c1d98bec..32258f6fa5 100644
--- a/lib/chef/knife/bootstrap/client_builder.rb
+++ b/lib/chef/knife/bootstrap/client_builder.rb
@@ -140,6 +140,9 @@ class Chef
node.run_list(normalized_run_list)
node.normal_attrs = first_boot_attributes if first_boot_attributes
node.environment(environment) if environment
+ (knife_config[:tags] || []).each do |tag|
+ node.tags << tag
+ end
node
end
end
diff --git a/spec/unit/knife/bootstrap/client_builder_spec.rb b/spec/unit/knife/bootstrap/client_builder_spec.rb
index e6aa307c7e..930ae8c9d3 100644
--- a/spec/unit/knife/bootstrap/client_builder_spec.rb
+++ b/spec/unit/knife/bootstrap/client_builder_spec.rb
@@ -149,6 +149,22 @@ describe Chef::Knife::Bootstrap::ClientBuilder do
client_builder.run
end
+ it "does not add tags by default" do
+ allow(node).to receive(:run_list).with([])
+ expect(node).to_not receive(:tags)
+ client_builder.run
+ end
+
+ it "adds tags to the node when given" do
+ tag_receiver = []
+
+ knife_config[:tags] = %w[foo bar]
+ allow(node).to receive(:run_list).with([])
+ allow(node).to receive(:tags).and_return(tag_receiver)
+ client_builder.run
+ expect(tag_receiver).to eq %w[foo bar]
+ end
+
it "builds a node when the run_list is a string" do
knife_config[:run_list] = "role[base],role[app]"
expect(node).to receive(:run_list).with(["role[base]", "role[app]"])