capistrano-postgresql 4.2.1 → 4.7.0

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
- SHA1:
3
- metadata.gz: 6c6b91718c3a2c3c0c8733300c1c7a19585e5c27
4
- data.tar.gz: 6f4f36cde7ed8b8c4868b1ad858028cf44d4400f
2
+ SHA256:
3
+ metadata.gz: cc75baf6e5ae3f4072935434e99527a40d17ab23997b006bc5056ccaadcfaa10
4
+ data.tar.gz: f310c43d82487fde14eedbe428e10278efb2429dc0625c99f5f04e77eb8ee70d
5
5
  SHA512:
6
- metadata.gz: 5aeb539216266abc36d13bd9910bb0333cc67d090ba1c28294d5c3675d356419c15355ecf02c57239415e15556d0a82a9742a2a523365134011053a34ca5352f
7
- data.tar.gz: d1ade56e40f24515535ac8627adff68cdfe4c4b2b4085c2b3bc1dfea81a4081877c8e58623cae17a69ba3f91cb61f53f6a72834ad18c626406c1c0cea0cdbd97
6
+ metadata.gz: 4134b4d0711f378706881eaf7949162a7edaeeb365a1c8f6c61855f1753e6876d0ca8a5d6932d606289c1e97688834ac1707e1b23a8d1fb7dfce45cbdd51cf99
7
+ data.tar.gz: 39847b45d9d15eb0371e2b06af11c1e944db256ca758fecb026b1e30bd49ad4b9ec20207373a40ac56459c2478510583b6b01dd2984c71d6ffebc3cb403e9149
@@ -0,0 +1,3 @@
1
+ .idea
2
+ Gemfile.lock
3
+ *.gem
@@ -2,6 +2,30 @@
2
2
 
3
3
  ### master
4
4
 
5
+ ## v4.6.1, 2017-12-15
6
+ - Removing require 'pry' (silly mistake)
7
+
8
+ ## v4.6.0, 2017-12-10
9
+ - :pg_without_sudo added (thanks to snake66) so you can run remote psql commands on environments without sudo available.
10
+ - Fixed exists? methods, removing the need for arguments
11
+ - Printing sudo status to capistrano STDOUT
12
+
13
+ ## v4.4.1, 2017-10-26
14
+ - Switched back from CREATE ROLE to CREATE USER (so LOGIN rights are granted automatically)
15
+
16
+ ## v4.4.0, 2017-10-26
17
+ - :remove_yml_files task added to clean up old files on remote server
18
+
19
+ ## v4.3.1, 2017-10-21
20
+ - Fixed quoting on CREATE USER so we can include integers in usernames & Quoted other values like CREATE DATABASE so they can include integers too
21
+ - Changed to createuser convention "CREATE ROLE" instead of USER (v4.4.1 reversed this)
22
+
23
+ ## v4.3.0, 2017-10-21
24
+ - issues/27: raise on :deploy_to with ~ in it
25
+ - Net::SCP.upload! to replace old upload! for using :system_user
26
+ - issues/31 : psql -> psql_simple so user can be created regardless of database existence
27
+ - General cleanup
28
+
5
29
  ### v4.2.1, 2015-04-22
6
30
  - change `on roles(:db, primary: true)` to the correct `on primary :db`
7
31
 
data/README.md CHANGED
@@ -45,11 +45,19 @@ the following in `Capfile` file:
45
45
  require 'capistrano/postgresql'
46
46
 
47
47
  Make sure the `deploy_to` path exists and has the right privileges on the
48
- server (i.e. `/var/www/myapp`).<br/>
48
+ server (i.e. `/var/www/myapp`). Warning: The ~ symbol (i.e. `~/myapp`) is not supported.<br/>
49
49
  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
+
57
+ Optionally, you can run psql commands WITHOUT sudo if needed. Set the following:
58
+
59
+ set :pg_without_sudo, true # defaults to false
60
+
53
61
  To setup the server(s), run:
54
62
 
55
63
  $ bundle exec cap production setup
@@ -4,10 +4,10 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'capistrano/postgresql/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = "capistrano-postgresql"
7
+ gem.name = 'capistrano-postgresql'
8
8
  gem.version = Capistrano::Postgresql::VERSION
9
- gem.authors = ["Bruno Sutic"]
10
- gem.email = ["bruno.sutic@gmail.com"]
9
+ gem.authors = ['Bruno Sutic', 'Ruben Stranders']
10
+ gem.email = ['bruno.sutic@gmail.com', 'r.stranders@gmail.com']
11
11
  gem.description = <<-EOF.gsub(/^\s+/, '')
12
12
  Capistrano tasks for PostgreSQL configuration and management for Rails
13
13
  apps. Manages `database.yml` template on the server.
@@ -16,14 +16,13 @@ Gem::Specification.new do |gem|
16
16
  https://github.com/bruno-/capistrano2-postgresql
17
17
  EOF
18
18
  gem.summary = %q{Creates application database user and `database.yml` on the server. No SSH login required!}
19
- gem.homepage = "https://github.com/capistrano-plugins/capistrano-postgresql"
19
+ gem.homepage = 'https://github.com/capistrano-plugins/capistrano-postgresql'
20
20
 
21
- gem.license = "MIT"
21
+ gem.license = 'MIT'
22
22
 
23
23
  gem.files = `git ls-files`.split($/)
24
- gem.require_paths = ["lib"]
24
+ gem.require_paths = ['lib']
25
25
 
26
26
  gem.add_dependency 'capistrano', '>= 3.0'
27
-
28
- gem.add_development_dependency "rake"
27
+ gem.add_development_dependency 'rake'
29
28
  end
@@ -17,13 +17,15 @@ module Capistrano
17
17
 
18
18
  # location of database.yml file on clients
19
19
  def database_yml_file
20
+ raise(":deploy_to in your app/config/deploy/\#{environment}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
20
21
  shared_path.join('config/database.yml')
21
22
  end
22
23
 
23
24
  # location of archetypical database.yml file created on primary db role when user and
24
25
  # database are first created
25
26
  def archetype_database_yml_file
26
- deploy_path.join("db/database.yml")
27
+ raise(":deploy_to in your app/config/deploy/\#{environment}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
28
+ deploy_path.join('db/database.yml')
27
29
  end
28
30
  end
29
31
  end
@@ -2,9 +2,13 @@ module Capistrano
2
2
  module Postgresql
3
3
  module PsqlHelpers
4
4
 
5
- # returns true or false depending on the remote command exit status
6
5
  def psql(*args)
7
- psql_on_db(fetch(:pg_system_db), *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
8
12
  end
9
13
 
10
14
  # Runs psql on the application database
@@ -12,18 +16,24 @@ module Capistrano
12
16
  psql_on_db(fetch(:pg_database), *args)
13
17
  end
14
18
 
15
- def db_user_exists?(name)
16
- psql '-tAc', %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{name}';" | grep -q 1}
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}
17
21
  end
18
22
 
19
- def database_exists?(db_name)
20
- psql '-tAc', %Q{"SELECT 1 FROM pg_database WHERE datname='#{db_name}';" | grep -q 1}
23
+ 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}
21
25
  end
22
26
 
23
27
  private
24
- def psql_on_db(db_name, *args)
25
- test :sudo, "-u #{fetch(:pg_system_user)} psql -d #{db_name}", *args
26
- end
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
+ test *cmd.flatten
34
+ #test :sudo, "-u #{fetch(:pg_system_user)} psql -d #{db_name}", *args
35
+ end
36
+
27
37
  end
28
38
  end
29
39
  end
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module Postgresql
3
- VERSION = "4.2.1"
3
+ VERSION = '4.7.0'
4
4
  end
5
5
  end
@@ -8,11 +8,13 @@ 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
12
  set :pg_database, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
12
13
  set :pg_user, -> { fetch(:pg_database) }
13
14
  set :pg_ask_for_password, false
14
15
  set :pg_password, -> { ask_for_or_generate_password }
15
16
  set :pg_system_user, 'postgres'
17
+ set :pg_without_sudo, false # issues/22 | Contributed by snake66
16
18
  set :pg_system_db, 'postgres'
17
19
  set :pg_use_hstore, false
18
20
  set :pg_extensions, []
@@ -29,7 +31,6 @@ namespace :load do
29
31
  primary(:db).hostname
30
32
  end
31
33
  end
32
-
33
34
  end
34
35
  end
35
36
 
@@ -58,31 +59,25 @@ namespace :postgresql do
58
59
  end
59
60
 
60
61
  on roles :db do
61
- psql '-c', %Q{"DROP database #{fetch(:pg_database)};"}
62
- psql '-c', %Q{"DROP user #{fetch(:pg_user)};"}
62
+ psql '-c', %Q{"DROP database \\"#{fetch(:pg_database)}\\";"}
63
+ psql '-c', %Q{"DROP user \\"#{fetch(:pg_user)}\\";"}
63
64
  end
64
65
  end
65
66
 
66
- desc "Add the hstore extension to postgresql"
67
- task :add_hstore do
68
- next unless fetch(:pg_use_hstore)
69
- on roles :db do
70
- psql_on_app_db '-c', %Q{"CREATE EXTENSION IF NOT EXISTS hstore;"}
67
+ task :remove_yml_files do
68
+ on release_roles :all do
69
+ if test "[ -e #{database_yml_file} ]"
70
+ execute :rm, database_yml_file
71
+ end
71
72
  end
72
- end
73
-
74
- desc "Add pg_extension to postgresql db"
75
- task :add_extensions do
76
- next unless Array( fetch(:pg_extensions) ).any?
77
- on roles :db do
78
- # add extensions if extension is present
79
- Array( fetch(:pg_extensions) ).each do |ext|
80
- psql_on_app_db '-c', %Q{"CREATE EXTENSION IF NOT EXISTS #{ext};"} unless [nil, false, ""].include?(ext)
73
+ on primary :db do
74
+ if test "[ -e #{archetype_database_yml_file} ]"
75
+ execute :rm, archetype_database_yml_file
81
76
  end
82
77
  end
83
78
  end
84
79
 
85
- desc "Remove pg_extension from postgresql db"
80
+ desc 'Remove pg_extension from postgresql db'
86
81
  task :remove_extensions do
87
82
  next unless Array( fetch(:pg_extensions) ).any?
88
83
  on roles :db do
@@ -93,13 +88,26 @@ namespace :postgresql do
93
88
  end
94
89
  end
95
90
 
96
- desc 'Create DB user'
97
- task :create_db_user do
91
+ desc 'Add the hstore extension to postgresql'
92
+ task :add_hstore do
93
+ next unless fetch(:pg_use_hstore)
98
94
  on roles :db do
99
- next if db_user_exists? fetch(:pg_user)
100
- unless psql '-c', %Q{"CREATE user #{fetch(:pg_user)} WITH password '#{fetch(:pg_password)}';"}
101
- error 'postgresql: creating database user failed!'
102
- exit 1
95
+ psql_on_app_db '-c', %Q{"CREATE EXTENSION IF NOT EXISTS hstore;"}
96
+ end
97
+ end
98
+
99
+ desc 'Add pg_extension to postgresql db'
100
+ task :add_extensions do
101
+ next unless Array( fetch(:pg_extensions) ).any?
102
+ on roles :db do
103
+ Array( fetch(:pg_extensions) ).each do |ext|
104
+ next if [nil, false, ""].include?(ext)
105
+ if psql_on_app_db '-c', %Q{"CREATE EXTENSION IF NOT EXISTS #{ext};"}
106
+ puts "- Added extension #{ext} to #{fetch(:pg_database)}"
107
+ else
108
+ error "postgresql: adding extension #{ext} failed!"
109
+ exit 1
110
+ end
103
111
  end
104
112
  end
105
113
  end
@@ -107,14 +115,26 @@ namespace :postgresql do
107
115
  desc 'Create database'
108
116
  task :create_database do
109
117
  on roles :db do
110
- next if database_exists? fetch(:pg_database)
111
- unless psql '-c', %Q{"CREATE database #{fetch(:pg_database)} owner #{fetch(:pg_user)};"}
118
+ next if database_exists?
119
+ unless psql_on_db fetch(:pg_system_db), '-c', %Q{"CREATE DATABASE \\"#{fetch(:pg_database)}\\" OWNER \\"#{fetch(:pg_user)}\\";"}
112
120
  error 'postgresql: creating database failed!'
113
121
  exit 1
114
122
  end
115
123
  end
116
124
  end
117
125
 
126
+ desc 'Create DB user'
127
+ task :create_db_user do
128
+ on roles :db do
129
+ next if db_user_exists?
130
+ # 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)}';"}
132
+ error 'postgresql: creating database user failed!'
133
+ exit 1
134
+ end
135
+ end
136
+ end
137
+
118
138
  # This task creates the archetype database.yml file on the primary db server. This is done once when a
119
139
  # new DB user is created.
120
140
  desc 'Generate database.yml archetype'
@@ -122,7 +142,7 @@ namespace :postgresql do
122
142
  on primary :db do
123
143
  next if test "[ -e #{archetype_database_yml_file} ]"
124
144
  execute :mkdir, '-pv', File.dirname(archetype_database_yml_file)
125
- upload! pg_template('postgresql.yml.erb'), archetype_database_yml_file
145
+ Net::SCP.upload!(fetch(:pg_host), fetch(:system_user), pg_template('postgresql.yml.erb'), archetype_database_yml_file)
126
146
  end
127
147
  end
128
148
 
@@ -136,8 +156,8 @@ namespace :postgresql do
136
156
  end
137
157
 
138
158
  on release_roles :all do
139
- execute :mkdir, '-pv', shared_path.join('config')
140
- upload! StringIO.new(database_yml_contents), database_yml_file
159
+ 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)
141
161
  end
142
162
  end
143
163
 
@@ -149,12 +169,14 @@ namespace :postgresql do
149
169
 
150
170
  desc 'Postgresql setup tasks'
151
171
  task :setup do
152
- invoke "postgresql:create_db_user"
153
- invoke "postgresql:create_database"
172
+ 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.
174
+ invoke 'postgresql:create_db_user'
175
+ invoke 'postgresql:create_database'
154
176
  invoke 'postgresql:add_hstore'
155
177
  invoke 'postgresql:add_extensions'
156
- invoke "postgresql:generate_database_yml_archetype"
157
- invoke "postgresql:generate_database_yml"
178
+ invoke 'postgresql:generate_database_yml_archetype'
179
+ invoke 'postgresql:generate_database_yml'
158
180
  end
159
181
  end
160
182
 
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-postgresql
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.1
4
+ version: 4.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Sutic
8
+ - Ruben Stranders
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-04-22 00:00:00.000000000 Z
12
+ date: 2017-12-18 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: capistrano
@@ -45,10 +46,12 @@ description: |
45
46
  https://github.com/bruno-/capistrano2-postgresql
46
47
  email:
47
48
  - bruno.sutic@gmail.com
49
+ - r.stranders@gmail.com
48
50
  executables: []
49
51
  extensions: []
50
52
  extra_rdoc_files: []
51
53
  files:
54
+ - ".gitignore"
52
55
  - CHANGELOG.md
53
56
  - Gemfile
54
57
  - LICENSE.md
@@ -85,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
88
  version: '0'
86
89
  requirements: []
87
90
  rubyforge_project:
88
- rubygems_version: 2.2.2
91
+ rubygems_version: 2.7.3
89
92
  signing_key:
90
93
  specification_version: 4
91
94
  summary: Creates application database user and `database.yml` on the server. No SSH