mongo-db-utils 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -3
- data/lib/mongo-db-utils/cli.rb +2 -8
- data/lib/mongo-db-utils/cmd/mongotools.rb +24 -35
- data/lib/mongo-db-utils/cmd.rb +44 -38
- data/lib/mongo-db-utils/console.rb +109 -88
- data/lib/mongo-db-utils/models.rb +52 -3
- data/lib/mongo-db-utils/version.rb +1 -1
- data/mongo-db-utils.gemspec +1 -0
- data/spec/mongo_db_utils_spec.rb +8 -3
- metadata +2 -7
- data/.mongo-db-utils/config.yml +0 -2
- data/features/mongo-db-utils.feature +0 -9
- data/features/support/setup.rb +0 -1
data/README.md
CHANGED
@@ -12,9 +12,6 @@ It saves your database urls so any task is just a few clicks.
|
|
12
12
|
* backup a database and deploy it to Amazon S3
|
13
13
|
* copy a database from one server to another (whilst backing up locally the target db if it exists)
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
15
|
## Installation
|
19
16
|
|
20
17
|
You need to have *mongodump* and *mongorestore* on your path.
|
@@ -34,7 +31,13 @@ Only works on ruby 1.9.3 (to_yaml is throwing an error in earlier versions)
|
|
34
31
|
Once you launch the console it'll provide you with a set of options - pretty self explanatory.
|
35
32
|
When it does backups it stores them in ````~/.mongo-db-utils/backups/````. The naming convention is ````${server}_${port}/${database_name}/${timestamp}/db````
|
36
33
|
|
34
|
+
## Testing
|
35
|
+
|
36
|
+
bundle exec rspec spec
|
37
37
|
|
38
|
+
#cucumber can't handle interactive CLIs so need to wait on this.
|
39
|
+
#bundle exec cucumber features
|
40
|
+
|
38
41
|
## Building source
|
39
42
|
|
40
43
|
#run console
|
data/lib/mongo-db-utils/cli.rb
CHANGED
@@ -27,16 +27,10 @@ module MongoDbUtils
|
|
27
27
|
desc "backup_s3 MONGO_URI BUCKET ACCESS_KEY SECRET_ACCESS_KEY", "backup a db to Amason s3 with a mongo uri eg: mongodb://user:pass@server:port/dbname"
|
28
28
|
def backup_s3(mongo_uri, bucket_name, access_key_id, secret_access_key)
|
29
29
|
@config = MongoDbUtils::ConfigLoader.load
|
30
|
+
backup_folder = @config.backup_folder
|
30
31
|
db = MongoDbUtils::Model::Db.from_uri(mongo_uri)
|
31
32
|
raise "can't parse uri" if db.nil?
|
32
|
-
|
33
|
-
|
34
|
-
name = tar_file.gsub(File.expand_path(@config.backup_folder), "")
|
35
|
-
|
36
|
-
MongoDbUtils::S3::put_file(tar_file, name, bucket_name, access_key_id, secret_access_key)
|
37
|
-
file = File.basename(tar_file)
|
38
|
-
folder = tar_file.gsub(file, "")
|
39
|
-
`rm -fr #{folder}`
|
33
|
+
MongoDbUtils::Cmd.backup_s3(backup_folder, db, bucket_name, access_key_id, secret_access_key)
|
40
34
|
end
|
41
35
|
|
42
36
|
end
|
@@ -3,49 +3,35 @@ require 'aws/s3'
|
|
3
3
|
module MongoDbUtils
|
4
4
|
module Commands
|
5
5
|
|
6
|
-
class Option
|
7
|
-
attr_accessor :key, :value
|
8
|
-
|
9
|
-
def initialize(key,value)
|
10
|
-
@key = key
|
11
|
-
@value = value
|
12
|
-
end
|
13
|
-
|
14
|
-
def empty?
|
15
|
-
@value.nil? || @value.empty?
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
6
|
class MongoTools
|
20
7
|
|
8
|
+
# wrapper for monogdump shell command
|
21
9
|
def self.dump(host,port,db,output,username = "", password = "")
|
22
10
|
|
23
11
|
options = []
|
24
|
-
options <<
|
25
|
-
options <<
|
26
|
-
options <<
|
27
|
-
options <<
|
28
|
-
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)
|
29
17
|
|
30
18
|
cmd = "mongodump "
|
31
19
|
|
32
20
|
options.each do |o|
|
33
21
|
cmd << "#{o.key} #{o.value} " unless o.empty?
|
34
22
|
end
|
35
|
-
puts "cmd:"
|
36
|
-
puts cmd
|
37
23
|
`#{cmd}`
|
38
|
-
|
39
24
|
end
|
40
25
|
|
41
26
|
|
27
|
+
# wrapper for mongorestore shell command
|
42
28
|
def self.restore(host,port,db,source_folder,username = "", password = "")
|
43
29
|
|
44
30
|
options = []
|
45
|
-
options <<
|
46
|
-
options <<
|
47
|
-
options <<
|
48
|
-
options <<
|
31
|
+
options << o("-h", "#{host}:#{port}")
|
32
|
+
options << o("-db", db)
|
33
|
+
options << o("-u", username)
|
34
|
+
options << o("-p", password)
|
49
35
|
|
50
36
|
cmd = "mongorestore "
|
51
37
|
|
@@ -53,25 +39,28 @@ module MongoDbUtils
|
|
53
39
|
cmd << "#{o.key} #{o.value} " unless o.empty?
|
54
40
|
end
|
55
41
|
cmd << "#{source_folder}"
|
56
|
-
puts "cmd:"
|
57
|
-
puts cmd
|
58
42
|
`#{cmd}`
|
43
|
+
end
|
59
44
|
|
45
|
+
private
|
46
|
+
def self.o(key,value)
|
47
|
+
Option.new(key,value)
|
60
48
|
end
|
61
49
|
|
62
50
|
end
|
63
51
|
|
64
|
-
class
|
52
|
+
class Option
|
53
|
+
attr_accessor :key, :value
|
65
54
|
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
)
|
55
|
+
def initialize(key,value)
|
56
|
+
@key = key
|
57
|
+
@value = value
|
58
|
+
end
|
71
59
|
|
72
|
-
|
73
|
-
|
60
|
+
def empty?
|
61
|
+
@value.nil? || @value.empty?
|
74
62
|
end
|
75
63
|
end
|
64
|
+
|
76
65
|
end
|
77
66
|
end
|
data/lib/mongo-db-utils/cmd.rb
CHANGED
@@ -6,19 +6,15 @@ module MongoDbUtils
|
|
6
6
|
class Cmd
|
7
7
|
|
8
8
|
def self.backup(db, folder, final_path = nil, tar_it = true)
|
9
|
-
puts "--"
|
10
9
|
puts ">> Backing up: #{db}, #{folder}, #{final_path}"
|
11
10
|
unless( db_exists?(db) )
|
12
11
|
return false
|
13
12
|
end
|
14
13
|
|
15
|
-
t = Time.new
|
16
|
-
timestamp = t.strftime("%Y.%m.%d__%H.%M")
|
17
|
-
|
18
14
|
if( final_path.nil? )
|
19
15
|
out_path = "#{folder}/#{db.host}_#{db.port}/#{db.name}/#{timestamp}"
|
20
16
|
else
|
21
|
-
|
17
|
+
out_path = "#{folder}/#{final_path}"
|
22
18
|
end
|
23
19
|
|
24
20
|
full_path = File.expand_path(out_path)
|
@@ -38,9 +34,10 @@ module MongoDbUtils
|
|
38
34
|
Dir.chdir(full_path)
|
39
35
|
`tar cvf #{db.name}.tar #{db.name}`
|
40
36
|
`rm -fr #{full_path}/#{db.name}`
|
37
|
+
"#{full_path}/#{db.name}.tar"
|
38
|
+
else
|
39
|
+
"#{full_path}/#{db.name}"
|
41
40
|
end
|
42
|
-
|
43
|
-
"#{full_path}/#{db.name}.tar"
|
44
41
|
end
|
45
42
|
|
46
43
|
# With remote dbs you can't do a copy_database if you're not an admin.
|
@@ -52,11 +49,9 @@ module MongoDbUtils
|
|
52
49
|
# end
|
53
50
|
# host = "#{source.host}:#{source.port}"
|
54
51
|
# connection.copy_database(source.name, destination.name, host, source.username, source.password)
|
55
|
-
#
|
56
|
-
#
|
57
52
|
def self.copy(path, source, destination, halt_on_no_backup = true)
|
58
53
|
|
59
|
-
backup_made =
|
54
|
+
backup_made = backup(destination, path)
|
60
55
|
|
61
56
|
if( !backup_made && halt_on_no_backup)
|
62
57
|
puts "aborting - no backup was made"
|
@@ -68,53 +63,64 @@ module MongoDbUtils
|
|
68
63
|
FileUtils.mkdir_p(tmp_path)
|
69
64
|
|
70
65
|
puts "backup to: #{tmp_path}/#{source.name}"
|
71
|
-
backup(source,tmp_path, source.name, false)
|
66
|
+
tmp_dump_path = backup(source,tmp_path, source.name, false)
|
67
|
+
|
68
|
+
username = destination.username
|
69
|
+
password = destination.password
|
70
|
+
|
71
|
+
# if the destination db doesn't exist
|
72
|
+
# we assume that the user has admin control of the server
|
73
|
+
# eg its a local server (this needs to be thought through a bit more)
|
74
|
+
if( !db_exists?(destination) )
|
75
|
+
username = ""
|
76
|
+
password = ""
|
77
|
+
end
|
72
78
|
|
73
79
|
MongoDbUtils::Commands::MongoTools.restore(
|
74
80
|
destination.host,
|
75
81
|
destination.port,
|
76
82
|
destination.name,
|
77
|
-
"#{
|
78
|
-
|
79
|
-
|
83
|
+
"#{tmp_dump_path}",
|
84
|
+
username,
|
85
|
+
password)
|
80
86
|
|
81
87
|
`rm -fr #{tmp_path}`
|
82
88
|
end
|
83
89
|
|
90
|
+
def self.backup_s3(backup_folder, db, bucket_name, access_key_id, secret_access_key)
|
91
|
+
tar_file = MongoDbUtils::Cmd.backup(db, backup_folder)
|
92
|
+
name = tar_file.gsub(File.expand_path(backup_folder), "")
|
93
|
+
MongoDbUtils::S3::put_file(tar_file, name, bucket_name, access_key_id, secret_access_key)
|
94
|
+
file = File.basename(tar_file)
|
95
|
+
folder = tar_file.gsub(file, "")
|
96
|
+
`rm -fr #{folder}`
|
97
|
+
end
|
98
|
+
|
84
99
|
|
85
100
|
private
|
86
|
-
def self.db_exists?(db)
|
87
101
|
|
88
|
-
|
102
|
+
def self.timestamp
|
103
|
+
t = Time.new
|
104
|
+
t.strftime("%Y.%m.%d__%H.%M")
|
105
|
+
end
|
89
106
|
|
107
|
+
def self.db_exists?(db)
|
108
|
+
puts "DB exists? #{db.to_s}"
|
90
109
|
connection = Mongo::Connection.from_uri(db.to_s)
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
puts "mongo_db: #{mongo_db}"
|
98
|
-
if( db.authentication_required? && exists )
|
99
|
-
login_result = mongo_db.authenticate(db.username, db.password)
|
100
|
-
exists = !login_result.nil?
|
101
|
-
else
|
110
|
+
exists = true
|
111
|
+
begin
|
112
|
+
connection.ping
|
113
|
+
rescue Mongo::AuthenticationError => e
|
114
|
+
exists = false
|
102
115
|
end
|
103
116
|
connection.close
|
104
117
|
exists
|
105
118
|
end
|
119
|
+
=begin
|
120
|
+
connection.database_names
|
121
|
+
rescue Mongo::OperationFailure => e
|
122
|
+
=end
|
106
123
|
|
107
|
-
def self.remove_db(db)
|
108
|
-
if( db_exists?(db))
|
109
|
-
connection = Mongo::Connection.from_uri(db.to_s)
|
110
|
-
if( db.authentication_required? )
|
111
|
-
mongo_db = connection[db.name]
|
112
|
-
mongo_db.authenticate(db.username, db.password)
|
113
|
-
end
|
114
124
|
|
115
|
-
connection.drop_database(db.name)
|
116
|
-
connection.close
|
117
|
-
end
|
118
|
-
end
|
119
125
|
end
|
120
126
|
end
|
@@ -6,10 +6,10 @@ module MongoDbUtils
|
|
6
6
|
class Console
|
7
7
|
|
8
8
|
HEADER = <<-eos
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
=====================================
|
10
|
+
|| Mongo Db Utils - Version: #{MongoDbUtils::VERSION} ||
|
11
|
+
=====================================
|
12
|
+
eos
|
13
13
|
|
14
14
|
|
15
15
|
def initialize(config, cmd)
|
@@ -25,16 +25,16 @@ module MongoDbUtils
|
|
25
25
|
|
26
26
|
private
|
27
27
|
def main_menu
|
28
|
-
|
29
|
-
|
30
|
-
prep_menu(menu)
|
28
|
+
|
29
|
+
my_menu("What do you want to do?") do |menu|
|
31
30
|
menu.choice "copy a db" do copy_a_db end
|
32
|
-
menu.choice "backup a db" do do_backup end
|
31
|
+
menu.choice "backup a db locally" do do_backup end
|
32
|
+
menu.choice "backup a db to an amazon s3 bucket" do backup_to_s3 end
|
33
33
|
menu.choice "remove config" do remove_config end
|
34
34
|
menu.choice "show config" do show_config end
|
35
35
|
menu.choice "add server to config" do add_config end
|
36
|
+
menu.choice "add Amazon bucket to config" do add_bucket_to_config end
|
36
37
|
menu.choice "remove server from config" do remove_server_from_config end
|
37
|
-
menu.choice "exit" do say("goodbye") end
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -43,15 +43,60 @@ module MongoDbUtils
|
|
43
43
|
main_menu
|
44
44
|
end
|
45
45
|
|
46
|
+
#
|
46
47
|
def do_backup
|
47
|
-
|
48
48
|
if @config.empty?
|
49
49
|
get_config
|
50
50
|
else
|
51
|
-
|
51
|
+
db_list_menu("Choose a DB:") do |db|
|
52
|
+
@cmd.backup(db, @config.backup_folder)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def backup_to_s3
|
59
|
+
|
60
|
+
plan = Hash.new
|
61
|
+
|
62
|
+
db_list_menu("Choose a DB:") do |db|
|
63
|
+
plan[:db] = db
|
64
|
+
end
|
65
|
+
|
66
|
+
if @config.has_buckets?
|
67
|
+
list_buckets("Choose a Bucket:") do |bucket|
|
68
|
+
plan[:bucket] = bucket
|
69
|
+
end
|
70
|
+
else
|
71
|
+
say("You don't have any buckets yet - add one..")
|
72
|
+
add_bucket_to_config
|
73
|
+
return
|
52
74
|
end
|
75
|
+
|
76
|
+
@cmd.backup_s3(
|
77
|
+
@config.backup_folder,
|
78
|
+
plan[:db],
|
79
|
+
plan[:bucket].name,
|
80
|
+
plan[:bucket].access_key,
|
81
|
+
plan[:bucket].secret_key)
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
def add_bucket_to_config
|
86
|
+
say("add an amazon bucket:")
|
87
|
+
bucket = MongoDbUtils::Model::Bucket.new
|
88
|
+
|
89
|
+
bucket.name = ask("Name")
|
90
|
+
bucket.access_key = ask("Access Key")
|
91
|
+
bucket.secret_key = ask("Secret Key")
|
92
|
+
|
93
|
+
@config.add_bucket(bucket)
|
94
|
+
|
95
|
+
say("Bucket added")
|
96
|
+
my_menu("")
|
53
97
|
end
|
54
98
|
|
99
|
+
|
55
100
|
def get_config
|
56
101
|
say("You don't have any servers configured, please add one:")
|
57
102
|
add_config
|
@@ -63,18 +108,23 @@ module MongoDbUtils
|
|
63
108
|
say("dbs:")
|
64
109
|
say("--------------------")
|
65
110
|
@config.dbs.sort.each do |db|
|
66
|
-
say(
|
111
|
+
say(db.to_s_simple)
|
67
112
|
end
|
68
113
|
say("--------------------")
|
69
|
-
say("")
|
114
|
+
say("Amazon S3 Buckets")
|
115
|
+
if( @config.buckets.nil?)
|
116
|
+
say("no buckets")
|
117
|
+
else
|
118
|
+
@config.buckets.sort.each do |bucket|
|
119
|
+
say(bucket.to_s)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
70
123
|
say("backups folder:")
|
71
124
|
say("--------------------")
|
72
125
|
say("#{@config.backup_folder}")
|
73
126
|
say("--------------------")
|
74
|
-
|
75
|
-
prep_menu(menu)
|
76
|
-
menu.choice "back" do main_menu end
|
77
|
-
end
|
127
|
+
my_menu("")
|
78
128
|
end
|
79
129
|
|
80
130
|
def add_config
|
@@ -83,76 +133,29 @@ module MongoDbUtils
|
|
83
133
|
new_uri = entry[:mongo_uri].gsub(" ", "")
|
84
134
|
successful = @config.add_db_from_uri(new_uri)
|
85
135
|
|
86
|
-
|
87
|
-
say("added server")
|
88
|
-
choose do |menu|
|
89
|
-
prep_menu(menu)
|
90
|
-
menu.choice "add another?" do add_config end
|
91
|
-
menu.choice "done" do main_menu end
|
92
|
-
end
|
93
|
-
|
94
|
-
else
|
95
|
-
say("couldn't add uri")
|
96
|
-
add_config
|
97
|
-
end
|
98
|
-
end
|
136
|
+
say("bad uri!") unless successful
|
99
137
|
|
138
|
+
label = successful ? "add another?" : "try again?"
|
100
139
|
|
101
|
-
|
102
|
-
|
103
|
-
choose do |menu|
|
104
|
-
prep_menu(menu)
|
105
|
-
@config.dbs.each do |db|
|
106
|
-
menu.choice "#{db.to_s}" do backup(db) end
|
107
|
-
end
|
108
|
-
menu.choice "back" do main_menu end
|
140
|
+
my_menu("") do |menu|
|
141
|
+
menu.choice label do add_config end
|
109
142
|
end
|
110
143
|
end
|
111
144
|
|
112
|
-
|
113
145
|
def remove_server_from_config
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
@config.dbs.sort.each do |db|
|
118
|
-
menu.choice "#{db.to_s}" do
|
119
|
-
@config.remove_db(db)
|
120
|
-
remove_server_from_config
|
121
|
-
end
|
122
|
-
end
|
123
|
-
menu.choice "back" do main_menu end
|
146
|
+
db_list_menu("Remove server from config:") do |db|
|
147
|
+
@config.remove_db(db)
|
148
|
+
remove_server_from_config
|
124
149
|
end
|
125
150
|
end
|
126
151
|
|
127
152
|
def copy_a_db
|
128
|
-
|
129
153
|
copy_plan = Hash.new
|
130
|
-
|
131
|
-
|
132
|
-
choose do |menu|
|
133
|
-
prep_menu(menu)
|
134
|
-
@config.dbs.sort.each do |db|
|
135
|
-
menu.choice "#{db.to_s}" do
|
136
|
-
copy_plan[:source] = db
|
137
|
-
end
|
138
|
-
|
139
|
-
end
|
140
|
-
menu.choice "add server to config" do add_config end
|
141
|
-
menu.choice "back" do
|
142
|
-
main_menu
|
143
|
-
return
|
144
|
-
end
|
154
|
+
db_list_menu("Choose db to copy:") do |db|
|
155
|
+
copy_plan[:source] = db
|
145
156
|
end
|
146
|
-
|
147
|
-
|
148
|
-
choose do |menu|
|
149
|
-
prep_menu(menu)
|
150
|
-
@config.dbs.sort.each do |db|
|
151
|
-
menu.choice "#{db.to_s}" do
|
152
|
-
copy_plan[:destination] = db
|
153
|
-
end unless db == copy_plan[:source]
|
154
|
-
end
|
155
|
-
menu.choice "add server to config" do add_config end
|
157
|
+
db_list_menu("Choose a destination:") do |db|
|
158
|
+
copy_plan[:destination] = db
|
156
159
|
end
|
157
160
|
show_copy_plan(copy_plan)
|
158
161
|
end
|
@@ -161,31 +164,49 @@ module MongoDbUtils
|
|
161
164
|
say("Copy: (we'll backup the destination before we copy)")
|
162
165
|
say("#{plan[:source].to_s} --> #{plan[:destination].to_s}")
|
163
166
|
|
164
|
-
|
165
|
-
|
166
|
-
menu.choice "Begin" do begin_copy(plan) end
|
167
|
+
my_menu("") do |menu|
|
168
|
+
menu.choice "Do it!" do begin_copy(plan) end
|
167
169
|
menu.choice "Reverse" do
|
168
170
|
show_copy_plan( {:source => plan[:destination], :destination => plan[:source]})
|
169
171
|
end
|
170
|
-
menu.choice "Back" do main_menu end
|
171
172
|
end
|
172
173
|
end
|
173
174
|
|
174
175
|
def begin_copy(plan)
|
175
|
-
say("doing copy...")
|
176
176
|
@cmd.copy(@config.backup_folder, plan[:source], plan[:destination], false)
|
177
177
|
end
|
178
178
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
179
|
+
def my_menu(prompt, show_defaults = true)
|
180
|
+
say(prompt)
|
181
|
+
choose do |menu|
|
182
|
+
menu.shell = true
|
183
|
+
menu.index = :number
|
184
|
+
menu.prompt = "\n#{prompt}\n"
|
185
|
+
menu.index_suffix = ") "
|
186
|
+
menu.prompt = "?"
|
187
|
+
if( show_defaults)
|
188
|
+
menu.choice "Back" do main_menu end
|
189
|
+
menu.choice "Exit" do say("Goodbye"); abort("--"); end
|
190
|
+
end
|
191
|
+
say(" ")
|
192
|
+
yield menu if block_given?
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def db_list_menu(prompt)
|
197
|
+
my_menu(prompt, false) do |menu|
|
198
|
+
@config.dbs.sort.each do |db|
|
199
|
+
menu.choice "#{db.to_s_simple}" do yield db if block_given? end
|
200
|
+
end
|
201
|
+
end
|
183
202
|
end
|
184
203
|
|
185
|
-
def
|
186
|
-
|
187
|
-
|
188
|
-
|
204
|
+
def list_buckets(prompt)
|
205
|
+
my_menu(prompt) do |menu|
|
206
|
+
@config.buckets.sort.each do |bucket|
|
207
|
+
menu.choice "#{bucket}" do yield bucket if block_given? end
|
208
|
+
end
|
209
|
+
end
|
189
210
|
end
|
190
211
|
|
191
212
|
end
|
@@ -2,18 +2,24 @@ module MongoDbUtils
|
|
2
2
|
|
3
3
|
module Model
|
4
4
|
class Config
|
5
|
-
attr_reader :dbs
|
5
|
+
attr_reader :dbs, :buckets
|
6
6
|
attr_writer :writer
|
7
7
|
attr_accessor :backup_folder
|
8
8
|
|
9
9
|
def initialize
|
10
10
|
@dbs = []
|
11
|
+
@buckets = []
|
11
12
|
end
|
12
13
|
|
13
14
|
def empty?
|
14
15
|
@dbs.nil? || @dbs.empty?
|
15
16
|
end
|
16
17
|
|
18
|
+
def has_buckets?
|
19
|
+
!@buckets.nil? && !@buckets.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
|
17
23
|
def flush
|
18
24
|
@dbs = []
|
19
25
|
@writer.flush
|
@@ -27,7 +33,7 @@ module MongoDbUtils
|
|
27
33
|
def add_db_from_uri(uri)
|
28
34
|
@dbs = [] if @dbs.nil?
|
29
35
|
db = Db.from_uri(uri)
|
30
|
-
unless db.nil?
|
36
|
+
unless db.nil? || already_contains(db)
|
31
37
|
@dbs << db
|
32
38
|
@dbs.sort!
|
33
39
|
@writer.save(self)
|
@@ -35,6 +41,41 @@ module MongoDbUtils
|
|
35
41
|
!db.nil?
|
36
42
|
end
|
37
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
|
+
|
38
79
|
def save
|
39
80
|
@writer.save(self)
|
40
81
|
end
|
@@ -44,6 +85,14 @@ module MongoDbUtils
|
|
44
85
|
end
|
45
86
|
end
|
46
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
|
+
end
|
47
96
|
|
48
97
|
# A Db stored in the config
|
49
98
|
class Db
|
@@ -96,7 +145,7 @@ module MongoDbUtils
|
|
96
145
|
end
|
97
146
|
|
98
147
|
def to_s_simple
|
99
|
-
"#{@host}:#{@port}
|
148
|
+
"#{@name} on #{@host}:#{@port} - (#{@username}:#{@password})"
|
100
149
|
end
|
101
150
|
|
102
151
|
def <=>(other)
|
data/mongo-db-utils.gemspec
CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
14
|
gem.name = "mongo-db-utils"
|
15
15
|
gem.require_paths = ["lib"]
|
16
|
+
gem.required_ruby_version = '>= 1.9.3'
|
16
17
|
gem.version = MongoDbUtils::VERSION
|
17
18
|
gem.add_development_dependency "rspec", "~> 2.6"
|
18
19
|
gem.add_development_dependency "cucumber"
|
data/spec/mongo_db_utils_spec.rb
CHANGED
@@ -47,13 +47,18 @@ describe MongoDbUtils::Model do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
|
50
|
-
it "config should add dbs" do
|
50
|
+
it "config should add dbs only if they are different" do
|
51
51
|
config = MongoDbUtils::Model::Config.new
|
52
52
|
config.writer = MockWriter.new
|
53
53
|
|
54
|
-
config.add_db_from_uri("mongodb://blah:3333/blah")
|
54
|
+
config.add_db_from_uri("mongodb://blah:3333@server:123/blah")
|
55
55
|
config.dbs.length.should eql(1)
|
56
|
-
config.add_db_from_uri("mongodb://blah:3333/blah")
|
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")
|
57
62
|
config.dbs.length.should eql(2)
|
58
63
|
end
|
59
64
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo-db-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -164,14 +164,11 @@ extensions: []
|
|
164
164
|
extra_rdoc_files: []
|
165
165
|
files:
|
166
166
|
- .gitignore
|
167
|
-
- .mongo-db-utils/config.yml
|
168
167
|
- Gemfile
|
169
168
|
- LICENSE
|
170
169
|
- README.md
|
171
170
|
- Rakefile
|
172
171
|
- bin/mongo-db-utils
|
173
|
-
- features/mongo-db-utils.feature
|
174
|
-
- features/support/setup.rb
|
175
172
|
- images/sample.png
|
176
173
|
- install_rdebug.sh
|
177
174
|
- lib/mongo-db-utils/cli.rb
|
@@ -196,7 +193,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
196
193
|
requirements:
|
197
194
|
- - ! '>='
|
198
195
|
- !ruby/object:Gem::Version
|
199
|
-
version:
|
196
|
+
version: 1.9.3
|
200
197
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
201
198
|
none: false
|
202
199
|
requirements:
|
@@ -210,7 +207,5 @@ signing_key:
|
|
210
207
|
specification_version: 3
|
211
208
|
summary: some utilities for managing your mongod dbs
|
212
209
|
test_files:
|
213
|
-
- features/mongo-db-utils.feature
|
214
|
-
- features/support/setup.rb
|
215
210
|
- spec/config-loader-spec.rb
|
216
211
|
- spec/mongo_db_utils_spec.rb
|
data/.mongo-db-utils/config.yml
DELETED
data/features/support/setup.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'aruba/cucumber'
|