简体   繁体   English

使用包装工具中的.box中包含的文件进行流动供应

[英]Vagrant provision using files included in .box from packer

I'm building a vagrant .box file using packer. 我正在使用packer构建一个vagrant .box文件。 I've got something like this in my packer .json config: 我的打包器.json配置中有这样的东西:

"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. 它正在构建.box,如果我手动解压缩文件,我可以看到它们包含在那里。 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. 当我最终运行“vagrant box添加Mybox http://无论什么 ”时,我甚至可以在〜/ .vagrant.d / boxes / MyBox / 0 / virtualbox文件夹中看到它们。

 ~ $ 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" . 此文件看起来像基本的vagrant默认值,但在顶部它提到config.vm.box = "MyBox"

But then if I vagrant up , I get the following error: 但是如果我vagrant up ,我会收到以下错误:

* `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. 它看起来像/ Vagrant / MyBox中的VagrantFile引用了〜/ .vagrant.d /中的VagrantFile,这很好,这就是我想要的。 But then it's still using the paths relative to /Vagrant/MyBox, when the files are actually relative to ~/.vagrant.d/ 但是当文件实际上相对于〜/ .vagrant.d /时,它仍然使用相对于/ Vagrant / MyBox的路径

Am I missing a step that tells vagrant to copy those files to the instance directory? 我错过了一个告诉vagrant将这些文件复制到实例目录的步骤吗? Or am I referencing them incorrectly in my vagrant template? 或者我在我的流浪模板中错误地引用它们?

The scripts will be located relative to the Vagrantfile , so on the host machine. 脚本将相对于Vagrantfile ,因此在主机上。 Check out the path section in here: http://docs.vagrantup.com/v2/provisioning/shell.html 查看此处的path部分: 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. 相对路径(如上)相对于项目的根Vagrantfile的位置进行了扩展。

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. 我认为vagrant initvagrant init没有可以提取脚本并将其复制到Vagrantfile所在的本地文件夹的Vagrantfile I have no idea why they have this functionality in the Packer Vagrant post-processor, under include : 我不知道为什么他们在Packer Vagrant后处理器中有这个功能, include

Paths to files to include in the Vagrant box. 要包含在Vagrant框中的文件的路径。 These files will each be copied into the top level directory of the Vagrant box (regardless of their paths). 这些文件将被复制到Vagrant框的顶级目录中(无论它们的路径如何)。 They can then be used from the Vagrantfile. 然后可以从Vagrant文​​件中使用它们。

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: 在框中的Vagrantfile中,您可以使用__FILE__引用框中的其他文件到该框Vagrantfile的获取路径,然后使用path而不是inline shell配置文件,以便脚本上传到VM然后执行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM