22

我正在尝试为厨师石墨回购写一本包装食谱

在配方 carbon.rb 中,出现以下几行:

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

在 templates/default/storage-schemas.conf 中有一个我不喜欢的 storage-schemas.conf 文件。我可以内联编辑文件并实现我想要的,但如果我希望能够在没有合并冲突的情况下使我的 repo 保持最新,这似乎不是一个好的厨师实践。所以我想知道我是否可以用包装食谱来解决这个问题。

我的第一个虽然是

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

我会在基本配方完成后重新运行命令并将我想要的文件放在 wrappercookbook/templates/storage-schemas.conf.erb 中。这是一种常见的做法吗?感觉不是很干,但我想不出更干净的方法。

4

4 回答 4

28

你很接近。假设您的包装说明书中有 storage-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"

如果您想以不同的方式组织包装说明书中的文件。

于 2013-04-10T19:53:16.240 回答
14

作为戴夫答案的替代方案,您还可以使用chef-rewind.

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

来自 github repo 的快速使用示例

# 文件 postgresql/recipes/server.rb

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

# 文件 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
于 2013-08-27T17:45:48.513 回答
1

制作补丁并与上游合并是使用时推荐的做法,knife因为刀会自动为您执行一些 git 分支合并内容,并且您可以跟踪最初更改的内容。

简单地覆盖包装说明书中的文件是我之前没有遇到的一种做法,但看起来很有趣 ^^ 缺点:您必须手动维护上游更改并将其合并到修改后的模板中,这有时可能比让 git 做大部分工作要做更多的工作为你。

第三种方式:当您可以直接控制最终用户将使用哪些食谱时,依靠“食谱阴影”(已弃用):http: //tickets.opscode.com/browse/CHEF-2308

于 2013-04-10T19:43:36.520 回答
0

使用厨师 12,您可以使用 edit_resource

include_recipe 'communitycookbook'

edit_resource!(:template, '/etc/myapp.conf') do
  source 'other.erb'
  cookbook 'wrapper'
  variables.update(port: 8080)
end

更多关于你可以在这里找到的信息:https ://coderanger.net/rewind/

于 2019-09-25T13:05:46.733 回答