Dan Tenenbaum
Dan Tenenbaum

Reputation: 1889

directory resource does not create directory

I have a Vagrantfile that provisions a VM by running a chef recipe. 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.

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. 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. That works, but inside the second execute block I test to see whether /downloads has been created and it hasn't.

Clearly I'm missing something very basic.

Upvotes: 0

Views: 481

Answers (1)

Julian Dunn
Julian Dunn

Reputation: 286

Chef has two phases of execution: a compile phase and a converge phase.

In the compile phase, any resource declarations you write (like directory) are compiled but not executed. Any bare Ruby code you write is also executed at this time. 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.

If you want arbitrary Ruby code to execute at converge time, put it in a ruby_block resource.

Upvotes: 3

Related Questions