Change working directory for the shell provisioner

90 views
Skip to first unread message

Giacomo Tommaso Petrucci

unread,
Jun 29, 2023, 9:51:41 AM6/29/23
to Vagrant
Greetings,

I wrote the following Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.synced_folder "pep_code", "/home/vagrant/pep_code"
 
  # Update and install dependencies
  config.vm.provision "shell", inline: "echo Updating..."
  config.vm.provision "shell", inline: "sudo apt-get update && sudo apt-get -y upgrade"
  config.vm.provision "shell", inline: "sudo apt-get -y install  clang ninja-build golang golang-goprotobuf-dev ccache distcc git cmake valgrind libboost-all-dev zlib1g-dev libbz2-dev libsqlite3-dev libcurl4-openssl-dev curl libpam0g-dev libssl-dev libreadline-dev patch vim flex qtbase5-dev qtdeclarative5-dev qttools5-dev qttools5-dev-tools libunwind-dev libc6-dev libc6-dev-i386 software-properties-common gcc-multilib"
  config.vm.provision "file", source: "pep_code", destination: "/home/vagrant/pep_code"
  config.vm.provision "shell", inline: "cd /home/vagrant/pep_code"
  config.vm.provision "shell", inline: "git init"
  config.vm.provision "shell", inline: "git config --global user.name \"Giacomo\""
  config.vm.provision "shell", inline: "git config --global user.email \"[redacted]\""
  config.vm.provision "shell", inline: "git add * && git commit -m \"fix\""
  config.vm.provision "shell", inline: "mkdir build && cd build"
  config.vm.provision "shell", inline: "CC=clang CXX=clang++ cmake -DWITH_CASTOR=OFF .."
  config.vm.provision "shell", inline: "ninja"
end

It fails while provisioning with the following message:

default: CMake Error: The source directory "/home" does not appear to contain CMakeLists.txt.
   default: Specify --help for usage, or press the help button on the CMake GUI.
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.


I was expecting the lines

config.vm.provision "shell", inline: "cd /home/vagrant/pep_code"

and

config.vm.provision "shell", inline: "mkdir build && cd build"

to change the working directory of the provisioning process, but this doesn't seem the case. Docker has an instruction for this, WORKDIR. Does Vagrant have something similar? A quick Google search didn't turn up anything.
Thank you for your help,

Giacomo Tommaso Petrucci

Jim McGinness

unread,
Jun 29, 2023, 11:06:20 AM6/29/23
to vagra...@googlegroups.com
Each shell provisioning line runs as its own process. Directory changes do not persist from one line to the next. You will need to gather your individual lines into an actual script to get the result you want. For more info, see 


 -- jmcg

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/vagrant/issues
Discuss: https://discuss.hashicorp.com/c/vagrant/24
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/5ab0c5fe-f707-474a-9e3a-4720f31f331dn%40googlegroups.com.

dnmo...@gmail.com

unread,
Jun 29, 2023, 11:50:05 AM6/29/23
to Vagrant
Jim is correct. 

You typically would want to run the command from the root with the full path e.g. ./home/vagrant/pep_code/build/ninja (if ninja is a script).

If ninja is an app, you would do something like ninja <full path to file> e.g. ninja  /home/vagrant/pep_code/build/<build_file>

dnmo...@gmail.com

unread,
Jun 29, 2023, 11:52:28 AM6/29/23
to Vagrant
If neither one is an option you have to put multiple commands together like cd /home/path1/subpath && ninja

Giacomo Tommaso Petrucci

unread,
Jun 29, 2023, 12:46:28 PM6/29/23
to Vagrant
Thank you for your answers. Yes, this is a little inconvenient but doable.

Regards,

Giacomo Tommaso Petrucci

Alvaro Miranda Aguilera

unread,
Jul 4, 2023, 3:06:39 PM7/4/23
to vagra...@googlegroups.com
Hello

You can try to change the format of the script

to be inside a block

$script = <<-SCRIPT

cd /home/vagrant/pep_code
git init
git config --global user.name \"Giacomo\"
git config --global user.email \"[redacted]\"
git add * && git commit -m \"fix\"
mkdir build && cd build
CC=clang CXX=clang++ cmake -DWITH_CASTOR=OFF ..
ninja

SCRIPT


Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: $script
end







--
Alvaro

Reply all
Reply to author
Forward
0 new messages