2013-04-10 149 views
20

我試圖寫的chef graphite repo在包裝食譜

在配方carbon.rb包裝菜譜更換模板,會出現以下行:

template "#{node['graphite']['base_dir']}/conf/storage-schemas.conf" do 
    owner node['apache']['user'] 
    group node['apache']['group'] 
end 

模板哪裏/默認/storage-schemas.conf中有一個storage-schemas.conf文件不符合我的要求。我可以內聯編輯文件並實現我想要的功能,但如果我希望能夠保持我的回購沒有合併衝突,它似乎不是一個好的廚師做法。所以我想知道是否可以用包裝食譜來解決這個問題。

我的第一個雖然是像

include_recipe "graphite" 
template "#{node['graphite']['base_dir']}/conf/storage-schemas.conf" do 
    owner node['apache']['user'] 
    group node['apache']['group'] 
end 

在那裏我會剛剛完成基礎配方後,重新運行該命令,並把我在wrappercookbook /模板/存儲schemas.conf.erb想要的文件。這是常見的做法嗎?它感覺不太乾,但我想不出一個更乾淨的方式。

回答

27

你是八九不離十。假設你在包裝食譜有存儲schemas.conf.erb文件的修改版本,你可以做:

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 

您也可以使用這樣一行:

r.source "graphite-stuff/my-storage-schemas.conf.erb" 

,如果你希望以不同的方式組織包裝食譜中的文件。

+0

似乎沒有在廚師獨奏中工作,抱怨說,儘管包括基本食譜,但找不到資源。 – Noz 2015-01-12 23:04:08

1

使用knife時,建議您製作補丁並與上游進行合併,因爲knife會自動爲您自動合併東西,並且能夠跟蹤最初更改的內容。

簡單地覆蓋包裝食譜中的文件是一種我以前沒有遇到的實踐,但看起來很有趣^^缺點:必須手動維護併合並上遊更改到修改過的模板中,有時可能比讓git執行更多工作大部分的東西給你。

第三條道路:依靠「菜譜陰影」(不推薦),當你有直接的控制哪些食譜將最終用戶將使用效果:http://tickets.opscode.com/browse/CHEF-2308

14

作爲戴夫答案的替代方案,您也可以使用chef-rewind

https://github.com/bryanwb/chef-rewind

從GitHub庫

#文件的PostgreSQL /食譜/ server.rb

template "/var/pgsql/data/postgresql.conf" do 
    source "postgresql.conf.erb" 
    owner "postgres" 
end 

#文件我-的PostgreSQL /食譜/服務器快速使用示例。 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