Skip to content

Commit

Permalink
Feature/check for db role on servers in deployrb (#50)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
NorseGaud authored Mar 9, 2018
1 parent 1e194ef commit 75bf033
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/>
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

Expand Down
6 changes: 3 additions & 3 deletions lib/capistrano/postgresql/helper_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/postgresql/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Capistrano
module Postgresql
VERSION = '4.8.1'
VERSION = '4.9.0'
end
end
26 changes: 17 additions & 9 deletions lib/capistrano/tasks/postgresql.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 75bf033

Please sign in to comment.