初始化脚本
#!/usr/bin/env python
-- coding: utf-8 --
import glob, os,shutil,zipfile,socket,datetime,time,sys
def check_dir(filedir):
if os.path.exists(filedir):
pass
else:
print 'ERROR:::%s 目录不存在,请检测'%filedir
exit(1)
def list_file(filedir):
filesRead = r'%s'%filedir
list = glob.glob(filesRead)
filelist = []
for i in list:
filelist.append(i)
return filelist
class HS:
def init(self,yy_ip,db_ip):
self.yy_ip = yy_ip
self.db_ip = db_ip
self.redispw = '5136BA64F8D63BFB'
#self.Date = time.strftime('%y-%m-%d',time.localtime(time.time()))
self.RootPath = '/root/deployment'
self.DockerPath = self.RootPath + '/docker/docker-ce/docker-ce'
self.SoftwarePath = self.RootPath + '/software'
self.SoftwareOraclePath = self.RootPath + '/oracle'
self.TmpFile = '/tmp/hs_intall_tmp.txt'
check_dir(self.RootPath) shell = "root soft nofile 65535\nroot hard nofile 65535\n* soft nofile 65535\n* hard nofile 65535\n" self.check_file_bak_writefile("/etc/security/limits.conf",shell)def file_write(self,filename,shell): file_obj = open(filename,'a+') file_obj.write(shell) file_obj.close()def check_file_bak_shell(self,file1,shell): if os.path.exists('%s.bak'%file1): print 'INFO:::%s.bak备份存在,不再备份'%(file1) else: os.system('cp %s %s.bak'%(file1,file1)) print 'INFO:::%s备份完毕,文件名为:%s.bak'%(file1,file1) os.system(shell)def check_file_bak_writefile(self,file1,shell): if os.path.exists('%s.bak'%(file1)): print 'INFO:::%s.bak备份存在,不再备份'%(file1) else: os.system('cp %s %s.bak'%(file1,file1)) print 'INFO:::%s备份完毕,文件名为:%s.bak'%(file1,file1) self.file_write(file1,shell)def grep_image(self,depname): lines = os.popen("docker images |grep '%s' > %s"%(depname,self.TmpFile)).read().strip() check_dir(self.TmpFile) with open(self.TmpFile) as f: for line in f: REPOSITORY,TAG,IMAGEID,CREAT1,CREAT2,CREAT3,SIZE = map(str,line.split()) Image=REPOSITORY+':'+TAG return Image def install_docker(self): check_dir(self.DockerPath) os.chdir(self.DockerPath) print 'INFO:::yum docker 开始' os.system('yum install -y ./* >/dev/null 2>&1') #os.system('yum install -y ./* ') print 'INFO:::yum docker 完毕' os.chdir(self.RootPath+'/docker') check_dir('daemon.json') os.system('mkdir -p /etc/docker/ && \cp daemon.json /etc/docker/') check_dir('/usr/lib/systemd/system/docker.service') shell = r"sed -i '/ExecStart/i\ExecStartPost=\/bin\/bash \/root\/onDockerStart.sh' /usr/lib/systemd/system/docker.service" self.check_file_bak_shell("/usr/lib/systemd/system/docker.service",shell) os.system('\cp onDockerStart.sh /root/ && chmod +x /root/onDockerStart.sh && systemctl daemon-reload') self.check_file_bak_shell('/root/.bashrc','cat /root/deployment/docker/bash_docker.sh >> /root/.bashrc') os.system('service docker start') print 'INFO:::docker安装完毕'def init_yy(self): try: os.system('docker network create --subnet=172.21.0.0/16 huoshu 2> /dev/null') except : pass finally: print 'INFO:::huoshu网段设置完毕' AA = list_file('%s/*.tar'%self.SoftwarePath) print 'INFO:::开始load image镜像' for i in AA: os.system('docker load < %s'%i) check_dir(self.SoftwarePath+'/drgsburster') check_dir(self.SoftwarePath+'/front') print 'INFO:::配置drgsburster文件夹' os.system('\cp -r %s/drgsburster /opt'%self.SoftwarePath) print 'INFO:::配置front文件夹' os.system('mkdir -p /opt/apps/ && \cp -r %s/front /opt/apps/'%self.SoftwarePath)def init_db(self): AA = list_file('%s/*.tar'%self.SoftwareOraclePath) print 'INFO:::开始load image镜像' for i in AA: os.system('docker load < %s'%i) AA = list_file('%s/*.tar.gz'%self.SoftwareOraclePath) for i in AA: print 'INFO:::开始拷贝%s数据文件至/home'%i os.system('\cp %s /home/'%i) print 'INFO:::开始解压oracle数据文件' os.system('rm -rf /home/oracle') os.system('cd /home/ && tar -zxvf %s >/dev/null 2>&1' %i)def start_yy(self): NginxImage = self.grep_image('nginx') print 'INFO:::docker run 启动:%s'%NginxImage os.system('docker stop nginx 2>/dev/null && docker rm nginx 2>/dev/null') os.system("docker run -d --restart=always --net huoshu --ip=172.21.0.2 \
-p 80-120:80-120 -v /opt/apps/front:/opt/apps \
--name nginx -v /opt/apps/front/nginx.conf.d:/etc/nginx/conf.d \
-v /var/log/nginx:/var/log/nginx \
%s"%NginxImage)
RedisImage = self.grep_image('redis ') print 'INFO:::docker run 启动:%s'%RedisImage os.system('docker stop redis 2>/dev/null && docker rm redis 2>/dev/null') os.system("docker run -d --restart=always \
--net huoshu --ip=172.21.0.3 -p 6379:6379 \
--name redis \
-v /var/log/redis:/var/log/redis \
%s"%RedisImage)
Redis4Image = self.grep_image('redis4') print 'INFO:::docker run 启动:%s'%Redis4Image os.system('docker stop redis4 2>/dev/null && docker rm redis4 2>/dev/null') os.system("docker run -d --restart=always \
--net huoshu --ip=172.21.1.17 -p 7379:7379 \
--name redis4 \
-v /var/log/redis4:/var/log/redis \
%s"%RedisImage)
HdcImage = self.grep_image('hdc-manager') print 'INFO:::docker run 启动:%s'%HdcImage os.system('docker stop hdc-manager 2>/dev/null && docker rm hdc-manager 2>/dev/null') os.system("docker run -d --restart=always --name hdc-manager \
--net huoshu --ip=172.21.1.2 -p 8180:8080 \
-v /var/log/hdc-manager:/usr/local/apache-tomcat-8.5.4/logs \
-e ORACLE_ADDR=%s:1521:orcl \
-e LANG='en_US.UTF-8' \
-e HDC_ADDR=%s \
-e REDISPWD=%s \
-e PINPOINT=NO \
%s"%(self.db_ip,self.yy_ip,self.redispw,HdcImage))
EtlImage = self.grep_image('etlloader') print 'INFO:::docker run 启动:%s'%EtlImage os.system('docker stop etlloader 2>/dev/null && docker rm etlloader 2>/dev/null') os.system("docker run -d --restart=always --name etlloader \
--net huoshu --ip 172.21.1.4 -p 8380:8080 \
-e ORACLE_ADDR=%s:1521:orcl \
-e LANG='en_US.UTF-8' \
-e HDC_ADDR=%s \
-e REDISPWD=%s \
-e PINPOINT=NO \
-v /var/log/etlloader:/opt/etlloader/consolelog \
%s"%(self.db_ip,self.yy_ip,self.redispw,EtlImage))
CasImage = self.grep_image('cas') print 'INFO:::docker run 启动:%s'%CasImage os.system('docker stop cas 2>/dev/null && docker rm cas 2>/dev/null') os.system("docker run -d --restart=always --name cas \
--net huoshu --ip=172.21.1.6 -p 8480:8080 \
-v /var/log/cas:/usr/local/apache-tomcat-8.5.4/logs \
-e ORACLE_ADDR=%s:1521:orcl \
-e LANG='en_US.UTF-8' \
-e DBUSER=portal \
-e DBPWD=portal \
-e REDISPWD=%s \
%s"%(self.db_ip,self.redispw,CasImage))
DrgSmsImage = self.grep_image('drgsms') print 'INFO:::docker run 启动:%s'%DrgSmsImage os.system('docker stop drgsms 2>/dev/null && docker rm drgsms 2>/dev/null') os.system("docker run -d --restart=always --name drgsms \
--net huoshu --ip 172.21.1.21 -p 8996:9996 \
-e ORACLE_ADDR=%s:1521:orcl \
-e LANG='en_US.UTF-8' \
-e HDC_ADDR=%s \
-e REDISPWD=%s \
-e PINPOINT=NO \
-v /var/log/drgsms:/opt/drgsms/log \
%s" % (self.db_ip,self.yy_ip,self.redispw,DrgSmsImage))
DrgbursterImage = self.grep_image('drgburster') print 'INFO:::docker run 启动:%s'%DrgbursterImage os.system('docker stop drgburster 2>/dev/null && docker rm drgburster 2>/dev/null') os.system("docker run -d --restart=always --name drgburster \
--net huoshu --ip 172.21.1.31 -p 10668:10668 \
-e LANG='en_US.UTF-8' \
-v /opt/drgsburster:/opt/drgsburster \
-v /var/log/drgburster/:/opt/drgburster/log \
%s" % DrgbursterImage)
def start_db(self): OracleImage = self.grep_image('oracle') print 'INFO:::docker run 启动:%s'%OracleImage os.system('docker stop oracle 2>/dev/null && docker rm oracle 2>/dev/null') os.system("docker run -d --privileged --restart=always \
--shm-size 2147483648 \
--name oracle \
-p 1521:1521 \
-v /home/oracle:/opt/oracle \
%s"%OracleImage)
print 'INFO:::开启数据库'
os.system('/root/onDockerStart.sh >/dev/null 2>&1')
print 'INFO:::数据库启动完成'
def start_db_one_server(self): OracleImage = self.grep_image('oracle') print 'INFO:::docker run 启动:%s'%OracleImage os.system('docker stop oracle 2>/dev/null && docker rm oracle 2>/dev/null') os.system("docker run -d --privileged --restart=always \
--net huoshu --ip=172.21.0.4 \
--shm-size 2147483648 \
--name oracle \
-p 1521:1521 \
-v /home/oracle:/opt/oracle \
%s"%OracleImage)
print 'INFO:::开启数据库'
os.system('/root/onDockerStart.sh >/dev/null 2>&1')
print 'INFO:::数据库启动完成'
def clear(self): print 'INFO:::关闭并删除所有镜像' os.system('docker kill $(docker ps -q) >/dev/null 2>&1 ; docker rm $(docker ps -a -q) >/dev/null 2>&1;') print 'INFO:::清理文件' list_dir = ['/var/log/nginx','/var/log/redis','/var/log/redis4','/var/log/hdc-manager','/var/log/etlloader' \
,'/var/log/cas','/var/log/drgsms','/var/log/drgburster','/opt/apps/','/root/onDockerStart.sh','/home/oracle','/home/oracle.*.tar.gz']
for i in list_dir:
os.system('rm -rf %s'%i)
print 'INFO:::还原docker开机启动'
if check_dir('/usr/lib/systemd/system/docker.service.bak'):
pass
else:
os.system('\cp /usr/lib/systemd/system/docker.service.bak /usr/lib/systemd/system/docker.service')
os.system('systemctl daemon-reload')
if name == 'main':
if len(sys.argv) == 4:
yy_ip = sys.argv[1]
db_ip = sys.argv[2]
dep = sys.argv[3]
hs = HS(yy_ip,db_ip)
if dep == 'install_yy':
hs.install_docker()
hs.init_yy()
hs.start_yy()
elif dep == 'install_db':
hs.install_docker()
hs.init_db()
if db_ip == yy_ip:
hs.start_db_one_server()
else:
hs.start_db()
elif dep == 'clear': hs.clear() else: print 'Usage:( ./dep.py 应用ip 数据库ip install_yy or install_db or clear)'else: print 'Usage:( ./dep.py 应用ip 数据库ip install_yy or install_db or clear)' exit(1)