Python 部署项目(Tomcat 容器)
此前书写了多实例的 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 容器)的更多相关文章
- Docker Compose部署项目到容器-基于Tomcat和mysql的项目yml配置文件代码
场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
- Docker Compose部署项目到容器-基于Tomcat和mysql的商城项目(附源码和sql下载)
场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
- 将spring boot项目部署到tomcat容器中
一. 我这里用的环境 tomcat: tomcat 8 jdk: jdk 7 spring boot 版本: 1.5 二. 将创建好的spring boot项目做如下修改 2.1. 修改打包形式 在p ...
- 多个Spring Boot项目部署在一个Tomcat容器无法启动
转自https://www.cnblogs.com/tomxin7/p/9434085.html 业务介绍 最近用Spring Boot开发了一个翻译的小项目,但是服务器上还跑着其他项目,包括一个同样 ...
- 使用sts(SpringToolSuite4)无法将项目部署到tomcat容器
一般情况下maven项目不能添加到tomcat容器中 ,需要在项目上进行设置 但是sts没有安装此插件,可以改用eclipse进行开发.
- Jenkins - 部署在Tomcat容器里的Jenkins,提示“反向代理设置有误”
提示"反向代理设置有误"的背景 将jenkins.war放在tomcat容器中运行 访问Jenkins-系统管理,会提示"反向代理设置有误" 如何解决 在tom ...
- Jenkins部署在Tomcat容器提示:反向代理设置有误
这个错误我研究了一下,应该是部署在Tomcat时特有的错误,并且我把启动的目录设置成ROOT同样也还在,网上说在系统设置里面的Jenkins URL设置后可以解决,但我测试了同样不行. 如果使用jav ...
- Maven项目热部署到Tomcat容器下
第一步: 配置Tomcat的登陆的用户名与密码 在 apache-tomcat-7.0.33\conf\ tomcat-users.xml,第40行添加如下: <role rolenam ...
- 把spring-boot项目部署到tomcat容器中
http://blog.csdn.net/javahighness/article/details/52515226
随机推荐
- JAVA - 大数类详解
写在前面 对于ACMer来说,java语言最大的优势就是BigInteger,Bigdecimal,String三个类. 这三个类分别是高精度整数,高精度浮点数和字符串,之所以说这个是它的优势是因为j ...
- 以对象的方式来访问xml数据表(三)
怎样以对象的方式来访问xml数据表? 在讲如何具体实现(二)中所说的专门用于访问xml文件的动态链接库之前,我们先来看看这个动态链接库具体要实现什么功能. 动态链接库IXmlDB.dll的功能: 1. ...
- 论httpclient上传带参数【commons-httpclient和apache httpclient区别】
需要做一个httpclient上传,然后啪啪啪网上找资料 1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试 PostMethod filePost = ...
- AngularJS 最常用的功能
第一 迭代输出之ng-repeat标签ng-repeat让table ul ol等标签和js里的数组完美结合 1 2 3 4 5 <ul> <li ng-repeat="p ...
- 简洁侧边wordpress博客模板
模板描述:商务领航,尽现成熟稳重的个人小站风格 响应式Web设计,自适应电脑.平板电脑.移动设备 图标字体库,自适应各种终端设备,保证图形图标清晰无锯齿,支持Retina(视网膜屏幕) ...
- css权重是什么
css权重是什么? 概述 css Specificity中文一般译为css优先级.css权重.相比"权重","优先级"更好理解,mozilla官方中文文档就翻译 ...
- BINARY SEARCH in read table statement
1.for standard table, it must be sorted by search key. 2.for sorted table , binary search is used au ...
- windows 8 设置hyper-v网络设置
1 windwos 8 设置hyperv 比较简单,和装操作系统都不多做解释.我只多说说网络的设置问题,因为可能装提windows 2008虚拟机,根据网上设置网络的方式都是要不然只能虚拟 机上网 , ...
- 转:Web App开发入门
WebApp与Native App有何区别呢? Native App: 1.开发成本非常大.一般使用的开发语言为JAVA.C++.Objective-C. 2.更新体验较差.同时也比较麻烦.每一次发布 ...
- 转:HTTP 1.1与HTTP 1.0的比较
原文地址:http://blog.csdn.net/elifefly/article/details/3964766 HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用 ...