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
随机推荐
- SQL Server中中数据行批量插入脚本的存储实现
看到博友SQL Server MVP桦仔的一篇博文“将表里的数据批量生成INSERT语句的存储过程的实现”.我仔细看来博文中的两个存储代码,自我感觉两个都不太满意,都是生成的单行模式的插入,数 ...
- android:inputType参数类型说明
android:inputType参数类型说明 android:inputType="none"--输入普通字符 android:inputType="text" ...
- c# winForm 等待窗体的实现
最近在做一个项目,需要用到等待窗体,在DevExpress下面有SplashScreen控件可以使用,同时也有ProgressIndicator控件能用,但是如果没有用Dev开发的话,我们就需要自定义 ...
- 【WP8.1】富文本
之前写过一篇WP8下的富文本的文章,但是写的不是很好,整理了一下,分享一下WP8.1下的富文本处理 富文本处理主要是对表情和链接的处理,一般使用RichTextBlock进行呈现 问题说明: 由于Ri ...
- 周末web前端练习
在 CSS 样式定义中,以下哪种 RGB 颜色值是 Web 安全色? A]#111111B]#222222C]#333333D]#444444 答案:http://hovertree.com/ti ...
- java内部类的使用
内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) 显然, ...
- MotoG2刷机小结
昨天,终于受不了MotoG2的后台软件不停重启,手机经常卡死,于是决定刷机,网上的教程好多,实践后总结一下,下面这个教程完美通过. 1.解官方BL锁.第一步:http://pan.baidu.com/ ...
- C#开发规范总结(个人建议)
.NET开发编程规范 章程序的版式 版式虽然不会影响程序的功能,但会影响可读性.程序的版式追求清晰.美观,是程序风格的重要构成因素. 可以把程序的版式比喻为"书法".好的" ...
- PHP 操作mongodb api大部分方法
<?php /* PHP mongodb * 全部curd操作 * @author:xiaojiang * @date: 2014-10-27 */ //查看 mongo类版本 1.30 以后版 ...
- Linux守护进程的编程实现(转)
http://blog.csdn.net/zg_hover/article/details/2553321 http://blog.csdn.net/kongdefei5000/article/det ...