Let say I have a cookbook which configures and installs a magical deamon:
magical-deamon/recipes/default.rb:
template "/etc/magical-deamon/magical.conf" do
source "magical.conf"
mode 0644
notifies :restart, resources(:service => "magical-deamon")
end
magical-deamon/attributes/default.rb:
default['magical-deamon']['memory'] = 1024
magical-deamon/templates/default/magical.conf.erb:
memory = <%= node['magical-deamon']['memory'] %>
As I understood Chef, i would use either the node-attributes to set the memory value like:
{
"normal": {
"tags": [],
"magical-deamon": {
"memory": 256
}
},
"name": "server.example.com",
"chef_environment": "production",
"run_list": [
"role[base]"
]
}
Or through a role:
{
"name": "base",
"default_attributes": {
"magical-deamon": {
"memory": 756
}
},
"json_class": "Chef::Role",
"env_run_lists": {
},
"run_list": [
],
"description": "base role applied to all nodes",
"chef_type": "role",
"override_attributes": {
},
}
}
Or an Environment:
{
"name": "production",
"default_attributes": {
"magical-deamon": {
"memory": 756
}
},
"json_class": "Chef::Environment",
"description": "",
"cookbook_versions": {
},
"override_attributes": {
},
"chef_type": "environment"
}
So far so good...
Now i had the silly idea to set 'memory' to a node specific dynamic value.
Lets say our magical deamon should consume 75% of the total Memory the node possesses.
value = total_memory * 0.75
Coming from a programmer background, i like to leave that knowledge out of the cookbook, because i like my cookbook reusable for other people.
I thought the right place would be somewhere where attributes are being set. But doing such a calculation within json or ruby dsl is not possible.
So my questions are:
- Is my general approach (value = total_memory * 0.75) a stupid idea?
- How would you set that kind of attribute? Keep in mind: There will be more than one value, and one node :) And there might be some calculation involved MB -> KB and rounding and so forth. Hardwiring every attribute into the recipe, should not be an option ;)