Skip to content

Instantly share code, notes, and snippets.

@cpruitt
Created August 26, 2016 17:37
Show Gist options
  • Select an option

  • Save cpruitt/409f88436a49e5b78a1d1aa82e3ba4be to your computer and use it in GitHub Desktop.

Select an option

Save cpruitt/409f88436a49e5b78a1d1aa82e3ba4be to your computer and use it in GitHub Desktop.
Capistrano Tasks for .yml Config File Setup & DB Seed
# config/deploy/templates/database.yml.template
default: &default
adapter: postgresql
encoding: unicode
pool: 5
<%= db_stage %>:
<<: *default
host: localhost
database: <%= db_name %>
username: <%= db_user %>
password: <%= db_pass %>
# config/deploy/templates/secrets.yml.template
# Be sure to restart server when you modify this file.
default: &default
<%= secret_stage %>:
<<: *default
secret_key_base: <%= secret_key_base %>
# lib/capistrano/tasks/setup.rake
set :template_path, File.expand_path('config/deploy/templates')
namespace :setup do
namespace :config do
desc "Upload database.yml file."
task :database do
target_path = "#{shared_path}/config"
if target_path =~ /\~/
puts "INVALID: " + shared_path.to_s
puts "`deploy_to` path can not be relative using `~`."
puts " Must be complete path from `/`"
else
on roles(:app) do
if ! test "[ -f #{target_path}/database.yml ]"
file_path = File.expand_path('database.yml.template', fetch(:template_path))
template = File.read(file_path)
ask(:db_name, nil)
ask(:db_user, nil)
ask(:db_pass, nil)
db_name = fetch(:db_name)
db_user = fetch(:db_user)
db_pass = fetch(:db_pass)
db_stage = fetch(:stage)
config_file_contents = ERB.new(template).result(binding)
execute "mkdir -p #{target_path}"
contents = StringIO.new(config_file_contents)
upload! contents, "#{target_path}/database.yml"
else
puts "database.yml file already exists."
end
end
end
end
task :secrets do
target_path = "#{shared_path}/config"
if target_path =~ /\~/
puts "INVALID: " + shared_path.to_s
puts "`deploy_to` path can not be relative using `~`."
puts " Must be complete path from `/`"
else
on roles(:app) do
if ! test "[ -f #{target_path}/secrets.yml ]"
file_path = File.expand_path('secrets.yml.template', fetch(:template_path))
template = File.read(file_path)
secret_key_base = `rake secret`
secret_stage = fetch(:stage)
config_file_contents = ERB.new(template).result(binding)
execute "mkdir -p #{target_path}"
contents = StringIO.new(config_file_contents)
upload! contents, "#{target_path}/secrets.yml"
else
puts "database.yml file already exists."
end
end
end
end
end
namespace :db do
desc "Seed the database."
task :seed do
on roles(:app) do
within "#{current_path}" do
with rails_env: fetch(:stage) do
execute :rake, "db:seed"
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment