From 5a31e77fb41df625884d1fe7ded26cfa359cadfa Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Mon, 10 Dec 2018 11:31:44 +0000 Subject: [PATCH] Add ability to symlink host python packages into venv Some python packages have C bindings which tend to be very particular about the version of their underlying shared libraries. To ensure things run smoothly for stable releases, we opt to use the distro packages for these python packages and symlink the appropriate python library files and their bindings into the venv. This functionality is required for libvirt and ceph and is used across multiple roles. Change-Id: Ib5b7fa1d06abe1e1bb4f14aea7de4207b61aca88 --- defaults/main.yml | 9 +++++++++ tasks/python_venv_install.yml | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/defaults/main.yml b/defaults/main.yml index c2ce131..e5e88c0 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -59,6 +59,15 @@ venv_pip_packages: [] # Arguments to pass to pip when installing into the venv venv_pip_install_args: "" +# Some python packages have C bindings which tend to be very +# particular about the version of their underlying shared libraries. +# To ensure things run smoothly for stable releases, we opt to +# use the distro packages for these python packages and symlink the +# appropriate python library files and their bindings into the venv. +# This variable should contain the list of packages installed which +# should be symlinked into the venv. +venv_packages_to_symlink: [] + # The python executable to use for creating the venv venv_python_executable: "python2" diff --git a/tasks/python_venv_install.yml b/tasks/python_venv_install.yml index 89fd798..ae9dd39 100644 --- a/tasks/python_venv_install.yml +++ b/tasks/python_venv_install.yml @@ -83,3 +83,38 @@ delay: 2 notify: - venv changed + +- name: Add symlinks from distribution python packages + when: + - venv_packages_to_symlink | length > 0 + block: + - name: Find the venv's python version + find: + paths: "{{ venv_install_destination_path }}/lib/" + patterns: "python*" + file_type: directory + recurse: no + register: _python_venv_details + + - name: Set python venv details + set_fact: + venv_python_version: "{{ (_python_venv_details.files[0].path | basename) }}" + venv_python_major_version: "{{ (_python_venv_details.files[0].path | basename)[:-2] }}" + venv_python_lib_folder: "{{ _python_venv_details.files[0].path }}" + + - name: Search for lib files to link + shell: >- + {{ (ansible_pkg_mgr == 'apt') | ternary('dpkg -L ' ~ (venv_packages_to_symlink | join(' ')), 'rpm -ql ' ~ (venv_packages_to_symlink | join(' ')) ) }} + | egrep '^.*{{ venv_python_major_version }}.*/(site|dist)-packages/.*' + args: + warn: no + changed_when: false + register: _python_files + + - name: Link the python host package files into venv + file: + src: "{{ item }}" + dest: "{{ venv_python_lib_folder }}/site-packages/{{ item | basename }}" + state: link + force: yes + with_items: "{{ _python_files.stdout_lines }}"