Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database primary replica configuration #14

Open
wants to merge 2 commits into
base: chore/chef-12-aws
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions libraries/drivers_framework_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ def database_yml(db_driver)

database = db_driver.out
deploy_environment = deploy_env
replica_db_settings = node['deploy'][app['shortname']]['replica_db_settings']
database[:replica] = replica_db_settings unless replica_db_settings.nil?

# this will ensure that replica settings are available in the database.yml file so that
# staging and production database settings have similar settings
replica_database = node['deploy'][app['shortname']]['replica_db_settings']&.dup
replica_database = database.dup if replica_database.nil?
replica_database[:replica] = true

factory_database = node['deploy'][app['shortname']]['factory_database']

Expand All @@ -50,6 +54,7 @@ def database_yml(db_driver)
group www_group
variables(
database: database,
replica: replica_database,
environment: deploy_environment,
factory_database: factory_database
)
Expand Down
9 changes: 7 additions & 2 deletions libraries/drivers_worker_sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def add_replica_config
Chef::Log.info('Rewrite Sidekiq database.yml for read-replica')

deploy = node['deploy'][app['shortname']]
database = deploy['database']
replica_database = deploy['sidekiq_on_replica']&.dup
replica_database = database.dup if replica_database.nil?
replica_database[:replica] = true

Chef::Log.info(deploy.inspect)
context.template "#{deploy_dir(app)}/shared/config/database.yml" do
source 'sidekiq_database.yml.erb'
Expand All @@ -141,9 +146,9 @@ def add_replica_config
group node['deployer']['group']
owner node['deployer']['user']
variables(
database: deploy['database'],
database: database,
environment: deploy['global']['environment'],
sidekiq_on_replica: deploy['sidekiq_on_replica']
sidekiq_on_replica: replica_database,
)
end
end
Expand Down
32 changes: 18 additions & 14 deletions sidekiq_custom/templates/default/sidekiq_database.yml.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<% (['development', 'production'] + [@environment]).uniq.each do |env| -%>
<%= env %>:
database: <%= @database[:database].to_s.inspect %>
encoding: <%= (@database[:encoding] || 'utf8').to_s.inspect %>
adapter: <%= @database[:adapter].to_s.inspect %>
host: <%= (@database[:host] || 'localhost').to_s.inspect %>
username: <%= @database[:username].to_s.inspect %>
password: <%= @database[:password].to_s.inspect %>
reconnect: <%= @database[:reconnect] ? 'true' : 'false' %>
<%- if @database[:pool] -%>
pool: <%= @database[:pool].to_i.inspect %>
<%- end -%>
<%- if @database[:port] -%>
port: <%= @database[:port].to_i.inspect %>
<%- end -%>
primary:
database: <%= @database[:database].to_s.inspect %>
encoding: <%= (@database[:encoding] || 'utf8').to_s.inspect %>
adapter: <%= @database[:adapter].to_s.inspect %>
host: <%= (@database[:host] || 'localhost').to_s.inspect %>
username: <%= @database[:username].to_s.inspect %>
password: <%= @database[:password].to_s.inspect %>
reconnect: <%= @database[:reconnect] ? 'true' : 'false' %>
<%- if @database[:pool] -%>
pool: <%= @database[:pool].to_i.inspect %>
<%- end -%>
<%- if @database[:port] -%>
port: <%= @database[:port].to_i.inspect %>
<%- end -%>
<%- if @sidekiq_on_replica.present? -%>
replica:
primary_replica:
host: <%= (@sidekiq_on_replica[:host] || 'localhost').to_s.inspect %>
username: <%= @sidekiq_on_replica[:username].to_s.inspect %>
password: <%= @sidekiq_on_replica[:password].to_s.inspect %>
Expand All @@ -24,5 +25,8 @@
<%- if @sidekiq_on_replica[:port] -%>
port: <%= @sidekiq_on_replica[:port].to_i.inspect %>
<%- end -%>
<%- if @sidekiq_on_replica[:replica] -%>
replica: true
<%- end -%>
<% end -%>
<%- end -%>
5 changes: 4 additions & 1 deletion templates/default/database.yml.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<%=
config = {}
(['development', 'production'] + Array.wrap(@environment)).select(&:present?).uniq.each do |env|
config[env] = @database
config[env] = {
primary: @database,
primary_replica: @replica
}
end

if @factory_database
Expand Down