From 713d3e677256b7ff948f27ad3ec7f6a4e728b33b Mon Sep 17 00:00:00 2001 From: Martin Cozzi Date: Wed, 3 Sep 2014 17:30:53 -0700 Subject: [PATCH] Allows for extra configuration with the 'graphite' key. A Chef node considers hash keys as strings, by having symbols and strings the hash can not merge properly and as a result, adding another `graphite` key to the `extra_config` results in a hash with two `graphite` keys. The result is that there is no way at the moment to add any more values to the `graphite` key (see https://github.com/etsy/statsd/blob/master/docs/namespacing.md for more examples) To go around this problem the first thing to do is to remove symbols from the `config_hash` hash and turn them into strings. The second thing is to then do a "deep merge" since a regular merge will only go down one level in the hash. To accomplish this we take advantage of the Chef Mixins. `config_hash` should really be a node attribute to prevent this type of behavior and let Chef handle the deep merges but for the sake of backward compatibility it was left as is. However this should be considered when bumping the cookbook to a major release. This allows the user to do things like this: ```ruby node.default['statsd']['extra_config'] = { 'graphite' => { 'globalPrefix' => 'something' }, } ``` And the output of the config.js will be ``` "graphite": { "legacyNamespace": false, "globalPrefix": "something" }, ``` --- metadata.rb | 2 +- recipes/default.rb | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/metadata.rb b/metadata.rb index 9d5d459..2148657 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,7 +4,7 @@ license 'Apache 2.0' description 'Installs/Configures statsd' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '0.3.2' +version '0.3.3' depends 'build-essential' depends 'git' diff --git a/recipes/default.rb b/recipes/default.rb index 851cdbf..a1ebdad 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -16,6 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +require 'chef/mixin/deep_merge' include_recipe 'nodejs' include_recipe 'git' @@ -98,12 +99,12 @@ } if node['statsd']['graphite_enabled'] - config_hash[:graphite] = { legacyNamespace: node['statsd']['legacyNamespace'] } - config_hash[:graphitePort] = node['statsd']['graphite_port'] - config_hash[:graphiteHost] = node['statsd']['graphite_host'] + config_hash['graphite'] = { 'legacyNamespace' => node['statsd']['legacyNamespace'] } + config_hash['graphitePort'] = node['statsd']['graphite_port'] + config_hash['graphiteHost'] = node['statsd']['graphite_host'] end - config_hash = config_hash.merge(node['statsd']['extra_config']) + Chef::Mixin::DeepMerge.deep_merge!(node['statsd']['extra_config'], config_hash) variables config_hash: config_hash notifies :restart, 'service[statsd]', :delayed end