capistrano-postgresql 4.7.0 → 4.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc75baf6e5ae3f4072935434e99527a40d17ab23997b006bc5056ccaadcfaa10
4
- data.tar.gz: f310c43d82487fde14eedbe428e10278efb2429dc0625c99f5f04e77eb8ee70d
3
+ metadata.gz: 99782e17f1cfadc9a7bef273471fc8c0d9327badfb8cc1aaabd0e5d1d71b1e16
4
+ data.tar.gz: b243ef51314de1d4924727f9a693e086893f05180d736c0a2789d125f6d16a9f
5
5
  SHA512:
6
- metadata.gz: 4134b4d0711f378706881eaf7949162a7edaeeb365a1c8f6c61855f1753e6876d0ca8a5d6932d606289c1e97688834ac1707e1b23a8d1fb7dfce45cbdd51cf99
7
- data.tar.gz: 39847b45d9d15eb0371e2b06af11c1e944db256ca758fecb026b1e30bd49ad4b9ec20207373a40ac56459c2478510583b6b01dd2984c71d6ffebc3cb403e9149
6
+ metadata.gz: 158247310972ce9e07dd08a3cf1d6f9d169149d110121bf62b56a0af8be85cd3860c9e7b114be14dd68460631eaa09bd406910a5a41bac39671908b5c04d89c2
7
+ data.tar.gz: 6e9b98484fae0fd73af2cd2a97c03a1318e0ece3422d33b483633d3e6f1492c77e6bb2cbf4315db97c4793ae83f5b2c532100f2dec8c60db6da93657cfc22775
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ### master
4
4
 
5
+ ## v4.8.0, 2017-12-28
6
+ - issues/47: Added new pg_template helper code to handle maintaining the randomly generated password, :pg_ask_for_password, and user set pg_password
7
+ - Added the rest of the supported options for the database.yml
8
+ - pull/46 (Thanks to Tom Prats / tomprats): Fix for pg_host when localhost is used
9
+ - Removed system_user as it's not necessary if server values are defined properly in config/deploy/* files
10
+ - General cleanup of notes
11
+
12
+ ## v4.7.0, 2017-12-19
13
+ - Fixed create database and user tasks to use (:pg_system_db) and psql_on_db
14
+
5
15
  ## v4.6.1, 2017-12-15
6
16
  - Removing require 'pry' (silly mistake)
7
17
 
data/README.md CHANGED
@@ -30,7 +30,7 @@ Put the following in your application's `Gemfile`:
30
30
 
31
31
  group :development do
32
32
  gem 'capistrano', '~> 3.2.0'
33
- gem 'capistrano-postgresql', '~> 4.2.0'
33
+ gem 'capistrano-postgresql', '~> 4.8.0'
34
34
  end
35
35
 
36
36
  Then:
@@ -50,9 +50,6 @@ Or just install
50
50
  [capistrano-safe-deploy-to](https://github.com/capistrano-plugins/capistrano-safe-deploy-to)
51
51
  plugin and don't think about it.
52
52
 
53
- Within your app/config/deploy/#{environment}.rb files, make sure to specify a user with ssh access to all deployment servers:
54
-
55
- set :system_user, 'ssh_user' # defaults to root
56
53
 
57
54
  Optionally, you can run psql commands WITHOUT sudo if needed. Set the following:
58
55
 
@@ -4,15 +4,47 @@ module Capistrano
4
4
  module Postgresql
5
5
  module HelperMethods
6
6
 
7
- def pg_template(template_name)
8
- config_file = "#{fetch(:pg_templates_path)}/#{template_name}"
9
- # If there's no customized file in your rails app template directory,
10
- # proceed with the default.
11
- unless File.exists?(config_file)
12
- default_config_path = "../../generators/capistrano/postgresql/templates/#{template_name}"
13
- config_file = File.join(File.dirname(__FILE__), default_config_path)
7
+ def generate_database_yml_io(password=fetch(:pg_password))
8
+ StringIO.open do |s|
9
+ s.puts "#{fetch(:pg_env)}:"
10
+ {
11
+ adapter: 'postgresql',
12
+ encoding: fetch(:pg_encoding),
13
+ database: fetch(:pg_database),
14
+ pool: fetch(:pg_pool),
15
+ username: fetch(:pg_username),
16
+ password: password,
17
+ host: fetch(:pg_host),
18
+ socket: fetch(:pg_socket),
19
+ port: fetch(:pg_port),
20
+ timeout: fetch(:pg_timeout)
21
+ }.each { |option_name,option_value| s.puts " #{option_name}: #{option_value}" } # Yml does not support tabs. There are two spaces leading the config option line
22
+ s.string
14
23
  end
15
- StringIO.new ERB.new(File.read(config_file)).result(binding)
24
+ end
25
+
26
+ def pg_template(update=false,archetype_file=nil)
27
+ config_file = "#{fetch(:pg_templates_path)}/postgresql.yml.erb"
28
+ if update
29
+ raise('Updates need the original file to update from.') if archetype_file.nil?
30
+ 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
31
+ # 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
33
+ current_password = archetype_file.split("\n").grep(/password/)[0].split('password:')[1].strip
34
+ generate_database_yml_io(current_password)
35
+ else
36
+ generate_database_yml_io
37
+ end
38
+ else
39
+ if File.exists?(config_file) # If there is a customized file in your rails app template directory, use it and convert any ERB
40
+ StringIO.new ERB.new(File.read(config_file)).result(binding)
41
+ else # Else there's no customized file in your rails app template directory, proceed with the default.
42
+ # Build yml file from settings
43
+ ## We build the file line by line to avoid overwriting existing files
44
+ generate_database_yml_io
45
+ end
46
+ end
47
+
16
48
  end
17
49
 
18
50
  # location of database.yml file on clients
@@ -21,8 +53,7 @@ module Capistrano
21
53
  shared_path.join('config/database.yml')
22
54
  end
23
55
 
24
- # location of archetypical database.yml file created on primary db role when user and
25
- # database are first created
56
+ # location of archetypical database.yml file created on primary db role when user and database are first created
26
57
  def archetype_database_yml_file
27
58
  raise(":deploy_to in your app/config/deploy/\#{environment}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
28
59
  deploy_path.join('db/database.yml')
@@ -8,9 +8,7 @@ module Capistrano
8
8
  SecureRandom.hex(10)
9
9
  end
10
10
 
11
- # This method is invoked only if `:pg_password` is not already
12
- # set in `config/#{:stage}/deploy.rb`. Directly setting
13
- # `:pg_password` has precedence.
11
+ # This method is invoked only if :pg_password is not already set in config/#{:stage}/deploy.rb. Directly setting :pg_password has precedence.
14
12
  def ask_for_or_generate_password
15
13
  if fetch(:pg_ask_for_password)
16
14
  ask :pg_password, "Postgresql database password for the app: "
@@ -17,7 +17,7 @@ module Capistrano
17
17
  end
18
18
 
19
19
  def db_user_exists?
20
- psql_on_db fetch(:pg_system_db),'-tAc', %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{fetch(:pg_user)}';" | grep -q 1}
20
+ psql_on_db fetch(:pg_system_db),'-tAc', %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{fetch(:pg_username)}';" | grep -q 1}
21
21
  end
22
22
 
23
23
  def database_exists?
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module Postgresql
3
- VERSION = '4.7.0'
3
+ VERSION = '4.8.1'
4
4
  end
5
5
  end
@@ -8,29 +8,27 @@ include Capistrano::Postgresql::PsqlHelpers
8
8
 
9
9
  namespace :load do
10
10
  task :defaults do
11
- set :system_user, 'root' # Used for SCP commands to deploy files to remote servers
11
+ # Options necessary for database.yml creation (pg_template|helper_methods.rb)
12
+ set :pg_env, -> { fetch(:rails_env) || fetch(:stage) }
13
+ set :pg_encoding, 'unicode'
12
14
  set :pg_database, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
13
- set :pg_user, -> { fetch(:pg_database) }
14
- set :pg_ask_for_password, false
15
- set :pg_password, -> { ask_for_or_generate_password }
16
- set :pg_system_user, 'postgres'
15
+ set :pg_pool, 5
16
+ set :pg_username, -> { fetch(:pg_database) }
17
+ set :pg_password, nil
18
+ set :pg_socket, ''
19
+ set :pg_host, -> do # for multiple release nodes automatically use server hostname (IP?) in the database.yml
20
+ release_roles(:all).count == 1 && release_roles(:all).first == primary(:db) ? 'localhost' : primary(:db).hostname
21
+ end
22
+ set :pg_port, 5432
23
+ set :pg_timeout, 5000 # 5 seconds (rails default)
24
+ # General settings
17
25
  set :pg_without_sudo, false # issues/22 | Contributed by snake66
26
+ set :pg_system_user, 'postgres'
27
+ set :pg_ask_for_password, false
18
28
  set :pg_system_db, 'postgres'
19
29
  set :pg_use_hstore, false
20
30
  set :pg_extensions, []
21
- # template only settings
22
31
  set :pg_templates_path, 'config/deploy/templates'
23
- set :pg_env, -> { fetch(:rails_env) || fetch(:stage) }
24
- set :pg_pool, 5
25
- set :pg_encoding, 'unicode'
26
- # for multiple release nodes automatically use server hostname (IP?) in the database.yml
27
- set :pg_host, -> do
28
- if release_roles(:all).count == 1 && release_roles(:all).first == primary(:db)
29
- 'localhost'
30
- else
31
- primary(:db).hostname
32
- end
33
- end
34
32
  end
35
33
  end
36
34
 
@@ -60,21 +58,17 @@ namespace :postgresql do
60
58
 
61
59
  on roles :db do
62
60
  psql '-c', %Q{"DROP database \\"#{fetch(:pg_database)}\\";"}
63
- psql '-c', %Q{"DROP user \\"#{fetch(:pg_user)}\\";"}
61
+ psql '-c', %Q{"DROP user \\"#{fetch(:pg_username)}\\";"}
64
62
  end
65
63
  end
66
64
 
67
- task :remove_yml_files do
68
- on release_roles :all do
65
+ task :remove_app_database_yml_files do
66
+ # We should never delete archetype files. The generate_database_yml_archetype task will handle updates
67
+ on release_roles :app do
69
68
  if test "[ -e #{database_yml_file} ]"
70
69
  execute :rm, database_yml_file
71
70
  end
72
71
  end
73
- on primary :db do
74
- if test "[ -e #{archetype_database_yml_file} ]"
75
- execute :rm, archetype_database_yml_file
76
- end
77
- end
78
72
  end
79
73
 
80
74
  desc 'Remove pg_extension from postgresql db'
@@ -101,7 +95,7 @@ namespace :postgresql do
101
95
  next unless Array( fetch(:pg_extensions) ).any?
102
96
  on roles :db do
103
97
  Array( fetch(:pg_extensions) ).each do |ext|
104
- next if [nil, false, ""].include?(ext)
98
+ next if [nil, false, ''].include?(ext)
105
99
  if psql_on_app_db '-c', %Q{"CREATE EXTENSION IF NOT EXISTS #{ext};"}
106
100
  puts "- Added extension #{ext} to #{fetch(:pg_database)}"
107
101
  else
@@ -116,7 +110,7 @@ namespace :postgresql do
116
110
  task :create_database do
117
111
  on roles :db do
118
112
  next if database_exists?
119
- unless psql_on_db fetch(:pg_system_db), '-c', %Q{"CREATE DATABASE \\"#{fetch(:pg_database)}\\" OWNER \\"#{fetch(:pg_user)}\\";"}
113
+ unless psql_on_db fetch(:pg_system_db), '-c', %Q{"CREATE DATABASE \\"#{fetch(:pg_database)}\\" OWNER \\"#{fetch(:pg_username)}\\";"}
120
114
  error 'postgresql: creating database failed!'
121
115
  exit 1
122
116
  end
@@ -128,26 +122,28 @@ namespace :postgresql do
128
122
  on roles :db do
129
123
  next if db_user_exists?
130
124
  # 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.
131
- unless psql_on_db fetch(:pg_system_db), '-c', %Q{"CREATE USER \\"#{fetch(:pg_user)}\\" PASSWORD '#{fetch(:pg_password)}';"}
125
+ unless psql_on_db fetch(:pg_system_db), '-c', %Q{"CREATE USER \\"#{fetch(:pg_username)}\\" PASSWORD '#{fetch(:pg_password)}';"}
132
126
  error 'postgresql: creating database user failed!'
133
127
  exit 1
134
128
  end
135
129
  end
136
130
  end
137
131
 
138
- # This task creates the archetype database.yml file on the primary db server. This is done once when a
139
- # new DB user is created.
132
+ # This task creates the archetype database.yml file on the primary db server. This is done once when a new DB user is created.
140
133
  desc 'Generate database.yml archetype'
141
134
  task :generate_database_yml_archetype do
142
135
  on primary :db do
143
- next if test "[ -e #{archetype_database_yml_file} ]"
144
- execute :mkdir, '-pv', File.dirname(archetype_database_yml_file)
145
- Net::SCP.upload!(fetch(:pg_host), fetch(:system_user), pg_template('postgresql.yml.erb'), archetype_database_yml_file)
136
+ if test "[ -e #{archetype_database_yml_file} ]" # Archetype already exists. Just update values that changed. Make sure we don't overwrite it to protect generated passwords.
137
+ Net::SCP.upload!(self.host.hostname, self.host.user,StringIO.new(pg_template(true, download!(archetype_database_yml_file))),archetype_database_yml_file)
138
+ else
139
+ ask_for_or_generate_password if fetch(:pg_password).nil? || fetch(:pg_ask_for_password) == true # Avoid setting a random password or one from user prompt
140
+ execute :mkdir, '-pv', File.dirname(archetype_database_yml_file)
141
+ Net::SCP.upload!(self.host.hostname,self.host.user,StringIO.new(pg_template),archetype_database_yml_file)
142
+ end
146
143
  end
147
144
  end
148
145
 
149
- # This task copies the archetype database file on the primary db server to all clients. This is done on
150
- # every setup, to ensure new servers get a copy as well.
146
+ # This task copies the archetype database file on the primary db server to all clients. This is done on every setup, to ensure new servers get a copy as well.
151
147
  desc 'Copy archetype database.yml from primary db server to clients'
152
148
  task :generate_database_yml do
153
149
  database_yml_contents = nil
@@ -157,7 +153,7 @@ namespace :postgresql do
157
153
 
158
154
  on release_roles :all do
159
155
  execute :mkdir, '-pv', File.dirname(database_yml_file)
160
- Net::SCP.upload!(self.host.hostname, fetch(:system_user), StringIO.new(database_yml_contents), database_yml_file)
156
+ Net::SCP.upload!(self.host.hostname, self.host.user, StringIO.new(database_yml_contents), database_yml_file)
161
157
  end
162
158
  end
163
159
 
@@ -170,7 +166,7 @@ namespace :postgresql do
170
166
  desc 'Postgresql setup tasks'
171
167
  task :setup do
172
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* ============================= *"
173
- invoke 'postgresql:remove_yml_files' # Delete old yml files. Allows you to avoid having to manually delete the files on your web/app servers to get a new pool size for example.
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
174
170
  invoke 'postgresql:create_db_user'
175
171
  invoke 'postgresql:create_database'
176
172
  invoke 'postgresql:add_hstore'
@@ -3,6 +3,9 @@
3
3
  encoding: <%= fetch :pg_encoding %>
4
4
  database: <%= fetch :pg_database %>
5
5
  pool: <%= fetch :pg_pool %>
6
- username: <%= fetch :pg_user %>
6
+ username: <%= fetch :pg_username %>
7
7
  password: '<%= fetch :pg_password %>'
8
8
  host: <%= fetch :pg_host %>
9
+ socket: <%= fetch :pg_socket %>
10
+ port: <%= fetch :pg_port %>
11
+ timeout: <%= fetch :pg_timeout %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-postgresql
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.7.0
4
+ version: 4.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Sutic
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-12-18 00:00:00.000000000 Z
12
+ date: 2018-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capistrano
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project:
91
- rubygems_version: 2.7.3
91
+ rubygems_version: 2.7.4
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Creates application database user and `database.yml` on the server. No SSH