ansible-role-python_venv_build/tasks/python_venv_build.yml
Jesse Pretorius eea695ecaa Use the virtualenv's pip to build the wheels
Instead of requiring pip installed on the host, we can
use the pip in the virtualenv instead. This keeps the
host cleaner.

Change-Id: I8d6c77e2cfa2cbcd81df21c7ed22a0a344d3b55c
2018-08-19 20:15:37 +01:00

142 lines
5.1 KiB
YAML

---
# Copyright 2018, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Install distro packages for venv build
package:
name: "{{ venv_build_distro_package_list + venv_install_distro_package_list }}"
state: "{{ venv_distro_package_state }}"
update_cache: "{{ (ansible_pkg_mgr in ['apt', 'zypper']) | ternary('yes', omit) }}"
cache_valid_time: "{{ (ansible_pkg_mgr == 'apt') | ternary(distro_cache_valid_time, omit) }}"
when:
- (venv_build_distro_package_list | length > 0) or
(venv_install_distro_package_list | length > 0)
register: _install_distro_packages
until: _install_distro_packages is success
retries: 5
delay: 2
- name: Ensure a fresh venv_install_destination_path if venv_build_only is enabled
file:
path: "{{ venv_install_destination_path }}"
state: absent
when:
- venv_build_only | bool
- name: Create wheel/venv directories on the target host
file:
path: "{{ item }}"
state: directory
with_items:
- "{{ venv_build_archive_path }}"
- "{{ venv_build_wheel_path }}"
- name: Create the virtualenv (if it does not exist)
command: "virtualenv --no-site-packages {{ _venv_create_no_download }} {{ venv_install_destination_path }}"
args:
creates: "{{ venv_install_destination_path }}/bin/activate"
- name: Build wheels for the packages to be installed into the venv
command: >-
{{ venv_install_destination_path }}/bin/pip wheel
--wheel-dir {{ venv_build_wheel_path }}/
--find-links {{ venv_build_wheel_path }}/
--log /var/log/python_wheel_build.log
{{ venv_pip_install_args }}
{{ venv_pip_packages | join(' ') }}
when:
- venv_build_wheels | bool
- name: Build venv
pip:
name: "{{ venv_pip_packages }}"
state: "{{ venv_pip_package_state }}"
virtualenv: "{{ venv_install_destination_path }}"
virtualenv_site_packages: "no"
virtualenv_python: "{{ venv_python_executable }}"
extra_args: >-
--find-links {{ venv_build_wheel_path }}/
--log /var/log/python_venv_build.log
{{ venv_pip_install_args }}
register: _install_venv_pip_packages
until: _install_venv_pip_packages is success
retries: 5
delay: 2
notify:
- venv changed
- name: Package the venv when venv_build_package is enabled
when:
- venv_build_package | bool
block:
- name: Clean up the virtualenv before packaging
shell: |
find {{ venv_install_destination_path }}/bin -type f -name '*.pyc' -delete
when:
- _install_venv_pip_packages is mapping
- _install_venv_pip_packages is changed
# Note(odyssey4me):
# We purposefully use shell instead of the archive module
# here. The archive module's output is far too verbose to
# be practical when debugging.
- name: Package venv
command: |
tar czf '{{ venv_build_archive_path }}/{{ venv_install_destination_path | basename }}.tgz' -C '{{ venv_install_destination_path }}' .
args:
chdir: "{{ venv_install_destination_path }}"
warn: no
register: _venv_package_build
when:
- _install_venv_pip_packages is mapping
- _install_venv_pip_packages is changed
- name: Prepare checksum for packaged venv
shell: |
sha1sum '{{ venv_build_archive_path }}/{{ venv_install_destination_path | basename }}.tgz' | awk '{print $1}' > '{{ venv_build_archive_path }}/{{ venv_install_destination_path | basename }}.checksum'
args:
executable: /bin/bash
when:
- _venv_package_build is mapping
- _venv_package_build is changed
- name: Ensure that venv_install_source_path exists on the deployment host
file:
path: "{{ venv_install_source_path }}"
state: directory
owner: "{{ lookup('env', 'USER') }}"
recurse: yes
delegate_to: localhost
run_once: yes
when:
- _venv_source == 'file'
- inventory_hostname != 'localhost'
- name: Copy the packaged venv and checksum file to the deployment host
fetch:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
flat: yes
with_items:
- src: "{{ venv_build_archive_path }}/{{ venv_install_destination_path | basename }}.tgz"
dest: "{{ venv_install_source_path }}/{{ venv_install_destination_path | basename }}.tgz"
- src: "{{ venv_build_archive_path }}/{{ venv_install_destination_path | basename }}.checksum"
dest: "{{ venv_install_source_path }}/{{ venv_install_destination_path | basename }}.checksum"
when:
- _venv_source == 'file'
- _venv_package_build is mapping
- _venv_package_build is changed
- inventory_hostname != 'localhost'