How to upload a WAR file on Tomcat 7 cookbook by Chef?

↘锁芯ラ 提交于 2019-12-06 14:33:10
Stephen C

The simple way to do is is to use the service resource to stop Tomcat, use the bash resource or a file resource to copy the WAR file, and then use the service resource to start Tomcat again.

That cookbook you are trying to use doesn't do WAR file deployment. But I found this one - https://github.com/poise/application_java - that may do the job.

And for what it is worth, I think you could find a better Tomcat installation cookbook too. For a start, there is a "tomcat" recipe on the Opscode Community Cookbooks site that can handle Tomcat 7.

(Hint: don't just use the first cookbook you find with Google. Do a Github search, and try to assess the cookbooks functionality and quality)

Have you already found $tomcat/webapps/ folder?

Steps doing a manual hotdeployment and update. Avoiding possible conflict you should do a temporary mywebapp.war.zip filename trick. Trick I have used in linux and windows for years.

  • copy myapp.war to $tomcat/webapps/myapp.war.zip filename. Tomcat ignores it.
  • rename myapp.war.zip to myapp.war filename. Tomcat finds a new web application.
  • wait few seconds, you should find $tomcat/webapps/myapp/ extract folder made by tomcat. war file was autoextracted and now available.
  • browse to http://localhost:8080/myapp/ URL address

Update existing web application using overwrite trick.

  • copy myapp.war to $tomcat/webapps/myapp.war.zip filename
  • rename myapp.war.zip to myapp.war filename, force overwrite existing one.
  • wait few seconds, you should see $tomcat/webapps/myapp/ folder timestamp be updated
  • browse to http://localhost:8080/myapp/ URL address

Or update existing web application using delete-copy trick.

  • copy myapp.war to $tomcat/webapps/myapp.war.zip filename
  • delete existing myapp.war filename
  • wait few seconds, you should see $tomcat/webapps/myapp/ folder being removed
  • rename myapp.war.zip to myapp.war filename
  • wait few seconds, you should find $tomcat/webapps/myapp/ extract folder made by tomcat. war file was autoextracted and now available.
  • browse to http://localhost:8080/myapp/ URL address

Reason for a temporary war.zip filename is ensure file is fully copied to a destination folder before Tomcat touches it. Copying large files directly to .war filename may trigger Tomcat reading halfsized file content.

A very important part to remember while doing this in chef is to use a guard conditions to not let chef keep deploying the file on every run. As the steps explained above are fine but when you code in chef, it will keep copying the file in each run and deploying unless you have a guard condition.

Take for example below case, chef will execute this in each run.

# Extract
execute "tar" do
    user "#{tc7user}"
    group "#{tc7group}"
    installation_dir = "#{tc7target}"
    cwd installation_dir
    command "tar zxf /tmp/#{tc7tarball}"
    action :run
end

To prevent chef from executing the on each run use the creates property with parameter of the full path of the file being created. The next chef run will see that file was already created and will skip it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!