2

we have a use case where we want the chef orchestration to wait until a particular directory gets deleted in the machine. is there some way to achieve it?

I searched in the internet and found the following cookbook

I feel it can be used but i am having difficulty in understanding how can I use it, there is no read me about using it.

How can i achieve it?

edit to remove hold: say you have following recipe

execute 'making dir' do
  command 'mkdir /tmp/test2'
  not_if do ::File.directory?('/tmp/test1') end

end

reference: https://docs.chef.io/resource_common.html#not-if-examples

here, what i want by not_if was "wait until /tmp/test1 gets deleted" but how chef executs this is like "it found directory exixting so it did not execute the resource and exited"

i need a way to perform wait until a condition becomes true.

4

2 回答 2

2

It's actually a pattern I've seen from time to time in various cookbooks, often used to wait for a block device or to mount something, or to wait for a cloud resource. For the wait cookbook you've found, I had to dig up the actual source repo on Github to figure out how to use it. Here's an example:

until 'pigs fly' do
  command '/bin/false'
  wait_interval 5
  message 'sleeping for 5 seconds and retrying'
  action :run
end

It appears to call ruby's system(command) and sleep(wait_interval). Hope this helps!

EDIT: As other posters have stated, if you can do the whole thing in chef, a notification with a directory resource and delete action is a better solution. But you asked how to use the wait resource, so I wanted to answer that specifically.

于 2015-07-08T10:41:30.893 回答
2

First off, don't shell out to create a directory. You aren't gaining much other just writing a shell script if you only use Chef to exec shell commands. It is much better to rely on the Chef directory resource to do that for you. Then, you can be confident that it will work every time on every system. Plus, you will be able to make use of the the Chef resource that represents your directory so that you can do things such as notifications.

Here is a slight refactoring of both directory operations:

# Ensure that your directory gets deleted, if it exists.
directory '/tmp/test1' do
  action :delete
  notifies :action, 'directory[other_dir]', :immediately
end

# Define a resource for your directory, but don't actually do anything to the underlying machine for now.
directory 'other_dir' do
  action :nothing
  path '/tmp/test2'
end
于 2015-07-08T11:14:57.583 回答