capistrano2-postgresql 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86c3ce349d28888d7be08ce28e2f4aabaf35f82d
4
+ data.tar.gz: 121784094558c83db48305d076e467776c881f4e
5
+ SHA512:
6
+ metadata.gz: 49d4b1aecdcbc38733c2aaeb5eaa1a78b8ab38361021268e7e81525bf3a315cc36b1aa310e430f12131d0ba100931e77325128f7df7528a5028bccaa2013a7d8
7
+ data.tar.gz: 67d011e4f45bcd134cc8288ded9609570c0159920f81c7c0f5fae788d5d9f2398059ade15eeff2fcdba463b5dda67126a3a04c54da5b233be24838840c71daf7
data/.gitignore ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano2-postgresql.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -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.
data/README.md ADDED
@@ -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)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
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 = "capistrano2-postgresql"
8
+ gem.version = Capistrano::Postgresql::VERSION
9
+ gem.authors = ["Bruno Sutic"]
10
+ gem.email = ["bruno.sutic@gmail.com"]
11
+ gem.description = %q{Capistrano2 tasks for PostgreSQL configuration and management for Rails apps. Manages `database.yml` on the server. Works for Capistrano2. Does *not* support Capistrano3.}
12
+ gem.summary = %q{Creates application database user and creates `database.yml` on the server. No SSH login required!}
13
+ gem.homepage = "https://github.com/bruno-/capistrano2-postgresql"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'capistrano', '~> 2.0'
21
+
22
+ gem.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,105 @@
1
+ require 'capistrano'
2
+ require 'securerandom'
3
+
4
+ Capistrano::Configuration.instance.load do
5
+
6
+ def set_default(name, *args, &block)
7
+ set(name, *args, &block) unless exists?(name)
8
+ end
9
+
10
+ set_default(:postgresql_database) { application }
11
+ set_default(:postgresql_user) { "#{user}_#{application}" }
12
+ set_default(:postgresql_ask_for_password, false)
13
+ set_default(:postgresql_password) { ask_for_or_generate_password }
14
+ set_default(:postgresql_default_tasks, true) # postgres tasks are done automatically
15
+
16
+ # template only settings (used in postgresql.yml.erb)
17
+ set_default(:postgresql_pool, 5)
18
+ set_default(:postgresql_encoding, "unicode")
19
+ set_default(:postgresql_host, "localhost")
20
+
21
+ set_default(:templates_path, "config/deploy/templates")
22
+
23
+ namespace :postgresql do
24
+
25
+ desc "Install the latest stable release of PostgreSQL."
26
+ task :install, roles: :db, only: { primary: true } do
27
+ run "#{sudo} add-apt-repository ppa:pitti/postgresql"
28
+ run "#{sudo} apt-get -y update"
29
+ run "#{sudo} apt-get -y install postgresql libpq-dev"
30
+ end
31
+
32
+ desc "Drop a database for this application."
33
+ task :drop_database, roles: :db, only: { primary: true } do
34
+ run %Q{#{psql} -c "drop database #{postgresql_database};"}
35
+ run %Q{#{psql} -c "drop user #{postgresql_user};"}
36
+ end
37
+
38
+ desc "Create a database for this application."
39
+ task :create_database, roles: :db, only: { primary: true } do
40
+ ensure_db_user_created(postgresql_user, postgresql_password)
41
+ ensure_database_created(postgresql_database, postgresql_user)
42
+ end
43
+
44
+ desc "Generate the database.yml configuration file."
45
+ task :setup, roles: :app do
46
+ run "mkdir -p #{shared_path}/config"
47
+ unless remote_file_exists?("#{shared_path}/config/database.yml")
48
+ database_yml_template "postgresql.yml.erb", "#{shared_path}/config/database.yml"
49
+ end
50
+ end
51
+
52
+ desc "Symlink the database.yml file into latest release"
53
+ task :symlink, roles: :app do
54
+ run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
55
+ end
56
+
57
+ if postgresql_default_tasks
58
+ after "deploy:setup", "postgresql:create_database"
59
+ after "deploy:setup", "postgresql:setup"
60
+ after "deploy:finalize_update", "postgresql:symlink"
61
+ end
62
+ end
63
+
64
+ def database_yml_template(template_name, target)
65
+ config_file = "#{templates_path}/#{template_name}"
66
+ # If there's no customized file in your rails app template directory,
67
+ # proceed with the default.
68
+ unless File.exists?(config_file)
69
+ config_file = File.join(File.dirname(__FILE__), "../../generators/capistrano/postgresql/templates/#{template_name}")
70
+ end
71
+ put ERB.new(File.read(config_file)).result(binding), target
72
+ end
73
+
74
+ def ask_for_or_generate_password
75
+ if postgresql_ask_for_password
76
+ Capistrano::CLI.ui.ask "Postgresql database password for the app: "
77
+ else
78
+ generate_random_password
79
+ end
80
+ end
81
+
82
+ def generate_random_password
83
+ SecureRandom.hex(10)
84
+ end
85
+
86
+ # Checks if DB user already exits. If not - creates DB user.
87
+ def ensure_db_user_created(name, password)
88
+ run %Q{#{psql} -tAc "SELECT 1 FROM pg_roles WHERE rolname='#{name}';" | grep -q 1 || \
89
+ #{psql} -c "create user #{name} with password '#{password}';"}
90
+ end
91
+
92
+ # Checks if database already exits. If not - creates database.
93
+ def ensure_database_created(db_name, user_name)
94
+ run %Q{#{psql} -tAc "SELECT 1 FROM pg_database WHERE datname='#{db_name}';" | grep -q 1 || \
95
+ #{psql} -c "create database #{db_name} owner #{user_name};"}
96
+ end
97
+
98
+ def psql
99
+ %Q{#{sudo} -u postgres psql}
100
+ end
101
+
102
+ def remote_file_exists?(path)
103
+ capture("if [ -e #{path} ]; then echo 'true'; fi").strip == 'true'
104
+ end
105
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Postgresql
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "capistrano/postgresql/version"
2
+ require "capistrano/postgresql/tasks"
@@ -0,0 +1,10 @@
1
+ To create local `database.yml.erb` template in a default path
2
+ "config/deploy/templates" type this in your shell:
3
+
4
+ rails generate capistrano:postgresql:template
5
+
6
+ This is how you override the default path:
7
+
8
+ rails generate capistrano:postgresql:template "config/templates"
9
+
10
+ If you override templates path, don't forget to set "templates_path" 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
+ production:
2
+ adapter: postgresql
3
+ encoding: <%= postgresql_encoding %>
4
+ database: <%= postgresql_database %>
5
+ pool: <%= postgresql_pool %>
6
+ username: <%= postgresql_user %>
7
+ password: '<%= postgresql_password %>'
8
+ host: <%= postgresql_host %>
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano2-postgresql
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Sutic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-16 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: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.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: Capistrano2 tasks for PostgreSQL configuration and management for Rails
42
+ apps. Manages `database.yml` on the server. Works for Capistrano2. Does *not* support
43
+ Capistrano3.
44
+ email:
45
+ - bruno.sutic@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.md
53
+ - README.md
54
+ - Rakefile
55
+ - capistrano2-postgresql.gemspec
56
+ - lib/capistrano-postgresql.rb
57
+ - lib/capistrano/postgresql/tasks.rb
58
+ - lib/capistrano/postgresql/version.rb
59
+ - lib/generators/capistrano/postgresql/README.md
60
+ - lib/generators/capistrano/postgresql/template_generator.rb
61
+ - lib/generators/capistrano/postgresql/templates/postgresql.yml.erb
62
+ homepage: https://github.com/bruno-/capistrano2-postgresql
63
+ licenses: []
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.1.5
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Creates application database user and creates `database.yml` on the server.
85
+ No SSH login required!
86
+ test_files: []