PJ Simpo
PJ Simpo

Reputation: 224

Vagrant provisioning script installs software (Pyenv) which requires shell restart

I am using Vagrant to spin up an Ubuntu box and run a shell script which configures the VM to use Pyenv. The Pyenv docs recommend restarting the shell after installation & configuration.

Having exec "$SHELL" in the middle of the script interrupts Vagrant's provisioning, so I opted to split the commands across two files.

vagrantfile:

Vagrant.configure("2") do |config|
  
  config.vm.box = "hashicorp/bionic64"

  config.vm.synced_folder ".", "/vagrant", disabled: true

  config.vm.provision "shell", path: "one.sh", privileged: false
  config.vm.provision "shell", path: "two.sh", privileged: false


end

one.sh:

#!/bin/bash

# Install PyEnv

curl https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc


exec "$SHELL"

two.sh:

#!/bin/bash

# install python build deps
sudo apt update && sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

pyenv install 3.10

ten=$(pyenv latest 3.10)

pyenv global $ten

# Install Poetry 
curl -sSL https://install.python-poetry.org | python3.10 -
export PATH="/home/vagrant/.local/bin:$PATH"

# Install Nox
python3.10 -m pip install --user --upgrade nox

FWIW the vagrant command I run is:

vagrant up --provider=hyperv

When the second script attempts to execute the pyenv commands, its not found:

default: /tmp/vagrant-shell: line 12: pyenv: command not found However, I am then able to vagrant ssh into the VM where I can manually execute everything as I would expect.

vagrant ssh
vagrant@ubuntu-18:~$ pyenv --version
pyenv 2.3.9

I would expect the second provision script to 'pick up' where the last one stopped, or at the very least not have a differently configured environment to when I subsequently vagrant ssh into the VM.

I have tried with each of the 'reset' and 'reboot' options set to true on the first provisioning script. result was the same.

Upvotes: 0

Views: 125

Answers (0)

Related Questions