简体   繁体   English

目录资源未创建目录

[英]directory resource does not create directory

I have a Vagrantfile that provisions a VM by running a chef recipe. 我有一个Vagrantfile,可通过运行厨师食谱来配置VM。 The first resource in the chef recipe is: 厨师食谱中的第一个资源是:

directory "/downloads" do
  owner "root"
  group "root"
  mode "0755"
  action :create
end

# check that it worked:
raise "/downloads doesn't exist!" unless File.exists? "/downloads"

When I run this at work, it works fine. 当我在工作中运行它时,它工作正常。

When I run it at home, it fails, the exception is raised when I check to see if /downloads exists. 当我在家运行它时,它失败了,当我检查是否存在/downloads时会引发异常。

I'm not sure why this is happening. 我不确定为什么会这样。 I would expect it to behave identically, since the underlying Vagrant box is the same both at work and at home. 我希望它的行为相同,因为底层的Vagrant框在工作和家庭中都是相同的。 I am a chef newb so perhaps there is something I am not understanding about the order in which the resources are run within my recipe? 我是新手厨师,所以也许我对食谱中资源的运行顺序不了解? I would expect them to run in sequential order... 我希望它们按顺序运行...

I also tried putting a notifies call inside the directory block, where I call another execute block :immediately . 我还尝试在directory块内放置一个notifies调用,在此我调用另一个execute:immediately That works, but inside the second execute block I test to see whether /downloads has been created and it hasn't. 那行得通,但是在第二个execute块中,我测试了是否创建了/downloads是否尚未创建。

Clearly I'm missing something very basic. 显然,我缺少一些非常基本的东西。

Chef has two phases of execution: a compile phase and a converge phase. Chef具有两个执行阶段:一个编译阶段和一个收敛阶段。

In the compile phase, any resource declarations you write (like directory ) are compiled but not executed. 在编译阶段,您编写的所有资源声明(例如directory )都将被编译但不会执行。 Any bare Ruby code you write is also executed at this time. 您编写的任何裸露的Ruby代码也会在此时执行。 In the converge phase, any compiled resources are then converged in a test-and-set operation. 在融合阶段,所有编译的资源都将在测试和设置操作中融合。

I'm going to assume that at work, you already have a /downloads directory pre-existing, so the resource is a no-op during converge, and the raise doesn't happen during compile. 我将假设在工作中,您已经有一个/downloads目录,因此资源在聚合过程中是空操作,并且在编译过程中不会发生提升。

If you want arbitrary Ruby code to execute at converge time, put it in a ruby_block resource. 如果要在收敛时执行任意Ruby代码,请将其放在ruby_block资源中。

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

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