capistrano-rails-toolbox 0.4.0

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: 6af5d8ead11b4f25cec2134b99014398bd25f3d1
4
+ data.tar.gz: 6f5242744f185fea1b3ab8a5b138697463eb6c45
5
+ SHA512:
6
+ metadata.gz: 8a4875d95721131c84f4ac8a44260e5bc31febc640c5b40271c334d84f49f7aa995577b8405bdb0b401f7768278fc7a90692abf51ebc54f1626c14d086f2a5c2
7
+ data.tar.gz: d0e27c72c4ae42b87d5cc942c70ed446ea3195367e058b7b51ab013dffdb9c9b664d6979a39216237b100ef1ef410a0a2ccc140a54021e5aaf9a0ded0e7d8580
@@ -0,0 +1,20 @@
1
+ Copyright 2016 ACzero
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ Bundler::GemHelper.install_tasks
File without changes
@@ -0,0 +1,2 @@
1
+ require 'capistrano/rails/toolbox/version'
2
+ require 'capistrano/rails/toolbox/toolbox_tasks'
@@ -0,0 +1,118 @@
1
+ class NotSupportDatabaseAdapter < RuntimeError; end
2
+
3
+ namespace :toolbox do
4
+ namespace :db do
5
+ desc <<-DESC
6
+ load remote databse data to local, support adapter: mysql2, postgresql
7
+ DESC
8
+ task :load_remote do
9
+ on roles(:db) do |server|
10
+ info "make sure you are not connecting to the local database"
11
+
12
+ local_config_path = 'config/database.yml'
13
+ remote_config_path = "#{shared_path}/config/database.yml"
14
+
15
+ puts <<-MSG
16
+ default local config path: #{local_config_path}
17
+ default remote config path: #{remote_config_path}
18
+ MSG
19
+
20
+ ask(:enter_path, "enter your custom config path?(Y/N)")
21
+ if fetch(:enter_path) == 'Y'
22
+ ask(:local_config_path, 'local database config path')
23
+ ask(:remote_config_path, 'remote database config path')
24
+ local_config_path = fetch(:local_config_path)
25
+ remote_config_path = fetch(:remote_config_path)
26
+ end
27
+
28
+ remote_config = remote_db_config(remote_config_path)
29
+ local_config = local_db_config(local_config_path)
30
+
31
+ db_filename = "db_dump"
32
+ dump_file_path = "#{fetch(:deploy_to)}/#{db_filename}"
33
+ local_file_path = "/tmp/#{db_filename}"
34
+
35
+ info "start dumping database from remote..."
36
+ no_output do
37
+ execute dump_cmd(remote_config, dump_file_path)
38
+ end
39
+
40
+ info "downloading..."
41
+ download! dump_file_path, local_file_path
42
+
43
+ info "reset local database"
44
+ system "bundle exec rake db:drop"
45
+ system "bundle exec rake db:create"
46
+
47
+ info "loading data..."
48
+ system import_cmd(local_config, local_file_path)
49
+
50
+ info "removing dump file..."
51
+ system "rm #{local_file_path}"
52
+ execute "rm #{dump_file_path}"
53
+ info "done!"
54
+ end
55
+ end
56
+
57
+ def remote_db_config(path)
58
+ remote_config = no_output do
59
+ capture("cat #{path}")
60
+ end
61
+
62
+ YAML::load(remote_config)[fetch(:stage).to_s]
63
+ end
64
+
65
+ def local_db_config(path)
66
+ YAML::load_file(path)["development"]
67
+ end
68
+
69
+ def dump_cmd(config, dump_file_path)
70
+ case config['adapter']
71
+ when 'postgresql'
72
+ cmd = "export PGPASSWORD=#{config['password']}; pg_dump #{config['database']}" +
73
+ " --username=#{config['username']} --no-owner --no-acl --format=c"
74
+ cmd << " --host=#{config['host']}" unless config['host'].nil?
75
+ cmd << " --port=#{config['port']}" unless config['port'].nil?
76
+ cmd + " > #{dump_file_path}"
77
+ when 'mysql2'
78
+ cmd = "mysqldump --routines --user=#{config['username']}" +
79
+ " --password=#{config['password']}"
80
+ cmd << " --host=#{config['host']}" unless config['host'].nil?
81
+ cmd << " --port=#{config['port']}" unless config['port'].nil?
82
+ cmd + " #{config['database']} > #{dump_file_path}"
83
+ else
84
+ raise NotSupportDatabaseAdapter.new
85
+ end
86
+ end
87
+
88
+ def import_cmd(config, db_file_path)
89
+ case config['adapter']
90
+ when 'postgresql'
91
+ cmd = "export PGPASSWORD=#{config['password']}; pg_restore #{db_file_path}" +
92
+ " --no-owner --no-acl -d #{config['database']}"
93
+ cmd << " --host=#{config['host']}" unless config['host'].nil?
94
+ cmd << " --port=#{config['port']}" unless config['port'].nil?
95
+ cmd
96
+ when 'mysql2'
97
+ cmd = "mysql --user=#{config['username']} --password=#{config['password']}"
98
+ cmd << " --host=#{config['host']}" unless config['host'].nil?
99
+ cmd << " --port=#{config['port']}" unless config['port'].nil?
100
+ cmd + " #{config['database']} < #{db_file_path}"
101
+ else
102
+ raise NotSupportDatabaseAdapter.new
103
+ end
104
+ end
105
+
106
+ def no_output
107
+ default = SSHKit.config.output
108
+ SSHKit.config.output = SSHKit::Formatter::Pretty.new(String.new)
109
+ result = yield
110
+ SSHKit.config.output = default
111
+ result
112
+ end
113
+
114
+ def db_config_path_msg
115
+ "enter your custom config path?(Y/N)"
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,57 @@
1
+ namespace :toolbox do
2
+ namespace :remote do
3
+ desc <<-DESC
4
+ open a rails console for remote
5
+ DESC
6
+ task :console do
7
+ on roles(:app) do |server|
8
+ set :format, :pretty
9
+ puts "Opening a console on: #{host}...."
10
+ command = "cd #{fetch(:deploy_to)}/current && "\
11
+ "RAILS_ENV=#{fetch(:stage)} "\
12
+ "#{SSHKit.config.command_map[:bundle]} exec rails console"
13
+ puts command
14
+ exec "ssh #{server.user}@#{host} -t '#{command}'"
15
+ end
16
+ end
17
+
18
+ desc <<-DESC
19
+ tail rails log file
20
+ DESC
21
+ task :log do
22
+ on roles :app do
23
+ set :log_level, :debug
24
+ set :format, :pretty
25
+ configure_backend
26
+
27
+ log_path = "#{shared_path}/log/#{fetch(:rails_env)}.log"
28
+ command = "tail -F #{log_path}"
29
+ puts command
30
+ execute command
31
+ end
32
+ end
33
+
34
+ desc <<-DESC
35
+ download [stage].log file to local, enter the path to store the log file, current path for blank
36
+ DESC
37
+
38
+ task :download_log do
39
+ on roles :app do
40
+ log_fname = "#{fetch(:rails_env)}.log"
41
+ ask(:local_path,
42
+ '')
43
+ puts fetch(:local_path)
44
+ local_path = if fetch(:local_path).chars.count == 0
45
+ "#{`pwd`.delete("\n")}/#{log_fname}"
46
+ else
47
+ "#{fetch(:local_path)}/#{log_fname}"
48
+ end
49
+ log_path = "#{shared_path}/log/#{log_fname}"
50
+
51
+ info 'start download'
52
+ download! log_path, local_path
53
+ info "download complete, your log file is in #{local_path}"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,2 @@
1
+ load File.expand_path('../tasks/remote.rake', __FILE__)
2
+ load File.expand_path('../tasks/db.rake', __FILE__)
@@ -0,0 +1,7 @@
1
+ module Capistrano
2
+ module Rails
3
+ module Toolbox
4
+ VERSION = "0.4.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-rails-toolbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - ACzero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: capistrano
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: capistrano-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sshkit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: tasks include loading db from server, tailing log and etc..
84
+ email:
85
+ - lzh.scut@hotmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - Rakefile
92
+ - lib/capistrano-rails-toolbox.rb
93
+ - lib/capistrano/rails/toolbox.rb
94
+ - lib/capistrano/rails/toolbox/tasks/db.rake
95
+ - lib/capistrano/rails/toolbox/tasks/remote.rake
96
+ - lib/capistrano/rails/toolbox/toolbox_tasks.rb
97
+ - lib/capistrano/rails/toolbox/version.rb
98
+ homepage: https://github.com/AdaChina/capistrano-rails-toolbox
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.2.5
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: some useful tasks for capistrano-rails
122
+ test_files: []