1. 程式人生 > >CentOS6u8 java和tomcat多版本模板的ansible批量部署實現(四)

CentOS6u8 java和tomcat多版本模板的ansible批量部署實現(四)

業務部署:

192.168.77.100操作:

su - deploy
mkdir -pv web_pro_env/{inventory,roles}
# 配置被操控的主機資訊
cat >web_pro_env/inventory/proenv<<EOF
192.168.77.200
[Server]
192.168.77.200
EOF

# 建立web_pro角色,實現業務賬號部署和專案部署
mkdir -pv web_pro_env/roles/web_pro/{files,templates,tasks}
cat >web_pro_env/roles/web_pro/templates/web_pro_clear.sh.j2<<
EOFALL #!/bin/bash source ~/.bash_profile if [ -f /var/spool/cron/web_pro ] then rm -rf /var/spool/cron/web_pro for Pid in \$(ps -ef|grep '^web_pro '|grep -v grep|awk '{print \$2}');do kill -9 \${Pid};done fi if [ -d /web ] then chattr -i -R /web rm -rf /web fi grep -q ^web_pro /etc/passwd if [ \$? -eq 0 ] then userdel -r web_pro fi sed -i '/^web_pro.*/d' /etc/security/limits.conf sed -i '/^session required pam_limits.so$/d' /etc/pam.d/login EOFALL
cat >web_pro_env/roles/web_pro/templates/web_pro_useradd.sh.j2<<EOFALL #!/bin/bash source ~/.bash_profile useradd -u 8080 web_pro echo web_pro|passwd --stdin web_pro echo 'web_pro soft nproc 2047'>>/etc/security/limits.conf echo 'web_pro hard nproc 16384'>>/etc/security/limits.conf echo 'web_pro soft nofile 1024'>>/etc/security/limits.conf echo 'web_pro hard nofile 65536'>>/etc/security/limits.conf echo 'web_pro soft stack 10240'>>/etc/security/limits.conf echo 'web_pro hard stack 32768'>>/etc/security/limits.conf echo 'session required pam_limits.so'>>/etc/pam.d/login EOFALL
cat >web_pro_env/roles/web_pro/templates/web_pro_checkTOMCAT.sh.j2<<EOFALL #!/bin/bash source ~/.bash_profile mkdir -pv /web/{profile,project,logs,checkTOMCAT} chown web_pro: -R /web chmod 750 -R /web cd /web/checkTOMCAT cat>checktomcat.sh<<EOF #!/bin/bash source /etc/profile # 工作目錄 CPWD=\$(dirname \${0}) # 配置表單 CHKLIST=\${CPWD}/checktomcat.lst # 超時對比檔案 TIMESTANDARD=\${CPWD}/checktomcat.tsd # 自動拉起行為日誌 CHECKLOG=\${CPWD}/checktomcat_log.txt # 指令碼執行日誌 RUNLOG=\${CPWD}/checktomcat_run_\$(date +%F).log echo "\$(date +%F.%T) Check file \${CHKLIST}">>\${RUNLOG} # 處理過程: while read line do if [ "\${line:0:1}" == "#" -o "\${line}" == "" ] then continue # 如果配置表單為註釋行或者空行,則忽略 fi # 標誌性專案名:BIN目錄:統計程序數專案名:啟動命令:監控日誌:日誌超時時間 # 標誌性專案名 便於人工識別專案 # 統計程序數專案名 唯一定義該專案的程序 不能有歧義 VNAME=\$(echo \${line}|awk -F ':' '{print \$1}') VWORKDIR=\$(echo \${line}|awk -F ':' '{print \$2}') VPROC=\$(echo \${line}|awk -F ':' '{print \$3}') VSTART=\$(echo \${line}|awk -F ':' '{print \$4}') VLOG=\$(echo \${line}|awk -F ':' '{print \$5}') VTIME=\$(echo \${line}|awk -F ':' '{print \$6}') # 如果配置表單該行BIN目錄不存在,則打日誌並忽略該行處理 if [ -d "\${VWORKDIR}" ] then cd "\${VWORKDIR}" else echo "\$(date +%F.%T) WARNNING \${VWORKDIR} is not exists, check \${VNAME} is skip...">>\$CHECKLOG continue fi # 當前該專案的程序數量 PROCCOUNTS=\$(ps -ef|grep "\${VPROC}"|grep java|grep -v "grep"|wc -l) # 注意:\${VPROC}必須能夠唯一標識該專案的程序 # 如果當前該專案的程序數量為0,則拉起該專案 if [ "\${PROCCOUNTS}" == "0" ] then echo \${VNAME} not running, restarted.>>\${CHECKLOG} echo "\$(date +%F.%T) \${VNAME} not running">>\${CHECKLOG} echo "\$(date +%F.%T) \${VSTART}">>\${CHECKLOG} \${VSTART} & continue fi # 日誌超時檢測 if [ "\${VTIME}" != "0" ] then # 重新整理對比檔案的時間戳 touch -t \$(date -d "-\${VTIME} second" +"%Y%m%d%H%M.%S") \${TIMESTANDARD} # 對比日誌是否比對比檔案時間戳新 LOGFILECOUNTS=\$(find \${VLOG} -newer \${TIMESTANDARD}|wc -l) if [ "\${LOGFILECOUNTS}" == "0" ] then echo "\${VLOG} is not exists or is expired \${VTIME} second.">>\${CHECKLOG} echo "\$(date +"%F.%T") \${VNAME} logfile is expired \${VTIME} second">>\${CHECKLOG} echo "\$(date +"%F.%T") \${VSTART}">>\${CHECKLOG} touch \${VLOG} for i in \$(ps -ef|grep "\${VPROC}"|grep java|grep -v "grep"|awk '{print \$2}') do kill -9 \${i} done # 殺掉該專案的所有程序 \${VSTART} & continue fi fi done<\${CHKLIST} cd \${CPWD} EOF chmod 700 checktomcat.sh # 建立配置表單 echo '# TOMCAT_NAME:BIN_PATH:PROCESS_NAME:START_SCRIPT:CHECK_LOG:LOG_TIMEOUT:PORT_OFFSET'>checktomcat.lst # 生效自動任務 >/var/spool/cron/web_pro chown web_pro: /var/spool/cron/web_pro chmod 0600 /var/spool/cron/web_pro echo "# Check TOMCAT Process" >>/var/spool/cron/web_pro echo "*/1 * * * * /web/checkTOMCAT/checktomcat.sh> /dev/null 2>&1" >>/var/spool/cron/web_pro echo "*/1 * * * * sleep 10;/web/checkTOMCAT/checktomcat.sh> /dev/null 2>&1" >>/var/spool/cron/web_pro echo "*/1 * * * * sleep 20;/web/checkTOMCAT/checktomcat.sh> /dev/null 2>&1" >>/var/spool/cron/web_pro echo "*/1 * * * * sleep 30;/web/checkTOMCAT/checktomcat.sh> /dev/null 2>&1" >>/var/spool/cron/web_pro echo "*/1 * * * * sleep 40;/web/checkTOMCAT/checktomcat.sh> /dev/null 2>&1" >>/var/spool/cron/web_pro echo "*/1 * * * * sleep 50;/web/checkTOMCAT/checktomcat.sh> /dev/null 2>&1" >>/var/spool/cron/web_pro echo "0 3 * * * /usr/bin/find /web/checkTOMCAT/checktomcat_run_*.log -type f -mtime +30 -exec rm -rf {} \;" >>/var/spool/cron/web_pro chown web_pro: -R /web EOFALL sed -i 's/\$/\\$/g' web_pro_env/roles/web_pro/templates/web_pro_checkTOMCAT.sh.j2 cat >web_pro_env/roles/web_pro/templates/web_pro_pro_deploy.sh.j2<<EOFALL #!/bin/bash source ~/.bash_profile cd /web/checkTOMCAT cat >pro_deploy.sh<<EOF #!/bin/bash source ~/.bash_profile # JAVA環境目錄 JAVA_ENV=/usr/local/java # TOMCAT模板目錄 TOMCAT_ENV=/usr/local/tomcat # 例項目錄 IPWD=/web # 工作目錄 CPWD=\$(dirname \${0}) # 配置表單 CHKLIST=\${CPWD}/checktomcat.lst # 執行幫助 if [ "\$#" -ne 6 ] then echo \$"Usage: bash \$(basename \$0) -n PRO_NAME -j JAVA_VERSION -t TOMCAT_VERSION" echo \$"Example: bash \$(basename \$0) -n vincent_test -j java_1.6 -t tomcat6" echo \$"JAVA_VERSION can be java_1.6/java_1.7/java_1.8" echo \$"TOMCAT_VERSION can be tomcat6/tomcat7/tomcat8" exit 1 fi while [ "\$#" -gt 0 ] do case "\$1" in -n) shift typeset -l PRO_NAME="\${1}" shift ;; -j) shift typeset -l JAVA_VERSION="\${1}" shift ;; -t) shift typeset -l TOMCAT_VERSION="\${1}" shift ;; esac done # 埠偏移 PORT_OFFSET=\$(awk -F':' '{if(\$NF~/[0-9]+/) print \$NF}' \${CHKLIST}|wc -l) # 例項名稱 TOMCAT_NAME=\${TOMCAT_VERSION}_\$((8080+\$PORT_OFFSET))_\${PRO_NAME} # 例項複製 cp -a \${TOMCAT_ENV}/\${TOMCAT_VERSION}/ \${IPWD}/\${TOMCAT_NAME} # 配置檔案修改 sed -i "s|#!/bin/sh|&\nsource \${JAVA_ENV}/\${JAVA_VERSION}_env|g" \${IPWD}/\${TOMCAT_NAME}/bin/catalina.sh sed -i "s/tomcat.*\$/\${TOMCAT_NAME}/g" \${IPWD}/\${TOMCAT_NAME}/bin/cat.sh sed -i "s/18080/\$((18080+\$PORT_OFFSET))/g" \${IPWD}/\${TOMCAT_NAME}/conf/server.xml sed -i "s/8080/\$((8080+\$PORT_OFFSET))/g" \${IPWD}/\${TOMCAT_NAME}/conf/server.xml sed -i "s/9443/\$((9443+\$PORT_OFFSET))/g" \${IPWD}/\${TOMCAT_NAME}/conf/server.xml sed -i "s/28080/\$((28080+\$PORT_OFFSET))/g" \${IPWD}/\${TOMCAT_NAME}/conf/server.xml sed -i "s/tomcat[678]/\${TOMCAT_NAME}/g" \${IPWD}/\${TOMCAT_NAME}/conf/server.xml # 配置表單新增 echo "\${PRO_NAME}:\${IPWD}/\${TOMCAT_NAME}/bin:\${TOMCAT_NAME}:./startup.sh:CHECK_LOG:0:\${PORT_OFFSET}">>\${CHKLIST} # 新增日誌日切任務 crontab -l>/tmp/web_pro_crontab.txt echo "# \${TOMCAT_NAME} DAILY LOG ARCHIVE" >>/tmp/web_pro_crontab.txt echo "0 3 * * * /bin/bash /web/\${TOMCAT_NAME}/bin/cat.sh">>/tmp/web_pro_crontab.txt cat /tmp/web_pro_crontab.txt |crontab rm -rf /tmp/web_pro_crontab.txt # 生成測試頁面: mkdir -p /web/project/\${TOMCAT_NAME}/ROOT echo "\$(hostname -i):\${TOMCAT_NAME}">>/web/project/\${TOMCAT_NAME}/ROOT/index.html echo "curl http://\$(hostname -i):\$((8080+\$PORT_OFFSET))/index.html" EOF chmod +x pro_deploy.sh chown web_pro: -R /web chattr +i /web/tomcat*/bin chattr +i /web/tomcat*/conf chattr +i /web/tomcat*/lib cd /web/checkTOMCAT/ chattr +i *.sh EOFALL sed -i 's/\$/\\$/g' web_pro_env/roles/web_pro/templates/web_pro_pro_deploy.sh.j2 cat >web_pro_env/roles/web_pro/tasks/main.yml<<EOF - name: rsync web_pro_clear.sh template: src=roles/web_pro/templates/web_pro_clear.sh.j2 dest=/tmp/web_pro_clear.sh - name: rsync web_pro_useradd.sh template: src=roles/web_pro/templates/web_pro_useradd.sh.j2 dest=/tmp/web_pro_useradd.sh - name: rsync web_pro_checkTOMCAT.sh template: src=roles/web_pro/templates/web_pro_checkTOMCAT.sh.j2 dest=/tmp/web_pro_checkTOMCAT.sh - name: rsync web_pro_pro_deploy.sh template: src=roles/web_pro/templates/web_pro_pro_deploy.sh.j2 dest=/tmp/web_pro_pro_deploy.sh - name: clear web_pro command: "bash /tmp/web_pro_clear.sh" - name: useradd web_pro command: "bash /tmp/web_pro_useradd.sh" - name: web_pro add checkTOMCAT command: "bash /tmp/web_pro_checkTOMCAT.sh" - name: web_pro add pro_deploy command: "bash /tmp/web_pro_pro_deploy.sh" EOF cat >web_pro_env/deploy.yml<<EOF - hosts: "Server" gather_facts: true remote_user: root roles: - web_pro EOF cd web_pro_env ansible-playbook -i inventory/proenv ./deploy.yml cd

登陸192.168.77.200進行測試:

su - web_pro
cd /web/checkTOMCAT
bash pro_deploy.sh -n test1 -j java_1.6 -t tomcat6
bash pro_deploy.sh -n test2 -j java_1.7 -t tomcat7
bash pro_deploy.sh -n test3 -j java_1.8 -t tomcat8

[TOC]