Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import os |
| 3 | import logging |
| 4 | import sys |
| 5 | |
| 6 | from sqlalchemy import create_engine |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 7 | from sqlalchemy import text |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 8 | |
| 9 | try: |
| 10 | import ConfigParser |
| 11 | PARSER_OPTS = {} |
| 12 | except ImportError: |
| 13 | import configparser as ConfigParser |
| 14 | PARSER_OPTS = {"strict": False} |
| 15 | |
| 16 | # Create logger, console handler and formatter |
| 17 | logger = logging.getLogger('OpenStack-Helm Keystone Endpoint management') |
| 18 | logger.setLevel(logging.DEBUG) |
| 19 | ch = logging.StreamHandler() |
| 20 | ch.setLevel(logging.DEBUG) |
| 21 | formatter = logging.Formatter( |
| 22 | '%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 23 | |
| 24 | # Set the formatter and add the handler |
| 25 | ch.setFormatter(formatter) |
| 26 | logger.addHandler(ch) |
| 27 | |
| 28 | # Get the connection string for the service db |
| 29 | if "OPENSTACK_CONFIG_FILE" in os.environ: |
| 30 | os_conf = os.environ['OPENSTACK_CONFIG_FILE'] |
| 31 | if "OPENSTACK_CONFIG_DB_SECTION" in os.environ: |
| 32 | os_conf_section = os.environ['OPENSTACK_CONFIG_DB_SECTION'] |
| 33 | else: |
| 34 | logger.critical( |
| 35 | 'environment variable OPENSTACK_CONFIG_DB_SECTION not set') |
| 36 | sys.exit(1) |
| 37 | if "OPENSTACK_CONFIG_DB_KEY" in os.environ: |
| 38 | os_conf_key = os.environ['OPENSTACK_CONFIG_DB_KEY'] |
| 39 | else: |
| 40 | logger.critical('environment variable OPENSTACK_CONFIG_DB_KEY not set') |
| 41 | sys.exit(1) |
| 42 | try: |
| 43 | config = ConfigParser.RawConfigParser(**PARSER_OPTS) |
| 44 | logger.info("Using {0} as db config source".format(os_conf)) |
| 45 | config.read(os_conf) |
| 46 | logger.info("Trying to load db config from {0}:{1}".format( |
| 47 | os_conf_section, os_conf_key)) |
| 48 | user_db_conn = config.get(os_conf_section, os_conf_key) |
| 49 | logger.info("Got config from {0}".format(os_conf)) |
| 50 | except: |
| 51 | logger.critical( |
| 52 | "Tried to load config from {0} but failed.".format(os_conf)) |
| 53 | raise |
| 54 | elif "DB_CONNECTION" in os.environ: |
| 55 | user_db_conn = os.environ['DB_CONNECTION'] |
| 56 | logger.info('Got config from DB_CONNECTION env var') |
| 57 | else: |
| 58 | logger.critical( |
| 59 | 'Could not get db config, either from config file or env var') |
| 60 | sys.exit(1) |
| 61 | |
| 62 | # User DB engine |
| 63 | try: |
| 64 | user_engine = create_engine(user_db_conn) |
| 65 | except: |
| 66 | logger.critical('Could not get user database config') |
| 67 | raise |
| 68 | |
| 69 | # Set Internal Endpoint |
| 70 | try: |
| 71 | endpoint_url = os.environ['OS_BOOTSTRAP_INTERNAL_URL'] |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 72 | region_id = os.environ['OS_REGION_NAME'] |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 73 | cmd = text("update endpoint set url = :endpoint_url where interface ='internal' and " |
| 74 | "service_id = (select id from service where " |
| 75 | "service.type = 'identity') and " |
| 76 | "region_id = :region_id") |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 77 | with user_engine.connect() as connection: |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 78 | connection.execute(cmd, {"endpoint_url": endpoint_url, "region_id": region_id}) |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 79 | try: |
| 80 | connection.commit() |
| 81 | except AttributeError: |
| 82 | pass |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 83 | except: |
| 84 | logger.critical("Could not update internal endpoint") |
| 85 | raise |
| 86 | |
| 87 | # Set Admin Endpoint |
| 88 | try: |
| 89 | endpoint_url = os.environ['OS_BOOTSTRAP_ADMIN_URL'] |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 90 | region_id = os.environ['OS_REGION_NAME'] |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 91 | cmd = text("update endpoint set url = :endpoint_url where interface ='admin' " |
| 92 | "and service_id = (select id from service where " |
| 93 | "service.type = 'identity') " |
| 94 | "and region_id = :region_id") |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 95 | with user_engine.connect() as connection: |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 96 | connection.execute(cmd, {"endpoint_url": endpoint_url, "region_id": region_id}) |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 97 | try: |
| 98 | connection.commit() |
| 99 | except AttributeError: |
| 100 | pass |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 101 | except: |
| 102 | logger.critical("Could not update admin endpoint") |
| 103 | raise |
| 104 | |
| 105 | # Set Public Endpoint |
| 106 | try: |
| 107 | endpoint_url = os.environ['OS_BOOTSTRAP_PUBLIC_URL'] |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 108 | region_id = os.environ['OS_REGION_NAME'] |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 109 | cmd = text("update endpoint set url = :endpoint_url where interface ='public' " |
| 110 | "and service_id = (select id from service where " |
| 111 | "service.type = 'identity') " |
| 112 | "and region_id = :region_id") |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 113 | with user_engine.connect() as connection: |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 114 | connection.execute(cmd, {"endpoint_url": endpoint_url, "region_id": region_id}) |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 115 | try: |
| 116 | connection.commit() |
| 117 | except AttributeError: |
| 118 | pass |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 119 | except: |
| 120 | logger.critical("Could not update public endpoint") |
| 121 | raise |
| 122 | |
| 123 | # Print endpoints |
| 124 | try: |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 125 | with user_engine.connect() as connection: |
| 126 | endpoints = connection.execute( |
Oleksandr K. | 3b80011 | 2024-11-12 06:44:15 +0100 | [diff] [blame] | 127 | text("select interface, url from endpoint where service_id = " |
| 128 | "(select id from service where service.type = 'identity')") |
Oleksandr K. | 582fd5e | 2024-07-19 04:39:01 +0200 | [diff] [blame] | 129 | ).fetchall() |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 130 | for row in endpoints: |
| 131 | logger.info("endpoint ({0}): {1}".format(row[0], row[1])) |
| 132 | except: |
| 133 | logger.critical("Could not update endpoint") |
| 134 | raise |
| 135 | |
| 136 | logger.info('Finished Endpoint Management') |