Replacing a template in a wrapper cookbook

旧巷老猫 提交于 2019-12-03 04:17:19

问题


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.


回答1:


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.




回答2:


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



回答3:


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




回答4:


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/



来源:https://stackoverflow.com/questions/15933157/replacing-a-template-in-a-wrapper-cookbook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!