Tomcat manager remote deploy script
Asked Answered
S

6

42

I'm writing a shell script to auto deploy/undeploy using the tomcat manager.

Following the instructions on http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Deploy_A_New_Application_Remotely, I use curl for my deployment

curl --anyauth -u username:pwd -d path=/something -d war=file:target/someWar.war https://someurl.com/manager/deploy

And I get the response saying HTTP method POST is not supported by this URL.

So I change my curl to be a get using -G

curl --anyauth -u username:pwd -G -d path=/something -d war=file:target/someWar.war https://someurl.com/manager/deploy

I get a response of FAIL - Failed to deploy application at context path /something and it seems to be looking for the file locally on the server instead of my machine. There are pluings which do remote deploy without having to scp the file over so I'm wondering what I'm missing.

I'm currently out of ideas (I don't see any other option on the tomcat manager configuration page).

Sibship answered 13/12, 2010 at 19:29 Comment(0)
P
45

This way is working for me on Tomcat 6 (See jevelopers answer for tomcat 7):

curl --upload-file <path to warfile> "http://<tomcat username>:<tomcat password>@<hostname>:<port>/manager/deploy?path=/<context>&update=true"

Example:

curl --upload-file target\debug.war "http://tomcat:tomcat@localhost:8088/manager/deploy?path=/debug&update=true"

Very easy peasy. Output is like this:

OK - Undeployed application at context path /debug
OK - Deployed application at context path /debug
Polytechnic answered 7/3, 2011 at 17:42 Comment(4)
Got my info from the cURL manual: curl.haxx.se/docs/httpscripting.html check out section 5Polytechnic
This does not work for Tomcat 7. Take a look at the answer from jeveloperRivero
Thanks, Michael R! I updated the text to say Tomcat 6 and added reef to jevelopers answers... just in case someone doesn't read commentsPolytechnic
thanks a lot! I confirm it works on Tomcat 8.5.29 as follows: curl.exe -v -u "user:pass" --upload-file /path/to/my-war.war "localhost:8080/manager/text/deploy?path = /my-context-path&update = true"Trinomial
T
59

Providing an update to this question.

Tomcat 7 has changed it's manager API.

Please refer to: Manager commands

Following new URL pattern :

http://{host}:{port}/manager/text/{command}?{parameters}

Example

curl -T "myapp.war" "http://manager:manager@localhost:8080/manager/text/deploy?path=/myapp&update=true"

Security

Keep in mind the server must be able to accept your remote IP. This is a sample configuration:

<Context privileged="true" antiResourceLocking="false"
         docBase="${catalina.home}/webapps/manager">
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.0\.0\.1" />
</Context>

This is an optional setting and isn't required but having Cross domain role and proper manager credentials is a must.

Tomcat 8 - the same rules apply as Tomcat 7. Same commands.

Here is a full documentation:

http://tomcat.apache.org/tomcat-8.0-doc/manager-howto.html

Tolidine answered 13/11, 2012 at 19:34 Comment(5)
It should be noted that the user deploying to tomcat needs to have manager-script role enabled in order to access the text interface.Mima
can we do this for deploying in a remote machine tomcat?Timbre
@Timbre You absolutely can. As long as your firewall allows connection from outside. You're also encouraged to use HTTPSTolidine
this saves me a lot of time when deploying to a remote machine which has a cluster (same WAR for every instance) and for which I have ssh accessCurable
Hi Friend, I am able to upload war file by your example and it is working fine. One Issue: When i fire this curl on Linux machine, it deployed on server, but goes on wait forever. any idea?Ira
P
45

This way is working for me on Tomcat 6 (See jevelopers answer for tomcat 7):

curl --upload-file <path to warfile> "http://<tomcat username>:<tomcat password>@<hostname>:<port>/manager/deploy?path=/<context>&update=true"

Example:

curl --upload-file target\debug.war "http://tomcat:tomcat@localhost:8088/manager/deploy?path=/debug&update=true"

Very easy peasy. Output is like this:

OK - Undeployed application at context path /debug
OK - Deployed application at context path /debug
Polytechnic answered 7/3, 2011 at 17:42 Comment(4)
Got my info from the cURL manual: curl.haxx.se/docs/httpscripting.html check out section 5Polytechnic
This does not work for Tomcat 7. Take a look at the answer from jeveloperRivero
Thanks, Michael R! I updated the text to say Tomcat 6 and added reef to jevelopers answers... just in case someone doesn't read commentsPolytechnic
thanks a lot! I confirm it works on Tomcat 8.5.29 as follows: curl.exe -v -u "user:pass" --upload-file /path/to/my-war.war "localhost:8080/manager/text/deploy?path = /my-context-path&update = true"Trinomial
Y
3

For those who use Jenkins and want to deploy using shell script in GitBash on a Windows machine instead of Jenkins deploy plugin

tomcat_host=192.10.10.100
tomcat_port=8080
tomcat_username=admin
tomcat_password=12345

context_path=myApplication

curl -v -u ${tomcat_username}:${tomcat_password} -T ${artifact} 'http://'${tomcat_host}':'${tomcat_port}'/manager/text/deploy?path=//'${context_path}''

Note:

  1. curl -v option is verbose (optional)
  2. // two forward slashes before the context path works for GitBash on a Windows machine (/ single forward slash will not somehow)
  3. Also when deploying on a remote server, consider your firewall yeah!
Yorkist answered 17/8, 2015 at 5:43 Comment(1)
Pretty sure you want tomcat_username and tomcat_password after the -u in this script, and not the host and port.Continuity
P
3

Improving Jet answer, this works for me in tomcat 8, java 64 bits.

This was what I execute:

curl -v -u some_user:some_password -T /../my_app.war 'http://127.0.0.1:tomcat_port/manager/text/deploy?path=/my_app&update=true'

This will work if we configure tomcat users in :

/.../.../apache-tomcat-8.5.0_001/conf/tomcat-users.xml

with:

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>

<user username="some_user" password="some_password" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

Restart tomcat and it will be ready to deploy wars from remote clients like curl, jenkins, travis, etc

Palmer answered 3/6, 2016 at 20:29 Comment(0)
H
1

The easiest way to deploy an app is to write an Ant script. The only other thing (apart from Ant) you will need is catalina-ant.jar to be present in the classpath.

Have a look at this chapter of the manual: http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Executing_Manager_Commands_With_Ant

The script does a similar thing: uses HTTP to deploy your .war to the manager app. You might even want to capture the packets to see the exact headers if you still want to use curl. I would not recommend curl though as I think Ant solution is more portable and error-prone (e.g what if they will change low level deployment API?).

Hygrometer answered 14/12, 2010 at 10:43 Comment(3)
Thanks for the response. I guess as a worst case scenario I will use ant, but it would be nice to use some sort of deploy mechanism without any dependencies (easier to scale out).Sibship
The catalina-ant.jar is part of every Tomcat installation so I am not sure if understood your point. Even if you (re)deploy thousands of webapps every day, the overhead of launching JVM should be negligible.Hygrometer
We're not concerned about the server, to deploy using ant you still need ant installed on the client machine.Sibship
A
0

I was getting error

curl: Can't open webapp.war 

when I only mentioned

curl -T 'webapp.war'

But it worked when I used the complete path of build artifact like

curl -T ./target/webapp.war
Astrea answered 25/9, 2020 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.