capistrano3-postgres 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7c23d7ab59b5640be8e7f7db1ccf3f4ad5c6198a
4
+ data.tar.gz: 9da68ed48cc63b1036448ba8aa2e93aa704592df
5
+ SHA512:
6
+ metadata.gz: 270e308bd8bac7d9036f3db4adb416a8aadea9e9af6dd2dd930868b29d051023484a9bc922b186151ed15a9ca83a4e077c22901ee59567a852ef24dbb4b92c12
7
+ data.tar.gz: 243f91f63dcb7944478ecd3daf1b84b25455cc59901757bd3ca27d238fa8fac1272c7ab59d0a16b294239e2b850d32df7e794acfadb78c68e73641c9bcfb03d4
@@ -0,0 +1,19 @@
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
+
19
+ lib/capistrano3/.DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano3-postgres.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 spilin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex Krasynskyi
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,55 @@
1
+ # Capistrano3::Postgres
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'capistrano3-postgres'
8
+
9
+ or:
10
+
11
+ gem 'capistrano3-postgres' , group: :development
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install capistrano3-postgres
20
+
21
+ ## Usage
22
+ ```ruby
23
+ # Capfile
24
+
25
+ require 'capistrano3/postgres'
26
+ ```
27
+
28
+ then you can use ```cap -vT``` to list tasks
29
+ ```
30
+ cap postgres:backup:create
31
+ cap postgres:backup:download
32
+ cap postgres:backup:import
33
+ cap postgres:replicate
34
+ ```
35
+ You will be prompted for password and local database name that you want to use for restore.
36
+
37
+ Sometimes it's a good idea to create dump before each deploy.
38
+ ```
39
+ before 'deploy:starting', 'postgres:backup:create'
40
+ ```
41
+
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( http://github.com/spilin/capistrano3-postgres/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
50
+
51
+
52
+
53
+
54
+
55
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano3/postgres/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "capistrano3-postgres"
8
+ spec.version = Capistrano3::Postgres::VERSION
9
+ spec.authors = ["Alex Krasynskyi"]
10
+ spec.email = ["lyoshakr@gmail.com"]
11
+ spec.summary = %q{Create, download and restore postgres database. }
12
+ spec.description = %q{Create postgres dumps, download and replicate locally.}
13
+ spec.homepage = "https://github.com/spilin/capistrano3-postgres"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "capistrano", "~> 3.0"
22
+ end
@@ -0,0 +1,2 @@
1
+ load File.expand_path('../tasks/postgres.rb', __FILE__)
2
+
@@ -0,0 +1,5 @@
1
+ module Capistrano3
2
+ module Postgres
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,118 @@
1
+ namespace :load do
2
+ task :defaults do
3
+ set :postgres_backup_dir, -> { 'postgres_backup' }
4
+ set :postgres_role, :app
5
+ set :postgres_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
6
+ set :postgres_remote_sqlc_file_path, -> { nil }
7
+ set :postgres_local_database_config, -> { nil }
8
+ set :postgres_remote_database_config, -> { nil }
9
+ end
10
+ end
11
+
12
+ namespace :postgres do
13
+
14
+ namespace :backup do
15
+
16
+ desc 'Create database dump'
17
+ task :create do
18
+ grab_remote_database_config
19
+ on roles(fetch(:postgres_role)) do |role|
20
+ config = fetch(:postgres_remote_database_config)
21
+
22
+ unless fetch(:postgres_remote_sqlc_file_path)
23
+ file_name = "db_backup.#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}.sqlc"
24
+ set :postgres_remote_sqlc_file_path, "#{shared_path}/#{fetch(:postgres_backup_dir)}/#{file_name}"
25
+ end
26
+
27
+ execute :pg_dump, "-U #{config['user'] || config['username']} -h #{config['host']} -Fc --file=#{fetch(:postgres_remote_sqlc_file_path)} #{config['database']}" do |ch, stream, out|
28
+ ch.send_data "#{config['password']}\n" if out =~ /^Password:/
29
+ end
30
+ end
31
+ end
32
+
33
+ desc 'Download last database dump'
34
+ task :download do
35
+ on roles(fetch(:postgres_role)) do |role|
36
+ unless fetch(:postgres_remote_sqlc_file_path)
37
+ file_name = capture("ls -v #{shared_path}/#{fetch :postgres_backup_dir}").split(/\n/).last
38
+ set :postgres_remote_sqlc_file_path, "#{shared_path}/#{fetch :postgres_backup_dir}/#{file_name}"
39
+ end
40
+
41
+ download!(fetch(:postgres_remote_sqlc_file_path), "tmp/#{fetch :postgres_backup_dir}/#{Pathname.new(fetch(:postgres_remote_sqlc_file_path)).basename}")
42
+ end
43
+ end
44
+
45
+ desc "Import last dump"
46
+ task :import do
47
+ grab_local_database_config
48
+ run_locally do
49
+ config = fetch(:postgres_local_database_config)
50
+
51
+ unless fetch(:database_name)
52
+ ask(:database_name, config['database'])
53
+ end
54
+
55
+ with rails_env: :development do
56
+ file_name = capture("ls -v tmp/#{fetch :postgres_backup_dir}").split(/\n/).last
57
+ file_path = "tmp/#{fetch :postgres_backup_dir}/#{file_name}"
58
+ begin
59
+ execute :pg_restore, "-c -U #{config['user'] || config['username']} -W --no-owner -h #{config['host']} -d #{fetch(:database_name)} #{file_path}" do |ch, stream, out|
60
+ ch.send_data "#{config['password']}\n" if out =~ /^Password:/
61
+ end
62
+ rescue SSHKit::Command::Failed => e
63
+ warn e.inspect
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ # Ensure that remote dirs for postgres backup exist
70
+ before :create, :ensure_remote_dirs do
71
+ on roles(fetch(:postgres_role)) do |role|
72
+ execute :mkdir, "-p #{shared_path}/#{fetch(:postgres_backup_dir)}"
73
+ end
74
+ end
75
+
76
+ # Ensure that loca dirs for postgres backup exist
77
+ before :download, :ensure_local_dirs do
78
+ on roles(fetch(:postgres_role)) do |role|
79
+ run_locally do
80
+ execute :mkdir, "-p tmp/#{fetch :postgres_backup_dir}"
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ desc 'Replecate database locally'
87
+ task :replicate do
88
+ grab_local_database_config
89
+ config = fetch(:postgres_local_database_config)
90
+ ask(:database_name, config['database'])
91
+ invoke "postgres:backup:create"
92
+ invoke "postgres:backup:download"
93
+ invoke "postgres:backup:import"
94
+ end
95
+
96
+ # Grabs local database config before importing dump
97
+ def grab_local_database_config
98
+ return if fetch(:postgres_local_database_config)
99
+ on roles(fetch(:postgres_role)) do |role|
100
+ run_locally do
101
+ env = 'development'
102
+ yaml_content = capture "cat config/database.yml"
103
+ set :postgres_local_database_config, YAML::load(yaml_content)[env]
104
+ end
105
+ end
106
+ end
107
+
108
+ # Grabs remote database config before creating dump
109
+ def grab_remote_database_config
110
+ return if fetch(:postgres_remote_database_config)
111
+ on roles(fetch(:postgres_role)) do |role|
112
+ env = fetch(:postgres_env).to_s.downcase
113
+ yaml_content = capture "cat #{deploy_to}/current/config/database.yml"
114
+ set :postgres_remote_database_config, YAML::load(yaml_content)[env]
115
+ end
116
+ end
117
+
118
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano3-postgres
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Krasynskyi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-11 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: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Create postgres dumps, download and replicate locally.
28
+ email:
29
+ - lyoshakr@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - capistrano3-postgres.gemspec
41
+ - lib/capistrano3/postgres.rb
42
+ - lib/capistrano3/postgres/version.rb
43
+ - lib/capistrano3/tasks/postgres.rb
44
+ homepage: https://github.com/spilin/capistrano3-postgres
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.2.0
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Create, download and restore postgres database.
68
+ test_files: []