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
随机推荐
- EF增删查改(三)------终极版
1.Add #region 1.1 新增学生信息(定义成Int类型,返回受影响的行数) /// <summary> /// 新增学生信息 /// </summary> /// ...
- 【iOS】利用Runtime特性做监控
最近在看Object-C运行时特性,其中有一个特别好用的特性叫 Method Swizzling ,可以动态交换函数地址,在应用程序加载的时候,通过运行时特性互换两个函数的地址,不改变原有代码而改变原 ...
- 可拖拽的ListBox
之前在写播放器的时候,遇到了一个问题,现在播放器无论是千千,KuGoo还是比较原始的MediaPlayer,它们的播放表都是可以拖拽的,直接把文件拖到播放表实现歌曲的添加那个先暂且不说,光是播放表里面 ...
- 用C#编程的建议
1.如果可能尽量使用接口来编程 .NET框架包括类和接口,在编写程序的时候,你可能知道正在用.NET的哪个类.然而,在这种情况下如果你用.NET支持的接口而不是它的类来编程时,代码会变得更 ...
- (转载)IO-同步、异步、阻塞、非阻塞
一.概述 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不 ...
- sql where 1=1和 0=1 的作用
sql where 1=1和 0=1 的作用 摘自: http://www.cnblogs.com/junyuz/archive/2011/03/10/1979646.html where 1=1; ...
- Linux 安装 PHP 环境
使用虚拟机玩linux时,发现CentOS中的php版本是5.1.6.如果要安装新版的php,需要把旧的版本删除. 先查看下php版本:# php -v 如果执行该命令提示该命令不存在,那么可以通过以 ...
- JavaScripts 基础详细笔记整理
一.JS简介 JavaScript 是 Web 的编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理 ...
- js和html5实现画板
html5新添了一个重要又强大的标签元素<canvas>,该标签真有彻底替换掉flash的尽头,现在很多网页游戏就是用<canvas>完成的,下面代码就是用该标签制作的一个画板 ...
- linq 动态组合条件
http://www.albahari.com/nutshell/predicatebuilder.aspx Dynamically Composing Expression Predicates S ...