Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
Database.yml files using Chef Server

I’ve been spending an awful lot of time lately deploying rails applications using chef server. I’ll be blogging some of the more interesting ways we’re using chef server. One of the most useful features of chef server is search. This allows you to write recipes that reference nodes in your stack that match particular roles. In this post I’ll describe how we use search to generate a rails database.yml. This becomes particularly useful when creating and deploying the same rails application to multiple stacks.

First of all we added a simple file read in our database.yml file in place of the connection details.

<% if File.exist?('/opt/apps/fbfcats/shared/database_include.yml') %>
<%='/opt/apps/fbfcats/shared/database_include.yml') %>
<% end %>

In our chef project we have a recipe that searches for the database server node and writes out the database_include.yml file using a template resource.

master = search(:node, "role:db_master AND chef_environment:#{node.chef_environment}").first
template "/opt/apps/ms/#{app_name}/shared/database_include.yml" do
  owner "gemini"
  group "gemini"
  source "database_include.yml.erb"
  mode "0600"
  variables(:hostname => master,
  :database_name => "fbfcats" + "_" + node[:rails_env],
  :user => 'fbfcats',
  :password => 'mittens',
  :rails_env => node[:rails_env])

Finally the template.

<%= @rails_env %>:
  host: <%= @hostname %>
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: <%= @database_name %>
  pool: 5
  username: <%= @user %>
  password: <%= @password %>


Share This