Skip to content

Commit 75bf033

Browse files
authored
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
1 parent 1e194ef commit 75bf033

4 files changed

Lines changed: 30 additions & 25 deletions

File tree

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,17 @@ Then:
4242
If you're deploying a standard rails app, all you need to do is put
4343
the following in `Capfile` file:
4444

45-
require 'capistrano/postgresql'
45+
```
46+
require 'capistrano/postgresql'
47+
```
4648

47-
Make sure the `deploy_to` path exists and has the right privileges on the
48-
server (i.e. `/var/www/myapp`). Warning: The ~ symbol (i.e. `~/myapp`) is not supported.<br/>
49-
Or just install
50-
[capistrano-safe-deploy-to](https://github.com/capistrano-plugins/capistrano-safe-deploy-to)
51-
plugin and don't think about it.
49+
* Make sure the `deploy_to` path exists and has the right privileges on the
50+
server (i.e. `/var/www/myapp`). Warning: The ~ symbol (i.e. `~/myapp`) is not supported.
51+
* Within your app/config/deploy/{env}.rb files, you need to specify at least one :app and one :db server.
52+
* It's also suggested to specify `:primary => true` on the end of your primary :db server line.
53+
* Optionally, you can run psql commands WITHOUT sudo if needed. Set the following (which defaults to false): `set :pg_without_sudo, true`
5254

53-
54-
Optionally, you can run psql commands WITHOUT sudo if needed. Set the following:
55-
56-
set :pg_without_sudo, true # defaults to false
57-
58-
To setup the server(s), run:
55+
Finally, to setup the server(s), run:
5956

6057
$ bundle exec cap production setup
6158

lib/capistrano/postgresql/helper_methods.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ def pg_template(update=false,archetype_file=nil)
4949

5050
# location of database.yml file on clients
5151
def database_yml_file
52-
raise(":deploy_to in your app/config/deploy/\#{environment}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
52+
raise(":deploy_to in your app/config/deploy/#{fetch(:rails_env)}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
5353
shared_path.join('config/database.yml')
5454
end
5555

56-
# location of archetypical database.yml file created on primary db role when user and database are first created
56+
# location of archetypal database.yml file created on primary db role when user and database are first created
5757
def archetype_database_yml_file
58-
raise(":deploy_to in your app/config/deploy/\#{environment}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
58+
raise(":deploy_to in your app/config/deploy/#{fetch(:rails_env)}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
5959
deploy_path.join('db/database.yml')
6060
end
6161
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Capistrano
22
module Postgresql
3-
VERSION = '4.8.1'
3+
VERSION = '4.9.0'
44
end
55
end

lib/capistrano/tasks/postgresql.rake

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ namespace :postgresql do
123123
next if db_user_exists?
124124
# 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.
125125
unless psql_on_db fetch(:pg_system_db), '-c', %Q{"CREATE USER \\"#{fetch(:pg_username)}\\" PASSWORD '#{fetch(:pg_password)}';"}
126-
error 'postgresql: creating database user failed!'
126+
error "postgresql: creating database user \"#{fetch(:pg_username)}\" failed!"
127127
exit 1
128128
end
129129
end
@@ -165,14 +165,22 @@ namespace :postgresql do
165165

166166
desc 'Postgresql setup tasks'
167167
task :setup do
168-
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* ============================= *"
169-
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
170-
invoke 'postgresql:create_db_user'
171-
invoke 'postgresql:create_database'
172-
invoke 'postgresql:add_hstore'
173-
invoke 'postgresql:add_extensions'
174-
invoke 'postgresql:generate_database_yml_archetype'
175-
invoke 'postgresql:generate_database_yml'
168+
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* ============================= *"
169+
if release_roles(:app).empty?
170+
puts "There are no servers in your app/config/deploy/#{fetch(:rails_env)}.rb with a :app role... Skipping Postgresql setup."
171+
else
172+
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.
173+
if release_roles(:db).empty? # Test to be sure we have a :db role host
174+
puts "There is no server in your app/config/deploy/#{fetch(:rails_env)}.rb with a :db role... Skipping Postgresql setup."
175+
else
176+
invoke 'postgresql:create_db_user'
177+
invoke 'postgresql:create_database'
178+
invoke 'postgresql:add_hstore'
179+
invoke 'postgresql:add_extensions'
180+
invoke 'postgresql:generate_database_yml_archetype'
181+
invoke 'postgresql:generate_database_yml'
182+
end
183+
end
176184
end
177185
end
178186

0 commit comments

Comments
 (0)