cloner 0.2.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: 084c4eb25834594c2bf7ab958a545f6b85a918c7
4
+ data.tar.gz: 653be534290038354984642b050a681c6bece755
5
+ SHA512:
6
+ metadata.gz: 9887f526f47f8482302a99d0e203d88e792369867dba64fc1c7985f5bc05a726157fc2eb2fb9c118f2e2ebfc1a00770a135b1a55b0a3b02991df8d33b307837b
7
+ data.tar.gz: 86e4e6c4fe618380ae1aff54f4d3406527b6170fc41d2cbecacd3323743cf4cff09598d959fec25105f591fa5d611aa2e579e7b1d9ca0879b46cdda4578da5e4
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ cloner
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cloner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 glebtv
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.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Cloner
2
+
3
+ Easily clone your production Mongoid database and files for local development
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cloner'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cloner
20
+
21
+ ## Usage
22
+
23
+ create ```lib/tasks/dl.thor``` with following content:
24
+
25
+ require 'cloner'
26
+
27
+ class Dl < Cloner::Base
28
+ no_commands do
29
+ def rails_path
30
+ File.expand_path("../../../config/environment", __FILE__)
31
+ end
32
+ def ssh_host
33
+ 'hottea.ru'
34
+ end
35
+ def ssh_user
36
+ 'tea'
37
+ end
38
+ def remote_dump_path
39
+ '/data/tea/dump'
40
+ end
41
+ def remote_app_path
42
+ "/data/tea/app/current"
43
+ end
44
+ end
45
+
46
+ desc "download", "clone files and DB from production"
47
+ def download
48
+ load_env
49
+ clone_db
50
+ rsync_public("ckeditor_assets")
51
+ rsync_public("uploads")
52
+ end
53
+ end
54
+
55
+ Adjust it to your project and deployment.
56
+
57
+ Run it:
58
+
59
+ thor dl
60
+
61
+ ## Additional
62
+
63
+ All functions from cloner/internal.rb can be overriden, for example:
64
+
65
+
66
+ def verbose?
67
+ false
68
+ end
69
+ def env_from
70
+ 'production'
71
+ end
72
+ def ssh_opts
73
+ {}
74
+ end
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it ( https://github.com/[my-github-username]/cloner/fork )
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/cloner.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cloner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cloner"
8
+ spec.version = Cloner::VERSION
9
+ spec.authors = ["glebtv"]
10
+ spec.email = ["glebtv@gmail.com"]
11
+ spec.summary = %q{Easily clone your production Mongoid database and files for local development}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/rs-pro/cloner"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency "thor"
22
+ spec.add_dependency "activesupport"
23
+ spec.add_dependency 'net-ssh'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
28
+
@@ -0,0 +1,10 @@
1
+ class Cloner::Base < Thor
2
+ include Thor::Actions
3
+
4
+ no_commands do
5
+ include Cloner::Internal
6
+ end
7
+
8
+ default_command :download
9
+ end
10
+
@@ -0,0 +1,25 @@
1
+ module Cloner::Internal
2
+ extend ActiveSupport::Concern
3
+ include Cloner::MongoDB
4
+ include Cloner::SSH
5
+ include Cloner::RSync
6
+
7
+ def load_env
8
+ unless defined?(Rails)
9
+ require rails_path
10
+ end
11
+ require 'net/ssh'
12
+ end
13
+
14
+ def verbose?
15
+ false
16
+ end
17
+ def env_from
18
+ ENV['CLONE_FROM'] || 'production'
19
+ end
20
+
21
+ def clone_db
22
+ clone_mongodb
23
+ end
24
+ end
25
+
@@ -0,0 +1,73 @@
1
+ module Cloner::MongoDB
2
+ extend ActiveSupport::Concern
3
+
4
+ def mongodb_conf
5
+ @conf ||= begin
6
+ YAML.load_file(Rails.root.join('config', 'mongoid.yml'))[Rails.env]['sessions']['default']
7
+ end
8
+ end
9
+
10
+ def mongodb_to
11
+ mongodb_conf['database']
12
+ end
13
+
14
+ def mongodb_r_conf
15
+ @r_conf ||= begin
16
+ Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
17
+ ret = ssh_exec!(ssh, "cat #{remote_app_path}/config/mongoid.yml")
18
+ check_ssh_err(ret)
19
+ YAML.load(ret[0])[env_from]['sessions']['default']
20
+ end
21
+ end
22
+ end
23
+
24
+ def mongodb_local_auth
25
+ if mongodb_conf['password'].nil?
26
+ ""
27
+ else
28
+ "-u #{mongodb_conf['username']} -p #{mongodb_conf['password']}"
29
+ end
30
+ end
31
+
32
+ def mongodb_dump_remote
33
+ puts "backup remote DB via ssh"
34
+ Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
35
+ ssh.exec!("rm -R #{remote_dump_path}")
36
+ ret = ssh_exec!(ssh, "mkdir -p #{remote_dump_path}")
37
+ check_ssh_err(ret)
38
+ dump = "mongodump -u #{mongodb_r_conf['username']} -p #{mongodb_r_conf['password']} -d #{mongodb_r_conf['database']} --authenticationDatabase #{mongodb_r_conf['database']} -o #{remote_dump_path}"
39
+ puts dump if verbose?
40
+ ret = ssh_exec!(ssh, dump)
41
+ check_ssh_err(ret)
42
+ end
43
+ end
44
+
45
+ def mongodb_dump_restore
46
+ puts "restoring DB"
47
+ restore = "mongorestore --drop -d #{mongodb_to} #{mongodb_local_auth} #{mongodb_path}"
48
+ puts restore if verbose?
49
+ pipe = IO.popen(restore)
50
+ while (line = pipe.gets)
51
+ print line if verbose?
52
+ end
53
+ ret = $?.to_i
54
+ if ret != 0
55
+ puts "Error: local command exited with #{ret}"
56
+ end
57
+ end
58
+
59
+ def mongodb_path
60
+ Rails.root.join("tmp", "dump", mongodb_to).to_s
61
+ end
62
+
63
+ def mongodb_dump_copy
64
+ `mkdir -p #{mongodb_path}`
65
+ rsync("#{remote_dump_path}/#{mongodb_r_conf['database']}", mongodb_path)
66
+ end
67
+
68
+ def clone_mongodb
69
+ mongodb_dump_remote()
70
+ mongodb_dump_copy()
71
+ mongodb_dump_restore()
72
+ end
73
+ end
@@ -0,0 +1,20 @@
1
+ module Cloner::RSync
2
+ extend ActiveSupport::Concern
3
+ def rsync(from, to)
4
+ cmd = "rsync -e ssh -zutvr --checksum #{ssh_user}@#{ssh_host}:#{from}/ #{to}/"
5
+ puts "Running RSync: #{cmd}"
6
+ pipe = IO.popen(cmd)
7
+ while (line = pipe.gets)
8
+ print line if verbose?
9
+ end
10
+ pipe.close
11
+ ret = $?.to_i
12
+ if ret != 0
13
+ puts "Error: local command exited with #{ret}"
14
+ end
15
+ end
16
+
17
+ def rsync_public(folder)
18
+ rsync("#{remote_app_path}/public/#{folder}", Rails.root.join("public/#{folder}"))
19
+ end
20
+ end
data/lib/cloner/ssh.rb ADDED
@@ -0,0 +1,48 @@
1
+ module Cloner::SSH
2
+ extend ActiveSupport::Concern
3
+
4
+ def ssh_opts
5
+ {}
6
+ end
7
+
8
+ # http://stackoverflow.com/questions/3386233/how-to-get-exit-status-with-rubys-netssh-library
9
+ def ssh_exec!(ssh, command)
10
+ stdout_data = ""
11
+ stderr_data = ""
12
+ exit_code = nil
13
+ exit_signal = nil
14
+ ssh.open_channel do |channel|
15
+ channel.exec(command) do |ch, success|
16
+ unless success
17
+ abort "FAILED: couldn't execute command (ssh.channel.exec)"
18
+ end
19
+ channel.on_data do |ch,data|
20
+ stdout_data+=data
21
+ end
22
+
23
+ channel.on_extended_data do |ch,type,data|
24
+ stderr_data+=data
25
+ end
26
+
27
+ channel.on_request("exit-status") do |ch,data|
28
+ exit_code = data.read_long
29
+ end
30
+
31
+ channel.on_request("exit-signal") do |ch, data|
32
+ exit_signal = data.read_long
33
+ end
34
+ end
35
+ end
36
+ ssh.loop
37
+ [stdout_data, stderr_data, exit_code, exit_signal]
38
+ end
39
+
40
+ def check_ssh_err(ret)
41
+ if ret[2] != 0
42
+ puts "Error: SSH command exited with #{ret[2]}"
43
+ puts ret[0]
44
+ puts ret[1]
45
+ exit 1
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Cloner
2
+ VERSION = "0.2.0"
3
+ end
data/lib/cloner.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "cloner/version"
2
+ require 'thor'
3
+ require 'active_support/concern'
4
+ require "cloner/mongodb"
5
+ require "cloner/ssh"
6
+ require "cloner/rsync"
7
+ require "cloner/internal"
8
+ require "cloner/base"
9
+
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - glebtv
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: net-ssh
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: ''
84
+ email:
85
+ - glebtv@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".ruby-gemset"
92
+ - ".ruby-version"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - cloner.gemspec
98
+ - lib/cloner.rb
99
+ - lib/cloner/base.rb
100
+ - lib/cloner/internal.rb
101
+ - lib/cloner/mongodb.rb
102
+ - lib/cloner/rsync.rb
103
+ - lib/cloner/ssh.rb
104
+ - lib/cloner/version.rb
105
+ homepage: https://github.com/rs-pro/cloner
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Easily clone your production Mongoid database and files for local development
129
+ test_files: []