From 0f603e3363636577d554347ea1fd85e9d75c7176 Mon Sep 17 00:00:00 2001 From: Tim Visher Date: Sun, 11 Apr 2021 20:15:01 -0400 Subject: [PATCH] Vagrantfile: Fix setup for tests --- .github/CONTRIBUTING.md | 2 ++ Vagrantfile | 58 +++++------------------------------------ scripts/install_rvm | 19 ++++++++++++++ scripts/setup_tests | 49 ++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 scripts/install_rvm create mode 100644 scripts/setup_tests diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index f317834b8..0453b1a26 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -34,6 +34,8 @@ No pull request template is provided on GitHub. The expected changes are often a ### Setup a development installation of Vagrant +*A Vagrantfile is provided that should take care setting up a VM for running the rspec tests.* If you only need to run those tests and don't also want to run a development version of Vagrant from a host machine then it's recommended to use that. + There are a few prerequisites for setting up a development environment with Vagrant. Ensure you have the following installed on your machine: * git diff --git a/Vagrantfile b/Vagrantfile index 9004ec678..8d2016ff0 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -14,7 +14,12 @@ Vagrant.configure("2") do |config| end end - config.vm.provision "shell", inline: $shell + # We split apart `install_rvm` from `setup_tests` because rvm says to + # logout and log back in just after installing RVM. + # https://github.com/rvm/ubuntu_rvm#3-reboot + config.vm.provision "shell", path: "scripts/install_rvm" + + config.vm.provision "shell", path: "scripts/setup_tests" config.push.define "www", strategy: "local-exec" do |push| push.script = "scripts/website_push_www.sh" @@ -24,54 +29,3 @@ Vagrant.configure("2") do |config| push.script = "scripts/website_push_docs.sh" end end - -$shell = <<-'CONTENTS' -export DEBIAN_FRONTEND=noninteractive -MARKER_FILE="/usr/local/etc/vagrant_provision_marker" -RUBY_VER_REQ=$(awk '$1 == "s.required_ruby_version" { print $4 }' /vagrant/vagrant.gemspec | tr -d '"') - -# Only provision once -if [ -f "${MARKER_FILE}" ]; then - exit 0 -fi - -# Add ubuntu_rvm repo -apt-add-repository -y ppa:rael-gc/rvm - -# Update apt -apt-get update --quiet - -# Add vagrant user to sudo group: -# ubuntu_rvm only adds users in group sudo to group rvm -usermod -a -G sudo vagrant - -# Install basic dependencies and RVM -apt-get install -qy build-essential bsdtar rvm - -# Import the mpapis public key to verify downloaded releases -su -l -c 'gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3' vagrant - -# Install next-to-last Ruby that complies with Vagrant's version constraint -RUBY_VER=$(su -l -c 'rvm list known' vagrant | tr '[]-' ' ' | awk "/^ ruby ${RUBY_VER_REQ:0:1}\./ { print \$2 }" | sort -r | sed -n '2p') -su -l -c "rvm install ${RUBY_VER}" vagrant -su -l -c "rvm --default use ${RUBY_VER}" vagrant - -# Output the Ruby version (for sanity) -su -l -c 'ruby --version' vagrant - -# Install Git -apt-get install -qy git - -# Upgrade Rubygems -su -l -c "rvm ${RUBY_VER} do gem update --system" vagrant - -# Prepare to run unit tests -su -l -c 'cd /vagrant; bundle install' vagrant - -# Automatically move into the shared folder, but only add the command -# if it's not already there. -grep -q 'cd /vagrant' /home/vagrant/.bash_profile 2>/dev/null || echo 'cd /vagrant' >> /home/vagrant/.bash_profile - -# Touch the marker file so we don't do this again -touch ${MARKER_FILE} -CONTENTS diff --git a/scripts/install_rvm b/scripts/install_rvm new file mode 100644 index 000000000..6a43772b9 --- /dev/null +++ b/scripts/install_rvm @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +export DEBIAN_FRONTEND=noninteractive + +if ! rvm --version +then + # https://github.com/rvm/ubuntu_rvm#install + apt-add-repository -y ppa:rael-gc/rvm && + apt-get update -y && + apt-get install -y rvm || { + echo 'Failed to install rvm' >&2 + exit 1 + } +fi + +usermod -a -G rvm vagrant || { + echo 'Failed to add vagrant to the rvm group' >&2 + exit 1 +} diff --git a/scripts/setup_tests b/scripts/setup_tests new file mode 100644 index 000000000..a28c16297 --- /dev/null +++ b/scripts/setup_tests @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +rvm --version || { + echo 'rvm not installed' >&2 + exit 1 +} + +# bsdtar is required for a small subset of the tests +if ! bsdtar --version +then + apt-get install -y bsdtar && + bsdtar --version || { + echo 'Failed to install bsdtar' >&2 + exit 1 + } +fi + +# Install next-to-last Ruby that complies with Vagrant's version +# constraint +RUBY_VER_REQ=$( + awk ' + $1 == "s.required_ruby_version" { print $4 } + ' /vagrant/vagrant.gemspec | + tr -d '"') +RUBY_VER=$(sudo -u vagrant -i rvm list known | + tr '[]-' ' ' | + awk "/^ ruby ${RUBY_VER_REQ:0:1}\./ { print \$2 }" | + sort -r | + sed -n '2p') +sudo -u vagrant -i rvm install "${RUBY_VER}" +sudo -u vagrant -i rvm --default use "${RUBY_VER}" + +# Output the Ruby version (for sanity) +sudo -u vagrant -i ruby --version + +# Upgrade Rubygems +sudo -u vagrant -i rvm "${RUBY_VER}" do gem update --system + +# Prepare to run unit tests +sudo -u vagrant -i bash -c 'cd /vagrant; bundle install' + +# Automatically move into the shared folder, but only add the command if +# it's not already there. +if ! grep -q 'cd /vagrant' /home/vagrant/.bash_profile +then + cat <> /home/vagrant/.bash_profile +cd /vagrant +EOF +fi