Skip to content

Commit ae71112

Browse files
authored
## v5.0.0, 2018-06-05 (#54)
- Code cleanup - Removal of legacy add hstore method - Using execute and test properly so we can see what the gem is doing in the STDOUT - Expanded remove_all task to actually cover everything - Added deploy config option pg_generate_random_password, instead of using it by default when pg_password is excluded - issues/53: Bug fixed for updates to the archetype when using random password - projects/1: Prep for RSPEC testing project
1 parent 80f2b99 commit ae71112

File tree

7 files changed

+123
-111
lines changed

7 files changed

+123
-111
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
### master
44

5+
## v5.0.0, 2018-06-05
6+
- Code cleanup
7+
- Removal of legacy add hstore method
8+
- Using execute and test properly so we can see what the gem is doing in the STDOUT
9+
- Expanded remove_all task to actually cover everything
10+
- Added deploy config option pg_generate_random_password, instead of using it by default when pg_password is excluded
11+
- issues/53: Bug fixed for updates to the archetype when using random password
12+
- projects/1: Prep for RSPEC testing project
13+
514
## v4.9.1, 2018-06-04
615
- Added back set :pg_ask_for_password, false and ask_for_or_generate_password
716

README.md

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Capistrano::PostgreSQL
22

3-
**Note: this plugin works only with Capistrano 3.** Plase check the capistrano
3+
**Note: this plugin works only with Capistrano 3.** Please check the capistrano
44
gem version you're using before installing this gem:
55
`$ bundle show | grep capistrano`
6-
7-
Plugin for Capistrano 2 [is here](https://github.com/bruno-/capistrano2-postgresql).
6+
The plugin for Capistrano 2 [is here](https://github.com/bruno-/capistrano2-postgresql).
87

98
### About
109

@@ -14,11 +13,9 @@ tasks for PostgreSQL when deploying rails apps.
1413
Here are the specific things this plugin does for your capistrano deployment
1514
process:
1615

17-
* creates a new PostgreSQL database and database user on the server
18-
* generates and populates `database.yml` file on all release nodes
19-
(no need to ssh to the server and do this manually!)
20-
* zero-config
21-
* support for multi-server setup: separate `db` and `app` nodes (from version 4.0)
16+
* Creates a new PostgreSQL database and database user on the server
17+
* Generates and populates `database.yml` file on all release nodes (using ssh)
18+
* Support for multi-server setup: separate `db` and `app` nodes ( versions > 4.0 )
2219

2320
**Note**: gem version 4 introduces some breaking changes. If you installed gem
2421
version 3 or below you might want to follow the
@@ -30,7 +27,7 @@ Put the following in your application's `Gemfile`:
3027

3128
group :development do
3229
gem 'capistrano', '~> 3.2.0'
33-
gem 'capistrano-postgresql', '~> 4.8.0'
30+
gem 'capistrano-postgresql', '~> 5.0.0'
3431
end
3532

3633
Then:
@@ -39,31 +36,55 @@ Then:
3936

4037
### Usage
4138

42-
If you're deploying a standard rails app, all you need to do is put
43-
the following in `Capfile` file:
39+
In a standard RAILS app, you need to do is put the following in `Capfile` file:
4440

4541
```
4642
require 'capistrano/postgresql'
4743
```
4844

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`
45+
You need to include ONLY ONE of the following in your config/deploy/*.rb files:
46+
47+
```
48+
set :pg_password, ENV['DATABASE_USER_PASSWORD']
49+
set :pg_ask_for_password, true
50+
set :pg_generate_random_password, true
51+
```
52+
53+
Example config:
54+
55+
```
56+
server 'growtrader.dev', user: 'growtrader', roles: %w{app db}
57+
set :stage, :development
58+
set :branch, 'development'
59+
# ==================
60+
# Postgresql setup
61+
set :pg_without_sudo, false
62+
set :pg_host, 'growtrader.dev'
63+
set :pg_database, 'growtrader'
64+
set :pg_username, 'growtrader'
65+
#set :pg_generate_random_password, true
66+
#set :pg_ask_for_password, true
67+
set :pg_password, ENV['GROWTRADER_PGPASS']
68+
set :pg_extensions, ['citext','hstore']
69+
set :pg_encoding, 'UTF-8'
70+
set :pg_pool, '100'
71+
```
5472

5573
Finally, to setup the server(s), run:
5674

5775
$ bundle exec cap production setup
5876

59-
### Gotchas
77+
### Requirements
6078

61-
Be sure to remove `config/database.yml` from your application's version control.
79+
* Be sure to remove `config/database.yml` from your application's version control.
80+
* Your pg_hba.conf must include `local all all trust`
81+
* Make sure the `deploy_to` path exists and has the right privileges on your servers. The ~ symbol (i.e. `~/myapp`) is not supported.
82+
* Within your app/config/deploy/{env}.rb files, you need to specify at least one :app and one :db server.
83+
* If you have multiple :db role hosts, it's necessary to specify `:primary => true` on the end of your primary :db server.
6284

6385
### How it works
6486

6587
[How the plugin works](https://github.com/capistrano-plugins/capistrano-postgresql/wiki/How-it-works)
66-
wiki page contains a list of actions the plugin executes.
6788

6889
Read it only if you want to learn more about the plugin internals.
6990

@@ -87,7 +108,7 @@ Check out [capistrano-plugins](https://github.com/capistrano-plugins) github org
87108

88109
Contributions and improvements are very welcome.
89110

90-
If something is not working for you, or you find a bug please report it.
111+
If something is not working for you, or you find a bug, please report it.
91112

92113
### Thanks
93114

lib/capistrano/postgresql/helper_methods.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ module Capistrano
44
module Postgresql
55
module HelperMethods
66

7+
def extension_exists?(extension)
8+
psql 'test', fetch(:pg_system_db), '-tAc', %Q{"SELECT 1 FROM pg_extension WHERE extname='#{extension}';" | grep -q 1}
9+
end
10+
11+
def remove_extensions
12+
if Array( fetch(:pg_extensions) ).any?
13+
on roles :db do
14+
# remove in reverse order if extension is present
15+
Array( fetch(:pg_extensions) ).reverse.each do |ext|
16+
next if [nil, false, ""].include?(ext)
17+
psql 'execute', fetch(:pg_system_db), '-c', %Q{"DROP EXTENSION IF EXISTS #{ext};"} if extension_exists?(ext)
18+
end
19+
end
20+
end
21+
end
22+
723
def generate_database_yml_io(password=fetch(:pg_password))
824
StringIO.open do |s|
925
s.puts "#{fetch(:pg_env)}:"
@@ -26,10 +42,10 @@ def generate_database_yml_io(password=fetch(:pg_password))
2642
def pg_template(update=false,archetype_file=nil)
2743
config_file = "#{fetch(:pg_templates_path)}/postgresql.yml.erb"
2844
if update
29-
raise('Updates need the original file to update from.') if archetype_file.nil?
45+
raise('Regeneration of archetype database.yml need the original file to update from.') if archetype_file.nil?
3046
raise('Cannot update a custom postgresql.yml.erb file.') if File.exists?(config_file) # Skip custom postgresql.yml.erb if we're updating. It's not supported
3147
# Update yml file from settings
32-
if fetch(:pg_password).nil? && fetch(:pg_ask_for_password) == false # User isn't generating a random password or wanting to set it manually from prompt
48+
if fetch(:pg_generate_random_password) || !fetch(:pg_password) # We need to prevent updating the archetype file if we've done a random or "ask"ed password
3349
current_password = archetype_file.split("\n").grep(/password/)[0].split('password:')[1].strip
3450
generate_database_yml_io(current_password)
3551
else

lib/capistrano/postgresql/password_helpers.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ def generate_random_password
88
SecureRandom.hex(10)
99
end
1010

11-
# This method is invoked only if :pg_password is not already set in config/#{:stage}/deploy.rb. Directly setting :pg_password has precedence.
12-
def ask_for_or_generate_password
11+
def pg_password_generate
1312
if fetch(:pg_ask_for_password)
1413
ask :pg_password, "Postgresql database password for the app: "
15-
else
14+
elsif fetch(:pg_generate_random_password)
1615
set :pg_password, generate_random_password
16+
else
17+
set :pg_password, nil # Necessary for pg_template
1718
end
1819
end
19-
2020
end
2121
end
2222
end

lib/capistrano/postgresql/psql_helpers.rb

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,28 @@ module Capistrano
22
module Postgresql
33
module PsqlHelpers
44

5-
def psql(*args)
6-
# Reminder: -u #{fetch(:pg_system_user)} seen below differs slightly from -U, an option on the psql command: https://www.postgresql.org/docs/9.6/static/app-psql.html
7-
args.unshift("-U #{fetch(:pg_system_user)}") if fetch(:pg_without_sudo) # Add the :pg_system_user to psql command since we aren't using sudo anymore
8-
# test :sudo, "-u #{fetch(:pg_system_user)} psql", *args
9-
cmd = [ :psql, *args ]
10-
cmd = [ :sudo, "-u #{fetch(:pg_system_user)}", *cmd ] unless fetch(:pg_without_sudo)
11-
test *cmd.flatten
12-
end
13-
14-
# Runs psql on the application database
15-
def psql_on_app_db(*args)
16-
psql_on_db(fetch(:pg_database), *args)
5+
def psql(type, database, *args)
6+
cmd = [ :psql, "-d #{database}", *args ]
7+
if fetch(:pg_without_sudo)
8+
args.unshift("-U #{fetch(:pg_system_user)}") # Add the :pg_system_user to psql command since we aren't using sudo anymore
9+
else
10+
cmd = [:sudo, "-i -u #{fetch(:pg_system_user)}", *cmd]
11+
end
12+
if type == 'test'
13+
test *cmd.flatten
14+
else
15+
execute *cmd.flatten
16+
end
1717
end
1818

19-
def db_user_exists?
20-
psql_on_db fetch(:pg_system_db),'-tAc', %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{fetch(:pg_username)}';" | grep -q 1}
19+
def database_user_exists?
20+
psql 'test', fetch(:pg_system_db),'-tAc', %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{fetch(:pg_username)}';" | grep -q 1}
2121
end
2222

2323
def database_exists?
24-
psql_on_db fetch(:pg_system_db), '-tAc', %Q{"SELECT 1 FROM pg_database WHERE datname='#{fetch(:pg_database)}';" | grep -q 1}
24+
psql 'test', fetch(:pg_system_db), '-tAc', %Q{"SELECT 1 FROM pg_database WHERE datname='#{fetch(:pg_database)}';" | grep -q 1}
2525
end
2626

27-
private
28-
29-
def psql_on_db(db_name, *args)
30-
args.unshift("-U #{fetch(:pg_system_user)}") if fetch(:pg_without_sudo) # Add the :pg_system_user to psql command since we aren't using sudo anymore
31-
cmd = [ :psql, "-d #{db_name}", *args ]
32-
cmd = [ :sudo, "-u #{fetch(:pg_system_user)}", *cmd ] unless fetch(:pg_without_sudo)
33-
puts "Executing #{cmd.flatten}"
34-
test *cmd.flatten
35-
#test :sudo, "-u #{fetch(:pg_system_user)} psql -d #{db_name}", *args
36-
end
37-
3827
end
3928
end
4029
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.9.2'
3+
VERSION = '5.0.0'
44
end
55
end

0 commit comments

Comments
 (0)