From 67ae84ce39815d985f4fe0584906e7a2b0175b85 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Mon, 4 Oct 2021 09:57:49 +1100 Subject: [PATCH] ensure-rust: rework global install I think the secondary "rustup default" was intended to make sure the Zuul user sets up to use the installed rust toolchain (I32f9b285904a7036f9a80ada8a49fa9cf31b5163) but actually results in a re-download of components and another local installation. This isn't really the intention, and also doubles the time spent installing. From the linked comment, it seems like we're not doing our global install correctly; even putting it in /usr doesn't avoid the need for RUST_HOME to be set. Take it's suggestion and install out-of-the-way in /opt, use a small /usr/local/bin wrapper to call with correct env vars set and then setup the installed global binary names to be called via that. Change-Id: I28ef747b809a17664305bfd9754022251390647b --- roles/ensure-rust/README.rst | 11 ++++++----- roles/ensure-rust/defaults/main.yaml | 2 +- roles/ensure-rust/tasks/rustup.yaml | 18 ++++++++++++++++-- .../templates/rust-wrap-setup.sh.j2 | 16 ++++++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 roles/ensure-rust/templates/rust-wrap-setup.sh.j2 diff --git a/roles/ensure-rust/README.rst b/roles/ensure-rust/README.rst index 79a4ea323..ad2ebd3e2 100644 --- a/roles/ensure-rust/README.rst +++ b/roles/ensure-rust/README.rst @@ -7,7 +7,8 @@ Install the Rust toolchain .. zuul:rolevar:: ensure_rust_rustup :default: True - Install Rust via the ``rustup`` installer. + Install Rust via the ``rustup`` installer. This installs the toolchain + globally (for all users). .. zuul:rolevar:: ensure_rust_rustup_toolchain :default: stable @@ -15,11 +16,11 @@ Install the Rust toolchain The Rust toolchain to install with ``rustup``. .. zuul:rolevar:: ensure_rust_rustup_path - :default: /usr + :default: /opt/rust - Where to install Rust/Cargo with ``rustup``. ``/usr`` provides the - tools globally. This may conflict with distribution Rust packages - if installed. + Where to install Rust/Cargo with ``rustup``. Wrappers will + be installed in ``/usr/local/bin/`` to make them available for + all users. .. zuul:rolevar:: ensure_rust_packages :default: False diff --git a/roles/ensure-rust/defaults/main.yaml b/roles/ensure-rust/defaults/main.yaml index ea739b307..5d92c9715 100644 --- a/roles/ensure-rust/defaults/main.yaml +++ b/roles/ensure-rust/defaults/main.yaml @@ -1,4 +1,4 @@ ensure_rust_rustup: true ensure_rust_packages: false ensure_rust_rustup_toolchain: stable -ensure_rust_rustup_path: /usr +ensure_rust_rustup_path: /opt/rust diff --git a/roles/ensure-rust/tasks/rustup.yaml b/roles/ensure-rust/tasks/rustup.yaml index a2cffdd75..a52180117 100644 --- a/roles/ensure-rust/tasks/rustup.yaml +++ b/roles/ensure-rust/tasks/rustup.yaml @@ -9,5 +9,19 @@ executable: /bin/bash become: yes -- name: Use as selected Rust toolchain - command: rustup default {{ ensure_rust_rustup_toolchain }} +# Install wrappers that set env vars for global usage. See +# https://github.com/rust-lang/rustup/issues/1085#issuecomment-296604244 + +- name: Install wrapper helper script + template: + src: rust-wrap-setup.sh.j2 + dest: /usr/local/bin/rust-wrap-setup + owner: root + group: root + mode: 0755 + become: yes + +- name: Run wrapper installation + shell: | + /usr/local/bin/rust-wrap-setup + become: yes diff --git a/roles/ensure-rust/templates/rust-wrap-setup.sh.j2 b/roles/ensure-rust/templates/rust-wrap-setup.sh.j2 new file mode 100644 index 000000000..70232ce62 --- /dev/null +++ b/roles/ensure-rust/templates/rust-wrap-setup.sh.j2 @@ -0,0 +1,16 @@ +#!/bin/bash + +cat <<'EOF' > /usr/local/bin/rust-wrap +#!/bin/sh + +RUSTUP_HOME={{ ensure_rust_rustup_path }} exec {{ ensure_rust_rustup_path }}/bin/${0##*/} "$@" +EOF + +chmod 0755 /usr/local/bin/rust-wrap + +for f in {{ ensure_rust_rustup_path }}/bin/* +do + name=$(basename ${f}) + echo "Install link for ${name}" + ln -f /usr/local/bin/rust-wrap /usr/local/bin/${name} +done