Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save afiune/041487354152dd6c9b4d to your computer and use it in GitHub Desktop.
Save afiune/041487354152dd6c9b4d to your computer and use it in GitHub Desktop.
Delivery Publish Cookbook to Multiple Chef Server/Organizations

Publish Cookbook to Multiple Chef Servers in Delivery

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.

Prerequisites

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'

Publish Cookbook to a single Chef Server

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) 
end

Publish Cookbook to Multiple Chef Servers

Lets 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
@pburkholder
Copy link

(technically, not an LWRP, but a 'custom resource')

@dreamnite
Copy link

As a note: If you do create/deploy a knife.rb automatically, you will need to do so at compile time. The DeliverySugar::ChefServer.new() call happens at compile time, and if the knife.rb is not already present by that point, it will fail.

The easiest way to do this is with your template/file call being ending with end.run_action(:create) to cause it to be called at compile time. For example:

template "#{delivery_workspace}/.chef/knife-#{chef_server}.rb" do 
        source 'knife_rb.erb'
        mode '755'
        variables ({ :chef_url => chef_server_url })
        action :nothing
    end.run_action(:create)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment