capistrano-postgresql 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e2dac275e309610b300e6673f9494259d963d38e
4
+ data.tar.gz: 5226e2c5a19a04f6666a640bf8fdd24a9164a79f
5
+ SHA512:
6
+ metadata.gz: dee66b2eb1e43e5128b9947303ff2f5dcd2a9430bde3aea592de5f0becfa9b5348dd1ff06c7d9c823fc3f58efef4731fedd26a2ad0e2ae8d9f8077b78a957661
7
+ data.tar.gz: ecd277b7db65cb20f815edcacd8a239c36e9c47d609e70217bcb3fa399b5598675bad75646c0f4c417a99492a622aa1c43d0bad183093c7d0d71cd23cfe70a6c
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-postgresql.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ capistrano-postgresql is MIT licensed.
2
+
3
+ Copyright (C) 2014 Bruno Sutic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the "Software"),
7
+ to deal in the Software without restriction, including without limitation
8
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
+ and/or sell copies of the Software, and to permit persons to whom the
10
+ Software is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included
13
+ in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21
+ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,179 @@
1
+ # Capistrano::PostgreSQL
2
+
3
+ **Note: this plugin works only with Capistrano 3.** Plase check the capistrano
4
+ gem version you're using before installing this gem:
5
+ `$ bundle show | grep capistrano`
6
+
7
+ Plugin for Capistrano 2 [is here](https://github.com/bruno-/capistrano2-postgresql).
8
+
9
+ ### About
10
+
11
+ Capistrano PostgreSQL plugin abstracts and speeds up common administration
12
+ tasks for PostgreSQL when deploying rails apps.
13
+
14
+ Here are the specific things this plugin does for your capistrano deployment
15
+ process:
16
+
17
+ * creates a new PostgreSQL database and database user on the server
18
+ * generates and populates `database.yml` file with the right data on the server
19
+ (no need to ssh to the server and do this manually!)
20
+ * no config necessary (or it's kept to a minimum)
21
+
22
+ ### Installation
23
+
24
+ Put the following in your application's `Gemfile`:
25
+
26
+ group :development do
27
+ gem 'capistrano' ~> '3.1'
28
+ gem 'capistrano-postgresql'
29
+ end
30
+
31
+ Install the gem with:
32
+
33
+ $ bundle install
34
+
35
+ ### Standard usage
36
+
37
+ If you're deploying a standard rails app, all you need to do is put
38
+ the following in `Capfile` file:
39
+
40
+ require 'capistrano/postgresql'
41
+
42
+ Easy, right?
43
+
44
+ Check below to see what happens in the background.
45
+
46
+ ### How it works
47
+
48
+ Check here for the full capistrano deployment flow
49
+ [http://capistranorb.com/documentation/getting-started/flow/](http://capistranorb.com/documentation/getting-started/flow/).
50
+
51
+ The following tasks run automatically after `deploy:started` task:
52
+
53
+ * `postgresql:create_database`<br/>
54
+ creates a postgresql user and a database for your app. Password for the user is
55
+ automatically generated and used in the next step.
56
+ * `postgresql:generate_database_yml`<br/>
57
+ creates a `database.yml` file and copies it to
58
+ `#{shared_path}/config/database.yml` on the server.
59
+ * `postgresql:ensure_database_yml_symlink`<br/>
60
+ adds `config/database.yml` to the `linked_files` array. Capistrano handles
61
+ symlinking `database.yml` to the application release path.
62
+
63
+ The above tasks are all you need for getting rails app to work with PostgreSQL.
64
+
65
+ ### Gotchas
66
+
67
+ Be sure to remove `config/database.yml` from your application's version control.
68
+
69
+ ### Debugging
70
+
71
+ A debugging task is provided. Run `bundle exec <your_stage> cap postgres:debug`
72
+ and you should get a list of all capistrano-postgresql settings and their
73
+ values.
74
+
75
+ I deeply hope you'll never need to use this, as this plugins strives to be
76
+ very easy to use with minimal or no configuration.
77
+
78
+ ### Configuration
79
+
80
+ This plugin should just work with no configuration whatsoever. However,
81
+ configuration is possible. Put all your configs in capistrano stage files i.e.
82
+ `config/deploy/production.rb`, `config/deploy/staging.rb`.
83
+
84
+ Here's the list of options and the defaults for each option:
85
+
86
+ * `set :postgresql_database`<br/>
87
+ Name of the database for your app. Defaults to `#{application}_#{stage}`,
88
+ example: `myface_production`.
89
+
90
+ * `set :postgresql_user`<br/>
91
+ Name of the database user. Defaults to whatever is set for `postgresql_database`
92
+ option.
93
+
94
+ * `set :postgresql_password`<br/>
95
+ Password for the database user. By default this option is not set and
96
+ **new random password** is generated each time you create a new database.<br/>
97
+ If you set this option to `"some_secure_password"` - that will be the db user's
98
+ password. Keep in mind that having a hardcoded password in `deploy.rb` (or
99
+ anywhere in version control) is a bad practice.<br/>
100
+ I recommend sticking to the default and generating a new secure and random
101
+ password each time a db user is generated. That way you don't have to worry
102
+ about it or try to remember it.
103
+
104
+ * `set :postgresql_ask_for_password`<br/>
105
+ Default `false`. Set this option to `true` if you want to be prompted for the
106
+ password when database user is created. This is safer than setting the password
107
+ via `postgresql_password`. The downside is you have to choose and remember
108
+ yet another fricking password.<br/>
109
+ `postgresql_password` option has precedence. If it is set,
110
+ `postgresql_ask_for_password` is ignored.
111
+
112
+ * `set :postgresql_default_tasks`<br/>
113
+ This task determines whether capistrano tasks from this plugin are executed
114
+ automatically during capistrano deploy process. Defaults to `true`. Tasks that
115
+ are run automatically are: `postgresql:create_database`,
116
+ `postgresql:generate_database_yml` and `postgresql:ensure_database_yml_symlink`.
117
+
118
+ `database.yml` template-only settings:
119
+
120
+ * `set :postgresql_pool`<br/>
121
+ Pool config in `database.yml` template. Defaults to `5`.
122
+
123
+ * `set :postgresql_host`<br/>
124
+ `hostname` config in `database.yml` template. Defaults to `localhost`.
125
+
126
+ * `set :postgresql_encoding`<br/>
127
+ `encoding` config in `database.yml` template. Defaults to `unicode`.
128
+
129
+ ### Customizing the `database.yml` template
130
+
131
+ This is the default `database.yml` template that gets copied to the capistrano
132
+ shared directory on the server:
133
+
134
+ ```yml
135
+ <%= fetch :stage %>:
136
+ adapter: postgresql
137
+ encoding: <%= postgresql_encoding %>
138
+ database: <%= postgresql_database %>
139
+ pool: <%= postgresql_pool %>
140
+ username: <%= postgresql_user %>
141
+ password: '<%= postgresql_password %>'
142
+ host: <%= postgresql_host %>
143
+ ```
144
+
145
+ If for any reason you want to edit or tweak this template, you can copy it to
146
+ `config/deploy/templates/postgresql.yml.erb` with this command:
147
+
148
+ bundle exec rails g capistrano:postgresql:template
149
+
150
+ After you edit this newly created file in your repo, it will be used as a
151
+ template for `database.yml` on the server.
152
+
153
+ You can configure the template location. For example:
154
+ `set :postgresql_templates_path, "config"` and the template will be copied to
155
+ `config/postgresql.yml.erb`.
156
+
157
+ ### Contributing and bug reports
158
+
159
+ Contributions and improvements are very welcome. Just open a pull request and
160
+ I'll look it up shortly.
161
+
162
+ If something is not working for you, or you find a bug please report it.
163
+
164
+ ### Thanks
165
+
166
+ Here are other plugins and people this project was based upon:
167
+
168
+ * [Matt Bridges](https://github.com/mattdbridges) - capistrano postgresql tasks
169
+ from this plugin are heavily based on his
170
+ [capistrano-recipes repo](https://github.com/mattdbridges/capistrano-recipes).
171
+
172
+ * [Kalys Osmonom](https://github.com/kalys) - his
173
+ [capistrano-nginx-unicorn](https://github.com/kalys/capistrano-nginx-unicorn)
174
+ gem structure was an inspiration for this plugin. A lot of the features were
175
+ directly copied from his project (example: `database.yml` template generator).
176
+
177
+ ### License
178
+
179
+ [MIT](LICENSE.md)
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano/postgresql/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "capistrano-postgresql"
8
+ gem.version = Capistrano::Postgresql::VERSION
9
+ gem.authors = ["Bruno Sutic"]
10
+ gem.email = ["bruno.sutic@gmail.com"]
11
+ gem.description = <<-EOF.gsub(/^\s+/, '')
12
+ Capistrano tasks for PostgreSQL configuration and management for Rails
13
+ apps. Manages `database.yml` template on the server.
14
+
15
+ Works with Capistrano 3 (only!). For Capistrano 2 support see:
16
+ https://github.com/bruno-/capistrano2-postgresql
17
+ EOF
18
+ gem.summary = %q{Creates application database user and `database.yml` on the server. No SSH login required!}
19
+ gem.homepage = "https://github.com/bruno-/capistrano-postgresql"
20
+
21
+ gem.license = "MIT"
22
+
23
+ gem.files = `git ls-files`.split($/)
24
+ gem.require_paths = ["lib"]
25
+
26
+ gem.add_dependency 'capistrano', '~> 3.0'
27
+
28
+ gem.add_development_dependency "rake"
29
+ end
File without changes
@@ -0,0 +1 @@
1
+ load File.expand_path("../tasks/postgresql.rake", __FILE__)
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Postgresql
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,180 @@
1
+ require 'securerandom'
2
+ require 'erb'
3
+
4
+ def database_yml_template(template_name, target)
5
+ config_file = "#{fetch(:postgresql_templates_path)}/#{template_name}"
6
+ # If there's no customized file in your rails app template directory,
7
+ # proceed with the default.
8
+ unless File.exists?(config_file)
9
+ config_file = File.join(File.dirname(__FILE__), "../../generators/capistrano/postgresql/templates/#{template_name}")
10
+ end
11
+ upload! StringIO.new(ERB.new(File.read(config_file)).result(binding)), target
12
+ end
13
+
14
+ # This method is invoked only if `:postgresql_password` is not already set in
15
+ # `config/deploy.rb`. Directly setting `:postgresql_password` has precedence.
16
+ def ask_for_or_generate_password
17
+ if fetch(:postgresql_ask_for_password)
18
+ ask :postgresql_password, "Postgresql database password for the app: "
19
+ else
20
+ set :postgresql_password, generate_random_password
21
+ end
22
+ end
23
+
24
+ def generate_random_password
25
+ SecureRandom.hex(10)
26
+ end
27
+
28
+ def db_user_exists?(name)
29
+ psql "-tAc", %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{name}';" | grep -q 1}
30
+ end
31
+
32
+ def create_db_user(name, password)
33
+ if psql "-c", %Q{"CREATE user #{name} WITH password '#{password}';"}
34
+ info "postgresq: database user '#{name}' created"
35
+ else
36
+ error "postgresql: creating database user failed!"
37
+ exit 1
38
+ end
39
+ end
40
+
41
+ def ensure_db_user_created(name, password)
42
+ unless db_user_exists?(name)
43
+ create_db_user(name, password)
44
+ end
45
+ end
46
+
47
+ def database_exists?(db_name)
48
+ psql "-tAc", %Q{"SELECT 1 FROM pg_database WHERE datname='#{db_name}';" | grep -q 1}
49
+ end
50
+
51
+ def create_database(db_name, user_name)
52
+ if psql "-c", %Q{"CREATE database #{db_name} owner #{user_name};"}
53
+ info "postgresql: database '#{db_name}' created"
54
+ else
55
+ error "postgresql: creating database '#{db_name}' failed!"
56
+ exit 1
57
+ end
58
+ end
59
+
60
+ def ensure_database_created(db_name, user_name)
61
+ unless database_exists?(db_name)
62
+ create_database(db_name, user_name)
63
+ end
64
+ end
65
+
66
+ # returns true or false depending on the remote command exit status
67
+ def psql(*args)
68
+ test :sudo, "-u postgres psql", *args
69
+ end
70
+
71
+ def remote_file_exists?(path)
72
+ test "[ -e #{path} ]"
73
+ end
74
+
75
+ namespace :load do
76
+ task :defaults do
77
+ set :postgresql_database, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
78
+ set :postgresql_user, -> { fetch(:postgresql_database) }
79
+ set :postgresql_password, -> { ask_for_or_generate_password }
80
+ set :postgresql_ask_for_password, false
81
+ set :postgresql_default_tasks, true
82
+
83
+ # template only settings (used in postgresql.yml.erb)
84
+ set :postgresql_templates_path, "config/deploy/templates"
85
+ set :postgresql_pool, 5
86
+ set :postgresql_encoding, "unicode"
87
+ set :postgresql_host, "localhost"
88
+ end
89
+ end
90
+
91
+ namespace :postgresql do
92
+
93
+ desc "Print all the variables"
94
+ task :debug do
95
+ puts "postgresql_database: #{fetch(:postgresql_database)}"
96
+ puts "postgresql_user: #{fetch(:postgresql_user)}"
97
+ puts "postgresql_password: #{fetch(:postgresql_password)}"
98
+ puts "postgresql_ask_for_password: #{fetch(:postgresql_ask_for_password)}"
99
+ puts "postgresql_default_tasks: #{fetch(:postgresql_default_tasks)}"
100
+ puts "postgresql_pool: #{fetch(:postgresql_pool)}"
101
+ puts "postgresql_encoding: #{fetch(:postgresql_encoding)}"
102
+ puts "postgresql_host #{fetch(:postgresql_host)}"
103
+ end
104
+
105
+ # This task never runs automatically
106
+ # desc "Drop a database for this application"
107
+ task :drop_database do
108
+ on roles(:db) do
109
+ psql "-c", %Q{"DROP database #{fetch(:postgresql_database)};"}
110
+ end
111
+ end
112
+
113
+ # This task never runs automatically
114
+ # desc "Delete database user for this application"
115
+ task :delete_db_user do
116
+ on roles(:db) do
117
+ psql "-c", %Q{"DROP user #{fetch(:postgresql_user)};"}
118
+ end
119
+ end
120
+
121
+ # this task never runs automatically
122
+ # desc "Delete `config/database.yml` from the shared path on the server"
123
+ task :delete_database_yml do
124
+ on roles(:app) do
125
+ database_yml_path = shared_path.join("config/database.yml")
126
+ if remote_file_exists? database_yml_path
127
+ execute :rm, database_yml_path
128
+ end
129
+ end
130
+ end
131
+
132
+ # this task never runs automatically. DANGEROUS! Destroys database on the server!
133
+ # desc "Removes application database, DB user and removes `database.yml` from the server"
134
+ task remove_all: [:drop_database, :delete_db_user, :delete_database_yml]
135
+
136
+
137
+ desc "Create a database for this application"
138
+ task :create_database do
139
+ on roles(:db) do
140
+ ensure_db_user_created fetch(:postgresql_user), fetch(:postgresql_password)
141
+ ensure_database_created fetch(:postgresql_database), fetch(:postgresql_user)
142
+ end
143
+ end
144
+
145
+ desc "Generate the database.yml configuration file"
146
+ task :generate_database_yml do
147
+ on roles(:app) do
148
+ database_yml_path = shared_path.join("config/database.yml")
149
+ if remote_file_exists? database_yml_path
150
+ info "postgresql: database.yml already exists in the shared path"
151
+ else
152
+ info "postgresql: generating database.yml in shared path"
153
+ execute :mkdir, "-p", shared_path.join("config")
154
+ database_yml_template "postgresql.yml.erb", database_yml_path
155
+ end
156
+ end
157
+ end
158
+
159
+ desc "Adds `config/database.yml` to the linked_files array"
160
+ task :ensure_database_yml_symlink do
161
+ on roles(:app) do
162
+ if fetch(:linked_files).nil?
163
+ set :linked_files, ["config/database.yml"]
164
+ elsif !fetch(:linked_files).include? "config/database.yml"
165
+ fetch(:linked_files) << "config/database.yml"
166
+ end
167
+ end
168
+ end
169
+
170
+ after "deploy:started", "postgresql:started" do
171
+ # `postgresql_default_tasks` true by default -> capistrano-postgresql tasks
172
+ # run automatically on deploy
173
+ if fetch(:postgresql_default_tasks)
174
+ invoke "postgresql:create_database"
175
+ invoke "postgresql:generate_database_yml"
176
+ invoke "postgresql:ensure_database_yml_symlink"
177
+ end
178
+ end
179
+
180
+ end
@@ -0,0 +1,11 @@
1
+ To create local `database.yml.erb` template in a default path
2
+ "config/deploy/templates" type this in your shell:
3
+
4
+ bundle exec rails generate capistrano:postgresql:template
5
+
6
+ This is how you override the default path:
7
+
8
+ bundle exec rails generate capistrano:postgresql:template "config/templates"
9
+
10
+ If you override templates path, don't forget to set "postgresql_templates_path"
11
+ variable in your deploy.rb
@@ -0,0 +1,19 @@
1
+ module Capistrano
2
+ module Postgresql
3
+ module Generators
4
+ class TemplateGenerator < Rails::Generators::Base
5
+
6
+ desc "Create local postgresql.yml.erb (database.yml on the server) template file for customization"
7
+ source_root File.expand_path('../templates', __FILE__)
8
+ argument :templates_path, type: :string,
9
+ default: "config/deploy/templates",
10
+ banner: "path to templates"
11
+
12
+ def copy_template
13
+ copy_file "postgresql.yml.erb", "#{templates_path}/postgresql.yml.erb"
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ <%= fetch :stage %>:
2
+ adapter: postgresql
3
+ encoding: <%= fetch :postgresql_encoding %>
4
+ database: <%= fetch :postgresql_database %>
5
+ pool: <%= fetch :postgresql_pool %>
6
+ username: <%= fetch :postgresql_user %>
7
+ password: '<%= fetch :postgresql_password %>'
8
+ host: <%= fetch :postgresql_host %>
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-postgresql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Sutic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |
42
+ Capistrano tasks for PostgreSQL configuration and management for Rails
43
+ apps. Manages `database.yml` template on the server.
44
+ Works with Capistrano 3 (only!). For Capistrano 2 support see:
45
+ https://github.com/bruno-/capistrano2-postgresql
46
+ email:
47
+ - bruno.sutic@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - Gemfile
53
+ - LICENSE.md
54
+ - README.md
55
+ - Rakefile
56
+ - capistrano-postgresql.gemspec
57
+ - lib/capistrano-postgresql.rb
58
+ - lib/capistrano/postgresql.rb
59
+ - lib/capistrano/postgresql/version.rb
60
+ - lib/capistrano/tasks/postgresql.rake
61
+ - lib/generators/capistrano/postgresql/README.md
62
+ - lib/generators/capistrano/postgresql/template_generator.rb
63
+ - lib/generators/capistrano/postgresql/templates/postgresql.yml.erb
64
+ homepage: https://github.com/bruno-/capistrano-postgresql
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.1.5
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Creates application database user and `database.yml` on the server. No SSH
88
+ login required!
89
+ test_files: []