Skip to content

Commit 1cda1ac

Browse files
committed
Switch to Dynamic Python Version Detection
This commit removes the hardcoded KOLLA_DISTRO_PYTHON_VERSION and replaces it with dynamic Python version detection during container images build. Dockerfiles and scripts now use general python3 references, simplifying maintenance and ensuring compatibility with future Python versions. Test cases relying on distro_python_version were also cleaned up. Change-Id: I891749598247a5e951ed4a873b16acfd0566f59c
1 parent 015fd8d commit 1cda1ac

File tree

9 files changed

+36
-38
lines changed

9 files changed

+36
-38
lines changed

docker/base/Dockerfile.j2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ LABEL kolla_version="{{ kolla_version }}"
2323
{% block base_header %}{% endblock %}
2424

2525
ENV KOLLA_BASE_DISTRO={{ base_distro }} \
26-
KOLLA_DISTRO_PYTHON_VERSION={{ distro_python_version }} \
2726
KOLLA_BASE_ARCH={{ base_arch }}
2827

2928

docker/horizon/Dockerfile.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ RUN ln -s horizon-source/* horizon \
6666
&& if [ "$(ls /plugins)" ]; then \
6767
{{ macros.install_pip(horizon_plugins_pip_packages) }}; \
6868
fi \
69-
&& for locale in /var/lib/kolla/venv/lib/python{{distro_python_version}}/site-packages/*/locale; do \
69+
&& for locale in /var/lib/kolla/venv/lib/python3/site-packages/*/locale; do \
7070
(cd ${locale%/*} && /var/lib/kolla/venv/bin/django-admin compilemessages) \
7171
done \
7272
&& chmod 644 /usr/local/bin/kolla_extend_start

docker/horizon/extend_start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -o errexit
55
FORCE_GENERATE="${FORCE_GENERATE:-no}"
66
HASH_PATH=/var/lib/kolla/.settings.md5sum.txt
77

8-
SITE_PACKAGES="/var/lib/kolla/venv/lib/python${KOLLA_DISTRO_PYTHON_VERSION}/site-packages"
8+
SITE_PACKAGES="/var/lib/kolla/venv/lib/python3/site-packages"
99

1010
MANAGE_PY="/var/lib/kolla/venv/bin/python /var/lib/kolla/venv/bin/manage.py"
1111

docker/kolla-toolbox/Dockerfile.j2

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ LABEL maintainer="{{ maintainer }}" name="{{ image_name }}" build-date="{{ build
66
{% block kolla_toolbox_header %}{% endblock %}
77

88
{% set venv_path='/opt/ansible' %}
9-
{% if base_package_type == 'rpm' %}
10-
{% set distro_python_version = '3.12' %}
11-
{% endif %}
12-
{% set os_client_config= venv_path + '/lib/python' + distro_python_version + '/site-packages/os_client_config/defaults.json' %}
9+
{% set os_client_config= venv_path + '/lib/python3/site-packages/os_client_config/defaults.json' %}
1310

1411
{% import "macros.j2" as macros with context %}
1512

@@ -63,11 +60,32 @@ COPY apt_preferences_rabbitmq.{{ base_distro }} /etc/apt/preferences.d/rabbitmq
6360
{% endif %}
6461
{{ macros.install_packages(kolla_toolbox_packages | customizable("packages")) }}
6562

63+
{# NOTE(kevko):
64+
In all distros, there is always a /usr/bin/python3 -> python3.X.
65+
However, this is disrupted for RHEL-based systems because, unlike
66+
other distros, we upgrade Python above [1], but we forget to set
67+
the default python3 to the new python3.X. As a result also, everything
68+
outside of the virtual environment ends up running on Python 3.9.
69+
70+
So, let's correctly set python3 -> python3.X since we had to install
71+
it and now have default python3 points to the new version.
72+
73+
[1] https://review.opendev.org/c/openstack/kolla/+/924245
74+
#}
75+
{% if base_package_type == 'rpm' %}
76+
RUN cd /usr/bin && \
77+
rm -f python3 && \
78+
ln -s python3.12 python3
79+
{% endif %}
80+
6681
{% block kolla_toolbox_upper_constraints %}
6782
RUN mkdir -p /requirements \
6883
&& curl -o /requirements/upper-constraints.txt ${UPPER_CONSTRAINTS_FILE:-https://releases.openstack.org/constraints/upper/{{ openstack_release }}} \
6984
&& {{ macros.upper_constraints_remove("openstacksdk") }} \
70-
&& python{{ distro_python_version }} -m venv --system-site-packages {{ venv_path }}
85+
&& python3 -m venv --system-site-packages {{ venv_path }} \
86+
&& KOLLA_DISTRO_PYTHON_VERSION=$(/usr/bin/python3 -c "import sys; print('{}.{}'.format(sys.version_info.major, sys.version_info.minor))") \
87+
&& cd {{ venv_path }}/lib \
88+
&& ln -s python${KOLLA_DISTRO_PYTHON_VERSION} {{ venv_path }}/lib/python3
7189
{% endblock %}
7290

7391
ENV PATH {{ venv_path }}/bin:$PATH
@@ -86,8 +104,8 @@ ENV PATH {{ venv_path }}/bin:$PATH
86104
'pyudev',
87105
] %}
88106

89-
RUN {{ macros.install_pip(['pip', 'wheel', 'setuptools'], python_version=distro_python_version) }} \
90-
&& {{ macros.install_pip((kolla_toolbox_pip_packages | customizable("pip_packages")), python_version=distro_python_version) }} \
107+
RUN {{ macros.install_pip(['pip', 'wheel', 'setuptools']) }} \
108+
&& {{ macros.install_pip((kolla_toolbox_pip_packages | customizable("pip_packages"))) }} \
91109
&& mkdir -p /etc/ansible /usr/share/ansible \
92110
&& echo 'localhost ansible_connection=local ansible_python_interpreter={{ venv_path }}/bin/python' > /etc/ansible/hosts \
93111
&& sed -i 's| "identity_api_version": "2.0",| "identity_api_version": "3",|' {{ os_client_config }}

docker/openstack-base/Dockerfile.j2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ RUN ln -s openstack-base-source/* /requirements \
195195
#}
196196
&& {{ macros.upper_constraints_remove("ovs") }} \
197197
&& mkdir -p /var/lib/kolla \
198-
&& python3 -m venv --system-site-packages /var/lib/kolla/venv
198+
&& python3 -m venv --system-site-packages /var/lib/kolla/venv \
199+
&& KOLLA_DISTRO_PYTHON_VERSION=$(/usr/bin/python3 -c "import sys; print('{}.{}'.format(sys.version_info.major, sys.version_info.minor))") \
200+
&& ln -s python${KOLLA_DISTRO_PYTHON_VERSION} /var/lib/kolla/venv/lib/python3
199201

200202
{# Block used for overriding global version constraints.
201203

docker/skyline/skyline-apiserver/extend_start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Bootstrap and exit if KOLLA_BOOTSTRAP variable is set. This catches all cases
44
# of the KOLLA_BOOTSTRAP variable being set, including empty.
55
if [[ "${!KOLLA_BOOTSTRAP[@]}" ]]; then
6-
SITE_PACKAGES="/var/lib/kolla/venv/lib/python${KOLLA_DISTRO_PYTHON_VERSION}/site-packages"
6+
SITE_PACKAGES="/var/lib/kolla/venv/lib/python3/site-packages"
77
pushd ${SITE_PACKAGES}/skyline_apiserver/db/alembic
88
alembic upgrade head
99
popd

kolla/image/kolla_worker.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,14 @@ def __init__(self, conf):
114114
self.rpm_setup = self.build_rpm_setup(rpm_setup_config)
115115

116116
if self.base in ['centos', 'rocky']:
117-
self.conf.distro_python_version = "3.9"
118117
self.distro_package_manager = 'dnf'
119118
self.base_package_type = 'rpm'
120119
elif self.base in ['debian']:
121-
self.conf.distro_python_version = "3.11"
122120
self.distro_package_manager = 'apt'
123121
self.base_package_type = 'deb'
124122
elif self.base in ['ubuntu']:
125-
self.conf.distro_python_version = "3.10"
126123
self.distro_package_manager = 'apt'
127124
self.base_package_type = 'deb'
128-
else:
129-
# Assume worst
130-
self.conf.distro_python_version = "3.8"
131125

132126
if self.conf.distro_package_manager is not None:
133127
self.distro_package_manager = self.conf.distro_package_manager
@@ -147,7 +141,6 @@ def __init__(self, conf):
147141
self.image_statuses_unbuildable = dict()
148142
self.image_statuses_allowed_to_fail = dict()
149143
self.maintainer = conf.maintainer
150-
self.distro_python_version = conf.distro_python_version
151144

152145
try:
153146
self.engine_client = engine.getEngineClient(self.conf)
@@ -326,7 +319,6 @@ def create_dockerfiles(self):
326319
'kolla_version': kolla_version,
327320
'image_name': image_name,
328321
'users': self.get_users(),
329-
'distro_python_version': self.distro_python_version,
330322
'distro_package_manager': self.distro_package_manager,
331323
'rpm_setup': self.rpm_setup,
332324
'build_date': build_date,

kolla/tests/test_build.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -661,24 +661,6 @@ def test_build_rpm_setup(self):
661661
kolla = build.KollaWorker(self.conf)
662662
self.assertEqual(2, len(kolla.rpm_setup))
663663

664-
def test_build_distro_python_version_debian(self):
665-
"""check distro_python_version for Debian"""
666-
self.conf.set_override('base', 'debian')
667-
kolla = build.KollaWorker(self.conf)
668-
self.assertEqual('3.11', kolla.distro_python_version)
669-
670-
def test_build_distro_python_version_ubuntu(self):
671-
"""check distro_python_version for Ubuntu"""
672-
self.conf.set_override('base', 'ubuntu')
673-
kolla = build.KollaWorker(self.conf)
674-
self.assertEqual('3.10', kolla.distro_python_version)
675-
676-
def test_build_distro_python_version_centos(self):
677-
"""check distro_python_version for CentOS Stream 9"""
678-
self.conf.set_override('base', 'centos')
679-
kolla = build.KollaWorker(self.conf)
680-
self.assertEqual('3.9', kolla.distro_python_version)
681-
682664
def test_build_distro_package_manager(self):
683665
"""check distro_package_manager conf value is taken"""
684666
self.conf.set_override('distro_package_manager', 'foo')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
upgrade:
3+
- |
4+
Environment variable ``KOLLA_DISTRO_PYTHON_VERSION`` has
5+
been dropped.

0 commit comments

Comments
 (0)