如果该源文件存在,如何编写Chef配方来复制文件? only_if后卫不工作

问题描述 投票:0回答:1

我目前正在开发一个简单的脚本来处理Chef部署期间的属性文件。我正在用chef-apply script.rb执行以下代码

class_path = '/var/lib/tomcat/webapps/ROOT/WEB-INF/classes'
file '/home/corp/working/corp.properties' do
  owner 'root'
  group 'root'
  mode 0644
  p File.exist?("#{class_path}")
  content File.open("#{class_path}/corp.properties").read
  action :create_if_missing
  only_if { File.exist?("#{class_path}/corp.properties") }
end

但它运行错误

false
[2017-12-20T11:37:40-05:00] FATAL: Stacktrace dumped to /home/corp/.chef/cache/chef-stacktrace.out
[2017-12-20T11:37:40-05:00] FATAL: Stacktrace dumped to /home/corp/.chef/cache/chef-stacktrace.out
[2017-12-20T11:37:40-05:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2017-12-20T11:37:40-05:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2017-12-20T11:37:40-05:00] FATAL: Errno::ENOENT: No such file or directory @ rb_sysopen - /var/lib/tomcat/webapps/ROOT/WEB-INF/classes/corp.properties
[2017-12-20T11:37:40-05:00] FATAL: Errno::ENOENT: No such file or directory @ rb_sysopen - /var/lib/tomcat/webapps/ROOT/WEB-INF/classes/corp.properties

正如您所看到的,即使File.exist?调用返回false,也会执行File.open而不管后卫。

ruby tomcat deployment chef
1个回答
2
投票

资源块中的任何代码都默认在编译时运行。守卫在收敛时间运行。你可以通过使content lazy { File.open("#{class_path}/corp.properties").read }强制文件读取在收敛时间来解决这个问题。有关更多信息,请参阅https://coderanger.net/two-pass/

© www.soinside.com 2019 - 2024. All rights reserved.