blob: a3e64e9346ee5ad94daf2071d4f5c5202867c7c3 [file] [log] [blame]
Mohammed Naserf3f59a72023-01-15 21:02:04 -05001#!/usr/bin/env python
2import os
3import logging
4import sys
5
6from sqlalchemy import create_engine
Oleksandr K.3b800112024-11-12 06:44:15 +01007from sqlalchemy import text
Mohammed Naserf3f59a72023-01-15 21:02:04 -05008
9try:
10 import ConfigParser
11 PARSER_OPTS = {}
12except ImportError:
13 import configparser as ConfigParser
14 PARSER_OPTS = {"strict": False}
15
16# Create logger, console handler and formatter
17logger = logging.getLogger('OpenStack-Helm Keystone Endpoint management')
18logger.setLevel(logging.DEBUG)
19ch = logging.StreamHandler()
20ch.setLevel(logging.DEBUG)
21formatter = logging.Formatter(
22 '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
23
24# Set the formatter and add the handler
25ch.setFormatter(formatter)
26logger.addHandler(ch)
27
28# Get the connection string for the service db
29if "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
54elif "DB_CONNECTION" in os.environ:
55 user_db_conn = os.environ['DB_CONNECTION']
56 logger.info('Got config from DB_CONNECTION env var')
57else:
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
63try:
64 user_engine = create_engine(user_db_conn)
65except:
66 logger.critical('Could not get user database config')
67 raise
68
69# Set Internal Endpoint
70try:
71 endpoint_url = os.environ['OS_BOOTSTRAP_INTERNAL_URL']
Oleksandr K.582fd5e2024-07-19 04:39:01 +020072 region_id = os.environ['OS_REGION_NAME']
Oleksandr K.3b800112024-11-12 06:44:15 +010073 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.582fd5e2024-07-19 04:39:01 +020077 with user_engine.connect() as connection:
Oleksandr K.3b800112024-11-12 06:44:15 +010078 connection.execute(cmd, {"endpoint_url": endpoint_url, "region_id": region_id})
Oleksandr K.582fd5e2024-07-19 04:39:01 +020079 try:
80 connection.commit()
81 except AttributeError:
82 pass
Mohammed Naserf3f59a72023-01-15 21:02:04 -050083except:
84 logger.critical("Could not update internal endpoint")
85 raise
86
87# Set Admin Endpoint
88try:
89 endpoint_url = os.environ['OS_BOOTSTRAP_ADMIN_URL']
Oleksandr K.582fd5e2024-07-19 04:39:01 +020090 region_id = os.environ['OS_REGION_NAME']
Oleksandr K.3b800112024-11-12 06:44:15 +010091 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.582fd5e2024-07-19 04:39:01 +020095 with user_engine.connect() as connection:
Oleksandr K.3b800112024-11-12 06:44:15 +010096 connection.execute(cmd, {"endpoint_url": endpoint_url, "region_id": region_id})
Oleksandr K.582fd5e2024-07-19 04:39:01 +020097 try:
98 connection.commit()
99 except AttributeError:
100 pass
Mohammed Naserf3f59a72023-01-15 21:02:04 -0500101except:
102 logger.critical("Could not update admin endpoint")
103 raise
104
105# Set Public Endpoint
106try:
107 endpoint_url = os.environ['OS_BOOTSTRAP_PUBLIC_URL']
Oleksandr K.582fd5e2024-07-19 04:39:01 +0200108 region_id = os.environ['OS_REGION_NAME']
Oleksandr K.3b800112024-11-12 06:44:15 +0100109 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.582fd5e2024-07-19 04:39:01 +0200113 with user_engine.connect() as connection:
Oleksandr K.3b800112024-11-12 06:44:15 +0100114 connection.execute(cmd, {"endpoint_url": endpoint_url, "region_id": region_id})
Oleksandr K.582fd5e2024-07-19 04:39:01 +0200115 try:
116 connection.commit()
117 except AttributeError:
118 pass
Mohammed Naserf3f59a72023-01-15 21:02:04 -0500119except:
120 logger.critical("Could not update public endpoint")
121 raise
122
123# Print endpoints
124try:
Oleksandr K.582fd5e2024-07-19 04:39:01 +0200125 with user_engine.connect() as connection:
126 endpoints = connection.execute(
Oleksandr K.3b800112024-11-12 06:44:15 +0100127 text("select interface, url from endpoint where service_id = "
128 "(select id from service where service.type = 'identity')")
Oleksandr K.582fd5e2024-07-19 04:39:01 +0200129 ).fetchall()
Mohammed Naserf3f59a72023-01-15 21:02:04 -0500130 for row in endpoints:
131 logger.info("endpoint ({0}): {1}".format(row[0], row[1]))
132except:
133 logger.critical("Could not update endpoint")
134 raise
135
136logger.info('Finished Endpoint Management')