2016-10-03 17:49:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-12-30 11:12:29 +01:00
|
|
|
""" Odoo databases management script """
|
|
|
|
|
2016-10-03 17:49:15 +02:00
|
|
|
import argparse
|
|
|
|
import common
|
2017-12-30 11:12:29 +01:00
|
|
|
import odoorpc
|
|
|
|
|
2016-10-03 17:49:15 +02:00
|
|
|
|
2017-12-30 11:12:29 +01:00
|
|
|
def str2bool(val):
|
|
|
|
""" Convert sense str to boolean """
|
|
|
|
return val.lower() in ('yes', 'true', '1')
|
2016-10-03 17:49:15 +02:00
|
|
|
|
2017-12-30 11:12:29 +01:00
|
|
|
def dump(odoo, args):
|
|
|
|
""" Backup database on path """
|
|
|
|
dump_path = u'{}/{}_{}.zip'.format(args.dump_path, args.host, args.database)
|
|
|
|
dump_file = odoo.db.dump(args.password, args.database)
|
|
|
|
with open(dump_path, 'wb') as dump_zip:
|
|
|
|
dump_zip.write(dump_file.read())
|
2016-10-03 17:49:15 +02:00
|
|
|
|
2017-12-30 11:12:29 +01:00
|
|
|
def restore(odoo, args):
|
|
|
|
""" Restores database from local file to local or remote host """
|
2018-01-02 10:07:03 +01:00
|
|
|
if args.dump_full_path:
|
|
|
|
dump_path = args.dump_full_path
|
|
|
|
else:
|
|
|
|
dump_path = u'{}/{}_{}.zip'.format(args.dump_path, args.host, args.database)
|
2017-12-30 11:12:29 +01:00
|
|
|
with open(dump_path, 'rb') as dump_file:
|
|
|
|
if args.target_host and args.target_port and args.target_password:
|
|
|
|
connection = odoorpc.ODOO(args.target_host, port=args.target_port)
|
|
|
|
connection.config['timeout'] = 600
|
|
|
|
password = args.target_password
|
|
|
|
else:
|
|
|
|
connection = odoo
|
|
|
|
password = args.password
|
2018-01-02 17:58:20 +01:00
|
|
|
connection.db.restore(password, args.database_target or args.database,
|
|
|
|
dump_file, args.restore_copy)
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
""" Main function with argument parsing """
|
|
|
|
parser = argparse.ArgumentParser(description='Odoo database creation '
|
|
|
|
'according to their names')
|
|
|
|
parser = common.parser(parser)
|
2018-01-02 17:58:20 +01:00
|
|
|
parser.add_argument('-a', '--action', type=str, required=True,
|
|
|
|
help=('action for the database : create, drop, '
|
|
|
|
'duplicate, dump or restore'))
|
2016-10-03 17:49:15 +02:00
|
|
|
parser.add_argument('-d', '--database', type=str, required=True,
|
|
|
|
help='the targetted database')
|
|
|
|
parser.add_argument('-pw', '--password', type=str, required=True,
|
|
|
|
help='the odoo superadmin password')
|
|
|
|
parser.add_argument('-dm', '--demo', type=str, default='False',
|
|
|
|
help='demonstration data')
|
|
|
|
parser.add_argument('-l', '--lang', type=str, default='fr_FR',
|
|
|
|
help='default language')
|
2017-01-11 10:05:53 +01:00
|
|
|
parser.add_argument('-dbpw', '--database_password', type=str, default='admin',
|
|
|
|
help='the database admin password, default to admin')
|
2017-12-30 11:12:29 +01:00
|
|
|
parser.add_argument('-dp', '--dump-path', type=str, default='/tmp/',
|
|
|
|
help='the absolute path where to dump the database, '
|
|
|
|
'defaults to /tmp/')
|
2018-01-02 10:07:03 +01:00
|
|
|
parser.add_argument('-dfp', '--dump-full-path', type=str,
|
|
|
|
help='the absolute full path where to dump the database, '
|
|
|
|
'used if given, else dump path is used')
|
2017-12-30 11:12:29 +01:00
|
|
|
parser.add_argument('-dt', '--database-target', type=str,
|
|
|
|
help=('name for duplicated or restore targetted database '
|
|
|
|
' (optional : name_copy otherwise'))
|
|
|
|
parser.add_argument('-rc', '--restore-copy', type=bool, default=False,
|
|
|
|
help=('Is restore a copy or a move ? Defaults to False, '
|
|
|
|
'then move'))
|
|
|
|
parser.add_argument('-th', '--target-host', type=str,
|
|
|
|
help='targetted host, per default equal host')
|
|
|
|
parser.add_argument('-tpw', '--target-password', type=str,
|
|
|
|
help='the targeted host odoo superadmin password')
|
|
|
|
parser.add_argument('-tpp', '--target-port', type=str,
|
|
|
|
help='the targeted host port')
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Connection
|
|
|
|
odoo = common.connection(args)
|
|
|
|
|
|
|
|
# Databases actions
|
|
|
|
dblist = odoo.db.list()
|
2018-01-02 17:58:20 +01:00
|
|
|
if args.action == 'create' or args.action == 'restore':
|
2016-10-03 17:49:15 +02:00
|
|
|
if args.database in dblist:
|
|
|
|
print 'database already exists'
|
|
|
|
else:
|
2018-01-02 17:58:20 +01:00
|
|
|
if args.action == 'create':
|
|
|
|
odoo.db.create(args.password, args.database, str2bool(args.demo),
|
|
|
|
args.lang, args.database_password)
|
|
|
|
elif args.action == 'restore':
|
|
|
|
restore(odoo, args)
|
2016-10-03 17:49:15 +02:00
|
|
|
else:
|
|
|
|
if args.database not in dblist:
|
|
|
|
print 'database not found'
|
|
|
|
elif args.action == 'drop':
|
|
|
|
odoo.db.drop(args.password, args.database)
|
2017-12-30 11:12:29 +01:00
|
|
|
elif args.action == 'duplicate': # local or remote
|
|
|
|
name = args.database_target or args.database + '_copy'
|
|
|
|
if args.target_host and args.target_port and args.target_password:
|
|
|
|
dump(odoo, args)
|
|
|
|
restore(odoo, args)
|
|
|
|
else:
|
|
|
|
odoo.db.duplicate(args.password, args.database, name)
|
|
|
|
elif args.action == 'dump':
|
|
|
|
dump(odoo, args)
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|