def restore(database_name, password, data, update=False):
    logger = logging.getLogger('database')
    security.check_super(password)
    try:
        database = Database().connect()
        cursor = database.cursor()
        cursor.close(close=True)
        raise Exception("Database already exists!")
    except Exception:
        pass
    Database.restore(database_name, data)
    logger.info('RESTORE DB: %s' % (database_name))
    if update:
        cursor = Database(database_name).connect().cursor()
        cursor.execute('SELECT code FROM ir_lang ' \
                'WHERE translatable')
        lang = [x[0] for x in cursor.fetchall()]
        cursor.execute('UPDATE ir_module_module SET ' \
                "state = 'to upgrade' " \
                "WHERE state = 'installed'")
        cursor.commit()
        cursor.close()
        Pool(database_name).init(update=update, lang=lang)
        logger.info('Update/Init succeed!')
    return True
def dump(database_name, password):
    security.check_super(password)
    Database(database_name).close()
    # Sleep to let connections close
    time.sleep(1)
    logger = logging.getLogger('database')

    data = Database.dump(database_name)
    logger.info('DUMP DB: %s' % (database_name))
    return buffer(data)
Beispiel #3
0
def create(database_name, password, lang, admin_password):
    '''
    Create a database

    :param database_name: the database name
    :param password: the server password
    :param lang: the default language for the database
    :param admin_password: the admin password
    :return: True if succeed
    '''
    Database = backend.get('Database')
    security.check_super(password)
    res = False

    try:
        with Transaction().start(None, 0, close=True, autocommit=True) \
                as transaction:
            transaction.database.create(transaction.cursor, database_name)
            transaction.cursor.commit()

        with Transaction().start(database_name, 0) as transaction:
            Database.init(transaction.cursor)
            transaction.cursor.execute(*ir_configuration.insert(
                    [ir_configuration.language], [[lang]]))
            transaction.cursor.commit()

        pool = Pool(database_name)
        pool.init(update=['res', 'ir'], lang=[lang])
        with Transaction().start(database_name, 0) as transaction:
            User = pool.get('res.user')
            Lang = pool.get('ir.lang')
            language, = Lang.search([('code', '=', lang)])
            language.translatable = True
            language.save()
            users = User.search([('login', '!=', 'root')])
            User.write(users, {
                    'language': language.id,
                    })
            admin, = User.search([('login', '=', 'admin')])
            User.write([admin], {
                    'password': admin_password,
                    })
            Module = pool.get('ir.module.module')
            if Module:
                Module.update_list()
            transaction.cursor.commit()
            res = True
    except Exception:
        logger.error('CREATE DB: %s failed', database_name, exc_info=True)
        raise
    else:
        logger.info('CREATE DB: %s', database_name)
    return res
Beispiel #4
0
def create(database_name, password, lang, admin_password):
    '''
    Create a database

    :param database_name: the database name
    :param password: the server password
    :param lang: the default language for the database
    :param admin_password: the admin password
    :return: True if succeed
    '''
    Database = backend.get('Database')
    security.check_super(password)
    res = False

    try:
        with Transaction().start(None, 0, close=True, autocommit=True) \
                as transaction:
            transaction.database.create(transaction.cursor, database_name)
            transaction.cursor.commit()

        with Transaction().start(database_name, 0) as transaction:
            Database.init(transaction.cursor)
            transaction.cursor.execute(*ir_configuration.insert(
                    [ir_configuration.language], [[lang]]))
            transaction.cursor.commit()

        pool = Pool(database_name)
        pool.init(update=['res', 'ir'], lang=[lang])
        with Transaction().start(database_name, 0) as transaction:
            User = pool.get('res.user')
            Lang = pool.get('ir.lang')
            language, = Lang.search([('code', '=', lang)])
            language.translatable = True
            language.save()
            users = User.search([('login', '!=', 'root')])
            User.write(users, {
                    'language': language.id,
                    })
            admin, = User.search([('login', '=', 'admin')])
            User.write([admin], {
                    'password': admin_password,
                    })
            Module = pool.get('ir.module')
            if Module:
                Module.update_list()
            transaction.cursor.commit()
            res = True
    except Exception:
        logger.error('CREATE DB: %s failed', database_name, exc_info=True)
        raise
    else:
        logger.info('CREATE DB: %s', database_name)
    return res
Beispiel #5
0
def dump(database_name, password):
    Database = backend.get('Database')
    security.check_super(password)
    Database(database_name).close()
    # Sleep to let connections close
    time.sleep(1)

    data = Database.dump(database_name)
    logger.info('DUMP DB: %s', database_name)
    if bytes == str:
        return bytearray(data)
    else:
        return bytes(data)
Beispiel #6
0
def dump(request, database_name, password):
    Database = backend.get('Database')
    security.check_super(password)
    Database(database_name).close()
    # Sleep to let connections close
    time.sleep(1)

    data = Database.dump(database_name)
    logger.info('DUMP DB: %s', database_name)
    if bytes == str:
        return bytearray(data)
    else:
        return bytes(data)
Beispiel #7
0
def drop(database_name, password):
    Database = backend.get('Database')
    security.check_super(password)
    Database(database_name).close()
    # Sleep to let connections close
    time.sleep(1)

    with Transaction().start(None, 0, close=True, autocommit=True) \
            as transaction:
        cursor = transaction.cursor
        try:
            Database.drop(cursor, database_name)
            cursor.commit()
        except Exception:
            logger.error('DROP DB: %s failed', database_name, exc_info=True)
            raise
        else:
            logger.info('DROP DB: %s', database_name)
            Pool.stop(database_name)
    return True
Beispiel #8
0
def drop(request, database_name, password):
    Database = backend.get('Database')
    security.check_super(password)
    database = Database(database_name)
    database.close()
    # Sleep to let connections close
    time.sleep(1)

    with Transaction().start(None, 0, close=True, autocommit=True) \
            as transaction:
        try:
            database.drop(transaction.connection, database_name)
        except Exception:
            logger.error('DROP DB: %s failed', database_name, exc_info=True)
            raise
        else:
            logger.info('DROP DB: %s', database_name)
            Pool.stop(database_name)
            Cache.drop(database_name)
    return True
Beispiel #9
0
def restore(database_name, password, data, update=False):
    Database = backend.get('Database')
    security.check_super(password)
    try:
        database = Database().connect()
        cursor = database.cursor()
        cursor.close(close=True)
        raise Exception("Database already exists!")
    except Exception:
        pass
    Database.restore(database_name, data)
    logger.info('RESTORE DB: %s', database_name)
    if update:
        with Transaction().start(database_name, 0) as transaction:
            cursor = transaction.cursor
            cursor.execute(*ir_lang.select(ir_lang.code,
                    where=ir_lang.translatable))
            lang = [x[0] for x in cursor.fetchall()]
            cursor.execute(*ir_module.select(ir_module.name,
                    where=(ir_module.state == 'installed')))
            update = [x[0] for x in cursor.fetchall()]
        Pool(database_name).init(update=update, lang=lang)
        logger.info('Update/Init succeed!')
    return True
def drop(database_name, password):
    security.check_super(password)
    Database(database_name).close()
    # Sleep to let connections close
    time.sleep(1)
    logger = logging.getLogger('database')

    database = Database().connect()
    cursor = database.cursor(autocommit=True)
    try:
        try:
            database.drop(cursor, database_name)
            cursor.commit()
        except Exception:
            logger.error('DROP DB: %s failed' % (database_name,))
            tb_s = ''.join(traceback.format_exception(*sys.exc_info()))
            logger.error('Exception in call: \n' + tb_s)
            raise
        else:
            logger.info('DROP DB: %s' % (database_name))
            Pool.stop(database_name)
    finally:
        cursor.close(close=True)
    return True
Beispiel #11
0
def restore(request, database_name, password, data, update=False):
    Database = backend.get('Database')
    security.check_super(password)
    try:
        Database(database_name).connect()
        existing = True
    except Exception:
        existing = False
    if existing:
        raise Exception('Database already exists!')
    Database.restore(database_name, data)
    logger.info('RESTORE DB: %s', database_name)
    if update:
        with Transaction().start(database_name, 0) as transaction,\
                transaction.connection.cursor() as cursor:
            cursor.execute(*ir_lang.select(ir_lang.code,
                    where=ir_lang.translatable))
            lang = [x[0] for x in cursor.fetchall()]
            cursor.execute(*ir_module.select(ir_module.name,
                    where=(ir_module.state == 'installed')))
            update = [x[0] for x in cursor.fetchall()]
        Pool(database_name).init(update=update, lang=lang)
        logger.info('Update/Init succeed!')
    return True
def create(database_name, password, lang, admin_password):
    '''
    Create a database

    :param database_name: the database name
    :param password: the server password
    :param lang: the default language for the database
    :param admin_password: the admin password
    :return: True if succeed
    '''
    security.check_super(password)
    res = False
    logger = logging.getLogger('database')

    try:
        database = Database().connect()
        cursor = database.cursor(autocommit=True)
        try:
            database.create(cursor, database_name)
            cursor.commit()
            cursor.close(close=True)
        except Exception:
            cursor.close()
            raise

        with Transaction().start(database_name, 0) as transaction:
            database.init(transaction.cursor)
            transaction.cursor.commit()

        pool = Pool(database_name)
        pool.init(update=True, lang=[lang])
        with Transaction().start(database_name, 0) as transaction:
            cursor = transaction.cursor
            #XXX replace with model write
            cursor.execute('UPDATE ir_lang ' \
                    'SET translatable = %s ' \
                    'WHERE code = %s', (True, lang))
            cursor.execute('UPDATE res_user ' \
                    'SET language = (' + \
                        cursor.limit_clause('SELECT id FROM ir_lang ' \
                        'WHERE code = %s', 1) + ')' \
                    'WHERE login <> \'root\'', (lang,))
            if hashlib:
                admin_password = hashlib.sha1(admin_password).hexdigest()
            else:
                admin_password = sha.new(admin_password).hexdigest()
            cursor.execute('UPDATE res_user ' \
                    'SET password = %s ' \
                    'WHERE login = \'admin\'', (admin_password,))
            module_obj = pool.get('ir.module.module')
            if module_obj:
                module_obj.update_list()
            cursor.commit()
            res = True
    except Exception:
        logger.error('CREATE DB: %s failed' % (database_name,))
        tb_s = ''.join(traceback.format_exception(*sys.exc_info()))
        logger.error('Exception in call: \n' + tb_s)
        raise
    else:
        logger.info('CREATE DB: %s' % (database_name,))
    return res