Changeset - 8457949f27d4
[Not reviewed]
0 3 0
Branko Majic (branko) - 2 months ago 2024-02-11 23:12:31
branko@majic.rs
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).
3 files changed with 44 insertions and 22 deletions:
0 comments (0 inline, 0 general)
roles/common/tasks/main.yml
Show inline comments
 
@@ -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:
roles/wsgi_website/molecule/default/tests/test_default.py
Show inline comments
 
@@ -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):
 
    """
roles/wsgi_website/tasks/main.yml
Show inline comments
 
@@ -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
 

	
0 comments (0 inline, 0 general)