summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <mcquin@users.noreply.github.com>2015-07-21 10:51:54 -0700
committerClaire McQuin <mcquin@users.noreply.github.com>2015-07-21 10:51:54 -0700
commit2c4d7c7cf40346ca8236054901c023f35e1300c9 (patch)
tree523f274bc62e0d9b7aaa2c5af6d3bd69ab03c5a9
parentc91b3371036de811c9b2ba0e26f287e676dfd96f (diff)
parent14075fd8ac3dd08e80d0e2da08403afbecf798e0 (diff)
downloadchef-2c4d7c7cf40346ca8236054901c023f35e1300c9.tar.gz
Merge pull request #3668 from chef/mcquin/first-boot-chef-environment
Set chef_environment in attributes JSON
-rw-r--r--DOC_CHANGES.md16
-rw-r--r--lib/chef/node.rb19
-rw-r--r--spec/unit/node_spec.rb7
3 files changed, 42 insertions, 0 deletions
diff --git a/DOC_CHANGES.md b/DOC_CHANGES.md
index b1121e2bf4..92a178e20b 100644
--- a/DOC_CHANGES.md
+++ b/DOC_CHANGES.md
@@ -6,3 +6,19 @@ Example Doc Change:
Description of the required change.
-->
+### chef-client -j JSON
+Add to the [description of chef-client options](https://docs.chef.io/ctl_chef_client.html#options):
+
+> This option can also be used to set a node's `chef_environment`. For example,
+running `chef-client -j /path/to/file.json` where `/path/to/file.json` is
+similar to:
+```
+{
+ "chef_environment": "pre-production"
+}
+```
+will set the node's environment to `"pre-production"`.
+
+> *Note that the environment specified by `chef_environment` in your JSON will
+take precedence over an environment specified by `-E ENVIROMENT` when both options
+are provided.*
diff --git a/lib/chef/node.rb b/lib/chef/node.rb
index d5078371c5..22c7d5bd8e 100644
--- a/lib/chef/node.rb
+++ b/lib/chef/node.rb
@@ -315,6 +315,7 @@ class Chef
# Consumes the combined run_list and other attributes in +attrs+
def consume_attributes(attrs)
normal_attrs_to_merge = consume_run_list(attrs)
+ normal_attrs_to_merge = consume_chef_environment(normal_attrs_to_merge)
Chef::Log.debug("Applying attributes from json file")
self.normal_attrs = Chef::Mixin::DeepMerge.merge(normal_attrs,normal_attrs_to_merge)
self.tags # make sure they're defined
@@ -347,6 +348,24 @@ class Chef
attrs
end
+ # chef_environment when set in -j JSON will take precedence over
+ # -E ENVIRONMENT. Ideally, IMO, the order of precedence should be (lowest to
+ # highest):
+ # config_file
+ # -j JSON
+ # -E ENVIRONMENT
+ # so that users could reuse their JSON and override the chef_environment
+ # configured within it with -E ENVIRONMENT. Because command line options are
+ # merged with Chef::Config there is currently no way to distinguish between
+ # an environment set via config from an environment set via command line.
+ def consume_chef_environment(attrs)
+ attrs = attrs ? attrs.dup : {}
+ if env = attrs.delete("chef_environment")
+ chef_environment(env)
+ end
+ attrs
+ end
+
# Clear defaults and overrides, so that any deleted attributes
# between runs are still gone.
def reset_defaults_and_overrides
diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb
index 032cb1accb..b7752eb734 100644
--- a/spec/unit/node_spec.rb
+++ b/spec/unit/node_spec.rb
@@ -672,6 +672,13 @@ describe Chef::Node do
expect(node.run_list).to eq([ "role[base]", "recipe[chef::server]" ])
end
+ it "sets the node chef_environment" do
+ attrs = { "chef_environment" => "foo_environment", "bar" => "baz" }
+ expect(node.consume_chef_environment(attrs)).to eq({ "bar" => "baz" })
+ expect(node.chef_environment).to eq("foo_environment")
+ expect(node['chef_environment']).to be nil
+ end
+
it "should overwrites the run list with the run list it consumes" do
node.consume_run_list "recipes" => [ "one", "two" ]
node.consume_run_list "recipes" => [ "three" ]