【发布时间】:2016-05-17 16:31:39
【问题描述】:
使用returning a value from a provision script to vagrant 中的建议,我正在运行一个将值写入文件的配置脚本。我希望 Ruby 打开这个文件并根据值有条件地重新加载 VM(通过vagrant plugin install vagrant-reload 安装插件可以实现config.vm.provision :reload 行)。然后我想运行第二个配置脚本。
我对如何以及何时使用 vagrantfile 中的以下代码执行 Ruby 命令感到困惑:当第一个配置程序脚本完成时,它会直接执行一秒,而不打印任何内容或重新加载( requiresreboot.txt 中的值是真的)。
我需要对文件处理代码进行哪些更改才能在第一个配置脚本完成后运行?
# -*- mode: ruby -*-
# vi: set ft=ruby :
$script1 = <<SCRIPT
echo "Run this provisioner first to write a value to requiresreboot.txt... "
start-process "C:\\...\\vagrant_provisioning_file1.bat" -wait
SCRIPT
$script2 = <<SCRIPT
echo "Run this provisioner second... "
start-process "C:\\...\\vagrant_provisioning_file2.bat" -wait
SCRIPT
Vagrant.configure(2) do |config|
# Other vagrant setup.....
# ........................
File.new("requiresreboot.txt", "w+");
# Enable provisioning with a shell script 1.
config.vm.provision "shell", inline: $script1
file = File.open("requiresreboot.txt", "r")
contents = file.read
if contents == "max_rearms_reached"
print "Cannot extend Windows 7 trial: maximum number of rearms reached."
elsif contents == "true"
# trigger reload (reboot to apply changes for Windows trial renewal)
print "Windows trial renewal is required"
config.vm.provision :reload
elsif contents == "false"
print "No reload required, continuing with provisioning..."
end
file.close
# Enable provisioning with a shell script 2.
config.vm.provision "shell", inline: $script2
end
【问题讨论】:
标签: ruby vagrant provisioning