blob: a7cbe6ba4933881cd14d380ea2557082004907ca [file] [log] [blame]
Mohammed Naserf3f59a72023-01-15 21:02:04 -05001{{/*
2Licensed under the Apache License, Version 2.0 (the "License");
3you may not use this file except in compliance with the License.
4You may obtain a copy of the License at
5 http://www.apache.org/licenses/LICENSE-2.0
6Unless required by applicable law or agreed to in writing, software
7distributed under the License is distributed on an "AS IS" BASIS,
8WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9See the License for the specific language governing permissions and
10limitations 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
23import os
24import sys
25try:
26 import ConfigParser
27 PARSER_OPTS = {}
28except ImportError:
29 import configparser as ConfigParser
30 PARSER_OPTS = {"strict": False}
31import logging
32from sqlalchemy import create_engine
Oleksandr K.3b800112024-11-12 06:44:15 +010033from sqlalchemy import text
Mohammed Naserf3f59a72023-01-15 21:02:04 -050034
35# Create logger, console handler and formatter
36logger = logging.getLogger('OpenStack-Helm DB Drop')
37logger.setLevel(logging.DEBUG)
38ch = logging.StreamHandler()
39ch.setLevel(logging.DEBUG)
40formatter = logging.Formatter(
41 '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
42
43# Set the formatter and add the handler
44ch.setFormatter(formatter)
45logger.addHandler(ch)
46
47
48# Get the connection string for the service db root user
49if "ROOT_DB_CONNECTION" in os.environ:
50 db_connection = os.environ['ROOT_DB_CONNECTION']
51 logger.info('Got DB root connection')
52else:
53 logger.critical('environment variable ROOT_DB_CONNECTION not set')
54 sys.exit(1)
55
56mysql_x509 = os.getenv('MARIADB_X509', "")
57ssl_args = {}
58if 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
64if "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
89elif "DB_CONNECTION" in os.environ:
90 user_db_conn = os.environ['DB_CONNECTION']
91 logger.info('Got config from DB_CONNECTION env var')
92else:
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
98try:
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))
112except:
113 logger.critical('Could not connect to database as root user')
114 raise
115
116# User DB engine
117try:
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')
124except:
125 logger.critical('Could not get user database config')
126 raise
127
128# Delete all entries from credential table
129
130try:
Oleksandr K.3b800112024-11-12 06:44:15 +0100131 cmd = text("DELETE FROM credential")
Oleksandr K.582fd5e2024-07-19 04:39:01 +0200132 with user_engine.connect() as connection:
133 connection.execute(cmd)
134 try:
135 connection.commit()
136 except AttributeError:
137 pass
Mohammed Naserf3f59a72023-01-15 21:02:04 -0500138 logger.info('Deleted all entries in credential table')
139except:
140 logger.critical('Failed to clean up credential table in keystone db')
141 raise
142
143logger.info('Finished DB Management')