2017-12-20 6 views
0

저는 현재 Chef 배포 중에 속성 파일을 처리하기위한 간단한 스크립트로 작업하고 있습니다. 내가 chef-apply script.rb소스 파일이있는 경우 파일을 복사하는 요리사 레시피를 작성하는 방법은 무엇입니까? only_if 가드가 작동하지 않습니다.

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가 실행되고 가드와 상관없이.

답변

2

리소스 블록의 코드는 기본적으로 컴파일 할 때 실행됩니다. 경비원은 수렴 시간에 달린다. 이것을 content lazy { File.open("#{class_path}/corp.properties").read }으로 수정하여 수렴 시간에 파일을 강제로 읽도록 할 수 있습니다. 자세한 내용은 https://coderanger.net/two-pass/을 참조하십시오.

관련 문제