Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Tools
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Database.yml files using Chef Server

Colin Shield
Sunday, February 24, 2013

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') %>
<%= File.read('/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])
end

Finally the template.


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

 

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Add New Comment Cancel reply

Your email address will not be published.

Colin Shield

Colin Shield
San Francisco

Recent Posts

  • Standup Oct/19/2010: Cedar, a BDD Testing Framework for Objective C
  • Donkey & Goat Open House
  • Standup 1/14 actionmailer content type issue
Subscribe to Colin's Feed

Author Topics

chef server (1)
agile standup (2)
performance (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Tools
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >