Our delivery-sugar cookbook exposes some libraries and a LWRP that we can use to
publish a cookbook (or multiple) to multiple Chef Servers or Organizations.
The only prerequisite we have before start coding is the generation of the knife.rb
and the client_key on the build-nodes that we will use in the build-cookbook. This can
be done manually by loging in to the build-nodes and laying down the file, or in an
automated way with some extra file/template resources that we will not cover on this
document. (Generate Knife.rb automatically)
Here is an example of a knife.rb for a dummy user that points to the chef-server
chef-dummy-server.example.com and the organization star_wars:
current_dir = File.dirname(__FILE__)
log_location STDOUT
node_name 'dummy'
client_key "#{current_dir}/dummy.pem"
trusted_certs_dir '/etc/chef/trusted_certs'
chef_server_url 'https://chef-dummy-server.example.com/organizations/star_wars'
This example will show you how to use the delivery_chef_cookbook LWRP to publish a cookbook
called gandalf to a single Chef Server.
knife_rb = 'path/to/the/knife_rb/file/in/the/build-node/knife.rb'
delivery_chef_cookbook 'gandalf' do
path 'path/to/the/cookbook/in/the/build-node/gandalf'
chef_server DeliverySugar::ChefServer.new(knife_rb)
endLets imagine we have two Chef Servers, one in San Francisco and another one in
New York. We have already layed down the right knife.rb and the client_key on
the build-nodes for each Chef Server.
We want the cookbook to be uploaded at the very end of our pipeline, that is in
the Delivered Stage, into the Functional Phase. We will modify the recipe
recipes/functional.rb of the build-cookbook within your project.
# Run it only in Delivered::Functional
#
# This helper is coming from delivery-sugar
# => https://github.com/chef-cookbooks/delivery-sugar/blob/master/libraries/delivery_dsl.rb#L105,L113
if delivery_environment.eql?('delivered')
# Previously generated knife.rb files
ny_knife_rb = '/var/opt/delivery/workspace/chef_servers/ny/knife.rb'
sf_knife_rb = '/var/opt/delivery/workspace/chef_servers/sf/knife.rb'
# ChefServer Objects
chef_server_ny = DeliverySugar::ChefServer.new(ny_knife_rb)
chef_server_sf = DeliverySugar::ChefServer.new(sf_knife_rb)
delivery_chef_cookbook delivery_project do
path delivery_workspace_repo
chef_server [chef_server_ny, chef_server_sf]
end
end
(technically, not an LWRP, but a 'custom resource')