Changeset - f4772b5c7f3a
[Not reviewed]
0 3 0
Branko Majic (branko) - 2 months ago 2024-03-03 13:28:29
branko@majic.rs
MAR-192: Fix incorrect prompt for wsgi_website virtual environments:

- Ensure that the virtualenv indicator string ends with a whitespace.
- The virtualenv prompt parameter behaves slightly different between
the versions of Debian 11 Bullseye and Debian 12 Bookworm - the
former being a bit more user-friendly.
- Recreate virtual environment if the prompt does not match-up.
3 files changed with 41 insertions and 3 deletions:
0 comments (0 inline, 0 general)
roles/wsgi_website/defaults/main.yml
Show inline comments
 
@@ -22,3 +22,4 @@ user: "web-{{ fqdn | replace('.', '_') }}"
 
home: "/var/www/{{ fqdn }}"
 
python_interpreter: "/usr/bin/python3"
 
pip_check_requirements_upgrades_directory: "/etc/pip_check_requirements_upgrades"
 
virtualenv_prompt: "{% if ansible_distribution_release == 'bullseye' %}({{ fqdn }}) {% else %}{{ fqdn }}{% endif %}"
roles/wsgi_website/molecule/default/tests/test_default.py
Show inline comments
 
@@ -249,6 +249,30 @@ def test_python_virtualenv_project_directory_config(host, project_file, expected
 
        assert project.mode == 0o640
 

	
 

	
 
@pytest.mark.parametrize("virtualenv_dir, admin_user, expected_prompt", [
 
    ('/var/www/parameters-mandatory/virtualenv', 'admin-parameters-mandatory', '(parameters-mandatory) '),
 
    ('/var/www/parameters-optional.local/virtualenv', 'admin-parameters-optional_local', '(parameters-optional.local) '),
 
    ('/var/www/parameters-paste-req/virtualenv', 'admin-parameters-paste-req', '(parameters-paste-req) '),
 
])
 
def test_python_virtualenv_prompt(host, virtualenv_dir, admin_user, expected_prompt):
 
    """
 
    Tests if Python virtual environment prompt has been set-up
 
    correctly.
 
    """
 

	
 
    with host.sudo(admin_user):
 
        prompt = host.run('bash -c "source %s/bin/activate; printenv PS1"', virtualenv_dir)
 

	
 
        # Chop off trailing newline if present (this is from the
 
        # host.run itself).
 
        if prompt.stdout.endswith("\n"):
 
            prompt_stdout = prompt.stdout[:-1]
 
        else:
 
            prompt_stdout = prompt.stdout
 

	
 
        assert prompt_stdout == expected_prompt
 

	
 

	
 
@pytest.mark.parametrize("wrapper_script, expected_owner, expected_group", [
 
    ('/var/www/parameters-mandatory/virtualenv/bin/exec', 'admin-parameters-mandatory', 'web-parameters-mandatory'),
 
    ('/var/www/parameters-optional.local/virtualenv/bin/exec', 'admin-parameters-optional_local', 'web-parameters-optional_local'),
roles/wsgi_website/tasks/main.yml
Show inline comments
 
@@ -101,11 +101,24 @@
 
  changed_when: false
 
  register: virtualenv_python_version
 

	
 
- name: Remove virtual environment if Python version does not match
 
- name: Retrieve virtual environment prompt
 
  command:
 
    argv:
 
      - "bash"
 
      - "-c"
 
      - "source '{{ home }}/virtualenv/bin/activate'; printenv PS1"
 
  failed_when: false
 
  changed_when: false
 
  register: current_virtualenv_prompt
 

	
 
- name: Remove virtual environment in case of mismatches
 
  file:
 
    path: "{{ home }}/virtualenv"
 
    state: absent
 
  when: "virtualenv_python_version.rc != 0 or virtualenv_python_version.stdout.strip() != python_interpreter_version.stdout.strip()"
 
  when: |
 
    virtualenv_python_version.rc != 0 or
 
    virtualenv_python_version.stdout.strip() != python_interpreter_version.stdout.strip() or
 
    current_virtualenv_prompt.stdout != "(" + fqdn + ") "
 
  notify:
 
    - Restart WSGI services
 

	
 
@@ -118,7 +131,7 @@
 
    mode: 02750
 

	
 
- name: Create Python virtual environment
 
  command: '/usr/bin/virtualenv --python "{{ python_interpreter }}" --prompt "({{ fqdn }})" "{{ home }}/virtualenv"'
 
  command: '/usr/bin/virtualenv --python "{{ python_interpreter }}" --prompt "{{ virtualenv_prompt }}" "{{ home }}/virtualenv"'
 
  args:
 
    creates: "{{ home }}/virtualenv/bin/{{ python_interpreter | basename }}"
 
  become: true
0 comments (0 inline, 0 general)