opscode chef 可以执行等到条件变为真吗?

Can opscode chef perform wait until a condition becomes true?

我们有一个用例,我们希望 chef orchestration 等到机器中的特定目录被删除。有什么办法可以实现吗?

我在网上搜索了一下,发现了以下cookbook

我觉得它可以使用,但我很难理解我该如何使用它,没有关于使用它的读物。

如何实现?

编辑以删除保留: 说你有以下食谱

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

end

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

在这里,not_if 我想要的是 "wait until /tmp/test1 gets deleted" 但是厨师如何执行它就像 "it found directory exixting so it did not execute the resource and exited"

我需要一种方法来执行等待,直到条件变为真。

实际上是我在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中不时看到的一种模式,以弄清楚如何使用它。这是一个例子:

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

它似乎调用了 ruby 的 system(command)sleep(wait_interval)。希望这对您有所帮助!

编辑:正如其他发帖人所说,如果您可以在 Chef 中完成所有操作,则带有目录资源和删除操作的通知是更好的解决方案。但是你问到wait资源怎么用,所以我想专门回答一下。

首先,不要shell出去创建目录。如果您仅使用 Chef 来执行 shell 命令,那么仅编写 shell 脚本不会获得太多其他好处。依靠 Chef directory 资源为您做这件事要好得多。然后,您可以确信它每次都能在每个系统上运行。此外,您将能够利用代表您的目录的 Chef 资源,以便您可以执行通知等操作。

这里是对两个目录操作的轻微重构:

# 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