From 36e8c376c4e06b23a196ee307f183a4297bc0174 Mon Sep 17 00:00:00 2001 From: archetipo Date: Wed, 2 Sep 2015 11:23:26 +0200 Subject: [PATCH] [FIX] handled ssl hosts [FIX] Flake8 --- auto_backup/model/backup_scheduler.py | 53 ++++++++++++++++++--------- auto_backup/tests/test_auto_backup.py | 5 ++- auto_backup/view/bkp_conf_view.xml | 2 + 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/auto_backup/model/backup_scheduler.py b/auto_backup/model/backup_scheduler.py index 48df3a25a..f074bb7a3 100644 --- a/auto_backup/model/backup_scheduler.py +++ b/auto_backup/model/backup_scheduler.py @@ -50,12 +50,18 @@ def execute(connector, method, *args): class db_backup(models.Model): _name = 'db.backup' - def get_connection(self, host, port): + def get_connection_uri(self, host, port, secure=False): uri = 'http://%s:%s' % (host, port) + if secure: + uri = 'https://%s:%s' % (host, port) + return uri + + def get_connection(self, host, port, secure=False): + uri = self.get_connection_uri(host, port, secure) return xmlrpclib.ServerProxy(uri + '/xmlrpc/db') - def get_db_list(self, host, port): - conn = self.get_connection(host, port) + def get_db_list(self, host, port, secure=False): + conn = self.get_connection(host, port, secure) db_list = execute(conn, 'list') return db_list @@ -66,13 +72,26 @@ class db_backup(models.Model): # Columns local server host = fields.Char( string='Host', default='localhost', size=100, required=True) + + securehost = fields.Boolean(string='Secure Host') + port = fields.Char( string='Port', default='8069', size=10, required=True) + name = fields.Char( string='Database', size=100, required=True, default=_get_db_name, help='Database you want to schedule backups for' ) + + adminpassword = fields.Char( + string='Admin user Password', + help=( + "The password Admin password of Odoo Instance." + ), + required=True + ) + bkp_dir = fields.Char( string='Backup Directory', size=100, default='/odoo/backups', @@ -171,7 +190,7 @@ class db_backup(models.Model): @api.multi def _check_db_exist(self): for rec in self: - db_list = self.get_db_list(rec.host, rec.port) + db_list = self.get_db_list(rec.host, rec.port, rec.securehost) if rec.name in db_list: return True return False @@ -226,25 +245,23 @@ class db_backup(models.Model): def schedule_backup(self): for rec in self.search([]): - db_list = self.get_db_list(rec.host, rec.port) + db_list = self.get_db_list(rec.host, rec.port, rec.securehost) if rec.name in db_list: file_path = '' bkp_file = '' try: if not os.path.isdir(rec.bkp_dir): os.makedirs(rec.bkp_dir) - except: - raise - # Create name for dumpfile. - bkp_file = '%s_%s.dump.zip' % ( - time.strftime('%d_%m_%Y_%H_%M_%S'), - rec.name) - file_path = os.path.join(rec.bkp_dir, bkp_file) - conn = self.get_connection(rec.host, rec.port) - bkp = '' - try: + # Create name for dumpfile. + bkp_file = '%s_%s.dump.zip' % ( + time.strftime('%d_%m_%Y_%H_%M_%S'), + rec.name) + file_path = os.path.join(rec.bkp_dir, bkp_file) + conn = self.get_connection( + rec.host, rec.port, rec.securehost) + bkp = '' bkp = execute( - conn, 'dump', tools.config['admin_passwd'], rec.name) + conn, 'dump', rec.adminpassword, rec.name) except: _logger.info( 'backup', netsvc.LOG_INFO, @@ -350,7 +367,8 @@ class db_backup(models.Model): if rec.sendmailsftpfail: self.send_notification(rec, e) - # Remove all old files (on local server) in case this is configured.. + # Remove all old files (on local server) + # in case this is configured.. if rec.autoremove is True: self.remove_folder(rec) @@ -384,7 +402,6 @@ class db_backup(models.Model): 'Exception %s' % tools.ustr(e) ) - # This is done after the SFTP writing to prevent unusual behaviour: # If the user would set local back-ups to be kept 0 days and the SFTP # to keep backups xx days there wouldn't be any new back-ups added diff --git a/auto_backup/tests/test_auto_backup.py b/auto_backup/tests/test_auto_backup.py index a383143a0..773fd6746 100644 --- a/auto_backup/tests/test_auto_backup.py +++ b/auto_backup/tests/test_auto_backup.py @@ -32,12 +32,13 @@ class TestsAutoBackup(common.TransactionCase): def test_0(self): with self.assertRaises(except_orm): - self.abk_model.create({'name': 'abcd'}) + self.abk_model.create({'name': 'abcd', 'adminpassword': 'admin'}) def test_1(self): this = self.abk_model.create( { - 'bkp_dir': '/tmp' + 'bkp_dir': '/tmp', + 'adminpassword': 'admin' } ) self.assertEqual(this.host, 'localhost') diff --git a/auto_backup/view/bkp_conf_view.xml b/auto_backup/view/bkp_conf_view.xml index 1f35678e6..061bff016 100644 --- a/auto_backup/view/bkp_conf_view.xml +++ b/auto_backup/view/bkp_conf_view.xml @@ -13,8 +13,10 @@ + +