Exemple #1
0
def db_clean_legacy():
    conf = cfg.ConfigOpts()
    conf.register_cli_opts([
        cfg.strOpt('confirm-drop-alarm-table',
                   short='n',
                   help='confirm to drop the legacy alarm tables')])
    if not conf.confirm_drop_alarm_table:
        confirm = moves.input("Do you really want to drop the legacy alarm "
                              "tables? This will destroy data definitely "
                              "if it exist. Please type 'YES' to confirm: ")
        if confirm != 'YES':
            print("DB legacy cleanup aborted!")
            return

    service.prepare_service(conf=conf)
    for purpose in ['metering', 'event']:
        url = (getattr(conf.database, '%s_connection' % purpose) or
               conf.database.connection)
        parsed = urlparse.urlparse(url)

        if parsed.password:
            masked_netloc = '****'.join(parsed.netloc.rsplit(parsed.password))
            masked_url = parsed._replace(netloc=masked_netloc)
            masked_url = urlparse.urlunparse(masked_url)
        else:
            masked_url = url
        LOG.info(_LI('Starting to drop alarm and alarm history tables in '
                     '%(purpose)s backend: %(url)s'), {
            'purpose': purpose, 'url': masked_url})

        connection_scheme = parsed.scheme
        conn = storage.get_connection_from_config(conf, purpose)
        if connection_scheme in ('mysql', 'mysql+pymysql', 'postgresql',
                                 'sqlite'):
            engine = conn._engine_facade.get_engine()
            meta = sa.MetaData(bind=engine)
            for table_name in ['alarm', 'alarm_history']:
                if engine.has_table(table_name):
                    alarm = sa.Table(table_name, meta, autoload=True)
                    alarm.drop()
                    LOG.info(_LI("Legacy %s table of SQL backend has been "
                                 "dropped."), table_name)
                else:
                    LOG.info(_LI('%s table does not exist.'), table_name)

        elif connection_scheme == 'hbase':
            with conn.conn_pool.connection() as h_conn:
                tables = h_conn.tables()
                table_name_mapping = {'alarm': 'alarm',
                                      'alarm_h': 'alarm history'}
                for table_name in ['alarm', 'alarm_h']:
                    try:
                        if table_name in tables:
                            h_conn.disable_table(table_name)
                            h_conn.delete_table(table_name)
                            LOG.info(_LI("Legacy %s table of Hbase backend "
                                         "has been dropped."),
                                     table_name_mapping[table_name])
                        else:
                            LOG.info(_LI('%s table does not exist.'),
                                     table_name_mapping[table_name])
                    except Exception as e:
                        LOG.error(_LE('Error occurred while dropping alarm '
                                      'tables of Hbase, %s'), e)

        elif connection_scheme == 'mongodb':
            for table_name in ['alarm', 'alarm_history']:
                if table_name in conn.db.conn.collection_names():
                    conn.db.conn.drop_collection(table_name)
                    LOG.info(_LI("Legacy %s table of Mongodb backend has been "
                                 "dropped."), table_name)
                else:
                    LOG.info(_LI('%s table does not exist.'), table_name)
    LOG.info('Legacy alarm tables cleanup done.')
Exemple #2
0
def db_clean_legacy():
    conf = cfg.ConfigOpts()
    conf.register_cli_opts([
        cfg.strOpt('confirm-drop-table',
                   short='n',
                   help='confirm to drop the legacy tables')
    ])
    if not conf.confirm_drop_table:
        confirm = moves.input("Do you really want to drop the legacy "
                              "alarm and event tables? This will destroy "
                              "data definitively if it exist. Please type "
                              "'YES' to confirm: ")
        if confirm != 'YES':
            print("DB legacy cleanup aborted!")
            return

    service.prepare_service(conf=conf)

    url = (getattr(conf.database, "metering_connection")
           or conf.database.connection)
    parsed = urlparse.urlparse(url)

    if parsed.password:
        masked_netloc = '****'.join(parsed.netloc.rsplit(parsed.password))
        masked_url = parsed._replace(netloc=masked_netloc)
        masked_url = urlparse.urlunparse(masked_url)
    else:
        masked_url = url
    LOG.info(
        'Starting to drop event, alarm and alarm history tables in '
        'backend: %s', masked_url)

    connection_scheme = parsed.scheme
    conn = storage.get_connection_from_config(conf)
    if connection_scheme in ('mysql', 'mysql+pymysql', 'postgresql', 'sqlite'):
        engine = conn._engine_facade.get_engine()
        meta = sa.MetaData(bind=engine)
        for table_name in ('alarm', 'alarm_history', 'trait_text', 'trait_int',
                           'trait_float', 'trait_datetime', 'event',
                           'event_type'):
            if engine.has_table(table_name):
                table = sa.Table(table_name, meta, autoload=True)
                table.drop()
                LOG.info("Legacy %s table of SQL backend has been "
                         "dropped.", table_name)
            else:
                LOG.info('%s table does not exist.', table_name)

    elif connection_scheme == 'hbase':
        with conn.conn_pool.connection() as h_conn:
            tables = h_conn.tables()
            table_name_mapping = {
                'alarm': 'alarm',
                'alarm_h': 'alarm history',
                'event': 'event'
            }
            for table_name in ('alarm', 'alarm_h', 'event'):
                try:
                    if table_name in tables:
                        h_conn.disable_table(table_name)
                        h_conn.delete_table(table_name)
                        LOG.info(
                            "Legacy %s table of Hbase backend "
                            "has been dropped.",
                            table_name_mapping[table_name])
                    else:
                        LOG.info('%s table does not exist.',
                                 table_name_mapping[table_name])
                except Exception as e:
                    LOG.error(
                        'Error occurred while dropping alarm '
                        'tables of Hbase, %s', e)

    elif connection_scheme == 'mongodb':
        for table_name in ('alarm', 'alarm_history', 'event'):
            if table_name in conn.db.conn.collection_names():
                conn.db.conn.drop_collection(table_name)
                LOG.info(
                    "Legacy %s table of Mongodb backend has been "
                    "dropped.", table_name)
            else:
                LOG.info('%s table does not exist.', table_name)
    LOG.info('Legacy alarm and event tables cleanup done.')
Exemple #3
0
def db_clean_legacy():
    cfg.CONF.register_cli_opts([
        cfg.strOpt('confirm-drop-alarm-table',
                   short='n',
                   help='confirm to drop the legacy alarm tables')
    ])
    if not cfg.CONF.confirm_drop_alarm_table:
        confirm = moves.input("Do you really want to drop the legacy alarm "
                              "tables? This will destroy data definitely "
                              "if it exist. Please type 'YES' to confirm: ")
        if confirm != 'YES':
            print("DB legacy cleanup aborted!")
            return

    service.prepare_service()
    for purpose in ['metering', 'event']:
        url = (getattr(cfg.CONF.database, '%s_connection' % purpose)
               or cfg.CONF.database.connection)
        parsed = urlparse.urlparse(url)

        if parsed.password:
            masked_netloc = '****'.join(parsed.netloc.rsplit(parsed.password))
            masked_url = parsed._replace(netloc=masked_netloc)
            masked_url = urlparse.urlunparse(masked_url)
        else:
            masked_url = url
        LOG.info(
            _LI('Starting to drop alarm and alarm history tables in '
                '%(purpose)s backend: %(url)s'), {
                    'purpose': purpose,
                    'url': masked_url
                })

        connection_scheme = parsed.scheme
        conn = storage.get_connection_from_config(cfg.CONF, purpose)
        if connection_scheme in ('mysql', 'mysql+pymysql', 'postgresql',
                                 'sqlite'):
            engine = conn._engine_facade.get_engine()
            meta = sa.MetaData(bind=engine)
            for table_name in ['alarm', 'alarm_history']:
                if engine.has_table(table_name):
                    alarm = sa.Table(table_name, meta, autoload=True)
                    alarm.drop()
                    LOG.info(
                        _LI("Legacy %s table of SQL backend has been "
                            "dropped."), table_name)
                else:
                    LOG.info(_LI('%s table does not exist.'), table_name)

        elif connection_scheme == 'hbase':
            with conn.conn_pool.connection() as h_conn:
                tables = h_conn.tables()
                table_name_mapping = {
                    'alarm': 'alarm',
                    'alarm_h': 'alarm history'
                }
                for table_name in ['alarm', 'alarm_h']:
                    try:
                        if table_name in tables:
                            h_conn.disable_table(table_name)
                            h_conn.delete_table(table_name)
                            LOG.info(
                                _LI("Legacy %s table of Hbase backend "
                                    "has been dropped."),
                                table_name_mapping[table_name])
                        else:
                            LOG.info(_LI('%s table does not exist.'),
                                     table_name_mapping[table_name])
                    except Exception as e:
                        LOG.error(
                            _LE('Error occurred while dropping alarm '
                                'tables of Hbase, %s'), e)

        elif connection_scheme == 'mongodb':
            for table_name in ['alarm', 'alarm_history']:
                if table_name in conn.db.conn.collection_names():
                    conn.db.conn.drop_collection(table_name)
                    LOG.info(
                        _LI("Legacy %s table of Mongodb backend has been "
                            "dropped."), table_name)
                else:
                    LOG.info(_LI('%s table does not exist.'), table_name)
    LOG.info('Legacy alarm tables cleanup done.')