capistrano-pg 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /vendor/ruby
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-pg.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ivan Tkalin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,144 @@
1
+ # Capistrano-Pg
2
+
3
+ Capistrano tasks for basic configuration and management of PostgreSQL database.
4
+
5
+ Provides capistrano tasks to:
6
+
7
+ * create database.yml in `shared` folder and symlink into `config`
8
+ * create user and database in postgres
9
+
10
+ Provides several capistrano variables for easy customization.
11
+ Also, for full customization, template of `database.yml` file can be copied to
12
+ the application using generator.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'capistrano-pg', require: false, group: :development
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install capistrano-pg
27
+
28
+ ## Usage
29
+
30
+ Add this line to your `deploy.rb`
31
+
32
+ require 'capistrano-pg'
33
+
34
+ Next, add role `pg` to the server which has running instance of PostgreSQL.
35
+ Address of this server will be automatically used as `pg_host` (see below),
36
+ `create_user` and `create_database` tasks will be executed on this server:
37
+
38
+ ```ruby
39
+ # for dedicated database server it could look like this:
40
+ server "192.168.33.12", :db, :pg, no_release: true
41
+
42
+ # for all-in-one server it could look like this:
43
+ server "192.168.33.11", :web, :app, :db, :pg, primary: true
44
+ ```
45
+
46
+ Note, that following capistrano variables should be defined in the `deploy.rb` file:
47
+
48
+ application
49
+ current_path
50
+ shared_path
51
+ user
52
+
53
+ You can check that new tasks are available (`cap -T`):
54
+
55
+ # create database user for the application
56
+ cap pg:create_user
57
+
58
+ # create database for the application
59
+ cap pg:create_database
60
+
61
+ # generates `database.yml` file in the `shared/config` folder
62
+ cap pg:setup
63
+
64
+ # symlinks `database.yml` from the `shared/config` folder to `current/config` folder
65
+ cap pg:symlink
66
+
67
+ There is no need to execute any of these tasks manually.
68
+ They will be called automatically on different deploy stages:
69
+
70
+ * `pg:create_user`, `pg:create_database` and `pg:setup` are hooked to `deploy:setup`
71
+ * `pg:symlink` is hooked to `deploy:finalize_update`
72
+
73
+ This means that if you run `cap deploy:setup`,
74
+ user and database will be created and `database.yml` file will be generated.
75
+ And on each deploy, `database.yml` will be automatically linked to current version.
76
+
77
+ However, if you changed variables or customized templates,
78
+ you can run any of these tasks to update configuration.
79
+
80
+ ## Customization
81
+
82
+ ### Using variables
83
+
84
+ You can customize `database.yml` using capistrano variables:
85
+
86
+ ```ruby
87
+ # path to customized templates (see below for details)
88
+ # default value: "config/deploy/templates"
89
+ set :templates_path, "config/deploy/templates"
90
+
91
+ # `host` value in `database.yml`
92
+ # default value: address of the server, marked with `pg` role
93
+ set :pg_host, "localhost"
94
+
95
+ # `port` value in `database.yml`
96
+ # default value: 5432
97
+ set :pg_port, "5432"
98
+
99
+ # `database` value in `database.yml`
100
+ # default value: application variable value
101
+ set :pg_database, "myapp_production"
102
+
103
+ # `pool` value in `database.yml`
104
+ # default value: 5
105
+ set :pg_pool, "5"
106
+
107
+ # `username` value in `database.yml`
108
+ # default value: application variable value
109
+ set :pg_user, application
110
+
111
+ # `password` value in `database.yml`
112
+ # default value: will be asked if not set
113
+ # setting this value in config file is not recommended
114
+ set :pg_password, application
115
+
116
+ # indicates, it new database should be created on `deploy:setup` or not
117
+ # default value: true
118
+ set :pg_create_db, true
119
+ ```
120
+
121
+ ### Template Customization
122
+
123
+ If you want to change default template, you can generate it using `rails generator`
124
+
125
+ rails g capistrano:pg:config
126
+
127
+ This will copy default template to `config/deploy/templates` directory,
128
+ so you can customize them as you like, and capistrano tasks will use this templates instead of default.
129
+
130
+ You can also provide path, where to generate template:
131
+
132
+ rails g capistrano:nginx_unicorn:config config/templates
133
+
134
+ # TODO:
135
+
136
+ * add tests
137
+
138
+ ## Contributing
139
+
140
+ 1. Fork it
141
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
142
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
143
+ 4. Push to the branch (`git push origin my-new-feature`)
144
+ 5. Create new Pull Request
@@ -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/pg/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "capistrano-pg"
8
+ gem.version = Capistrano::Pg::VERSION
9
+ gem.authors = ["Ivan Tkalin"]
10
+ gem.email = ["itkalin@gmail.com"]
11
+ gem.description = %q{Deprecated! Capistrano tasks for basic configuration and management of PostgreSQL database. Allows to create user/database and generate and symlink database.yml}
12
+ gem.summary = %q{Deprecated! Manage PostgreSQL and database.yml from capistrano}
13
+ gem.homepage = "https://github.com/ivalkeen/capistrano-pg"
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,2 @@
1
+ require "capistrano/pg/version"
2
+ require "capistrano/pg/tasks"
@@ -0,0 +1,60 @@
1
+ require 'capistrano'
2
+
3
+ Capistrano::Configuration.instance.load do
4
+ set_default(:templates_path, "config/deploy/templates")
5
+
6
+ set_default(:pg_host) { find_servers(roles: :pg)[0] || "localhost" }
7
+ set_default(:pg_port, "5432")
8
+ set_default(:pg_database, application)
9
+ set_default(:pg_pool, 5)
10
+ set_default(:pg_user, application)
11
+ set_default(:pg_password) { Capistrano::CLI.password_prompt "PG password: " }
12
+ set_default(:pg_create_db, true)
13
+
14
+ namespace :pg do
15
+ desc "Create user for application"
16
+ task :create_user, roles: :pg do
17
+ pg_params = "-p #{pg_port}"
18
+ check_user = %Q[#{sudo} -u postgres psql #{pg_params} postgres -c "select * from pg_user where usename='#{pg_user}'" | grep -c '#{pg_user}']
19
+ add_user = %Q[#{sudo} -u postgres psql #{pg_params} -c "create user #{pg_user}"]
20
+ run "#{check_user} || #{add_user}"
21
+ run %Q{#{sudo} -u postgres psql #{pg_params} -c "alter user #{pg_user} with password '#{pg_password}'" }
22
+ end
23
+
24
+ desc "Create database for application"
25
+ task :create_database, roles: :pg do
26
+ if pg_create_db
27
+ run %Q{#{sudo} -u postgres psql -c "create database #{pg_database} owner #{pg_user};"}
28
+ end
29
+ end
30
+
31
+ if find_servers(roles: :pg).size > 0
32
+ after "deploy:setup", "pg:create_user"
33
+ after "deploy:setup", "pg:create_database"
34
+ end
35
+
36
+ desc "Generate the database.yml configuration file."
37
+ task :setup, roles: :app do
38
+ run "mkdir -p #{shared_path}/config"
39
+ template "database.yml.erb", "#{shared_path}/config/database.yml"
40
+ end
41
+
42
+ after "deploy:setup", "pg:setup"
43
+
44
+ desc "Symlink the database.yml file into latest release"
45
+ task :symlink, roles: :app do
46
+ run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
47
+ end
48
+
49
+ after "deploy:finalize_update", "pg:symlink"
50
+
51
+ def template(template_name, target)
52
+ config_file = "#{templates_path}/#{template_name}"
53
+ # if no customized file, proceed with default
54
+ unless File.exists?(config_file)
55
+ config_file = File.join(File.dirname(__FILE__), "../../generators/capistrano/pg/templates/#{template_name}")
56
+ end
57
+ put ERB.new(File.read(config_file)).result(binding), target
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Pg
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ To create local database.yml.erb configuration file call
2
+
3
+ rails generate capistrano:pg:config [path]
4
+
5
+ The default path is "config/deploy/templates". You can override it like so:
6
+
7
+ rails generate capistrano:pg:config "config/templates"
8
+
9
+ If you override templates path, don't forget to set "templates_path" variable in your deploy.rb
@@ -0,0 +1,17 @@
1
+ module Capistrano
2
+ module Pg
3
+ module Generators
4
+ class ConfigGenerator < Rails::Generators::Base
5
+ desc "Create local database.yml.erb configuration files for customization"
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ argument :templates_path, :type => :string,
8
+ :default => "config/deploy/templates",
9
+ :banner => "path to templates"
10
+
11
+ def copy_template
12
+ copy_file "database.yml.erb", "#{templates_path}/database.yml.erb"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ production:
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ database: <%= pg_database %>
5
+ pool: <%= pg_pool %>
6
+ username: <%= pg_user %>
7
+ password: <%= pg_password %>
8
+ host: <%= pg_host %>
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-pg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ivan Tkalin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ prerelease: false
16
+ name: capistrano
17
+ version_requirements: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ none: false
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '2.0'
28
+ none: false
29
+ type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ prerelease: false
32
+ name: rake
33
+ version_requirements: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ none: false
39
+ requirement: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ none: false
45
+ type: :development
46
+ description: Deprecated! Capistrano tasks for basic configuration and management of
47
+ PostgreSQL database. Allows to create user/database and generate and symlink database.yml
48
+ email:
49
+ - itkalin@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - capistrano-pg.gemspec
60
+ - lib/capistrano-pg.rb
61
+ - lib/capistrano/pg/tasks.rb
62
+ - lib/capistrano/pg/version.rb
63
+ - lib/generators/capistrano/pg/USAGE
64
+ - lib/generators/capistrano/pg/config_generator.rb
65
+ - lib/generators/capistrano/pg/templates/database.yml.erb
66
+ homepage: https://github.com/ivalkeen/capistrano-pg
67
+ licenses: []
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
+ segments:
78
+ - 0
79
+ hash: -3226214497165467807
80
+ none: false
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: -3226214497165467807
89
+ none: false
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.23
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Deprecated! Manage PostgreSQL and database.yml from capistrano
96
+ test_files: []