Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 1 | {{/* |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | Unless required by applicable law or agreed to in writing, software |
| 7 | distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | See the License for the specific language governing permissions and |
| 10 | limitations under the License. |
| 11 | */}} |
| 12 | |
| 13 | #!/usr/bin/python |
| 14 | |
| 15 | # Drops db and user for an OpenStack Service: |
| 16 | # Set ROOT_DB_CONNECTION and DB_CONNECTION environment variables to contain |
| 17 | # SQLAlchemy strings for the root connection to the database and the one you |
| 18 | # wish the service to use. Alternatively, you can use an ini formatted config |
| 19 | # at the location specified by OPENSTACK_CONFIG_FILE, and extract the string |
| 20 | # from the key OPENSTACK_CONFIG_DB_KEY, in the section specified by |
| 21 | # OPENSTACK_CONFIG_DB_SECTION. |
| 22 | |
| 23 | import os |
| 24 | import sys |
| 25 | try: |
| 26 | import ConfigParser |
| 27 | PARSER_OPTS = {} |
| 28 | except ImportError: |
| 29 | import configparser as ConfigParser |
| 30 | PARSER_OPTS = {"strict": False} |
| 31 | import logging |
| 32 | from sqlalchemy import create_engine |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 33 | from sqlalchemy import text |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 34 | |
| 35 | # Create logger, console handler and formatter |
| 36 | logger = logging.getLogger('OpenStack-Helm DB Drop') |
| 37 | logger.setLevel(logging.DEBUG) |
| 38 | ch = logging.StreamHandler() |
| 39 | ch.setLevel(logging.DEBUG) |
| 40 | formatter = logging.Formatter( |
| 41 | '%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 42 | |
| 43 | # Set the formatter and add the handler |
| 44 | ch.setFormatter(formatter) |
| 45 | logger.addHandler(ch) |
| 46 | |
| 47 | |
| 48 | # Get the connection string for the service db root user |
| 49 | if "ROOT_DB_CONNECTION" in os.environ: |
| 50 | db_connection = os.environ['ROOT_DB_CONNECTION'] |
| 51 | logger.info('Got DB root connection') |
| 52 | else: |
| 53 | logger.critical('environment variable ROOT_DB_CONNECTION not set') |
| 54 | sys.exit(1) |
| 55 | |
| 56 | mysql_x509 = os.getenv('MARIADB_X509', "") |
| 57 | ssl_args = {} |
| 58 | if mysql_x509: |
| 59 | ssl_args = {'ssl': {'ca': '/etc/mysql/certs/ca.crt', |
| 60 | 'key': '/etc/mysql/certs/tls.key', |
| 61 | 'cert': '/etc/mysql/certs/tls.crt'}} |
| 62 | |
| 63 | # Get the connection string for the service db |
| 64 | if "OPENSTACK_CONFIG_FILE" in os.environ: |
| 65 | os_conf = os.environ['OPENSTACK_CONFIG_FILE'] |
| 66 | if "OPENSTACK_CONFIG_DB_SECTION" in os.environ: |
| 67 | os_conf_section = os.environ['OPENSTACK_CONFIG_DB_SECTION'] |
| 68 | else: |
| 69 | logger.critical( |
| 70 | 'environment variable OPENSTACK_CONFIG_DB_SECTION not set') |
| 71 | sys.exit(1) |
| 72 | if "OPENSTACK_CONFIG_DB_KEY" in os.environ: |
| 73 | os_conf_key = os.environ['OPENSTACK_CONFIG_DB_KEY'] |
| 74 | else: |
| 75 | logger.critical('environment variable OPENSTACK_CONFIG_DB_KEY not set') |
| 76 | sys.exit(1) |
| 77 | try: |
| 78 | config = ConfigParser.RawConfigParser(**PARSER_OPTS) |
| 79 | logger.info("Using {0} as db config source".format(os_conf)) |
| 80 | config.read(os_conf) |
| 81 | logger.info("Trying to load db config from {0}:{1}".format( |
| 82 | os_conf_section, os_conf_key)) |
| 83 | user_db_conn = config.get(os_conf_section, os_conf_key) |
| 84 | logger.info("Got config from {0}".format(os_conf)) |
| 85 | except: |
| 86 | logger.critical( |
| 87 | "Tried to load config from {0} but failed.".format(os_conf)) |
| 88 | raise |
| 89 | elif "DB_CONNECTION" in os.environ: |
| 90 | user_db_conn = os.environ['DB_CONNECTION'] |
| 91 | logger.info('Got config from DB_CONNECTION env var') |
| 92 | else: |
| 93 | logger.critical( |
| 94 | 'Could not get db config, either from config file or env var') |
| 95 | sys.exit(1) |
| 96 | |
| 97 | # Root DB engine |
| 98 | try: |
| 99 | root_engine_full = create_engine(db_connection) |
| 100 | root_user = root_engine_full.url.username |
| 101 | root_password = root_engine_full.url.password |
| 102 | drivername = root_engine_full.url.drivername |
| 103 | host = root_engine_full.url.host |
| 104 | port = root_engine_full.url.port |
| 105 | root_engine_url = ''.join([drivername, '://', root_user, ':', |
| 106 | root_password, '@', host, ':', str(port)]) |
| 107 | root_engine = create_engine(root_engine_url, connect_args=ssl_args) |
| 108 | connection = root_engine.connect() |
| 109 | connection.close() |
| 110 | logger.info("Tested connection to DB @ {0}:{1} as {2}".format( |
| 111 | host, port, root_user)) |
| 112 | except: |
| 113 | logger.critical('Could not connect to database as root user') |
| 114 | raise |
| 115 | |
| 116 | # User DB engine |
| 117 | try: |
| 118 | user_engine = create_engine(user_db_conn, connect_args=ssl_args) |
| 119 | # Get our user data out of the user_engine |
| 120 | database = user_engine.url.database |
| 121 | user = user_engine.url.username |
| 122 | password = user_engine.url.password |
| 123 | logger.info('Got user db config') |
| 124 | except: |
| 125 | logger.critical('Could not get user database config') |
| 126 | raise |
| 127 | |
| 128 | # Delete all entries from credential table |
| 129 | |
| 130 | try: |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 131 | cmd = text("DELETE FROM credential") |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 132 | with user_engine.connect() as connection: |
| 133 | connection.execute(cmd) |
| 134 | try: |
| 135 | connection.commit() |
| 136 | except AttributeError: |
| 137 | pass |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 138 | logger.info('Deleted all entries in credential table') |
| 139 | except: |
| 140 | logger.critical('Failed to clean up credential table in keystone db') |
| 141 | raise |
| 142 | |
| 143 | logger.info('Finished DB Management') |