Created
August 26, 2016 17:37
-
-
Save cpruitt/409f88436a49e5b78a1d1aa82e3ba4be to your computer and use it in GitHub Desktop.
Capistrano Tasks for .yml Config File Setup & DB Seed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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