此前书写了多实例的 Tomcat 启动等操作的脚本,今天完善 Tomcat 多实例部署(本脚本只提供思路)

脚本内容:

 #!/usr/bin/env python
# _*_coding:utf-8_*_
# Author: "Edward.Liu" # Import libary~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import subprocess
import time
import sys
import signal
import os
import argparse
import contextlib
import zipfile # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Tomcat(object):
def __init__(self, tomcat_exe):
self.tomcat_exe = tomcat_exe
self.Tomcat_Home = "/software/%s" % tomcat_exe
self.Tomcat_Log_Home = "/software/%s/logs" % tomcat_exe
self.counnt = 10
# deploy options
self.timeStr = time.strftime("%Y-%m-%d-%H:%M")
self.source_files = "/software/cybershop-front-0.0.1-SNAPSHOT.war"
self.dest_dir = "/software/upload_project/%s-%s" % (
self.timeStr, self.source_files.split('/')[2].split('.war')[0])
self.dest_deploy_dir = "/software/deploy-front/%s" % self.source_files.split('/')[2].split('.war')[0]
self.images_Home = "/software/newupload1"
self.static_images_lins = "%s/assets/upload" % self.dest_dir
self.static_Home = "/data/www"
self.static_home_link = "%s/www" % self.dest_dir
# deploy options --->end # Get Tomcat_PID~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def get_tomcat_pid(self):
# 自定义获取程序 pid 与启动命令
p = subprocess.Popen(['ps', '-Ao', 'pid,command'], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
if 'java' in line:
if self.tomcat_exe in line:
pid = int(line.split(None, 1)[0])
return pid
# 获取 END # Start Tomcat Process~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def start_tomcat(self):
if self.get_tomcat_pid() is not None:
print "\033[32m %s Is Started \033[0m" % self.tomcat_exe
else:
# Start Tomcat
command_start_tomcat = "%s/bin/startup.sh" % self.Tomcat_Home
p = subprocess.Popen(command_start_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
print stdout, stderr # Stop Tomcat process~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def stop_tomcat(self):
wait_sleep = 0
if self.get_tomcat_pid() is None:
print "\033[32m %s is Not Running\033[0m" % self.tomcat_exe + "~" * 20
else:
command_stop_tomcat = "%s/bin/shutdown.sh" % self.Tomcat_Home
p = subprocess.Popen(command_stop_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
while (self.get_tomcat_pid() is not None):
print "waiting for processes to exit\n"
wait_sleep += 1
time.sleep(1)
if wait_sleep == self.counnt:
os.kill(self.get_tomcat_pid(), signal.SIGKILL)
print "\033[32m Stop Tomcat is sucessful \033[0m"
break # View TomcatLogs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def tomcat_log(self):
command_tomcat_log = "tail -f %s/catalina.out " % self.Tomcat_Log_Home
p = subprocess.Popen(command_tomcat_log, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
returncode = p.poll()
try:
while returncode is None:
line = p.stdout.readline()
returncode = p.poll()
line = line.strip()
print line
print returncode
except KeyboardInterrupt:
print 'ctrl+d or z' # Unzip Project_name~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def unzip(self):
ret = 0
try:
with contextlib.closing(zipfile.ZipFile(self.source_files)) as zf:
if not os.path.exists(self.dest_dir):
print "\033[32mPath %s Is Not Exists Creating\033[0m" % self.dest_dir
os.makedirs(self.dest_dir)
zf.extractall(self.dest_dir)
os.remove(self.source_files)
ret = 2 except IOError:
print "\033[31m%s Is Not Exists Please send Files\033[0m" % self.source_files
return ret
# Create Soft Links
def soft_link(self):
if os.path.islink(self.dest_deploy_dir):
os.unlink(self.dest_deploy_dir)
print "\033[32mCreating Static Files/Images Link\033[0m "
os.symlink(self.images_Home, self.static_images_lins)
os.symlink(self.static_Home, self.static_home_link)
print self.dest_dir
print self.dest_deploy_dir
os.symlink(self.dest_dir, self.dest_deploy_dir)
else:
print "\033[32mCreating Static Files/Images Link\033[0m "
os.symlink(self.images_Home, self.static_images_lins)
os.symlink(self.static_Home, self.static_home_link)
print self.dest_dir
print self.dest_deploy_dir
os.symlink(self.dest_dir, self.dest_deploy_dir) if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="eg: '%(prog)s' -c tomcat-front|tomcat -d {start|stop|status|restart|log|deploy}")
# ADD Tomcat Apps ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parser.add_argument('-c', '--app_name', nargs='+', dest='choices',
choices=('tomcat-front', 'tomcat-mobile')) # choices 规定只能书写此处标出的, nargs='+' 至少有一个参数
parser.add_argument('-d', '--Handle', action='store', nargs='?', dest='handle', default='log',
help='Input One of the {start|stop|status|restart|log|deploy}') # nargs='?' 有一个货没有参数都可以
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args()
if len(sys.argv) <= 4:
parser.print_help()
else:
try:
Handle = Tomcat(args.choices[0])
if args.handle == 'log':
Handle.tomcat_log()
elif args.handle == 'start':
Handle.start_tomcat()
elif args.handle == 'stop':
Handle.stop_tomcat()
elif args.handle == 'restart':
Handle.stop_tomcat()
time.sleep(5)
Handle.start_tomcat()
elif args.handle == 'deploy':
Handle.stop_tomcat()
if Handle.unzip() != 0:
Handle.soft_link()
Handle.start_tomcat()
elif args.handle == 'status':
if Handle.get_tomcat_pid() is not None:
print "\033[32m %s Is Running is PID:\033[0m" % Handle.tomcat_exe + "\033[31m %s \033[0m" % Handle.get_tomcat_pid()
else:
print "\033[32m %s Not Running Or Not Exist \033[0m" % Handle.tomcat_exe
else:
print "\033[31mYou Input parameter Is Not Exist\033[0m"
except TypeError:
parser.print_help()

Python 部署项目(Tomcat 容器)的更多相关文章

  1. Docker Compose部署项目到容器-基于Tomcat和mysql的项目yml配置文件代码

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  2. Docker Compose部署项目到容器-基于Tomcat和mysql的商城项目(附源码和sql下载)

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  3. 将spring boot项目部署到tomcat容器中

    一. 我这里用的环境 tomcat: tomcat 8 jdk: jdk 7 spring boot 版本: 1.5 二. 将创建好的spring boot项目做如下修改 2.1. 修改打包形式 在p ...

  4. 多个Spring Boot项目部署在一个Tomcat容器无法启动

    转自https://www.cnblogs.com/tomxin7/p/9434085.html 业务介绍 最近用Spring Boot开发了一个翻译的小项目,但是服务器上还跑着其他项目,包括一个同样 ...

  5. 使用sts(SpringToolSuite4)无法将项目部署到tomcat容器

    一般情况下maven项目不能添加到tomcat容器中 ,需要在项目上进行设置 但是sts没有安装此插件,可以改用eclipse进行开发.

  6. Jenkins - 部署在Tomcat容器里的Jenkins,提示“反向代理设置有误”

    提示"反向代理设置有误"的背景 将jenkins.war放在tomcat容器中运行 访问Jenkins-系统管理,会提示"反向代理设置有误" 如何解决 在tom ...

  7. Jenkins部署在Tomcat容器提示:反向代理设置有误

    这个错误我研究了一下,应该是部署在Tomcat时特有的错误,并且我把启动的目录设置成ROOT同样也还在,网上说在系统设置里面的Jenkins URL设置后可以解决,但我测试了同样不行. 如果使用jav ...

  8. Maven项目热部署到Tomcat容器下

    第一步: 配置Tomcat的登陆的用户名与密码     在 apache-tomcat-7.0.33\conf\ tomcat-users.xml,第40行添加如下: <role rolenam ...

  9. 把spring-boot项目部署到tomcat容器中

    http://blog.csdn.net/javahighness/article/details/52515226

随机推荐

  1. ADO.NET---ExcuteScalar()方法复习

    ExcuteScalar(),返回的是查询结果的第一行第一列,返回值是object类型,一般用来查询表中有多少条数据,求最大值等 现在我们用ExcuteScalar()做个测试,需求:我要查询学生表里 ...

  2. 调试报“The source file is different from when the module was built.”问题的解决

    It is related to the checksums which is used to ensure that you are stepping in matching source. You ...

  3. 较友好的Web文件下载用户体验实例

    1.实际需求整理与分析 该问题起源于为公司做的一个B/S架构的游戏静态数据管理工具,其中有一个需求是点击页面上的一些按钮要下载文件,可能根据按钮类型的不同需要转换下载.json..zip..xlsx等 ...

  4. .Net Oauth2.0 第三方登录开发(Facebook ,LinkedIn )

    需求:OAuth2实现第三方网站授权并获取其相关数据来实现登录等功能 暂时支持Facebook ,LinkedIn ,基本大同小异,只是返回时的数据不同,需根据具体返回类型进行相应处理 1.OAuth ...

  5. linux怎么模糊查找一个文件

    linux如何模糊查找一个文件 在当前目录下搜索指定文件: find . -name test.txt 在当前目录下模糊搜索文件: find . -name '*.txt' 在当前目录下搜索特定属性的 ...

  6. 用EF6更新数据库时出现外键错误解决方式

    在“Package Manager Console”中执行update-database命令,出现异常信息: Introducing FOREIGN KEY constraint 'FK_dbo.Pr ...

  7. Design Patterns (简单工厂模式)

    文章很长很精彩,如是初学请耐心观看.(大神请绕道!) 简单工厂模式: 1.创建型模式 2.简单工厂模式概述 3.简单工厂模式的结构与实现 4.简单工厂模式的应用实例 5.创建对象与使用对象 6.简单工 ...

  8. Dom4j 锁竞争性能低下解决

    在最近的项目中使用 Dom4j 解析 xml 发现性能低下,有锁竞争的情况,解决如下: SAXParserFactory factory = new org.apache.xerces.jaxp.SA ...

  9. gulp入坑系列(4)——gulp的代码转换

    当然,gulp不仅仅能转换Sass,这里会提到如下转换: jsx转换成常规的JavaScript(说到jsx,玩过react的同学应该是知道的) es6转换为es5 Less,Sass转换为CSS 首 ...

  10. MUI - 图片预览(perviewimage)的优化

    主要对mui图片全屏预览插件做了以下三点补充 1.添加了预览图片文字说明,使用的时候需要添加以下css及DOM属性 .mui-slider-img-content { position: absolu ...