mongo-db-utils 0.0.9 → 0.0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,68 +0,0 @@
1
- require 'aws/s3'
2
-
3
- module MongoDbUtils
4
- module Commands
5
-
6
- class MongoTools
7
-
8
- # wrapper for monogdump shell command
9
- def self.dump(host,port,db,output,username = "", password = "")
10
-
11
- options = []
12
- options << o("-h", "#{host}:#{port}")
13
- options << o("-db", db)
14
- options << o("-o", output)
15
- options << o("-u", username)
16
- options << o("-p", password)
17
-
18
- cmd = "mongodump "
19
-
20
- options.each do |o|
21
- cmd << "#{o.key} #{o.value} " unless o.empty?
22
- end
23
- `#{cmd}`
24
- end
25
-
26
-
27
- # wrapper for mongorestore shell command
28
- def self.restore(host,port,db,source_folder,username = "", password = "")
29
-
30
- options = []
31
- options << o("-h", "#{host}:#{port}")
32
- options << o("-db", db)
33
- options << o("-u", username)
34
- options << o("-p", password)
35
-
36
- cmd = "mongorestore "
37
-
38
- options.each do |o|
39
- cmd << "#{o.key} #{o.value} " unless o.empty?
40
- end
41
- #ensure that we drop everything before we restore.
42
- cmd << "--drop "
43
- cmd << "#{source_folder}"
44
- `#{cmd}`
45
- end
46
-
47
- private
48
- def self.o(key,value)
49
- Option.new(key,value)
50
- end
51
-
52
- end
53
-
54
- class Option
55
- attr_accessor :key, :value
56
-
57
- def initialize(key,value)
58
- @key = key
59
- @value = value
60
- end
61
-
62
- def empty?
63
- @value.nil? || @value.empty?
64
- end
65
- end
66
-
67
- end
68
- end
@@ -1,161 +0,0 @@
1
- module MongoDbUtils
2
-
3
- module Model
4
- class Config
5
- attr_reader :dbs, :buckets
6
- attr_writer :writer
7
- attr_accessor :backup_folder
8
-
9
- def initialize
10
- @dbs = []
11
- @buckets = []
12
- end
13
-
14
- def empty?
15
- @dbs.nil? || @dbs.empty?
16
- end
17
-
18
- def has_buckets?
19
- !@buckets.nil? && !@buckets.empty?
20
- end
21
-
22
-
23
- def flush
24
- @dbs = []
25
- @writer.flush
26
- end
27
-
28
- def remove_db(db)
29
- @dbs = @dbs - [db]
30
- @writer.save(self)
31
- end
32
-
33
- def add_db_from_uri(uri)
34
- @dbs = [] if @dbs.nil?
35
- db = Db.from_uri(uri)
36
- unless db.nil? || already_contains(db)
37
- @dbs << db
38
- @dbs.sort!
39
- @writer.save(self)
40
- end
41
- !db.nil?
42
- end
43
-
44
- def already_contains(db)
45
- @dbs.each do |existing|
46
- if( existing.to_s == db.to_s)
47
- return true
48
- end
49
-
50
- if( existing.host == db.host && existing.name == db.name)
51
- return true
52
- end
53
-
54
- end
55
- return false
56
- end
57
-
58
- # because we are serializing the config - the bucket may be nil
59
- # at this point
60
- def add_bucket(bucket)
61
- @buckets = [] if @buckets.nil?
62
- unless bucket.nil? || already_contains_bucket?(bucket)
63
- @buckets << bucket
64
- @writer.save(self)
65
- end
66
- end
67
-
68
- def already_contains_bucket?(bucket)
69
- puts "@buckets: #{@buckets}"
70
- @buckets.each do |b|
71
- if( b.to_s == bucket.to_s )
72
- return true
73
- end
74
- end
75
- return false
76
- end
77
-
78
-
79
- def save
80
- @writer.save(self)
81
- end
82
-
83
- def to_s
84
- "Config"
85
- end
86
- end
87
-
88
- class Bucket
89
- attr_accessor :name, :access_key, :secret_key
90
-
91
- def to_s
92
- "#{name} | #{access_key} | #{secret_key}"
93
- end
94
-
95
- def <=> (other)
96
- self.name <=> other.name
97
- end
98
-
99
- end
100
-
101
- # A Db stored in the config
102
- class Db
103
-
104
- URI_NO_USER = /mongodb:\/\/(.*):(.*)\/(.*$)/
105
- URI_USER = /mongodb:\/\/(.*):(.*)@(.*):(.*)\/(.*$)/
106
-
107
- attr_accessor :host, :username, :password, :port, :name
108
-
109
- def initialize(name, host, port, username=nil, password=nil)
110
- @host = host
111
- @port = port
112
- @name = name
113
- @username = username
114
- @password = password
115
- end
116
-
117
- def self.from_uri(uri)
118
-
119
- user,pwd,host,port,db = nil
120
-
121
- if( uri.match(URI_USER))
122
- match, user, pwd, host, port, name = *uri.match(URI_USER)
123
- elsif(uri.match(URI_NO_USER))
124
- match, host, port, name = *uri.match(URI_NO_USER)
125
- user = ""
126
- pwd = ""
127
- end
128
-
129
- return nil if( host.nil? || port.nil? || name.nil? )
130
-
131
- Db.new(name,host,port,user,pwd)
132
- end
133
-
134
- def authentication_required?
135
- has?(self.username) && has?(self.password)
136
- end
137
-
138
- def has?(s)
139
- !s.nil? && !s.empty?
140
- end
141
-
142
-
143
- def to_s
144
- user_pass = ""
145
- unless(@username.empty? || @password.empty? )
146
- user_pass = "#{@username}:#{@password}@"
147
- end
148
- "mongodb://#{user_pass}#{@host}:#{@port}/#{@name}"
149
- end
150
-
151
- def to_s_simple
152
- "#{@name} on #{@host}:#{@port} - (#{@username}:#{@password})"
153
- end
154
-
155
- def <=>(other)
156
- self.to_s <=> other.to_s
157
- end
158
-
159
- end
160
- end
161
- end
@@ -1,21 +0,0 @@
1
- require 'mongo-db-utils/config-loader'
2
-
3
- describe MongoDbUtils do
4
-
5
- it "should create a config if one doesn't exist" do
6
-
7
- tmp_file = ".tmp_path/config.yml"
8
-
9
- FileUtils.rm_rf(".tmp_path")
10
-
11
- File.exist?(tmp_file).should eql(false)
12
-
13
- config = MongoDbUtils::ConfigLoader.load(".tmp_path/config.yml")
14
-
15
- File.exist?(tmp_file).should eql(true)
16
-
17
- FileUtils.rm_rf(".tmp_path")
18
-
19
- end
20
-
21
- end
@@ -1,65 +0,0 @@
1
- require 'mongo-db-utils/models'
2
-
3
-
4
- describe MongoDbUtils::Model do
5
-
6
- it "should parse mongo uris" do
7
-
8
- uri = "mongodb://localhost:27017/ed-backup"
9
- db = MongoDbUtils::Model::Db.from_uri(uri)
10
- db.to_s.should eql(uri)
11
- db.host.should eql("localhost")
12
- db.port.should eql("27017")
13
- db.name.should eql("ed-backup")
14
- db.username.should eql("")
15
-
16
- db.authentication_required?.should eql(false)
17
-
18
- end
19
-
20
- it "should parse mongo uris" do
21
-
22
- uri = "mongodb://ed:password@localhost:27017/ed-backup"
23
- db = MongoDbUtils::Model::Db.from_uri(uri)
24
- db.to_s.should eql(uri)
25
- db.host.should eql("localhost")
26
- db.port.should eql("27017")
27
- db.name.should eql("ed-backup")
28
- db.username.should eql("ed")
29
- db.password.should eql("password")
30
- db.authentication_required?.should eql(true)
31
-
32
- end
33
-
34
- it "should return nil if its a bad uri" do
35
-
36
- uri = ""
37
- db = MongoDbUtils::Model::Db.from_uri(uri)
38
- db.should be(nil)
39
- end
40
-
41
- class MockWriter
42
- def save(config)
43
- end
44
-
45
- def flush
46
- end
47
- end
48
-
49
-
50
- it "config should add dbs only if they are different" do
51
- config = MongoDbUtils::Model::Config.new
52
- config.writer = MockWriter.new
53
-
54
- config.add_db_from_uri("mongodb://blah:3333@server:123/blah")
55
- config.dbs.length.should eql(1)
56
- config.add_db_from_uri("mongodb://blah:3333@server:123/blah")
57
- config.dbs.length.should eql(1)
58
- # still the same server + name
59
- config.add_db_from_uri("mongodb://server:123/blah")
60
- config.dbs.length.should eql(1)
61
- config.add_db_from_uri("mongodb://server2:123/blah")
62
- config.dbs.length.should eql(2)
63
- end
64
-
65
- end