Skip to content

Commit

Permalink
Add support for magic check in EvmDatabaseOps
Browse files Browse the repository at this point in the history
Calls the newly created method `MiqFileStorage.magic_number_for` to
pre-fetch the magic from the file that will be downloaded, and send that
to `PostgresAdmin` as a `database_opts` to avoid checking it on it's
end.

Requires changes in gems-pending to work (should be already merged)
  • Loading branch information
NickLaMuro committed Nov 10, 2018
1 parent 3d2a9b6 commit 807629c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
25 changes: 18 additions & 7 deletions lib/evm_database_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ def self.restore(db_opts, connect_opts = {})
# :username => 'samba_one',
# :password => 'Zug-drep5s',

uri = with_file_storage(:restore, db_opts, connect_opts) do |database_opts|
prepare_for_restore(database_opts[:local_file])
uri = with_file_storage(:restore, db_opts, connect_opts) do |database_opts, backup_type|
prepare_for_restore(database_opts[:local_file], backup_type)

# remove all the connections before we restore; AR will reconnect on the next query
ActiveRecord::Base.connection_pool.disconnect!
PostgresAdmin.restore(database_opts)
PostgresAdmin.restore(database_opts.merge(:backup_type => backup_type))
end
_log.info("[#{merged_db_opts(db_opts)[:dbname]}] database has been restored from file: [#{uri}]")
uri
Expand Down Expand Up @@ -144,7 +144,14 @@ def self.restore(db_opts, connect_opts = {})
# are doing a `MiqFileStorage.download`, and the interface for that
# method is to pass a `nil` for the block form since we are streaming the
# data from the command that we are writting as part of the block.
send_args.unshift(nil) if action == :restore
if action == :restore
send_args.unshift(nil)
magic_numbers = {
:pgdump => PostgresAdmin::PG_DUMP_MAGIC,
:basebackup => PostgresAdmin::BASE_BACKUP_MAGIC
}
backup_type = file_storage.magic_number_for(uri, :accepted => magic_numbers)
end

# Note: `input_path` will always be a fifo stream (input coming from
# PostgresAdmin, and the output going to the `uri`), since we want to
Expand All @@ -161,15 +168,19 @@ def self.restore(db_opts, connect_opts = {})
# set `db_opts` local file to that stream.
file_storage.send(STORAGE_ACTIONS_TO_METHODS[action], *send_args) do |input_path|
db_opts[:local_file] = input_path
yield(db_opts)
if action == :restore
yield(db_opts, backup_type)
else
yield(db_opts)
end
end
end

uri
end

private_class_method def self.prepare_for_restore(filename)
backup_type = validate_backup_file_type(filename)
private_class_method def self.prepare_for_restore(filename, backup_type = nil)
backup_type ||= validate_backup_file_type(filename)

if application_connections?
message = "Database restore failed. Shut down all evmserverd processes before attempting a database restore"
Expand Down
5 changes: 3 additions & 2 deletions spec/lib/evm_database_ops_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,10 @@
allow(MiqSmbSession).to receive(:runcmd)
allow(MiqSmbSession).to receive(:raw_disconnect)
allow(file_storage).to receive(:settings_mount_point).and_return(tmpdir)
allow(file_storage).to receive(:magic_number_for).and_return(:pgdump)
allow(file_storage).to receive(:download).and_yield(input_path)

allow(PostgresAdmin).to receive(:runcmd_with_logging)
allow(PostgresAdmin).to receive(:pg_dump_file?).and_return(true)
allow(PostgresAdmin).to receive(:base_backup_file?).and_return(false)

allow(VmdbDatabaseConnection).to receive(:count).and_return(1)
end
Expand Down Expand Up @@ -286,6 +285,8 @@ def execute_with_file_storage(action = :backup)
end

context "for a restore action" do
before { expect(file_storage).to receive(:magic_number_for) }

it "updates db_opts[:local_file] in the method context" do
expect(file_storage).to receive(:send).with(:download, nil, "smb://tmp/foo")
execute_with_file_storage(:restore)
Expand Down

0 comments on commit 807629c

Please sign in to comment.