mongo_sync 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02b246444c20f0a2d3b86ebba7c368f96811e58f
4
+ data.tar.gz: a9ab33f1c9e0a2c3db648abf4555f321e1434860
5
+ SHA512:
6
+ metadata.gz: 9e17dc26dbfb7343cbbdc7a9cc5dceea1633afd2eab7aacdd6047e0733fae63384f822489e422dc15989a340ed12c2783ab08e5b490a2f3a48f8671b8a0e5166
7
+ data.tar.gz: 53da72230fb5985f6967169a1a630de0c8a10ecc4f329af4d4281b8ede384e57a901abb4b7a07cd8e6b601ac21f7cee3279bee39d816f89080757b9530ef1519
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sheharyar Naseer
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,30 @@
1
+ mongo_sync
2
+ ==========
3
+
4
+ > Sync Remote and Local MongoDB Databases in Ruby!
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'mongo_sync'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mongo_sync
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( http://github.com/<my-github-username>/mongo_sync/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ require 'yaml'
2
+
3
+ module MongoSync
4
+ module Configuration
5
+ def self.parse(config_file)
6
+ config_file ||= File.join(Rails.root, 'config', 'mongo_sync.yml') if defined? Rails
7
+
8
+ if config_file
9
+ begin
10
+ return YAML::load(IO.read(config_file))
11
+ rescue Errno::ENOENT
12
+ raise FileNotFound
13
+ end
14
+ else
15
+ raise FileNotFound
16
+ end
17
+ end
18
+
19
+ class FileNotFound < StandardError; end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require 'mongo_sync'
2
+ require 'rails'
3
+
4
+ module MongoSync
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :mongo_sync
7
+
8
+ rake_tasks do
9
+ load "tasks/mongo_sync.rake"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module MongoSync
2
+ VERSION = "0.1.1"
3
+ end
data/lib/mongo_sync.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'mongo_sync/version'
2
+ require 'mongo_sync/config'
3
+ require 'mongo_sync/railtie' if defined? Rails
4
+
5
+ module MongoSync
6
+ def self.pull(config = nil)
7
+ configs = MongoSync::Configuration.parse(config)
8
+ tmpdir = new_tmp_dir
9
+
10
+ puts "Dumping Remote DB to #{tmpdir}..."
11
+ system_run "mongodump \
12
+ -h #{configs['remote']['host']['url']}:#{configs['remote']['host']['port']} \
13
+ -d #{configs['remote']['db']} \
14
+ -u #{configs['remote']['access']['username']} \
15
+ -p #{configs['remote']['access']['password']} \
16
+ -o #{tmpdir} > /dev/null"
17
+
18
+ puts "Overwriting Local DB..."
19
+ system_run "mongorestore \
20
+ -d #{configs['local']['db']} \
21
+ #{tmpdir}/#{configs['remote']['db']} \
22
+ --drop > /dev/null"
23
+
24
+ clean_up(tmpdir)
25
+ done_msg
26
+ end
27
+
28
+ def self.push(config = nil)
29
+ configs = MongoSync::Configuration.parse(config)
30
+ tmpdir = new_tmp_dir
31
+
32
+ puts "Dumping Local DB to #{tmpdir}..."
33
+ system_run "mongodump \
34
+ -d #{configs['local']['db']} \
35
+ -o #{tmpdir} > /dev/null"
36
+
37
+ puts "Overwriting Remote DB with Dump..."
38
+ system_run "mongorestore \
39
+ -h #{configs['remote']['host']['url']}:#{configs['remote']['host']['port']} \
40
+ -d #{configs['remote']['db']} \
41
+ -u #{configs['remote']['access']['username']} \
42
+ -p #{configs['remote']['access']['password']} \
43
+ #{tmpdir}/#{configs['remote']['db']} \
44
+ --drop > /dev/null"
45
+
46
+ clean_up(tmpdir)
47
+ done_msg
48
+ end
49
+
50
+
51
+ private
52
+
53
+ def self.new_tmp_dir
54
+ "/tmp/mongo_sync/#{Time.now.to_i}"
55
+ end
56
+
57
+ def self.success_msg
58
+ puts 'Success!', ''
59
+ end
60
+
61
+ def self.done_msg
62
+ puts 'Done!', ''
63
+ end
64
+
65
+ def self.clean_up(dir)
66
+ puts "Cleaning Up..."
67
+ system_run "rm -rf #{dir}"
68
+ end
69
+
70
+ def self.system_run(command)
71
+ raise SystemCommandFailure.new("\n'#{command.strip.gsub(/\s+/, ' ')}' \n") if not system(command)
72
+ success_msg
73
+ end
74
+
75
+ class SystemCommandFailure < StandardError; end
76
+ end
@@ -0,0 +1,13 @@
1
+ module MongoSync
2
+ module Generators
3
+ class ConfigGenerator < Rails::Generators::Base
4
+ desc 'Creates a MongoSync Gem configuration file at config/mongo_sync.yml'
5
+
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def create_config_file
9
+ template 'mongo_sync.yml', File.join('config', 'mongo_sync.yml')
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # MongoSync Configurations
2
+
3
+ local:
4
+ db: 'local_db_name'
5
+
6
+ remote:
7
+ db: 'remote_db_name'
8
+ host:
9
+ url: 'some.remoteurl.com'
10
+ port: 27017
11
+ access:
12
+ username: 'remote_mongo_user'
13
+ password: 'remote_mongo_pass'
14
+
15
+ # For now :local only accepts DB name
16
+ # Assumes DB is at localhost:27017
17
+
18
+ # All of the parameters above are required,
19
+ # without them the tasks will fail
@@ -0,0 +1,30 @@
1
+ namespace :mongosync do
2
+
3
+ desc "Push DB to Remote from Local"
4
+ task :push, [:config] => :environment do |t, args|
5
+ if user_accepts("This will overwrite your remote database.")
6
+ MongoSync.push args[:config]
7
+ end
8
+ end
9
+
10
+ desc "Pull DB to Local from Remote"
11
+ task :pull, [:config] => :environment do |t, args|
12
+ if user_accepts("This will overwrite your local database.")
13
+ MongoSync.pull args[:config]
14
+ end
15
+ end
16
+
17
+ # ---
18
+
19
+ def user_accepts(message)
20
+ print "#{message} Enter 'yes' to continue: "
21
+ input = STDIN.gets.chomp.downcase
22
+
23
+ if input == 'yes' or input == 'y'
24
+ return true
25
+ else
26
+ puts "Wrong Input, aborting..."
27
+ return false
28
+ end
29
+ end
30
+ end
@@ -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 'mongo_sync/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mongo_sync'
8
+ spec.version = MongoSync::VERSION
9
+ spec.author = 'Sheharyar Naseer'
10
+ spec.email = 'hello@sheharyar.me'
11
+ spec.homepage = 'https://github.com/sheharyarn/mongo-sync-ruby'
12
+ spec.license = 'MIT'
13
+ spec.summary = 'A Ruby Gem for syncing local and remote Mongo Databases'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.5'
21
+ spec.add_development_dependency 'rake'
22
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sheharyar Naseer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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:
42
+ email: hello@sheharyar.me
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - Gemfile
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/mongo_sync.rb
53
+ - lib/mongo_sync/config.rb
54
+ - lib/mongo_sync/railtie.rb
55
+ - lib/mongo_sync/version.rb
56
+ - lib/rails/generators/mongo_sync/config/config_generator.rb
57
+ - lib/rails/generators/mongo_sync/config/templates/mongo_sync.yml
58
+ - lib/tasks/mongo_sync.rake
59
+ - mongo_sync.gemspec
60
+ homepage: https://github.com/sheharyarn/mongo-sync-ruby
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A Ruby Gem for syncing local and remote Mongo Databases
84
+ test_files: []