Tomcat Grayscale release shell script (from the network can be referenced)

Source: Internet
Author: User
Tags set time haproxy

Java Grayscale Script

Use scripting considerations:
1, the publishing machine needs to be able to resolve the Web server hostname, and configure SSH communication
2, the variables in the directory and users and other information needs to create their own, the script does not make judgments to create their own. My Web server is deployed using Ansible, and the relevant directories and users are created automatically.
3. The code is deployed using tag, but the code update is controlled using a soft connection, and the rollback switches to the previous soft connection
4, because Java is a compiled language, we use MAVEN to compile, so we need to install the MAVEN environment.
5, about the Environment profile: the configuration file for their own manual maintenance, each time is to delete the Git repository pull down the configuration file, the corresponding environment of the code files copied into the compilation directory to compile.

#!/bin/bash

# Set Time variables
ctime=$ (date "+%y-%m-%d-%h-%m")
# project name, proposal and Gitlab warehouse name consistent
project=
# Local Code directory (Gitlab in the directory after the replacement code)
code_dir=/data/gitlab/"$project"
# Temporary code directory for modifying configuration files and compiling packaged code
tmp_dir=/data/tmp/"$project"
# used to store war packages
war_dir=/data/war/"$project"
# corresponding Environment configuration file
deploy_conf=/data/conf/pro/"$project"/*
# configuration file path in code
local_conf= $TMP _dir/src/main/resources/config
# Remote Host Name
remote_host= "Tomcat-01 tomcat-02"
# Remote Host Code directory
remote_code_dir=/data/webapps/"$project"
# Remote Host User
Remote_user=root
# Remote host War Package storage directory
remote_war_dir=/data/war/
# Code TEMP Directory
code_tmp=/data/code_tmp/
# Online Log
Depkoy_log=/data/log/pro_log.log

# Script Use Help
Usage () {
echo $ "Usage: $ [Deploy tag | rollback_list | Rollback_pro ver]"
}

# Pull the replacement code
Git_pro () {
If [$#-lt 1];then
echo "please pass in tag"
Exit 1
Fi
Tag=$1
CD $CODE _dir && git checkout master && git pull && git checkout
If [$?! = 0];then
echo "Pull substitution code failed"
Exit 10
Fi
CD $CODE _dir && git pull 2>/dev/null >/dev/null
# Push Code to TEMP directory
Rsync-avz--delete $CODE _dir/$TMP _dir/2>/dev/null >/dev/null
}

# Set up the code configuration file
Config_pro () {
echo "Setup code profile"
Rm-f $local _conf/config.properties
Rm-f $local _conf/alipay.properties
Rm-f $local _conf/jdbc.properties
Rm-f $local _conf/log4j.properties
CP $deploy _conf $local _conf/
}

# Packing Code
Tar_pro () {
echo "Local Packaging code"
CD $TMP _dir &&/usr/local/maven/bin/mvn Clean compile War:war && cp target/"$project". War "$WAR _dir"/"$ Project "_" $tag "_" $CTIME ". War
}

# Push the war package to the remote server
Rsync_pro () {
echo "Push War package to remote server"
For host in $REMOTE _host;do
SCP "$WAR _dir"/"$project" _ "$tag" _ "$CTIME". WAR [email protected] $host: $REMOTE _war_dir
Done
}

# Unzip the code package
Solution_pro () {
echo "Extract code Package"
For host in $REMOTE _host;do
SSH [email protected] $host "unzip" $REMOTE _war_dir "" $project "_" $tag "_" $CTIME ". war-d" $CODE _tmp "" $project "_" $tag "_" $ CTIME "" 2>/dev/null >/dev/null
Done
}

# API Testing
Test_pro () {
# Run the API test script and exit the deployment if there is a problem with the API test
If [$?! = 0];then
echo "API test has a problem, exit Deployment"
Exit 10
Fi
}


# Deployment Code
Deploy_pro () {
echo "Deployment Code"
For host in $REMOTE _host;do
SSH haproxy "echo" Disable server $project/$host "| /usr/bin/socat/var/lib/haproxy/stats stdio "
SSH [email protected] $host "Rm-r $REMOTE _code_dir"
SSH [email protected] $host "ln-s" $CODE _tmp "" $project "_" $tag "_" $CTIME "/$REMOTE _code_dir"
echo "Restart $host"
SSH [email protected] $host "/etc/init.d/tomcat Restart"
Sleep 3
# Execute API Test
Test_pro
SSH Haproxy "echo" Enable server $project/$host "| /usr/bin/socat/var/lib/haproxy/stats stdio "
Done
}
# list versions that can be rolled back
Rollback_list () {
echo "------------Roll back version-------------"
SSH [email protected] $REMOTE _host "ls-r" $CODE _tmp "| Grep-o $project. * "
}

# rollback Code
Rollback_pro () {
echo "in Rollback"
For host in $REMOTE _host;do
SSH haproxy "echo" Disable server $project/$host "| /usr/bin/socat/var/lib/haproxy/stats stdio "
SSH [email protected] $host "RM-RF $REMOTE _code_dir"
SSH [email protected] $host "ln-s" $CODE _tmp "$1/$REMOTE _code_dir"
SSH [email protected] $host "/etc/init.d/tomcat Restart"
Sleep 3
SSH Haproxy "echo" Enable server $project/$host "| /usr/bin/socat/var/lib/haproxy/stats stdio "
Done
}

# Record Log
Record_log () {
echo "$CTIME Host: $REMOTE _host project: $project tag:$1" >> $DEPKOY _log
}

# Code Execution Option settings
Main () {
Case $ in
Deploy
Git_pro;
Config_pro;
Tar_pro;
Rsync_pro;
Solution_pro;
Deploy_pro;
Record_log;
;;
Rollback_list)
Rollback_list;
;;
Rollback_pro)
Rollback_pro;
Record_log;
;;
*)
Usage
Esac
}
Main $ $

Tomcat Grayscale release shell script (from the network can be referenced)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.