运行 Vagrant 的特定供应块
Running a specific provision block with Vagrant
我想 运行 在新配置的服务器上的 Vagrant 文件中创建一个单独的配置块。目前当我 运行 从我的 CI 服务器
vagrant up
以下块执行成功
config.vm.provider :linode do |provider, override|
#creates a new instance etc .. the following block runs on this instance
end
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeployagent"
end
现在我想 运行 一个单独的供应商。 (CI 服务器中的单独任务)即
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeploydatabaseagent"
end
我想弄清楚我需要什么
运行 vagrant up 以便它只执行第一个供应块
运行 vagrant 以便它只会 运行 在 1.
中创建的实例上的第二个供应商块
提前致谢
来自 Vagrant docs.
Vagrant.configure("2") do |config|
# ... other configuration
config.vm.provision "bootstrap", type: "shell" do |s|
s.inline = "echo hello"
end
end
The --provision-with flag can be used if you only want to run a
specific provisioner if you have multiple provisioners specified. For
example, if you have a shell and Puppet provisioner and only want to
run the shell one, you can do vagrant provision --provision-with
shell. The arguments to --provision-with can be the provisioner type
(such as "shell") or the provisioner name (such as "bootstrap" from
above).
Chef 是关于管理所需状态的,因此您应该希望它确保您的两个食谱每次都是 运行(并且它们应该是幂等的)。
我不知道有什么方法可以告诉 vagrant 使用不同参数的相同 provisioner 两次(为此我只能考虑 vagrant 文件中的丑陋 hack)
如果第二个配方依赖于第一个配方,那么你可以添加一个守卫来跳过第二个配方,直到第一个配方 运行。
使用 chef-solo 你可以做这样的事情:
- mydeployagent/recipes/default.rb
[...Whatever your recipe has to do (agent install, configuration, etc...]
# On a particular resource on your first recipe,
# trigger a notification to make a file which will be used as a guard for second recipe
remote_file "/opt/target/mydeplaoyagent" do
source [.. your source..]
notifies :create,"file[/opt/mydeployagent.initialized]"
end
file "/opt/mydeployagent.initialized" do
action :nothing
end
- mydeploydatabaseagent/recipes/default.rb
#start the recipe by a guard to avoid doing anythng if first recipe has not been run
return unless ::File::exists?("/opt/mydeployagent.initialized")
[... your second recipe code here ...]
在你的 vagrant 文件中,你可以将这两个食谱添加到你的供应商中,如:
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeployagent"
chef.add_recipe = "mydeploydatabaseagent"
end
显然,如果守卫不是幂等的,它也可以在第一个配方之上使用,但我强烈建议重新考虑它以便能够 运行 倍数,你会很高兴拥有它 运行 宁当你有一个配置更改传播到一个由这个配方管理的文件中时(相信我,你有一天会有这样的更新来管理)。
我想 运行 在新配置的服务器上的 Vagrant 文件中创建一个单独的配置块。目前当我 运行 从我的 CI 服务器
vagrant up
以下块执行成功
config.vm.provider :linode do |provider, override|
#creates a new instance etc .. the following block runs on this instance
end
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeployagent"
end
现在我想 运行 一个单独的供应商。 (CI 服务器中的单独任务)即
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeploydatabaseagent"
end
我想弄清楚我需要什么
运行 vagrant up 以便它只执行第一个供应块
运行 vagrant 以便它只会 运行 在 1.
中创建的实例上的第二个供应商块
提前致谢
来自 Vagrant docs.
Vagrant.configure("2") do |config|
# ... other configuration
config.vm.provision "bootstrap", type: "shell" do |s|
s.inline = "echo hello"
end
end
The --provision-with flag can be used if you only want to run a specific provisioner if you have multiple provisioners specified. For example, if you have a shell and Puppet provisioner and only want to run the shell one, you can do vagrant provision --provision-with shell. The arguments to --provision-with can be the provisioner type (such as "shell") or the provisioner name (such as "bootstrap" from above).
Chef 是关于管理所需状态的,因此您应该希望它确保您的两个食谱每次都是 运行(并且它们应该是幂等的)。
我不知道有什么方法可以告诉 vagrant 使用不同参数的相同 provisioner 两次(为此我只能考虑 vagrant 文件中的丑陋 hack)
如果第二个配方依赖于第一个配方,那么你可以添加一个守卫来跳过第二个配方,直到第一个配方 运行。
使用 chef-solo 你可以做这样的事情:
- mydeployagent/recipes/default.rb
[...Whatever your recipe has to do (agent install, configuration, etc...]
# On a particular resource on your first recipe,
# trigger a notification to make a file which will be used as a guard for second recipe
remote_file "/opt/target/mydeplaoyagent" do
source [.. your source..]
notifies :create,"file[/opt/mydeployagent.initialized]"
end
file "/opt/mydeployagent.initialized" do
action :nothing
end
- mydeploydatabaseagent/recipes/default.rb
#start the recipe by a guard to avoid doing anythng if first recipe has not been run
return unless ::File::exists?("/opt/mydeployagent.initialized")
[... your second recipe code here ...]
在你的 vagrant 文件中,你可以将这两个食谱添加到你的供应商中,如:
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeployagent"
chef.add_recipe = "mydeploydatabaseagent"
end
显然,如果守卫不是幂等的,它也可以在第一个配方之上使用,但我强烈建议重新考虑它以便能够 运行 倍数,你会很高兴拥有它 运行 宁当你有一个配置更改传播到一个由这个配方管理的文件中时(相信我,你有一天会有这样的更新来管理)。