Skip to content

Commit

Permalink
versions support for mongodb lifecycle feature
Browse files Browse the repository at this point in the history
Change-Id: If0fec1afbcddb22aca011234186bbcce2d438a11
  • Loading branch information
Andrew Liu committed Jul 8, 2012
1 parent 2a7833b commit 90df6a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 5 additions & 1 deletion mongodb/lib/mongodb_service/job/mongodb_snapshot.rb
Expand Up @@ -13,6 +13,8 @@ def execute
filename = "#{snapshot_id}.tgz"
dump_file_name = File.join(dump_path, filename)

setup_localdb
version = instance_version(name)
result = dump_database(name, dump_file_name)
raise "Failed to execute dump command to #{name}" unless result

Expand All @@ -23,7 +25,8 @@ def execute
:size => dump_file_size,
:files => [filename],
:manifest => {
:version => 1
:version => 1,
:service_version => version
}
}

Expand All @@ -38,6 +41,7 @@ def execute
dump_file_path = @snapshot_files[0]
raise "Snapshot file #{dump_file_path} doesn't exist" unless File.exists?(dump_file_path)

setup_localdb
result = restore_database(name, dump_file_path)
raise "Failed execute import command to #{name}" unless result

Expand Down
22 changes: 15 additions & 7 deletions mongodb/lib/mongodb_service/job/util.rb
Expand Up @@ -26,15 +26,13 @@ def dump_database(service_id, file, opts={})
return 1
end

mongodump_path = @config['mongodump_path'] ? @config['mongodump_path'] : 'mongodump'
tar_path = @config['tar_path'] ? @config['tar_path'] : 'tar'
cmd_timeout = @config['timeout'].to_f

DataMapper.setup(:default, @config['local_db'])
DataMapper::auto_upgrade!

tmp_dir = Dir.mktmpdir
service = Node::ProvisionedService.get(service_id)
version = service.version || @config["default_version"]
mongodump_path = @config['mongodump_path'] ? @config['mongodump_path'][version.to_s] : 'mongodump'

commands = [ "#{mongodump_path} -h 127.0.0.1:#{service.port} -u #{service.admin} -p #{service.adminpass} -o #{tmp_dir} ", \
"#{tar_path} czf #{file} -C #{tmp_dir} ." ]
Expand All @@ -56,6 +54,16 @@ def dump_database(service_id, file, opts={})
nil
end

def setup_localdb
DataMapper.setup(:default, @config['local_db'])
DataMapper::auto_upgrade!
end

def instance_version(service_id)
service = Node::ProvisionedService.get(service_id)
version = service.version || @config["default_version"]
end

def restore_database(service_id, file)
raise ArgumentError, "Missing options." unless service_id && file
make_logger
Expand All @@ -65,12 +73,12 @@ def restore_database(service_id, file)
return 1
end

mongorestore_path = @config['mongorestore_path'] ? @config['mongorestore_path'] : 'mongorestore'
@logger.debug("Manifest in snapshot: #{@manifest}")
version = @manifest["service_version"] || @config['default_version']
mongorestore_path = @config['mongorestore_path'] ? @config['mongorestore_path'][version.to_s] : 'mongorestore'
tar_path = @config['tar_path'] ? @config['tar_path'] : 'tar'
cmd_timeout = @config['timeout'].to_f

DataMapper.setup(:default, @config['local_db'])
DataMapper::auto_upgrade!

tmp_dir = Dir.mktmpdir

Expand Down

0 comments on commit 90df6a2

Please sign in to comment.