4

I'm building a vagrant .box file using packer. I've got something like this in my packer .json config:

"post-processors": [
//SNIP
  "include": [
    "./choco.ps1",
    "./vagrantUp.ps1",
    ]
]

It's building the .box, and if I manually untar the file I can see those are included in there. When I end up running "vagrant box add Mybox http://whatever" I can even see them in my ~/.vagrant.d/boxes/MyBox/0/virtualbox folder.

 ~ $ ls ~/.vagrant.d/boxes/MyBox/0/virtualbox/
Vagrantfile                 metadata.json
box.ovf                     packer-virtualbox-iso-1424829938-disk1.vmdk
choco.ps1                   vagrantUp.ps1

Also, in my packer vagrant template I have a line:

config.vm.provision  "shell", path: "vagrantUp.ps1"

Next, I want to initialize this machine. I did the following:

~ $ cd ~/Vagrant
Vagrant $ Vagrant mkdir MyBox; cd MyBox
MyBox $ vagrant init MyBox
MyBox $ ls
VagrantFile

This file looks like the basic vagrant default, but at the top it mentions config.vm.box = "MyBox".

But then if I vagrant up, I get the following error:

* `path` for shell provisioner does not exist on the host system: /Users/me/Vagrant/MyBox/vagrantUp.ps1

It looks to me like the VagrantFile in /Vagrant/MyBox is referencing the VagrantFile in ~/.vagrant.d/, which is good, that's what I want. But then it's still using the paths relative to /Vagrant/MyBox, when the files are actually relative to ~/.vagrant.d/

Am I missing a step that tells vagrant to copy those files to the instance directory? Or am I referencing them incorrectly in my vagrant template?

Jake Stevenson
  • 3,009
  • 6
  • 35
  • 40

2 Answers2

3

In the Vagrantfile within the box, you can refer to other files within the box using __FILE__ to the get path to that box Vagrantfile, and then use path instead of inline with the shell provisioner so that the script gets uploaded to the VM and then executed:

Vagrant.configure("2") do |config|
  # ... other configuration

  box_root = File.expand_path(File.dirname(__FILE__))
  config.vm.provision "shell",
    path: File.join(box_root, "vagrantUp.ps1")
end
Kelsey Francis
  • 522
  • 3
  • 8
2

The scripts will be located relative to the Vagrantfile, so on the host machine. Check out the path section in here: http://docs.vagrantup.com/v2/provisioning/shell.html

It clearly states:

Relative paths, such as above, are expanded relative to the location of the root Vagrantfile for your project.

I think that there is no functionality in vagrant init or vagrant init that can extract the scripts and copy it to the local folder where the Vagrantfile resides. I have no idea why they have this functionality in the Packer Vagrant post-processor, under include:

Paths to files to include in the Vagrant box. These files will each be copied into the top level directory of the Vagrant box (regardless of their paths). They can then be used from the Vagrantfile.

meilke
  • 3,280
  • 1
  • 15
  • 31
  • Ok. I make a "~/Vagrant/DevBox" folder and cd to it. Then I "vagrant init MyBox" there. All I end up with in that folder is a VagrantFile that references MyBox in the head. The scripts that got packed ended up alongside the vmdk in ~/.vagrant.d/boxes/MyBox/0/virtualbox back when I did a "vagrant add MyBox http://whatever". And the VagrantFile in THERE references the scripts. – Jake Stevenson Feb 25 '15 at 13:27
  • I've edited the question a bit to try and make myself a bit more clear. – Jake Stevenson Feb 25 '15 at 13:38
  • I adjusted my answer a bit. – meilke Feb 25 '15 at 14:03
  • So what are my options there? Use a gist to store my scripts? Or force my team to not only bring in the vagrant box but ALSO clone a git repository that has all the scripts and a vagrantfile? – Jake Stevenson Feb 25 '15 at 14:12
  • That is what we are doing in my team: Vagrantfile and scripts are stored in the same repo as our actual product. – meilke Feb 25 '15 at 15:25
  • 2
    After searching forever for some way to actually use Packer Vagrant post-processor's `include` option, I finally realized you can use `File.dirname(__FILE__)` to get the path to the extracted box. See https://stackoverflow.com/a/31977866/1856831 below. – Kelsey Francis Aug 13 '15 at 01:11