Class: DBGet::Loaders::Mongo

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/dbget/loaders/mongo.rb

Constant Summary

Constants included from Constants

Constants::CONFIG_PATH, Constants::MONGO_FILE_EXT, Constants::MONGO_INDEX_FILE

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dump, config) ⇒ Mongo

Returns a new instance of Mongo.



10
11
12
13
# File 'lib/dbget/loaders/mongo.rb', line 10

def initialize(dump, config)
  @dump = dump
  @config = config
end

Class Method Details

.boot(dump, config) ⇒ Object



6
7
8
# File 'lib/dbget/loaders/mongo.rb', line 6

def self.boot(dump, config)
  self.new(dump, config)
end

Instance Method Details

#extract_mongo_dump(temp_path) ⇒ Object



66
67
68
# File 'lib/dbget/loaders/mongo.rb', line 66

def extract_mongo_dump(temp_path)
  `#{Binaries.tar_cmd} -C #{temp_path} -xf #{@dump.decrypted_dump} 2> /dev/null`
end

#get_temp_pathObject



40
41
42
43
44
# File 'lib/dbget/loaders/mongo.rb', line 40

def get_temp_path
  random = Utils.randomize(16)
  decrypted_basename = File.basename(@dump.decrypted_dump, '.tar')
  temp_path = File.join(DBGet.cache_path, "#{decrypted_basename}_#{random}")
end

#index?(file) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/dbget/loaders/mongo.rb', line 50

def index?(file)
  File.basename(file) == "#{MONGO_INDEX_FILE}#{MONGO_FILE_EXT}"
end

#load!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dbget/loaders/mongo.rb', line 15

def load!
  temp_path = get_temp_path

  FileUtils.mkdir(temp_path) if !File.exists?(temp_path)

  @dump.form_db_name
  extract_mongo_dump(temp_path)
  prepare_bson_files(temp_path)

  dump_files = Dir["#{temp_path}/*#{MONGO_FILE_EXT}"]
  dump_files = specify_collections(dump_files, temp_path)
  mongo_restore(dump_files)
  remove_temp_path(temp_path)
end

#mongo_restore(dump_files) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/dbget/loaders/mongo.rb', line 30

def mongo_restore(dump_files)
  dump_files.each do |file|
    unless index?(file)
      mongo_restore = Binaries.mongorestore_cmd

      system "#{mongo_restore} -d #{@dump.target_db} #{file} --drop"
    end
  end
end

#prepare_bson_files(temp_path) ⇒ Object



70
71
72
73
74
# File 'lib/dbget/loaders/mongo.rb', line 70

def prepare_bson_files(temp_path)
  `#{Binaries.find_cmd} #{temp_path} -name '*#{MONGO_FILE_EXT}'`.each_line do |l|
    FileUtils.mv(l.chomp!, File.join(temp_path, File.basename(l)))
  end
end

#remove_temp_path(path) ⇒ Object



46
47
48
# File 'lib/dbget/loaders/mongo.rb', line 46

def remove_temp_path(path)
  FileUtils.rm_rf(path)
end

#specify_collections(dump_files, temp_path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dbget/loaders/mongo.rb', line 54

def specify_collections(dump_files, temp_path)
  if !@dump.collections.empty?
    @dump.collections = @dump.collections.collect do |c|
      File.join(temp_path, c.concat(MONGO_FILE_EXT))
    end

    dump_files &= @dump.collections
  end

  dump_files
end