#!/usr/bin/env python
創(chuàng)新互聯(lián)是一家專業(yè)提供長(zhǎng)汀企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、HTML5建站、小程序制作等業(yè)務(wù)。10年已為長(zhǎng)汀眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。import glob, os,shutil,zipfile,socket,datetime,time,sys
def check_dir(filedir):
if os.path.exists(filedir):
pass
else:
print 'ERROR:::%s 目錄不存在,請(qǐng)檢測(cè)'%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網(wǎng)段設(shè)置完畢'
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數(shù)據(jù)文件至/home'%i
os.system('\cp %s /home/'%i)
print 'INFO:::開始解壓oracle數(shù)據(jù)文件'
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 啟動(dòng):%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 啟動(dòng):%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 啟動(dòng):%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 啟動(dòng):%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 啟動(dòng):%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 啟動(dòng):%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 啟動(dòng):%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 啟動(dòng):%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 啟動(dòng):%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:::開啟數(shù)據(jù)庫(kù)'
os.system('/root/onDockerStart.sh >/dev/null 2>&1')
print 'INFO:::數(shù)據(jù)庫(kù)啟動(dòng)完成'
def start_db_one_server(self):
OracleImage = self.grep_image('oracle')
print 'INFO:::docker run 啟動(dòng):%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:::開啟數(shù)據(jù)庫(kù)'
os.system('/root/onDockerStart.sh >/dev/null 2>&1')
print 'INFO:::數(shù)據(jù)庫(kù)啟動(dòng)完成'
def clear(self):
print 'INFO:::關(guān)閉并刪除所有鏡像'
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開機(jī)啟動(dòng)'
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 應(yīng)用ip 數(shù)據(jù)庫(kù)ip install_yy or install_db or clear)'
else:
print 'Usage:( ./dep.py 應(yīng)用ip 數(shù)據(jù)庫(kù)ip install_yy or install_db or clear)'
exit(1)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站名稱:初始化腳本-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://www.rwnh.cn/article8/ccigop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、營(yíng)銷型網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容