Example #1
0
def create_entity(appendpoint, entity, objtype, databases, appfile, timeout):
    middleware = GogameMiddle(endpoint=appendpoint, entity=entity, objtype=objtype)

    conf = CONF['%s.%s' % (common.NAME, objtype)]
    _database = []
    # format database to class
    for subtype in databases:
        database_id = databases[subtype]
        schema = '%s_%s_%s_%d' % (common.NAME, objtype, subtype, entity)
        # 默认认证后缀
        postfix = '-%d' % entity
        auth = dict(user=conf.get('%s_%s' % (subtype, 'user')) + postfix,
                    passwd=conf.get('%s_%s' % (subtype, 'passwd')),
                    ro_user=conf.get('%s_%s' % (subtype, 'ro_user')) + postfix,
                    ro_passwd=conf.get('%s_%s' % (subtype, 'ro_passwd')),
                    # source='%s/%s' % (appendpoint.manager.ipnetwork.network, appendpoint.manager.ipnetwork.netmask),
                    source=conf.source or '%',
                    rosource=conf.rosource or '%')
        LOG.debug('Create schema %s in %d with auth %s' % (schema, database_id, str(auth)))
        _database.append(GogameDatabase(create=True, backup=None, update=None,
                                        database_id=database_id, schema=schema,
                                        character_set='utf8',
                                        subtype=subtype,
                                        host=None, port=None, **auth))

    app = application.Application(middleware,
                                  createtask=GogameAppCreate(middleware, timeout),
                                  databases=_database)

    book = LogBook(name='create_%s_%d' % (appendpoint.namespace, entity))
    store = dict(download_timeout=timeout)
    taskflow_session = sqlite.get_taskflow_session()
    create_flow = pipe.flow_factory(taskflow_session, book,
                                    applications=[app, ],
                                    upgradefile=GogameAppFile(source=appfile, objtype=objtype),
                                    store=store,
                                    create_cls=GogameDatabaseCreateTask)
    connection = Connection(taskflow_session)
    engine = load(connection, create_flow, store=store,
                  book=book, engine_cls=ParallelActionEngine)

    try:
        engine.run()
    except Exception as e:
        if LOG.isEnabledFor(logging.DEBUG):
            LOG.exception('Create task execute fail')
        else:
            LOG.error('Create task execute fail, %s %s' % (e.__class__.__name__, str(e)))
    finally:
        connection.destroy_logbook(book.uuid)
        for dberror in middleware.dberrors:
            LOG.error(str(dberror))
    return middleware
Example #2
0
def upgrade_entitys(appendpoint, objtype, objfiles, entitys, timeline):
    upgradefile = None
    backupfile = None
    download_time = 600
    upzip_timeout = 600
    if common.APPFILE in objfiles:
        objfile = objfiles[common.APPFILE]
        md5 = objfile.get('md5')
        backup = objfile.get('backup', True)
        revertable = objfile.get('revertable', False)
        rollback = objfile.get('rollback', True)
        timeout = objfile.get('timeout')
        if timeout < download_time:
            download_time = timeout
        if timeout < upzip_timeout:
            upzip_timeout = timeout
        # 程序更新文件
        upgradefile = GogameAppFile(md5,
                                    objtype,
                                    rollback=rollback,
                                    revertable=revertable)
        if backup:
            # 备份entity在flow_factory随机抽取
            outfile = os.path.join(
                appendpoint.endpoint_backup,
                '%s.%s.%d.gz' % (objtype, common.APPFILE, timeline))
            # 程序备份文件
            backupfile = GogameAppBackupFile(outfile, objtype)

    applications = []
    middlewares = []
    _updates = {}
    for entity in entitys:
        if objtype != appendpoint._objtype(entity):
            raise ValueError('Entity not the same objtype')
        middleware = GogameMiddle(endpoint=appendpoint,
                                  entity=entity,
                                  objtype=objtype)
        middlewares.append(middleware)
        _database = []
        # 备份数据库信息
        for subtype in (common.DATADB, common.LOGDB):
            if subtype in objfiles:
                objfile = objfiles[subtype]
                md5 = objfile.get('md5')
                revertable = objfile.get('revertable', False)
                rollback = objfile.get('rollback', False)
                timeout = objfile.get('timeout')
                if timeout < download_time:
                    download_time = timeout
                dbinfo = appendpoint.local_database_info(entity, subtype)
                try:
                    update = _updates[subtype]
                except KeyError:
                    LOG.debug('New %s update file' % subtype)
                    update = DbUpdateFile(md5, revertable, rollback)
                    _updates[subtype] = update
                # 数据库备份文件
                backup = None
                if objfile.get('backup', False):
                    outfile = os.path.join(
                        appendpoint.endpoint_backup, '%s.%d.%s.%d.gz' %
                        (objtype, entity, subtype, timeline))
                    backup = DbBackUpFile(outfile)
                _database.append(
                    GogameDatabase(backup=backup,
                                   update=update,
                                   timeout=timeout,
                                   **dbinfo))
        # 更新程序文件任务
        upgradetask = None
        if common.APPFILE in objfiles:
            upgradetask = AppFileUpgradeByFile(
                middleware,
                native=False,
                rebind=['upgradefile', 'upzip_timeout'])
        app = Application(middleware,
                          upgradetask=upgradetask,
                          databases=_database)
        applications.append(app)
    _updates.clear()

    book = LogBook(name='upgrade_%s' % appendpoint.namespace)
    store = dict(download_timeout=download_time, upzip_timeout=upzip_timeout)
    taskflow_session = sqlite.get_taskflow_session()
    upgrade_flow = pipe.flow_factory(taskflow_session,
                                     book,
                                     applications=applications,
                                     upgradefile=upgradefile,
                                     backupfile=backupfile,
                                     store=store)
    connection = Connection(taskflow_session)
    engine = load(connection,
                  upgrade_flow,
                  store=store,
                  book=book,
                  engine_cls=ParallelActionEngine,
                  max_workers=4)
    e = None
    try:
        engine.run()
    except Exception as e:
        if LOG.isEnabledFor(logging.DEBUG):
            LOG.exception('Upgrade task execute fail')
        else:
            LOG.error('Upgrade task execute fail, %s %s' %
                      (e.__class__.__name__, str(e)))
    finally:
        connection.destroy_logbook(book.uuid)
    return middlewares, e
Example #3
0
def hotfix_entitys(appendpoint, objtype, appfile, entitys, timeline):
    backupfile = None
    download_time = 600
    upzip_timeout = 600

    md5 = appfile.get('md5')
    backup = appfile.get('backup', True)
    revertable = appfile.get('revertable', False)
    rollback = appfile.get('rollback', True)
    timeout = appfile.get('timeout')
    if timeout < download_time:
        download_time = timeout
    if timeout < upzip_timeout:
        upzip_timeout = timeout
    stream = appfile.get('stream')

    # 程序更新文件
    upgradefile = GogameAppFile(md5,
                                objtype,
                                rollback=rollback,
                                revertable=revertable,
                                stream=stream)
    if backup:
        # 备份entity在flow_factory随机抽取
        outfile = os.path.join(
            appendpoint.endpoint_backup,
            '%s.%s.%d.gz' % (objtype, common.APPFILE, timeline))
        # 程序备份文件
        backupfile = GogameAppBackupFile(outfile, objtype)

    applications = []
    middlewares = []
    _updates = {}
    for entity in entitys:
        if objtype != appendpoint._objtype(entity):
            raise ValueError('Entity not the same objtype')
        middleware = GogameMiddle(endpoint=appendpoint,
                                  entity=entity,
                                  objtype=objtype)
        middlewares.append(middleware)
        _updates.clear()
        upgradetask = AppFileUpgradeByFile(
            middleware,
            native=False,
            exclude=hofixexcluer,
            rebind=['upgradefile', 'upzip_timeout'])
        app = Application(middleware, upgradetask=upgradetask)
        applications.append(app)

    book = LogBook(name='hotfix_%s' % appendpoint.namespace)
    store = dict(download_timeout=download_time, upzip_timeout=upzip_timeout)
    taskflow_session = sqlite.get_taskflow_session()
    upgrade_flow = pipe.flow_factory(taskflow_session,
                                     book,
                                     applications=applications,
                                     upgradefile=upgradefile,
                                     backupfile=backupfile,
                                     store=store)
    connection = Connection(taskflow_session)
    engine = load(connection,
                  upgrade_flow,
                  store=store,
                  book=book,
                  engine_cls=ParallelActionEngine,
                  max_workers=4)
    e = None
    try:
        engine.run()
    except Exception as e:
        if LOG.isEnabledFor(logging.DEBUG):
            LOG.exception('Hotfix task execute fail')
        else:
            LOG.error('Hotfix task execute fail, %s %s' %
                      (e.__class__.__name__, str(e)))
    finally:
        connection.destroy_logbook(book.uuid)
    if stream:
        upgradefile.clean()
    return middlewares, e