简体   繁体   English

Tomcat 管理器远程部署脚本

[英]Tomcat manager remote deploy script

I'm writing a shell script to auto deploy/undeploy using the tomcat manager.我正在编写一个 shell 脚本来使用 tomcat 管理器自动部署/取消部署。

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按照http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Deploy_A_New_Application_Remotely上的说明,我使用 curl 进行部署

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.我得到响应说这个 URL 不支持 HTTP 方法 POST。

So I change my curl to be a get using -G所以我改变了我的 curl 使用 -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.我收到 FAIL 的响应 - 无法在上下文路径 /something 部署应用程序,它似乎在服务器上本地而不是我的机器上查找文件。 There are pluings which do remote deploy without having to scp the file over so I'm wondering what I'm missing.有一些插件可以进行远程部署,而不必将文件 scp 过来,所以我想知道我错过了什么。

I'm currently out of ideas (I don't see any other option on the tomcat manager configuration page).我目前没有想法(我在 tomcat 管理器配置页面上没有看到任何其他选项)。

Providing an update to this question.提供此问题的更新。

Tomcat 7 has changed it's manager API. Tomcat 7已更改其管理器 API。

Please refer to: Manager commands请参考: 管理器命令

Following new URL pattern :遵循新的 URL 模式:

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.请记住,服务器必须能够接受您的远程 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. Tomcat 8 - 适用于 Tomcat 7 的相同规则。相同的命令。

Here is a full documentation:这是一个完整的文档:

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

This way is working for me on Tomcat 6 (See jevelopers answer for tomcat 7):这种方式在 Tomcat 6 上对我有用(请参阅 jevelopers 对 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

For those who use Jenkins and want to deploy using shell script in GitBash on a Windows machine instead of Jenkins deploy plugin对于那些使用 Jenkins 并希望在 Windows 机器上的 GitBash 中使用 shell 脚本而不是 Jenkins 部署插件进行部署的人

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) curl -v 选项很详细(可选)
  2. // two forward slashes before the context path works for GitBash on a Windows machine (/ single forward slash will not somehow) // 在上下文路径适用于 Windows 机器上的 GitBash 之前的两个正斜杠(/ 单个正斜杠不会以某种方式)
  3. Also when deploying on a remote server, consider your firewall yeah!此外,在远程服务器上部署时,请考虑您的防火墙是的!

Improving Jet answer, this works for me in tomcat 8, java 64 bits.改进 Jet 答案,这在 tomcat 8,java 64 位中对我有用。

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 :如果我们在以下位置配置 tomcat 用户,这将起作用:

/.../.../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重新启动 tomcat,它将准备好从 curl、jenkins、travis 等远程客户端部署战争

The easiest way to deploy an app is to write an Ant script.部署应用程序的最简单方法是编写 Ant 脚本。 The only other thing (apart from Ant) you will need is catalina-ant.jar to be present in the classpath.您唯一需要的其他东西(除了 Ant 之外)是catalina-ant.jar出现在类路径中。

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看看手册的这一章: 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.该脚本执行类似的操作:使用 HTTP 将您的 .war 部署到管理器应用程序。 You might even want to capture the packets to see the exact headers if you still want to use curl.如果您仍然想使用 curl,您甚至可能想要捕获数据包以查看确切的标头。 I would not recommend curl though as I think Ant solution is more portable and error-prone (eg what if they will change low level deployment API?).我不推荐 curl,因为我认为 Ant 解决方案更便携且更容易出错(例如,如果它们会更改低级部署 API 会怎样?)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM