From 7a01d139ffb45fb4b9381eb23213399d92a8f06d Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Fri, 23 Mar 2018 12:26:27 +0000 Subject: [PATCH] Ensure distribution/architecture separation Sometimes venvs are not very reusable across distributions and architectures, so to ensure this doesn't happen we store them in entirely different paths. --- defaults/main.yml | 11 +++++++++-- tasks/python_venv_build.yml | 4 ++-- tasks/python_venv_install.yml | 4 ++-- tasks/python_venv_preflight.yml | 20 +++++++++++--------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 442850d..81896a7 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -61,8 +61,15 @@ venv_pip_install_args: "" venv_reuse_enable: yes # The path where a built venv should be stored on the -# deployment host. -venv_reuse_download_path: "{{ lookup('env', 'HOME') | default('/opt', true) }}/cache" +# deployment host. By default, ensure that the location +# separates venvs per distribution and architecture to +# prevent re-use of venvs between them. +venv_reuse_download_path: "{{ lookup('env', 'HOME') | default('/opt', true) }}/cache/{{ venv_reuse_download_subfolder }}" + +# NOTE(hwoarang): ansible_distribution may return a string with spaces +# such as "openSUSE Leap" so we need to replace the space with underscore +# in order to create a more sensible repo name for the distro. +venv_reuse_download_subfolder: "{{ (ansible_distribution | lower) | replace(' ', '_') }}-{{ ansible_distribution_version.split('.')[:2] | join('.') }}-{{ ansible_architecture | lower }}" # The owner of the venv_reuse_download_path venv_reuse_download_path_owner: "{{ lookup('env', 'USER') | default('root', true) }}" diff --git a/tasks/python_venv_build.yml b/tasks/python_venv_build.yml index 27c2971..57b4a25 100644 --- a/tasks/python_venv_build.yml +++ b/tasks/python_venv_build.yml @@ -105,9 +105,9 @@ flat: yes with_items: - src: "{{ venv_destination_path }}.tgz" - dest: "{{ venv_reuse_download_path }}/{{ venv_destination_path }}.tgz" + dest: "{{ venv_reuse_download_path }}/{{ venv_destination_path | basename }}.tgz" - src: "{{ venv_destination_path }}.checksum" - dest: "{{ venv_reuse_download_path }}/{{ venv_destination_path }}.checksum" + dest: "{{ venv_reuse_download_path }}/{{ venv_destination_path | basename }}.checksum" when: - _venv_package_build is mapping - _venv_package_build | changed diff --git a/tasks/python_venv_install.yml b/tasks/python_venv_install.yml index db7abe5..c93be65 100644 --- a/tasks/python_venv_install.yml +++ b/tasks/python_venv_install.yml @@ -15,7 +15,7 @@ - name: Copy the venv checksum file to the target host copy: - src: "{{ venv_reuse_download_path }}/{{ venv_destination_path }}.checksum" + src: "{{ venv_reuse_download_path }}/{{ venv_destination_path | basename }}.checksum" dest: "{{ venv_destination_path | dirname }}" register: _venv_checksum_copy - _src_venv_present.stat.exists | bool @@ -34,7 +34,7 @@ - name: Unarchive pre-built venv unarchive: - src: "{{ venv_reuse_download_path }}/{{ venv_destination_path }}.tgz" + src: "{{ venv_reuse_download_path }}/{{ venv_destination_path | basename }}.tgz" dest: "{{ venv_destination_path }}" remote_src: no - _venv_checksum_copy is mapping diff --git a/tasks/python_venv_preflight.yml b/tasks/python_venv_preflight.yml index 998ea0c..b13a3ad 100644 --- a/tasks/python_venv_preflight.yml +++ b/tasks/python_venv_preflight.yml @@ -21,9 +21,19 @@ when: - venv_destination_path is not defined +- name: Ensure that venv_reuse_download_path exists on the deployment host + file: + path: "{{ venv_reuse_download_path }}" + state: directory + owner: "{{ venv_reuse_download_path_owner }}" + delegate_to: localhost + run_once: yes + when: + - venv_reuse_enable | bool + - name: Check if venv tgz is present on the deployment host stat: - path: "{{ venv_reuse_download_path }}/{{ venv_destination_path }}.tgz" + path: "{{ venv_reuse_download_path }}/{{ venv_destination_path | basename }}.tgz" get_attributes: no get_checksum: no get_md5: no @@ -31,13 +41,5 @@ register: _src_venv_present delegate_to: localhost run_once: yes - -- name: Ensure that venv_reuse_download_path exists on the deployment host - file: - path: "{{ venv_reuse_download_path }}/{{ venv_destination_path | dirname }}" - state: directory - owner: "{{ venv_reuse_download_path_owner }}" - delegate_to: localhost - run_once: yes when: - venv_reuse_enable | bool