From 879ae9734331d2a4fcae03b7a704009df8b57d99 Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Thu, 29 Mar 2018 18:28:56 +0100 Subject: [PATCH] Allow a venv to be downloaded from a URL In order to enable the ability to build venvs on a target, then serve them via http(s) from a central storage host, we enable the ability to set the path to be a URL. If this is done then the venvs will be sourced from that URL when installing. Change-Id: I166de361f568bfdf28c0f5d916ff8175827c45f4 --- defaults/main.yml | 14 +++++++++----- tasks/python_venv_build.yml | 4 +++- tasks/python_venv_install.yml | 25 +++++++++++++++++++++++-- tasks/python_venv_preflight.yml | 6 ++++++ tasks/python_venv_set_facts.yml | 3 +++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index fbd3e27..2c453e3 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -70,10 +70,13 @@ venv_reuse_build_only: no # venvs. venv_reuse_build_wheels: yes -# The path where a built venv should be stored on the -# deployment host. By default, ensure that the location -# separates venvs per distribution and architecture to -# prevent re-use of venvs between them. +# The path where a built venv is stored on the By default, +# a path on the deployment host is used which ensures that +# the location separates venvs per distribution and architecture +# to prevent re-use of venvs between them. If the path set +# begins with http(s):// then the install process will +# recognise this and deploy from a URL instead of the +# deployment host. venv_reuse_download_venv_path: "{{ lookup('env', 'HOME') | default('/opt', true) }}/cache/venvs/{{ venv_reuse_download_subfolder }}" # NOTE(hwoarang): ansible_distribution may return a string with spaces @@ -81,7 +84,8 @@ venv_reuse_download_venv_path: "{{ lookup('env', 'HOME') | default('/opt', true) # 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_venv_path +# The owner of the venv_reuse_download_venv_path if it is +# housed on the deployment host. venv_reuse_download_path_owner: "{{ lookup('env', 'USER') | default('root', true) }}" # The path where the wheels are cached on the target host diff --git a/tasks/python_venv_build.yml b/tasks/python_venv_build.yml index 89ab355..1d11338 100644 --- a/tasks/python_venv_build.yml +++ b/tasks/python_venv_build.yml @@ -79,7 +79,8 @@ - venv changed - name: Package the venv when venv_reuse_enable is enabled - when: venv_reuse_enable | bool + when: + - venv_reuse_enable | bool block: - name: Clean up the virtualenv before packaging @@ -124,5 +125,6 @@ - src: "{{ venv_destination_path }}.checksum" dest: "{{ venv_reuse_download_venv_path }}/{{ venv_destination_path | basename }}.checksum" when: + - _venv_source == 'file' - _venv_package_build is mapping - _venv_package_build | changed diff --git a/tasks/python_venv_install.yml b/tasks/python_venv_install.yml index f583468..71f2621 100644 --- a/tasks/python_venv_install.yml +++ b/tasks/python_venv_install.yml @@ -18,14 +18,33 @@ src: "{{ venv_reuse_download_venv_path }}/{{ venv_destination_path | basename }}.checksum" dest: "{{ venv_destination_path | dirname }}/" register: _venv_checksum_copy + when: + - _venv_source == 'file' + +- name: Retrieve checksum for venv download + uri: + url: "{{ venv_reuse_download_venv_path }}/{{ venv_destination_path | basename }}.checksum" + dest: "{{ venv_destination_path | dirname }}/" + return_content: yes + register: _venv_checksum_download + when: + - _venv_source == 'url' + +- name: Attempt venv download + get_url: + url: "{{ venv_reuse_download_venv_path }}/{{ venv_destination_path | basename }}.tgz" + dest: "{{ venv_destination_path | dirname }}/" + checksum: "sha1:{{ _venv_checksum_download.content | trim }}" + when: + - _venv_source == 'url' - name: Remove existing venv on target host if it is changing file: path: "{{ venv_destination_path }}" state: absent when: - - _venv_checksum_copy is mapping - - _venv_checksum_copy | changed + - (_venv_checksum_copy is mapping and _venv_checksum_copy | changed) or + (_venv_checksum_download is mapping and _venv_checksum_download | changed) - name: Create venv directory on the target host file: @@ -40,6 +59,7 @@ remote_src: no when: - (_venv_checksum_copy is mapping and _venv_checksum_copy | changed) or + (_venv_checksum_download is mapping and _venv_checksum_download | changed) or _venv_dir_create | changed notify: - venv changed @@ -53,6 +73,7 @@ warn: no when: - (_venv_checksum_copy is mapping and _venv_checksum_copy | changed) or + (_venv_checksum_download is mapping and _venv_checksum_download | changed) or _venv_dir_create | changed tags: - skip_ansible_lint diff --git a/tasks/python_venv_preflight.yml b/tasks/python_venv_preflight.yml index a1f9b01..7c04d9d 100644 --- a/tasks/python_venv_preflight.yml +++ b/tasks/python_venv_preflight.yml @@ -32,6 +32,10 @@ - venv_reuse_build_only | bool - not venv_reuse_enable | bool +- name: Check whether the venv_reuse_download_venv_path is a URL or a file path + set_fact: + _venv_source: "{{ (venv_reuse_download_venv_path is match('^https?://.*')) | ternary('url', 'file') }}" + - name: Ensure that venv_reuse_download_path exists on the deployment host file: path: "{{ venv_reuse_download_venv_path }}" @@ -40,6 +44,7 @@ delegate_to: localhost run_once: yes when: + - _venv_source == 'file' - venv_reuse_enable | bool - name: Check if venv tgz is present on the deployment host @@ -53,6 +58,7 @@ delegate_to: localhost run_once: yes when: + - _venv_source == 'file' - venv_reuse_enable | bool - name: Ensure that virtualenv is installed on the destination host diff --git a/tasks/python_venv_set_facts.yml b/tasks/python_venv_set_facts.yml index 2188040..7e09fe0 100644 --- a/tasks/python_venv_set_facts.yml +++ b/tasks/python_venv_set_facts.yml @@ -29,6 +29,9 @@ - (_venv_checksum_copy is defined and _venv_checksum_copy is mapping and _venv_checksum_copy | changed) or + (_venv_checksum_download is defined and + _venv_checksum_download is mapping and + _venv_checksum_download | changed) or (_install_venv_pip_packages is defined and _install_venv_pip_packages is mapping and _install_venv_pip_packages | changed)