diff options
author | Claire McQuin <claire@getchef.com> | 2015-07-14 14:56:00 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2015-07-14 15:18:03 -0700 |
commit | 64e0a1da0826f5befababb5693d8bb5b8de913e9 (patch) | |
tree | b9a85ca8077246f91b6b216f30080e188b27c546 | |
parent | 2c98a7b90507b2479ffda8ccd31bd464b1f5bee0 (diff) | |
download | chef-64e0a1da0826f5befababb5693d8bb5b8de913e9.tar.gz |
Set chef_environment in attributes JSON
-rw-r--r-- | lib/chef/node.rb | 19 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 7 |
2 files changed, 26 insertions, 0 deletions
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" ] |