From 800034dc0bbb9502893dedd9bcde7c170780c375 Mon Sep 17 00:00:00 2001 From: liusheng Date: Wed, 4 May 2016 17:18:49 +0800 Subject: [PATCH] Add a tool to clean the legacy alarm tables Since we have moved alarm out of ceilometer, when run ceilometer-dbsync, the alarm tables will still be created and won't be removed. This change added a tool that allow users to drop the legacy alarm and alarm_history tables with SQL backends. Change-Id: Ia831741319fd8e198f54a7d4a99b4d7833074e8e Closes-Bug: #1578128 Signed-off-by: liusheng --- ceilometer/cmd/storage.py | 74 ++++++++++++++++++- ...db-legacy-clean-tool-7b3e3714f414c448.yaml | 6 ++ setup.cfg | 1 + 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/add-db-legacy-clean-tool-7b3e3714f414c448.yaml diff --git a/ceilometer/cmd/storage.py b/ceilometer/cmd/storage.py index 977b192970..9e181c88f0 100644 --- a/ceilometer/cmd/storage.py +++ b/ceilometer/cmd/storage.py @@ -16,12 +16,13 @@ from oslo_config import cfg from oslo_log import log +import six.moves.urllib.parse as urlparse +import sqlalchemy as sa -from ceilometer.i18n import _LI +from ceilometer.i18n import _LE, _LI from ceilometer import service from ceilometer import storage - LOG = log.getLogger(__name__) @@ -51,3 +52,72 @@ def expirer(): else: LOG.info(_LI("Nothing to clean, database event time to live " "is disabled")) + + +def db_clean_legacy(): + confirm = raw_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.') diff --git a/releasenotes/notes/add-db-legacy-clean-tool-7b3e3714f414c448.yaml b/releasenotes/notes/add-db-legacy-clean-tool-7b3e3714f414c448.yaml new file mode 100644 index 0000000000..2845d5cebe --- /dev/null +++ b/releasenotes/notes/add-db-legacy-clean-tool-7b3e3714f414c448.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - > + [`bug 1578128 `_] + Add a tool that allow users to drop the legacy alarm and alarm_history + tables. diff --git a/setup.cfg b/setup.cfg index 0713f8d148..27de5358fc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -246,6 +246,7 @@ console_scripts = ceilometer-agent-notification = ceilometer.cmd.agent_notification:main ceilometer-send-sample = ceilometer.cmd.sample:send_sample ceilometer-dbsync = ceilometer.cmd.storage:dbsync + ceilometer-db-legacy-clean = ceilometer.cmd.storage:db_clean_legacy ceilometer-expirer = ceilometer.cmd.storage:expirer ceilometer-rootwrap = oslo_rootwrap.cmd:main ceilometer-collector = ceilometer.cmd.collector:main