Reputation: 2089
I'm trying to write a wrapper cookbook for the chef graphite repo
In the recipe carbon.rb, the following lines occur:
template "#{node['graphite']['base_dir']}/conf/storage-schemas.conf" do
owner node['apache']['user']
group node['apache']['group']
end
where in templates/default/storage-schemas.conf there is a storage-schemas.conf file that is not to my liking. I can edit the file inline and achieve what I want, but it doesn't seem like a good chef practice if I want to be able to keep my repo up to date without merge conflicts. So I was wondering if I could solve this with a wrapper cookbook.
My first though was something like
include_recipe "graphite"
template "#{node['graphite']['base_dir']}/conf/storage-schemas.conf" do
owner node['apache']['user']
group node['apache']['group']
end
where I would just rerun the command after the base recipe finished and put the file I wanted in wrappercookbook/templates/storage-schemas.conf.erb. Is this a common practice? It doesn't feel very DRY, but I can't think of a cleaner way.
Upvotes: 22
Views: 11997
Reputation: 41
with chef 12 you can use edit_resource
include_recipe 'communitycookbook'
edit_resource!(:template, '/etc/myapp.conf') do
source 'other.erb'
cookbook 'wrapper'
variables.update(port: 8080)
end
more about that you can find here: https://coderanger.net/rewind/
Upvotes: 0
Reputation: 760
As an alternative to Dave's answer, you can also use chef-rewind
.
https://github.com/bryanwb/chef-rewind
A quick usage example from the github repo
# file postgresql/recipes/server.rb
template "/var/pgsql/data/postgresql.conf" do
source "postgresql.conf.erb"
owner "postgres"
end
# file my-postgresql/recipes/server.rb
chef_gem "chef-rewind"
require 'chef/rewind'
include_recipe "postgresql::server"
# my-postgresql.conf.erb located inside my-postgresql/templates/default/my-postgresql.conf.erb
rewind :template => "/var/pgsql/data/postgresql.conf" do
source "my-postgresql.conf.erb"
cookbook_name "my-postgresql"
end
Upvotes: 14
Reputation: 6419
You're pretty close. Assuming you have a modified version of the storage-schemas.conf.erb file in your wrapper cookbook, you can just do:
include_recipe "graphite"
begin
r = resources(:template => "#{node['graphite']['base_dir']}/conf/storage-schemas.conf")
r.cookbook "my-cookbook"
rescue Chef::Exceptions::ResourceNotFound
Chef::Log.warn "could not find template to override!"
end
You can also use a line like:
r.source "graphite-stuff/my-storage-schemas.conf.erb"
if you want to organize the files in your wrapper cookbook in a different way.
Upvotes: 28
Reputation: 2624
Making your patches and merging with upstream is the recommended practice when using knife
because knife does some of the git branch merging stuff automatically for you and you are able to keep track of what you initially changed.
Simply overwriting files in your wrapper cookbook is a practice I did not encounter earlier but looks interesting ^^ Disadvantage: you have to maintain and merge upstream changes into your modified template manually and that may be sometimes more work than letting git do most of the stuff for you.
Third way: rely on "cookbook shadowing" (deprecated) that works when you have direct control over which cookbooks will the end user will use: http://tickets.opscode.com/browse/CHEF-2308
Upvotes: 1