go.sh部署脚本适用于全新服务器(纯净的服务器 无其他项目/数据库)
功能简介
- 自动安装Docker
- 自动安装Mysql8.0.27(docker版)
- 自动初始化shopxxb2b2c数据库
- 自动安装JVM并配置环境变量
- 自动组装项目运行所需的外部资源文件(删除多余文件)
- 自动启动
- 自动获取服务器公网IP(部署所需)
- 自动获取Jar名称(部署所需 请确保root目录下的文件中有且仅有一个所需部署的Jar包 请勿随意更改Jar名称 如非改不可 请同时修改go.sh中的JAR_NAME_FRAGMENT属性-Jar名称片段字符串 用于模糊匹配检查Jar是否存在)
一 上传文件到服务器root目录
- go.sh
- Jar文件
二 运行
sh /root/go.sh
go.sh
#!/bin/bash -e
APPLICATION_SERVER_PORT="80"
DATABASE="shopxxb2b2c"
MYSQL_PORT="3306"
MYSQL_ROOT_PAASWORD='Shopxx1234'
# BINDS_CONTAINER_PATH(外部资源位置) 前后不要带/符号
BINDS_CONTAINER_PATH='application'
MYSQL_IMAGE_VERSION="8.0.27"
MYSQL_CONTAINER_NAME="shopxx-b2b2c-mysql"
JAR_NAME_FRAGMENT='shopxx-b2b2c'
DEMO_IMAGE_URL_PREFIX='https://image.demo.b2b2c.shopxx.net/9.0'
INIT_SQL_SOURCE='init.ftl'
INIT_SQL='mysql.sql'
SHOPXX_B2B2C_DEPLOYMENT_INFO='shopxx-b2b2c-deployment-info.txt'
JAR_RESTART_SH='jarStart.sh'
SLEEP="30s"
start=$(date +%s)
function getDir() {
for item in `ls $1`
do
fileName=$1"/"$item
if [ -f $fileName ];then
echoFile $fileName
fi
done
}
function echoFile() {
fileName=$1
if [ ${fileName##*.} = jar ] && [[ $1 =~ $JAR_NAME_FRAGMENT ]];then
jarName=$(basename $1)
fi
}
dir="/root"
getDir $dir
if [[ -z $jarName ]];then
echo "[Warning] Jar not found or Jar name does not contain $JAR_NAME_FRAGMENT"
exit 8
fi
ecsUrl=$(curl ifconfig.co)
if [[ -z $ecsUrl ]];then
echo "[Warning] ECS URL is empty"
exit 8
fi
if [ ! -f "/root/$jarName" ]; then
echo "[Warning] Resource /root/$jarName not found"
exit 8
fi
yum install lsof -y
lsof -i :$MYSQL_PORT
if [ $? -eq 0 ]; then
echo "[Warning] $MYSQL_PORT use exception"
exit 8
fi
docker -v
if [ $? -eq 0 ]; then
echo "Docker has been installed"
else
echo "Init docker================================================================================================================================"
COUNTRY="CN"
STATE="shanghai"
CITY="shanghai"
ORGANIZATION="shanghai"
ORGANIZATIONAL_UNIT="Dev"
EMAIL="service@shopxx.net"
TIMEOUT=3
ECSURL=$ecsUrl
ECSPassword=Shopxx1234~
yum update -y
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
systemctl start docker
systemctl enable docker
echo '{
"registry-mirrors": [
"https://afjba8p7.mirror.aliyuncs.com",
"https://reg-mirror.qiniu.com/",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
],
"live-restore": true
}' >> /etc/docker/daemon.json
systemctl daemon-reload
systemctl restart docker
rm -rf /docker/ca
mkdir -p /docker/ca
cd /docker/ca
openssl genrsa -aes256 -passout "pass:$ECSPassword" -out ca-key.pem 2048
openssl req -new -x509 -passin "pass:$ECSPassword" -days 3650 -key ca-key.pem -sha256 -out ca.pem -subj "/C=$COUNTRY/ST=$STATE/L=$CITY/O=$ORGANIZATION/OU=$ORGANIZATIONAL_UNIT/CN=$ECSURL/emailAddress=$EMAIL"
openssl genrsa -out server-key.pem 2048
openssl req -subj "/CN=$ECSURL" -new -key server-key.pem -out server.csr
echo "subjectAltName=IP:${ECSURL},IP:0.0.0.0" >> extfile.cnf
echo "extendedKeyUsage=serverAuth" >> extfile.cnf
openssl x509 -req -days 3650 -in server.csr -CA ca.pem -CAkey ca-key.pem -passin "pass:$ECSPassword" -CAcreateserial -out server-cert.pem -extfile extfile.cnf
openssl genrsa -out key.pem 2048
openssl req -subj '/CN=client' -new -key key.pem -out client.csr
sh -c 'echo "extendedKeyUsage=clientAuth" > extfile.cnf'
openssl x509 -req -days 3650 -in client.csr -CA ca.pem -CAkey ca-key.pem -passin "pass:$ECSPassword" -CAcreateserial -out cert.pem -extfile extfile.cnf
chmod 0400 ca-key.pem key.pem server-key.pem
chmod 0444 ca.pem server-cert.pem cert.pem
rm -v client.csr server.csr
sed -i "s|\(ExecStart=/usr/bin/dockerd\).*\(-H fd:// --containerd=/run/containerd/containerd.sock\)|\1 --tlsverify --tlscacert=/docker/ca/ca.pem --tlscert=/docker/ca/server-cert.pem --tlskey=/docker/ca/server-key.pem -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \2|" /usr/lib/systemd/system/docker.service
sed -i "s|\(ExecStart=/usr/bin/dockerd-current\).*\(\\)|\1 --tlsverify --tlscacert=/docker/ca/ca.pem --tlscert=/docker/ca/server-cert.pem --tlskey=/docker/ca/server-key.pem -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \2|" /usr/lib/systemd/system/docker.service
ln -s /usr/libexec/docker/docker-runc-current /usr/bin/docker-runc
systemctl daemon-reload
systemctl restart docker.service
fi
cd /
docker inspect $MYSQL_CONTAINER_NAME -f '{{.Name}}' > /dev/null
if [ $? -eq 0 ] ;then
docker start $MYSQL_CONTAINER_NAME
else
echo "Init mysql docker container================================================================================================================"
docker pull mysql:$MYSQL_IMAGE_VERSION
docker run -d --name $MYSQL_CONTAINER_NAME \
-v /data:/var/lib/mysql \
-p $MYSQL_PORT:3306 \
-e MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PAASWORD \
mysql:$MYSQL_IMAGE_VERSION \
--lower_case_table_names=1
if ss -nutlp | grep -w ":$MYSQL_PORT\b" &> /dev/null;then
echo "$MYSQL_CONTAINER_NAME container create succeed"
else
echo "[Warning] $MYSQL_CONTAINER_NAME container create failed" && exit 1
fi
echo "MysqlRootPaasword:$MYSQL_ROOT_PAASWORD"
echo "Sleep $SLEEP to wait init mysql docker container"
sleep $SLEEP
fi
echo "Build resources================================================================================================================================"
yum install unzip -y
cd /
mkdir -p $BINDS_CONTAINER_PATH/resources
mv /root/$jarName /$BINDS_CONTAINER_PATH
cd /$BINDS_CONTAINER_PATH
unzip $jarName
cd /
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/templates /$BINDS_CONTAINER_PATH/resources
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/static /$BINDS_CONTAINER_PATH/resources
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/language /$BINDS_CONTAINER_PATH/resources
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/application-prod.yml /$BINDS_CONTAINER_PATH/resources
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/application.yml /$BINDS_CONTAINER_PATH/resources
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/ehcache.xml /$BINDS_CONTAINER_PATH/resources
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/library.properties /$BINDS_CONTAINER_PATH/resources
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/shopxx.xml /$BINDS_CONTAINER_PATH/resources
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/init-sql/mysql/$INIT_SQL_SOURCE /$BINDS_CONTAINER_PATH
mv /$BINDS_CONTAINER_PATH/BOOT-INF/classes/library /$BINDS_CONTAINER_PATH
rm -rf /$BINDS_CONTAINER_PATH/BOOT-INF
rm -rf /$BINDS_CONTAINER_PATH/META-INF
rm -rf /$BINDS_CONTAINER_PATH/org
echo "Build mysql init sql==========================================================================================================================="
cd /$BINDS_CONTAINER_PATH
find -name $INIT_SQL_SOURCE | xargs perl -pi -e 's|\$\{demoImageUrlPrefix\}|'$DEMO_IMAGE_URL_PREFIX'|g'
find -name $INIT_SQL_SOURCE | xargs perl -pi -e 's|<#noparse>||g'
find -name $INIT_SQL_SOURCE | xargs perl -pi -e 's|</#noparse>||g'
find -name $INIT_SQL_SOURCE | xargs perl -pi -e 's|<#if insertDemoData>||g'
find -name $INIT_SQL_SOURCE | xargs perl -pi -e 's|</#if>||g'
find -name $INIT_SQL_SOURCE | xargs perl -pi -e 's|\$\{md5\(adminPassword\)\}|21232f297a57a5a743894a0e4a801fc3|g'
find -name $INIT_SQL_SOURCE | xargs perl -pi -e 's|\$\{adminUsername\}|admin|g'
mv $INIT_SQL_SOURCE $INIT_SQL
echo "Init mysql database============================================================================================================================"
cd /
docker cp /$BINDS_CONTAINER_PATH/$INIT_SQL $MYSQL_CONTAINER_NAME:/tmp/
sudo su root -c "docker exec -i $MYSQL_CONTAINER_NAME bash << EOF
mysql -uroot -p$MYSQL_ROOT_PAASWORD
CREATE DATABASE $DATABASE DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE $DATABASE;
source /tmp/$INIT_SQL
EOF"
cd /
java -version
if [ $? -eq 0 ];then
echo 'JVM has been installed'
else
echo "Init mysql JVM============================================================================================================================="
yum install java-1.8.0-openjdk.x86_64 -y
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.322.b06-2.al8.x86_64
JRE_HOME=$JAVA_HOME/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
source /etc/profile
fi
jarStart="nohup java -jar $jarName --spring.profiles.active=prod --spring.config.location=/$BINDS_CONTAINER_PATH/resources/ --BINDS_CONTAINER_PATH=/$BINDS_CONTAINER_PATH --APPLICATION_SERVER_PORT=$APPLICATION_SERVER_PORT --APPLICATION_SPRING_DATASOURCE_PASSWORD=$MYSQL_ROOT_PAASWORD --APPLICATION_SPRING_DATASOURCE_URL=jdbc:mysql://localhost:$MYSQL_PORT/$DATABASE?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true &"
cd /$BINDS_CONTAINER_PATH
echo "Jar path:$BINDS_CONTAINER_PATH" > $SHOPXX_B2B2C_DEPLOYMENT_INFO
echo "$ecsUrl:$APPLICATION_SERVER_PORT" >> $SHOPXX_B2B2C_DEPLOYMENT_INFO
echo "$MYSQL_ROOT_PAASWORD(mysql root password)" >> $SHOPXX_B2B2C_DEPLOYMENT_INFO
echo "$DATABASE(mysql database)$MYSQL_PORT(mysql port)" >> $SHOPXX_B2B2C_DEPLOYMENT_INFO
echo "$MYSQL_CONTAINER_NAME(mysql docker container name)$MYSQL_IMAGE_VERSION(mysql version)" >> $SHOPXX_B2B2C_DEPLOYMENT_INFO
echo "Jar start instruction:" >> $SHOPXX_B2B2C_DEPLOYMENT_INFO
echo "$jarStart" >> $SHOPXX_B2B2C_DEPLOYMENT_INFO
echo "-------------------------Please save the following message------------------------------------"
echo " |"
cat $SHOPXX_B2B2C_DEPLOYMENT_INFO
echo " |"
echo "----------------------------------------------------------------------------------------------"
cd /$BINDS_CONTAINER_PATH
echo "#!/bin/bash -e" > $JAR_RESTART_SH
echo "" >> $JAR_RESTART_SH
echo "docker inspect $MYSQL_CONTAINER_NAME -f '{{.Name}}' > /dev/null" >> $JAR_RESTART_SH
echo "if [ $? -eq 0 ] ;then" >> $JAR_RESTART_SH
echo " docker start $MYSQL_CONTAINER_NAME" >> $JAR_RESTART_SH
echo "else" >> $JAR_RESTART_SH
echo " echo \"[Warning] Docker container $MYSQL_CONTAINER_NAME has missing, check the mysql docker container please!\"" >> $JAR_RESTART_SH
echo " exit 8" >> $JAR_RESTART_SH
echo "fi" >> $JAR_RESTART_SH
echo "cd /$BINDS_CONTAINER_PATH" >> $JAR_RESTART_SH
echo "sudo su root -c \"$jarStart\"" >> $JAR_RESTART_SH
echo "Jar start======================================================================================================================================"
if [ ! -d "/$BINDS_CONTAINER_PATH/library" ]; then
echo "[Warning] Resource /$BINDS_CONTAINER_PATH/library not found"
fi
cd /$BINDS_CONTAINER_PATH
sudo su root -c "$jarStart"
end=$(date +%s)
time=$(( end - start ))
echo "Shopxxb2b2c deployment completed!!! In $time seconds==========================================================================================="
cd /root
rm $0
Tips
部署完成后在Jar文件同级目录会生成:
- mysql初始化数据文件[mysql.sql] (用于初始化数据备份)
- shopxxb2b2c项目部署信息[shopxx-b2b2c-deployment-info.txt]
- Jar再启动脚本[jarStart.sh] (用于服务器重启后 启动Jar 注意 用go.sh(自带启动)部署项目时无需运行此脚本)