From 564d0ccab4949200d96fed4bb7403025c2b776ec Mon Sep 17 00:00:00 2001 From: eldar nugaev Date: Tue, 1 Mar 2016 13:36:05 +0000 Subject: [PATCH] Fix metadata curr_level Resolves bug: 1551734 Change-Id: I77f9d3827497b80e9b620543f2f4a805c00ff0ad --- freezer/backup.py | 11 +++---- freezer/job.py | 41 ++++++++++---------------- freezer/main.py | 18 +++++++----- tests/unit/test_job.py | 65 +++--------------------------------------- 4 files changed, 36 insertions(+), 99 deletions(-) diff --git a/freezer/backup.py b/freezer/backup.py index 210cd9c1..94b1f459 100644 --- a/freezer/backup.py +++ b/freezer/backup.py @@ -46,7 +46,7 @@ def backup_mode_sql_server(backup_opt_dict, storage): backup_opt_dict.sql_server_instance = sql_server_instance try: winutils.stop_sql_server(sql_server_instance) - backup(backup_opt_dict, storage, backup_opt_dict.engine) + return backup(backup_opt_dict, storage, backup_opt_dict.engine) finally: if not backup_opt_dict.snapshot: # if snapshot is false, wait until the backup is complete @@ -87,7 +87,7 @@ def backup_mode_mysql(backup_opt_dict, storage): raise Exception('[*] MySQL: {0}'.format(error)) # Execute backup - backup(backup_opt_dict, storage, backup_opt_dict.engine) + return backup(backup_opt_dict, storage, backup_opt_dict.engine) def backup_mode_mongo(backup_opt_dict, storage): @@ -111,11 +111,11 @@ def backup_mode_mongo(backup_opt_dict, storage): mongo_primary = master_dict['primary'] if mongo_me == mongo_primary: - backup(backup_opt_dict, storage, backup_opt_dict.engine) + return backup(backup_opt_dict, storage, backup_opt_dict.engine) else: logging.warning('[*] localhost {0} is not Master/Primary,\ exiting...'.format(local_hostname)) - return True + return None class BackupOs: @@ -282,12 +282,12 @@ def backup(backup_opt_dict, storage, engine): backup_opt_dict.restart_always_level, time_stamp=time_stamp) engine.backup(filepath, backup_instance) + return backup_instance finally: # whether an error occurred or not, remove the snapshot anyway if snapshot_taken: snapshot_remove(backup_opt_dict, backup_opt_dict.shadow, backup_opt_dict.windows_volume) - return backup_os = BackupOs(backup_opt_dict.client_manager, backup_opt_dict.container, @@ -304,3 +304,4 @@ def backup(backup_opt_dict, storage, engine): backup_os.backup_cinder_by_glance(backup_opt_dict.cinder_vol_id) else: raise Exception('unknown parameter backup_media %s' % backup_media) + return None diff --git a/freezer/job.py b/freezer/job.py index a827566d..66e2a4b2 100644 --- a/freezer/job.py +++ b/freezer/job.py @@ -33,7 +33,7 @@ logging = log.getLogger(__name__) class Job: """ - :type storage: freezer.storage.Storage + :type storage: freezer.storage.base.Storage :type engine: freezer.engine.engine.BackupEngine """ @@ -45,9 +45,6 @@ class Job: def execute(self): logging.info('[*] Action not implemented') - def get_metadata(self): - return None - @staticmethod def executemethod(func): def wrapper(self): @@ -83,19 +80,24 @@ class BackupJob(Job): logging.error('Error while sync exec: {0}'.format(error)) if self.conf.mode == 'fs': - backup.backup(self.conf, self.storage, self.engine) + backup_instance = \ + backup.backup(self.conf, self.storage, self.engine) elif self.conf.mode == 'mongo': - backup.backup_mode_mongo(self.conf, self.storage) + backup_instance = \ + backup.backup_mode_mongo(self.conf, self.storage) elif self.conf.mode == 'mysql': - backup.backup_mode_mysql(self.conf, self.storage) + backup_instance = \ + backup.backup_mode_mysql(self.conf, self.storage) elif self.conf.mode == 'sqlserver': - backup.backup_mode_sql_server(self.conf, self.storage) + backup_instance = \ + backup.backup_mode_sql_server(self.conf, self.storage) else: raise ValueError('Please provide a valid backup mode') - def get_metadata(self): + level = backup_instance.level if backup_instance else 0 + metadata = { - 'curr_backup_level': 0, + 'curr_backup_level': level, 'fs_real_path': (self.conf.lvm_auto_snap or self.conf.path_to_backup), 'vol_snap_path': @@ -142,6 +144,7 @@ class RestoreJob(Job): res.restore_cinder(conf.cindernative_vol_id, restore_timestamp) else: raise Exception("unknown backup type: %s" % conf.backup_media) + return {} class AdminJob(Job): @@ -156,6 +159,7 @@ class AdminJob(Job): self.storage.remove_older_than(timestamp, self.conf.hostname_backup_name) + return {} class ExecJob(Job): @@ -168,19 +172,4 @@ class ExecJob(Job): else: logging.warning( '[*] No command info options were set. Exiting.') - return False - return True - - -def create_job(conf, storage): - if conf.action == 'backup': - return BackupJob(conf, storage) - if conf.action == 'restore': - return RestoreJob(conf, storage) - if conf.action == 'info': - return InfoJob(conf, storage) - if conf.action == 'admin': - return AdminJob(conf, storage) - if conf.action == 'exec': - return ExecJob(conf, storage) - raise Exception('Action "{0}" not supported'.format(conf.action)) + return {} diff --git a/freezer/main.py b/freezer/main.py index 069751f4..0b17382a 100644 --- a/freezer/main.py +++ b/freezer/main.py @@ -121,14 +121,18 @@ def freezer_main(backup_args): run_job(backup_args, storage) -def run_job(backup_args, storage): - freezer_job = job.create_job(backup_args, storage) - freezer_job.execute() +def run_job(conf, storage): + freezer_job = { + 'backup': job.BackupJob, + 'restore': job.RestoreJob, + 'info': job.InfoJob, + 'admin': job.AdminJob, + 'exec': job.ExecJob}[conf.action](conf, storage) + response = freezer_job.execute() - if backup_args.metadata_out == '-': - metadata = freezer_job.get_metadata() - if metadata: - sys.stdout.write(json.dumps(metadata)) + if conf.metadata_out == '-': + if response: + sys.stdout.write(json.dumps(response)) def fail(exit_code, e, quiet, do_log=True): diff --git a/tests/unit/test_job.py b/tests/unit/test_job.py index 5f1f48ce..8aeef420 100644 --- a/tests/unit/test_job.py +++ b/tests/unit/test_job.py @@ -20,26 +20,13 @@ from freezer.tests.commons import * from freezer.job import ExecJob from freezer import backup -from freezer.job import Job, InfoJob, AdminJob, BackupJob, RestoreJob, \ - create_job +from freezer.job import Job, InfoJob, AdminJob, BackupJob from mock import patch, Mock import unittest -class FakeBackup: - def __init__(self): - pass - - def fake_backup_mode_mongo(self, *args, **kwargs): - return True - - def fake_backup_mode_mysql(self, *args, **kwargs): - return True - - class TestJob(unittest.TestCase): - fakebackup = FakeBackup() def test_execute(self): opt = BackupOpt1() @@ -69,22 +56,6 @@ class TestBackupJob(TestJob): job = BackupJob(backup_opt, backup_opt.storage) self.assertRaises(Exception, job.execute) - def test_execute_backup_mongo(self): - backup.backup_mode_mongo = self.fakebackup.fake_backup_mode_mongo - backup_opt = BackupOpt1() - backup_opt.no_incremental = False - backup_opt.mode = 'mongo' - job = BackupJob(backup_opt, backup_opt.storage) - assert job.execute() is None - - def test_execute_backup_mysql(self): - backup.backup_mode_mysql = self.fakebackup.fake_backup_mode_mysql - backup_opt = BackupOpt1() - backup_opt.no_incremental = False - backup_opt.mode = 'mysql' - job = BackupJob(backup_opt, backup_opt.storage) - assert job.execute() is None - def test_execute_raise(self): backup_opt = BackupOpt1() backup_opt.no_incremental = False @@ -96,8 +67,7 @@ class TestBackupJob(TestJob): class TestAdminJob(TestJob): def test_execute(self): backup_opt = BackupOpt1() - job = AdminJob(backup_opt, backup_opt.storage) - assert job.execute() is None + job = AdminJob(backup_opt, backup_opt.storage).execute() class TestExecJob(TestJob): @@ -115,15 +85,13 @@ class TestExecJob(TestJob): def test_execute_nothing_to_do(self): backup_opt = BackupOpt1() - job = ExecJob(backup_opt, backup_opt.storage) - assert job.execute() is False + ExecJob(backup_opt, backup_opt.storage).execute() def test_execute_script(self): self.mock_popen.return_value.returncode = 0 backup_opt = BackupOpt1() backup_opt.command='echo test' - job = ExecJob(backup_opt, backup_opt.storage) - assert job.execute() is True + ExecJob(backup_opt, backup_opt.storage).execute() def test_execute_raise(self): self.popen=patch('freezer.exec_cmd.subprocess.Popen') @@ -133,28 +101,3 @@ class TestExecJob(TestJob): backup_opt.command = 'echo test' job = ExecJob(backup_opt, backup_opt.storage) self.assertRaises(Exception, job.execute) - - def test_create_job(self): - backup_opt = BackupOpt1() - backup_opt.action = None - self.assertRaises(Exception, create_job, backup_opt) - - backup_opt.action = 'backup' - job = create_job(backup_opt, backup_opt.storage) - assert isinstance(job, BackupJob) - - backup_opt.action = 'restore' - job = create_job(backup_opt, backup_opt.storage) - assert isinstance(job, RestoreJob) - - backup_opt.action = 'info' - job = create_job(backup_opt, backup_opt.storage) - assert isinstance(job, InfoJob) - - backup_opt.action = 'admin' - job = create_job(backup_opt, backup_opt.storage) - assert isinstance(job, AdminJob) - - backup_opt.action = 'exec' - job = create_job(backup_opt, backup_opt.storage) - assert isinstance(job, ExecJob)