Skip to content

Commit 29d9d7c

Browse files
author
Bruno Sutic
committed
First working version
0 parents  commit 29d9d7c

File tree

12 files changed

+444
-0
lines changed

12 files changed

+444
-0
lines changed

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in capistrano-postgresql.gemspec
4+
gemspec

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
capistrano2-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.

README.md

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Capistrano 2 PostgreSQL
2+
3+
**Note: this plugin works only with Capistrano 2.** Plase check the capistrano
4+
gem version you're using before installing this gem:
5+
`$ bundle show | grep capistrano`
6+
7+
Plugin for Capistrano 3 coming soon.
8+
9+
### About
10+
11+
Capistrano 2 PostgreSQL plugin abstracts and speeds up common administration
12+
tasks for PostgreSQL when deploying rails applications.
13+
14+
Here are the specific things this plugin does for your rails app capistrano
15+
deployment 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 'capistrano2-postgresql', require: false
28+
end
29+
30+
Install the gem with:
31+
32+
$ bundle install
33+
34+
### Standard usage
35+
36+
If you're deploying a standard rails app, all that you need to do is put
37+
the following in `config/deploy.rb` file:
38+
39+
require "capistrano-postgresql"
40+
41+
Easy, right?
42+
43+
Check below to see what happens in the background.
44+
45+
### How it works
46+
47+
The following tasks run automatically after `cap deploy:setup`:
48+
49+
* `postgresql:create_database` creates a postgresql user and a database for
50+
your app. Password for the user is automatically generated and used in the
51+
next step.
52+
53+
* `postgresql:setup` it creates a `database.yml` file and copies it to
54+
`#{shared_path}/config/database.yml` on the server.
55+
56+
The following task runs automatically after `cap deploy:cold` and each
57+
`cap deploy`:
58+
59+
* `postgresql:symlink` ensures the `database.yml` from the previous step is
60+
always symlinked to `config/database.yml` for new app release.
61+
62+
The above tasks are all you need for getting rails app to work with PostgreSQL.
63+
64+
### Gotchas
65+
66+
Be sure to remove `database.yml` from your application's version control.
67+
68+
### Configuration
69+
70+
This plugin should just work with no configuration whatsoever. However,
71+
customization is possible. Here's the list of options and the defaults for each
72+
option:
73+
74+
* `set :postgresql_user`<br/>
75+
Name of the database user. Defaults to `"#{capistrano_user}_#{application}"`
76+
which is most likely something like `deploy_myapp`.
77+
78+
* `set :postgresql_database`<br/>
79+
Name of the database for your app. Defaults to `#{application}`.
80+
81+
* `set :postgresql_password`<br/>
82+
Password for the database user. By default this option is not set and
83+
**new random password** is generated each time you create a new database.
84+
If you set this option to `"some_secure_password"` - that will be the db
85+
password. Keep in mind that having a hardcoded password in `deploy.rb` (or
86+
anywhere in version control) is a bad practice.
87+
I recommend sticking to the default and generating a new secure and random
88+
password each time a database is generated. That way you don't have to worry
89+
about it or try to remember it.
90+
91+
* `set :postgresql_ask_for_password`, default `false`<br/>
92+
Set this option to `true` if you want to be prompted for the password when
93+
database user is created. This is safer than setting the password via
94+
`postgresql_password`, but the downside is you have to choose and remember
95+
yet another fricking password.<br/>
96+
`postgresql_password` option has precedence. If it is set,
97+
`postgresql_ask_for_password` is ignored.
98+
99+
* `set :postgresql_default_tasks`<br/>
100+
This task determines whether capistrano tasks from this plugin are executed
101+
automatically during capistrano deploy process. Defaults to `true`. Tasks that
102+
are run automatically are: `postgresql:create_database`, `postgresql:setup` and
103+
`postgresql:symlink`.
104+
105+
`database.yml` template-only settings:
106+
107+
* `set :postgresql_pool`<br/>
108+
Pool config in `database.yml` template. Defaults to `5`.
109+
110+
* `set :postgresql_host`<br/>
111+
`hostname` config in `database.yml` template. Defaults to `localhost`.
112+
113+
* `set :postgresql_encoding`<br/>
114+
`encogind` config in `database.yml` template. Defaults to `unicode`.
115+
116+
### Customizing the `database.yml` template
117+
118+
This is the default `database.yml` template that gets copied to the capistrano
119+
shared directory on the server:
120+
121+
```yml
122+
production:
123+
adapter: postgresql
124+
encoding: <%= postgresql_encoding %>
125+
database: <%= postgresql_database %>
126+
pool: <%= postgresql_pool %>
127+
username: <%= postgresql_user %>
128+
password: '<%= postgresql_password %>'
129+
host: <%= postgresql_host %>
130+
```
131+
132+
If for any reason you want to edit or tweak this template, you can copy it to
133+
`config/deploy/templates/postgresql.yml.erb` with this command:
134+
135+
rails g capistrano:postgresql:template
136+
137+
After you edit this newly created file in your repo, it will be used as a
138+
template for `database.yml` on the server.
139+
140+
You can configure the template location. For example:
141+
`set :templates_path, "config"` and the template will be copied to
142+
`config/postgresql.yml.erb`.
143+
144+
### Contributing and bug reports
145+
146+
Contributions and improvements are very welcome. Just open a pull request and
147+
I'll look it up shortly.
148+
149+
If something is not working for you, or you find a bug please report it.
150+
151+
### Thanks
152+
153+
Here are other plugins and people this project was based upon:
154+
155+
* [Matt Bridges](https://github.com/mattdbridges) - capistrano postgresql tasks
156+
from this plugin are heavily based on his
157+
[capistrano-recipes repo](https://github.com/mattdbridges/capistrano-recipes).
158+
159+
* [Kalys Osmonom](https://github.com/kalys) - his
160+
[capistrano-nginx-unicorn](https://github.com/kalys/capistrano-nginx-unicorn)
161+
gem structure was an inspiration for this plugin. A lot of the features were
162+
directly copied from his project (example: `database.yml` template generator).
163+
164+
### License
165+
166+
[MIT](LICENSE.md)

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "bundler/gem_tasks"

capistrano-postgresql.gemspec

+29
Original file line numberDiff line numberDiff line change
@@ -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 = ["[email protected]"]
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

lib/capistrano-postgresql.rb

Whitespace-only changes.

lib/capistrano/postgresql.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
load File.expand_path("../tasks/postgresql.rake", __FILE__)

lib/capistrano/postgresql/version.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Capistrano
2+
module Postgresql
3+
VERSION = "0.0.1"
4+
end
5+
end

0 commit comments

Comments
 (0)