【问题标题】:How do I delete a directory if a file has changed using Chef?如果使用 Chef 更改了文件,如何删除目录?
【发布时间】:2016-08-24 20:23:10
【问题描述】:
我想创建一个用于使用 tomcat 部署 java 应用程序的 Chef 配方。当我更新java应用程序(我的cookbook中的.WAR文件,上传并获取一个节点来运行更新的cookbook)时,我希望Chef停止tomcat,删除tomcat/webapps文件夹的内容,复制新的war然后启动tomcat再次。
我已经看到了 if File.exists 的语法,但没有看到 if 的任何内容。我怎样才能做到这一点?
我认为 Chef 具有此功能,因为它仅在文件校验和更改时更新文件。
【问题讨论】:
标签:
java
tomcat
chef-infra
【解决方案1】:
我遗漏了一些细节,例如如何将 WAR 文件放在正确的位置。所以我假设你使用cookbook_file 资源。
cookbook_file "path/to/release.war" do
notifies :execute, "cleanup tomcat webapp directory", :before
notifies :start, "service[tomcat]"
end
execute "cleanup tomcat directory" do
notifies :stop, "service[tomcat]", :before
command "rm -r path/tomcat/webapps/*"
action :nothing
end
在计时器需要 Chef 12.7 之前,服务 tomcat 必须在您将放置此代码的资源范围内可用。
但是,此解决方案可能会在运行 rm -r 时引入停机时间,并且需要完全停止/启动 tomcat。使用hot deploy(在正在运行的服务器上)不是更好吗?