From 75bf033dbbfe6222327bb50ba4a9e9b12768bc78 Mon Sep 17 00:00:00 2001 From: Nathan Pierce Date: Fri, 9 Mar 2018 11:32:57 -0500 Subject: [PATCH] Feature/check for db role on servers in deployrb (#50) * Added check for :db role in issues/20 proper rails_env now shows in cap STDOUT * Added check for at least one :app role server Added check for :db role in issues/20 proper rails_env now shows in cap STDOUT * Added check for at least one :app role server Added check for :db role in issues/20 proper rails_env now shows in cap STDOUT better error reporting for username creation * ReadMe update for :app and :db requirements * Readme updates for clarity * Readme updates for clarity * Readme updates for clarity * Readme updates for clarity --- README.md | 21 +++++++---------- lib/capistrano/postgresql/helper_methods.rb | 6 ++--- lib/capistrano/postgresql/version.rb | 2 +- lib/capistrano/tasks/postgresql.rake | 26 ++++++++++++++------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index d5eb528..972199f 100644 --- a/README.md +++ b/README.md @@ -42,20 +42,17 @@ Then: If you're deploying a standard rails app, all you need to do is put the following in `Capfile` file: - require 'capistrano/postgresql' +``` +require 'capistrano/postgresql' +``` -Make sure the `deploy_to` path exists and has the right privileges on the -server (i.e. `/var/www/myapp`). Warning: The ~ symbol (i.e. `~/myapp`) is not supported.
-Or just install -[capistrano-safe-deploy-to](https://github.com/capistrano-plugins/capistrano-safe-deploy-to) -plugin and don't think about it. +* Make sure the `deploy_to` path exists and has the right privileges on the +server (i.e. `/var/www/myapp`). Warning: The ~ symbol (i.e. `~/myapp`) is not supported. +* Within your app/config/deploy/{env}.rb files, you need to specify at least one :app and one :db server. +* It's also suggested to specify `:primary => true` on the end of your primary :db server line. +* Optionally, you can run psql commands WITHOUT sudo if needed. Set the following (which defaults to false): `set :pg_without_sudo, true` - -Optionally, you can run psql commands WITHOUT sudo if needed. Set the following: - - set :pg_without_sudo, true # defaults to false - -To setup the server(s), run: +Finally, to setup the server(s), run: $ bundle exec cap production setup diff --git a/lib/capistrano/postgresql/helper_methods.rb b/lib/capistrano/postgresql/helper_methods.rb index 99ee0d3..a2b07bd 100644 --- a/lib/capistrano/postgresql/helper_methods.rb +++ b/lib/capistrano/postgresql/helper_methods.rb @@ -49,13 +49,13 @@ def pg_template(update=false,archetype_file=nil) # location of database.yml file on clients def database_yml_file - raise(":deploy_to in your app/config/deploy/\#{environment}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27 + raise(":deploy_to in your app/config/deploy/#{fetch(:rails_env)}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27 shared_path.join('config/database.yml') end - # location of archetypical database.yml file created on primary db role when user and database are first created + # location of archetypal database.yml file created on primary db role when user and database are first created def archetype_database_yml_file - raise(":deploy_to in your app/config/deploy/\#{environment}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27 + raise(":deploy_to in your app/config/deploy/#{fetch(:rails_env)}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27 deploy_path.join('db/database.yml') end end diff --git a/lib/capistrano/postgresql/version.rb b/lib/capistrano/postgresql/version.rb index c070d49..532728b 100644 --- a/lib/capistrano/postgresql/version.rb +++ b/lib/capistrano/postgresql/version.rb @@ -1,5 +1,5 @@ module Capistrano module Postgresql - VERSION = '4.8.1' + VERSION = '4.9.0' end end diff --git a/lib/capistrano/tasks/postgresql.rake b/lib/capistrano/tasks/postgresql.rake index 8ee8c53..042f08e 100644 --- a/lib/capistrano/tasks/postgresql.rake +++ b/lib/capistrano/tasks/postgresql.rake @@ -123,7 +123,7 @@ namespace :postgresql do next if db_user_exists? # If you use CREATE USER instead of CREATE ROLE the LOGIN right is granted automatically; otherwise you must specify it in the WITH clause of the CREATE statement. unless psql_on_db fetch(:pg_system_db), '-c', %Q{"CREATE USER \\"#{fetch(:pg_username)}\\" PASSWORD '#{fetch(:pg_password)}';"} - error 'postgresql: creating database user failed!' + error "postgresql: creating database user \"#{fetch(:pg_username)}\" failed!" exit 1 end end @@ -165,14 +165,22 @@ namespace :postgresql do desc 'Postgresql setup tasks' task :setup do - puts "* ============================= * \n All psql commands will be run #{fetch(:pg_without_sudo) ? 'without sudo' : 'with sudo'}\n You can modify this in your deploy/{env}.rb by setting the pg_without_sudo boolean \n* ============================= *" - invoke 'postgresql:remove_app_database_yml_files' # Deletes old yml files from all app role servers. Allows you to avoid having to manually delete the files on your app servers to get a new pool size for example. Don't touch the archetype file to avoid deleting generated passwords - invoke 'postgresql:create_db_user' - invoke 'postgresql:create_database' - invoke 'postgresql:add_hstore' - invoke 'postgresql:add_extensions' - invoke 'postgresql:generate_database_yml_archetype' - invoke 'postgresql:generate_database_yml' + puts "* ============================= * \n All psql commands will be run #{fetch(:pg_without_sudo) ? 'without sudo' : 'with sudo'}\n You can modify this in your app/config/deploy/#{fetch(:rails_env)}.rb by setting the pg_without_sudo boolean \n* ============================= *" + if release_roles(:app).empty? + puts "There are no servers in your app/config/deploy/#{fetch(:rails_env)}.rb with a :app role... Skipping Postgresql setup." + else + invoke 'postgresql:remove_app_database_yml_files' # Deletes old yml files from all servers. Allows you to avoid having to manually delete the files on your app servers to get a new pool size for example. Don't touch the archetype file to avoid deleting generated passwords. + if release_roles(:db).empty? # Test to be sure we have a :db role host + puts "There is no server in your app/config/deploy/#{fetch(:rails_env)}.rb with a :db role... Skipping Postgresql setup." + else + invoke 'postgresql:create_db_user' + invoke 'postgresql:create_database' + invoke 'postgresql:add_hstore' + invoke 'postgresql:add_extensions' + invoke 'postgresql:generate_database_yml_archetype' + invoke 'postgresql:generate_database_yml' + end + end end end