mongo-util 0.0.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: 77460289e675fa2a3a550b5966f055228cddc427
4
+ data.tar.gz: c5b3a008606b6ade10db5007914ae9892e503b1c
5
+ SHA512:
6
+ metadata.gz: f1733d22c5efaf11a19bfa5b487bac8717d60ad6ac8bdbb4cef4f4be05cb73e20db82ac622d1baf86a09177e2c1e628dbeb4a5cb6c893e9e990e6559cc0c8bd2
7
+ data.tar.gz: baee2b80c9fb74919250f2704569165641b299be587dcbd08ec65f11c9865e357aaf881fc6380e62f4831461aa3cce036a42acfa031087ea1979fe4df9d27c27
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ mongo-util
2
+ ==========
3
+
4
+ Ruby Interface for copying Mongo Collections from one server to another
@@ -0,0 +1,5 @@
1
+ module Mongo
2
+ module Util
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/mongo/util.rb ADDED
@@ -0,0 +1,94 @@
1
+ require 'json'
2
+
3
+ module Mongo
4
+
5
+ class Util
6
+
7
+ STANDARD_TO_SETTINGS = { host: 'localhost', port: 27017 }
8
+
9
+ attr_reader :from, :to
10
+ attr_accessor :dump_dir
11
+
12
+ def initialize(from={}, to={}, dump_dir=nil)
13
+ @from = from
14
+ @to = STANDARD_TO_SETTINGS.merge(to)
15
+ @dump_dir = dump_dir || 'dump'
16
+ end
17
+
18
+ def from=(from)
19
+ @from = @from.merge(from)
20
+ end
21
+
22
+ def to=(to)
23
+ @to = @to.merge(to)
24
+ end
25
+
26
+ # Dump from @from{database}
27
+ def dump(options={})
28
+ unless @from[:host] && @from[:port] && @from[:db]
29
+ raise 'Cannot dump: needs @to[:host], @port & @db'
30
+ end
31
+
32
+ cmd = "mongodump --host #{@from[:host]} --port #{@from[:port]} -d #{@from[:db]}"
33
+ # Append collection, if neccessary
34
+ cmd += " -c #{options[:collection]}" if options[:collection]
35
+ # Append query, if neccessary
36
+ cmd += " -q '#{options[:query].to_json}'" if options[:query]
37
+ # Append auth, if neccessary
38
+ cmd += Mongo::Util.authentication(@from)
39
+
40
+ system(cmd)
41
+ end
42
+
43
+ # Restore contents of @dump_dr to @to{database}
44
+ def restore
45
+ unless @to[:host] && @to[:port] && @to[:db] && @from[:db]
46
+ raise 'Cannot restore: needs @to[:host], @to[:port], @to[:db] & @from[:db]'
47
+ end
48
+
49
+ cmd = "mongorestore --host #{@to[:host]} --port #{@to[:port]} -d#{@to[:db]} dump/#{@from[:db]}"
50
+ # Append auth, if neccessary
51
+ cmd += Mongo::Util.authentication(@to)
52
+
53
+ system(cmd)
54
+ end
55
+
56
+ # Removes all items / items which match options[:query]
57
+ # from collection
58
+ def remove(collection, options={})
59
+ unless @to[:host] && @to[:port] && @to[:db]
60
+ raise "Cannot remove #{collection}: needs @to[:host], @to[:port], @to[:db]"
61
+ end
62
+
63
+ cmd = "mongo #{@to[:db]} --host #{@to[:host]} --port #{@to[:port]}"
64
+ # Append auth, if neccessary
65
+ cmd += Mongo::Util.authentication(@to)
66
+ cmd += " --eval 'db.#{collection}.remove(#{options[:query] ? options[:query].to_json : ""});'"
67
+
68
+ system(cmd)
69
+ end
70
+
71
+ # Returns Array of all collection-names of @to{database}
72
+ def collections
73
+ unless @to[:host] && @to[:port] && @to[:db]
74
+ raise 'Cannot fetch collections: needs @to[:host], @to[:port], @to[:db]'
75
+ end
76
+
77
+ `mongo #{@to[:db]} --host #{@to[:host]} --port #{@to[:port]} --quiet --eval 'db.getCollectionNames()'`.rstrip.split(',')
78
+ end
79
+
80
+ # Deletes @dump_dir
81
+ def clean
82
+ system("rm -rf #{@dump_dir}")
83
+ end
84
+
85
+ # Enable auth if we set db_config contains user & password
86
+ def self.authentication(db_config)
87
+ if (db_config[:user] && db_config[:password])
88
+ " -u#{db_config[:user]} -p#{db_config[:password]}"
89
+ else
90
+ ""
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,20 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "mongo/util/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'mongo-util'
8
+ s.version = Mongo::Util::VERSION
9
+ s.date = '2014-11-03'
10
+ s.summary = "Ruby Interface for copying Mongo Collections from one server to another"
11
+ s.description = "Uses standard mongo shell tools to provide an easy copying interface. No extra ruby mongo driver required"
12
+ s.authors = ["Finn-Lenanrt Heemeyer"]
13
+ s.email = 'finn@heemeyer.net'
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = ['lib']
16
+ s.homepage = 'http://rubygems.org/gems/mongo-util'
17
+ s.license = 'MIT'
18
+
19
+ s.add_runtime_dependency 'json'
20
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo-util
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Finn-Lenanrt Heemeyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
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
+ description: Uses standard mongo shell tools to provide an easy copying interface.
28
+ No extra ruby mongo driver required
29
+ email: finn@heemeyer.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - lib/mongo/util.rb
37
+ - lib/mongo/util/version.rb
38
+ - mongo-util.gemspec
39
+ homepage: http://rubygems.org/gems/mongo-util
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.3.0
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Ruby Interface for copying Mongo Collections from one server to another
63
+ test_files: []