如何检查Chef中是否存在文件夹? [英] How do I check if a folder exists in Chef?

查看:73
本文介绍了如何检查Chef中是否存在文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

if !::File.exist?("#{node['iis']['home']}\\backup\\BkpB4Chef")
 windows_batch "Backup IIS Config" do
  code <<-EOH
   "#{node['iis']['home']}"\\appcmd add backup BkpB4Chef
  EOH
 end
end

总是说文件存在并执行循环。

It always says file exists and executes the loop.

推荐答案

您应该使用在此处警卫队。警卫队指定条件执行,但仍将资源插入资源集合中。在您的示例和jtblin答案中,资源从未添加到集合中(稍后我将作进一步解释)。

You should use Chef guards here. Guards specify conditional execution, but still insert the resource into the resource collection. In your example and jtblin answer, the resource is never added to the collection (which I'll explain a bit further in a moment).

这里有一些工作代码可以获取您开始了:

Here's some working code to get you started:

windows_batch "Backup IIS Config" do
  code %Q|#{node['iis']['home']}"\\appcmd add backup BkpB4Chef|
  not_if { ::File.directory?("#{node['iis']['home']}\\backup\\BkpB4Chef") }
end



使用 creates



许多非幂等厨师资源还支持 creates 参数,该参数说明了换句话说, windows_batch create是什么,它可以是文件,目录或可执行文件,因此以下代码等效于前一个答案。

Using creates

Many non-idempotent Chef resources also support a creates parameter, which explains what the resource does. In other words, what does the windows_batch "create". This can be a file, directory, or executable. So, the following code is equivalent to the former answer.

windows_batch "Backup IIS Config" do
  code %Q|#{node['iis']['home']}"\\appcmd add backup BkpB4Chef|
  creates"#{node['iis']['home']}\\backup\\BkpB4Chef"
end



为什么 not_if vs条件包装器



厨师分两个阶段执行-编译阶段和收敛阶段。在编译阶段,将避开配方并将资源添加到资源集合中。在融合阶段,将对资源收集中的资源进行执行并针对目标系统进行评估。因此,请考虑以下示例:

Why not_if vs the conditional wrapper

Chef executes in two phases - the compilation phase and the convergence phase. During the compilation phase, the recipes are evaled and the resources are added to the resource collection. In the convergence phase, the resources in the resource collection are executed and evaluated against the target system. So, consider the following example:

if false
  service 'foo' do
    action :start
  end
end

这是开始服务的相当简单的方法基于一些条件。但是,在编译阶段结束时, service 资源并未添加到资源集合中。因为配方DSL是 instance_eval ed,所以包装的 if false 条件语句可防止Ruby VM读取该代码。 。换句话说,就好像该服务永远不存在。

This is a fairly straight-forward recipe that starts a service based on some conditional. However, at the end of the compilation phase, the service resource isn't added to the resource collection. Since the recipe DSL is instance_evaled, the wrapping if false conditional prevents that code from ever being read by the Ruby VM. In other words, it's like that service never exists.

通知资源很常见。在本食谱的后面,由于配置更改,您可能想重新启动apache。做到这一点的正确方法是使用通知:

It's fairly common to notify resources. Later in the recipe, you might want to restart apache because of a configuration change. The "proper" way to do this is using notifications:

template '/var/www/conf.d/my.conf.file' do
  # ...
  notifies :restart, 'service[apache2]'
end

模板无法充分通知服务资源,因为它在资源集合中不存在。因此,此食谱将失败。这似乎是一个简单的例子,但是如果您将条件 if false 更改为节点属性test:

This template cannot adequately notify the service resource, because it doesn't exist in the resource collection. So this recipe will fail. It seems like a trivial example, but if you change the conditional if false to a node attribute test:

if node['cookbook']['use_apache']
  service 'apache2' do
    action :start
  end
end

您已在食谱中创建了二分法,该方法在50%的时间内都可以工作。不幸的是,大多数食谱都比两种资源复杂得多,因此,资源可以通知不存在的资源的极端情况的数量随着复杂性而急剧增加。可以使用资源保护措施解决所有问题(并表现出正确的行为):

you have created a dichotomy in your cookbook where it will work 50% of the time. Unfortunately most cookbooks are much more complex than two resources, so the number of edge cases where a resource can notify a non-existent resource drastically increases with complexity. This is all solvable (and exhibits the correct behavior) using resource guards:

service 'apache2' do
  action :start
  only_if { node['cookbook']['use_apache'] }
end

这篇关于如何检查Chef中是否存在文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆