From 8457949f27d4e9dcc7b377d9cf3ce2b25ef7ef81 2024-02-11 23:12:31 From: Branko Majic Date: 2024-02-11 23:12:31 Subject: [PATCH] MAR-154: Improve incorrect Python version handling in virtual environments: - This should be helpful when performing distribution upgrades as well (since it should be able to automatically detect a mismatch now). --- diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml index 248ea27ff22857fdd8b1a295abcd8cfbd6862478..d24406fc85c10506d136e5e036c11b7ade43c2ca 100644 --- a/roles/common/tasks/main.yml +++ b/roles/common/tasks/main.yml @@ -336,20 +336,31 @@ home: "/var/lib/pipreqcheck" state: present -- name: Check for presence of wrong Python versions - stat: - path: "{{ item.offending_binary }}" - with_items: - - offending_binary: "/var/lib/pipreqcheck/virtualenv/bin/python2" - virtualenv_directory: "/var/lib/pipreqcheck/virtualenv" - register: "pipreqcheck_wrong_python_version_check" +- name: Retrieve system Python interpreter version + command: + argv: + - "/usr/bin/python3" + - "-c" + - "import sys; print(sys.version.split(' ')[0])" + changed_when: false + register: python_interpreter_version + +- name: Retrieve virtual environment Python interpreter version (if initialised) + command: + argv: + - "/var/lib/pipreqcheck/virtualenv/bin/python" + - "-c" + - "import sys; print(sys.version.split(' ')[0])" + # Virtual environment perhaps does not exist. + failed_when: false + changed_when: false + register: virtualenv_python_version -- name: Drop Python virtual environment if wrong Python version has been detected within +- name: Remove virtual environment if Python version does not match file: - path: "{{ item.item.virtualenv_directory }}" + path: "/var/lib/pipreqcheck/virtualenv" state: absent - when: "item.stat.exists" - with_items: "{{ pipreqcheck_wrong_python_version_check.results }}" + when: "virtualenv_python_version.rc != 0 or virtualenv_python_version.stdout.strip() != python_interpreter_version.stdout.strip()" - name: Create directory for Python virtual environment used for installing/running pip-tools file: diff --git a/roles/wsgi_website/molecule/default/tests/test_default.py b/roles/wsgi_website/molecule/default/tests/test_default.py index 6c3c77eef198ee85b80a568dc099c6268551e1b3..930ff246ace330192242c42b88628491c4b683eb 100644 --- a/roles/wsgi_website/molecule/default/tests/test_default.py +++ b/roles/wsgi_website/molecule/default/tests/test_default.py @@ -496,9 +496,9 @@ def test_website_enabled(host, config_file, expected_content): @pytest.mark.parametrize("python_path, expected_version", [ - ("/var/www/parameters-mandatory/virtualenv/bin/python", "Python 3"), - ("/var/www/parameters-optional.local/virtualenv/bin/python", "Python 3"), - ("/var/www/parameters-paste-req/virtualenv/bin/python", "Python 3"), + ("/var/www/parameters-mandatory/virtualenv/bin/python", "Python 3.9.2"), + ("/var/www/parameters-optional.local/virtualenv/bin/python", "Python 3.9.2"), + ("/var/www/parameters-paste-req/virtualenv/bin/python", "Python 3.9.2"), ]) def test_python_version_in_virtualenv(host, python_path, expected_version): """ diff --git a/roles/wsgi_website/tasks/main.yml b/roles/wsgi_website/tasks/main.yml index 1510ce6f6e601c20449c1184c76e138e91da2510..9de83d175682f4473b0714f8c19d3af789ec6ccf 100644 --- a/roles/wsgi_website/tasks/main.yml +++ b/roles/wsgi_website/tasks/main.yml @@ -81,20 +81,31 @@ notify: - Restart WSGI services -# Ignore failures - the virtual environment might not have been -# created yet. Don't use --version because Python 2 outputs to stderr, -# and Python 3 outputs to stdout. -- name: Check current version of Python used in virtual environment (if any) - command: "{{ home }}/virtualenv/bin/python -c \"import sys; print(sys.version.split(' ')[0])\"" +- name: Retrieve requested Python interpreter version + command: + argv: + - "{{ python_interpreter }}" + - "-c" + - "import sys; print(sys.version.split(' ')[0])" + changed_when: false + register: python_interpreter_version + +- name: Retrieve virtual environment Python interpreter version (if initialised) + command: + argv: + - "{{ home }}/virtualenv/bin/python" + - "-c" + - "import sys; print(sys.version.split(' ')[0])" + # Virtual environment perhaps does not exist. failed_when: false changed_when: false - register: current_python_version + register: virtualenv_python_version -- name: Remove existing Python virtual environment (wrong Python version) +- name: Remove virtual environment if Python version does not match file: path: "{{ home }}/virtualenv" state: absent - when: "current_python_version.rc == 0 and not current_python_version.stdout.startswith('3')" + when: "virtualenv_python_version.rc != 0 or virtualenv_python_version.stdout.strip() != python_interpreter_version.stdout.strip()" notify: - Restart WSGI services