Browse Source

handled ssl hosts [FIX] Flake8

pull/636/head
archetipo 9 years ago
committed by Dave Lasley
parent
commit
798dd47096
  1. 41
      auto_backup/model/backup_scheduler.py
  2. 5
      auto_backup/tests/test_auto_backup.py
  3. 2
      auto_backup/view/bkp_conf_view.xml

41
auto_backup/model/backup_scheduler.py

@ -50,12 +50,18 @@ def execute(connector, method, *args):
class db_backup(models.Model): class db_backup(models.Model):
_name = 'db.backup' _name = 'db.backup'
def get_connection(self, host, port):
def get_connection_uri(self, host, port, secure=False):
uri = 'http://%s:%s' % (host, port) 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') 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') db_list = execute(conn, 'list')
return db_list return db_list
@ -66,13 +72,26 @@ class db_backup(models.Model):
# Columns local server # Columns local server
host = fields.Char( host = fields.Char(
string='Host', default='localhost', size=100, required=True) string='Host', default='localhost', size=100, required=True)
securehost = fields.Boolean(string='Secure Host')
port = fields.Char( port = fields.Char(
string='Port', default='8069', size=10, required=True) string='Port', default='8069', size=10, required=True)
name = fields.Char( name = fields.Char(
string='Database', size=100, required=True, string='Database', size=100, required=True,
default=_get_db_name, default=_get_db_name,
help='Database you want to schedule backups for' 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( bkp_dir = fields.Char(
string='Backup Directory', size=100, string='Backup Directory', size=100,
default='/odoo/backups', default='/odoo/backups',
@ -171,7 +190,7 @@ class db_backup(models.Model):
@api.multi @api.multi
def _check_db_exist(self): def _check_db_exist(self):
for rec in 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: if rec.name in db_list:
return True return True
return False return False
@ -226,25 +245,23 @@ class db_backup(models.Model):
def schedule_backup(self): def schedule_backup(self):
for rec in self.search([]): 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: if rec.name in db_list:
file_path = '' file_path = ''
bkp_file = '' bkp_file = ''
try: try:
if not os.path.isdir(rec.bkp_dir): if not os.path.isdir(rec.bkp_dir):
os.makedirs(rec.bkp_dir) os.makedirs(rec.bkp_dir)
except:
raise
# Create name for dumpfile. # Create name for dumpfile.
bkp_file = '%s_%s.dump.zip' % ( bkp_file = '%s_%s.dump.zip' % (
time.strftime('%d_%m_%Y_%H_%M_%S'), time.strftime('%d_%m_%Y_%H_%M_%S'),
rec.name) rec.name)
file_path = os.path.join(rec.bkp_dir, bkp_file) file_path = os.path.join(rec.bkp_dir, bkp_file)
conn = self.get_connection(rec.host, rec.port)
conn = self.get_connection(
rec.host, rec.port, rec.securehost)
bkp = '' bkp = ''
try:
bkp = execute( bkp = execute(
conn, 'dump', tools.config['admin_passwd'], rec.name)
conn, 'dump', rec.adminpassword, rec.name)
except: except:
_logger.info( _logger.info(
'backup', netsvc.LOG_INFO, 'backup', netsvc.LOG_INFO,
@ -350,7 +367,8 @@ class db_backup(models.Model):
if rec.sendmailsftpfail: if rec.sendmailsftpfail:
self.send_notification(rec, e) 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: if rec.autoremove is True:
self.remove_folder(rec) self.remove_folder(rec)
@ -384,7 +402,6 @@ class db_backup(models.Model):
'Exception %s' % tools.ustr(e) 'Exception %s' % tools.ustr(e)
) )
# This is done after the SFTP writing to prevent unusual behaviour: # 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 # 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 # to keep backups xx days there wouldn't be any new back-ups added

5
auto_backup/tests/test_auto_backup.py

@ -32,12 +32,13 @@ class TestsAutoBackup(common.TransactionCase):
def test_0(self): def test_0(self):
with self.assertRaises(except_orm): with self.assertRaises(except_orm):
self.abk_model.create({'name': 'abcd'})
self.abk_model.create({'name': 'abcd', 'adminpassword': 'admin'})
def test_1(self): def test_1(self):
this = self.abk_model.create( this = self.abk_model.create(
{ {
'bkp_dir': '/tmp'
'bkp_dir': '/tmp',
'adminpassword': 'admin'
} }
) )
self.assertEqual(this.host, 'localhost') self.assertEqual(this.host, 'localhost')

2
auto_backup/view/bkp_conf_view.xml

@ -13,8 +13,10 @@
</group> </group>
<group> <group>
<field name="host" colspan="2"/> <field name="host" colspan="2"/>
<field name="securehost" colspan="2"/>
<field name="name"/> <field name="name"/>
<field name="port"/> <field name="port"/>
<field name="adminpassword" password="True"/>
<field name="bkp_dir"/> <field name="bkp_dir"/>
<field name="autoremove"/> <field name="autoremove"/>
<field name="daystokeep" attrs="{'invisible': [('autoremove','=',False)]}"/> <field name="daystokeep" attrs="{'invisible': [('autoremove','=',False)]}"/>

Loading…
Cancel
Save