Skip to content

Instantly share code, notes, and snippets.

@johndalton
Forked from jeremy2/database.yml.erb
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndalton/9213047 to your computer and use it in GitHub Desktop.
Save johndalton/9213047 to your computer and use it in GitHub Desktop.
#
# This file should be in .../cookbooks/database/templates/default/database.yml.erb
#
<%= @environment %>:
adapter: <%= @adapter %>
database: <%= @database %>
username: <%= @username %>
password: <%= @password %>
host: <%= @host %>
reconnect: true
# This produces a block for each slave, named "slave", "slave_1", "slave_2", etc.
<% @slaves.each_with_index do |slave,i| %>
<%= i == 0 ? "slave" : "slave_#{i}" %>:
slavename: <%= slave["name"].nil? ? "---" : "'"+slave["name"]+"'" %>
adapter: <%= @adapter %>
database: '<%= @dbname %>'
username: '<%= @dbuser %>'
password: '<%= @dbpass %>'
host: '<%= slave['private_hostname'] %>'
reconnect: true
<% end %>
#
# Cookbook Name: database
# Recipe: default
#
# Description:
# Configure application servers to use mysql2 adapter for the database.yml config.
# All parameters except the adapter are pulled from the EY node JSON element. See
# http://docs.engineyard.com/use-deploy-hooks-with-engine-yard-cloud.html for an
# example of the node JSON object. This object is also used for by deploy hooks
# at Engine Yard.
#
# This file should be in .../cookbooks/database/recipes/default.rb
#
#
# Q: Why might we need this custom recipe?
#
# A: You may need to generate a custom database.yml file because Engine Yard's
# default database.yml file generator always generates a config file that uses the
# mysql adapter for Rails 2 apps. (For rails[34] apps in MySQL environents it
# attempts to detect whether the mysql or mysql2 gem is in use.)
#
# This recipes forces the mysql2 adapter for an existing Rails 2 app.
#
# The default database.yml lists any DB replicas in an environment, but some gems
# which distribute reads may require a custom syntax - in such cases the template
# can be modified to produce the appropriate syntax.
#
if ['solo', 'app_master', 'app', 'util'].include?(node[:instance_role])
# for each application
node[:engineyard][:environment][:apps].each do |app|
# create new database.yml
template "/data/#{app[:name]}/shared/config/database.yml" do
source 'database.yml.erb'
owner node[:users][0][:username]
group node[:users][0][:username]
mode 0644
variables({
:environment => node[:environment][:framework_env],
:adapter => 'mysql2',
:database => app[:database_name],
:username => node[:users][0][:username],
:password => node[:users][0][:password],
:host => node[:db_host]
:slaves => node.environment.instances.select{|i| i["role"] =="db_slave"},
})
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment