Linux通过Shell自动部署springboot
Posted lsc。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux通过Shell自动部署springboot相关的知识,希望对你有一定的参考价值。
需要提前配置jdk、git、maven、mysql环境,这里主要介绍maven环境配置
一、maven环境配置
1、下载
maven官方下载地址:https://maven.apache.org/download.cgi
方案1:直接点击下载,然后上传到服务器
方案2:(推荐)
复制下载链接,然后在服务器上用wget命令下载
wget https://dlcdn.apache.org/maven/maven-3/3.9.1/binaries/apache-maven-3.9.1-bin.tar.gz
2、解压
将下载好的maven安装包放在磁盘的 /usr/local/ 目录下然后解压
mv apache-maven-3.9.1-bin.tar.gz /usr/local/
tar -zxvf apache-maven-3.9.1-bin.tar.gz
3、配置环境变量
编辑/etc/profile, 配置MVN_HOME环境变量
vim /etc/profile
profile里添加export (/usr/local/ 是解压后文件目录)
export MAVEN_HOME=/usr/local/apache-maven-3.9.1
export PATH=$PATH:$MAVEN_HOME/bin
配置完成刷新环境变量
source /etc/profile
执行mvn -v 查看安装maven版本
二、项目部署
1、拉取git项目
拉取已经写好并且上传到git上面需要部署的代码
git clone https://gitee.com/lsc00/hm-take.git
2、创建shell脚本文件
vi reggieStart.sh
复制下面内容并保存
#!/bin/sh
echo =================================
echo 自动化部署脚本启动
echo =================================
echo 停止原来运行中的工程
APP_NAME=hm-take
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk 'print $2'`
if [ $tpid ]; then
echo 'Stop Process...'
kill -15 $tpid
fi
sleep 2
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk 'print $2'`
if [ $tpid ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
echo 准备从Git仓库拉取最新代码
cd /home/admin/lsc/Java/project/take/hm-take
echo 开始从Git仓库拉取最新代码
git pull
echo 代码拉取完成
echo 开始打包
output=`mvn clean package -Dmaven.test.skip=true`
cd target
echo 启动项目
nohup java -jar hm-take-1.0-SNAPSHOT.jar &> hm-take.log &
echo 项目启动完成
给脚本授予权限
chmod 777 reggieStart.sh
3、运行脚本
./reggieStart.sh
运行脚本时会自动加载依赖,第一次启动因为需要加载依赖需要时间比较久
shell自动部署linux环境下的组件安装配置
为了业务部署的便利,将手动在linux中执行的组件部署任务,系统配置任务,编写shell实现自动安装配置
本文shell参考的就是附件链接中文档的“4 Linux课程镜像的创建”章节编码,同时该链接也上传了该脚本
阿里云盘链接:「部署课程脚本」https://www.aliyundrive.com/s/AZWnXaXk3Mx
代码如下
#! /bin/bash
##############################################################################
# Func Box: Multi-function for Learning Space
# Date Created: 2019-08-14
# Author: EbowTang
# Description: auto install linux course
# object: Learning Space
# Return 0: Success
#-----------------------------------------------------------------------------
# Modification History
# DATE NAME DESCRIPTION
# 2019-08-14 installUbuntu 1,auto install ubuntu
# 2019-08-18 installCentOSRedHat 2,auto install centos/redhat;
##############################################################################
global_path=$(pwd)
global_iscloudclass=99
# check if run as root user
#if [ `id -u` -ne 0 ]; then
# echo "You need root privileges to run this script."
# exit 1
#fi
if [ `whoami` != 'root' ]; then
echo -e "User checking failure: You need root privileges to run this script."
exit 1
fi
# check is Learning Space or CloudClass?
CLOUDCLASS_SSD_DIR=/gluster/ssd
CLOUDCLASS_HDD_DIR=/gluster/hdd
LearningSpace_CVK_DIR=/opt/H3C/H3CDHostAgent
LearningSpace_CVM_DIR=/opt/H3C/H3CDServer
if [ -f /etc/cas_cvk-version ] || [ -f /etc/cas_cvk-version/cas_cvk-version ]; then
if [ -d $CLOUDCLASS_SSD_DIR ] || [ -d $CLOUDCLASS_HDD_DIR ]; then
global_iscloudclass=1 #CloudClass
elif [ -d $LearningSpace_CVK_DIR ] || [ -d $LearningSpace_CVM_DIR ]; then
global_iscloudclass=0 #Learning Space
else
global_iscloudclass=2 #just cas system
fi
else
global_iscloudclass=2 #other system
fi
filename=$1
time=`date +%y%m%d%H%M`
DIR_LOG=/usr/local
[ ! -d $DIR_LOG ] && mkdir -p $DIR_LOG
cd $DIR_LOG
dir=$DIR_LOG/AutoInstall_$filename$time
if [ ! -d $dir ]
then
mkdir -p $dir
fi
LOGFILE=$dir/auto_install.log
# check if execute this by bash,not support sh
declare -a test_bash_array >/dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "Can not run this file by 'sh', you need to run it by:"
echo " chmod +x $0 && ./$0"
echo " or"
echo " bash $0" | tee -a $LOGFILE
exit 1
fi
function loadscipt ()
if [ -d /opt/H3C/H3CDServer/nginx/version/ ]; then
cp $global_path/auto_install.sh /opt/H3C/H3CDServer/nginx/version/auto_install.sh
echo -e "[INFO]:Script successfully loaded to nginx." | tee -a $LOGFILE
echo -e "[INFO]:You can download this script from Linux course by browser, and proceed with this script." | tee -a $LOGFILE
echo -e "[INFO]:Download this script: enter < brokerIP:8880/version > from Ubuntu/CentOS/Redhat browser." | tee -a $LOGFILE
exit 1
fi
function installUbuntu ()
if [ "$(cat /etc/issue | awk 'NR==1print $1')" != "Ubuntu" ] || [ "$(uname -i)" != "x86_64" ] || [ $global_iscloudclass -ne 2 ]; then
echo -e "[FATAL]:System does not meet the requirements, please check!!!" | tee -a $LOGFILE
exit 1
fi
if [ ! -b /dev/sr0 ]; then
echo -e "[FAILED]:There is no CD-ROM, please check!!!" | tee -a $LOGFILE
return 1
fi
df -h | grep "castools" >> tmpfile1
df -h | grep "castools" | awk 'print $6'>> tmpfile2
var_castool=$(cat tmpfile1)
var_castool_path=$(cat tmpfile2)
rm tmpfile1 tmpfile2
if [ "$var_castool" == "" ]; then
echo -e "[ERROR]:Wrong CD-ROM,please remount castools CD-ROM!" | tee -a $LOGFILE
echo -e "[INFO]:If you already mount castools CD-ROM,you can check by <df -h> after reboot this course." | tee -a $LOGFILE
df -h
exit 1
else
cd $var_castool_path/linux
fi
read -p "Please enter Broker IP:" BrokerIP
ping -c1 -w2 $BrokerIP > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "[INFO]:The Broker($BrokerIP) is connected." | tee -a $LOGFILE
else
echo "[FATAL]:The Broker($BrokerIP) is not connected!!!" | tee -a $LOGFILE
return 1
fi
./CAS_tools_install.sh | tee -a $LOGFILE
cd /usr/local/
var_version="ubuntu14.04-64bit"
version_number=$(cat /etc/issue | awk 'NR==1print $2')
if [ "$version_number" == "14.04.2" ]; then
var_version="ubuntu14.04-64bit"
elif [ "$version_number" == "12.04.5" ]; then
var_version="ubuntu12.04-64bit"
fi
wget --no-check-certificate http://$BrokerIP:8880/version/H3CDAgent/Linux%26Ubuntu/$var_version/mydebs.tar.bz2 | tee -a $LOGFILE
if [ -f /usr/local/mydebs.tar.bz2 ]; then
tar -xvf mydebs.tar.bz2
echo -e "[INFO]:Download mydebs.tar.bz2 complete" | tee -a $LOGFILE
sleep 1
else
echo -e "[FAILED]:Download mydebs.tar.bz2 failed, you can manually download it to /usr/local/, and continue this script。" | tee -a $LOGFILE
exit 1
fi
cp /etc/apt/sources.list /etc/apt/sources.list.bak
cat /dev/null > /etc/apt/sources.list
echo "deb file:///usr/local/mydebs ./" > /etc/apt/sources.list
apt-get update | tee -a $LOGFILE
sleep 1
apt-get install h3cdagent
h3cdagent $BrokerIP
#edit grub.cfg
cp /boot/grub/grub.cfg /boot/grub/grub.cfg.bak
sed -i 's/'"set timeout=30"'/'"set timeout=0"'/g' /boot/grub/grub.cfg
sed -i 's/'"set timeout=-1"'/'"set timeout=0"'/g' /boot/grub/grub.cfg
service h3cdservice status
function installCentOSRedHat ()
local overflag
overflag=0
sysflag=100
if [ "$(cat /etc/issue | awk 'NR==1print $1')" == "CentOS" ]; then
sysflag=0
elif [ "$(cat /etc/issue | awk 'NR==1print $1')" == "Red" ]; then
sysflag=1
else
echo -e "[FATAL]:System does not meet the requirements, please check!!!" | tee -a $LOGFILE
exit 1
fi
if [ "$(uname -i)" != "x86_64" ] || [ $global_iscloudclass -ne 2 ]; then
echo -e "[FATAL]:System does not meet the requirements, please check!!!" | tee -a $LOGFILE
exit 1
fi
if [ ! -b /dev/sr0 ]; then
echo -e "[FAILED]:There is no CD-ROM, please check!!!" | tee -a $LOGFILE
return 1
fi
[ ! -d /usr/local ] && mkdir -p /usr/local
if [ ! -f /usr/local/agent_check.conf ]; then
if [ $sysflag -eq 0 ]; then
if [ ! -f /usr/local/mountflag.tmp ]; then
df -h | grep "CentOS" >> tmpfile1
var_centos=$(cat tmpfile1)
rm -rf tmpfile1
if [ "$var_centos" == "" ]; then
echo -e "[ERROR]:Wrong CD-ROM,please mount CentOS CD-ROM!" | tee -a $LOGFILE
echo -e "[INFO]:If you already click CentOS CD-ROM,you can check by <df -h> after reboot this course." | tee -a $LOGFILE
df -h
exit 1
fi
#mount centos
umount /dev/sr0 | tee -a $LOGFILE
[ ! -d /installmnt ] && mkdir /installmnt
mount /dev/sr0 /installmnt | tee -a $LOGFILE && cat /dev/null > /usr/local/mountflag.tmp
fi
#config CentOS-Base.repo
confile=/etc/yum.repos.d/CentOS-Base.repo
[ -f $confile ] && cp $confile $confile.bak || echo -e "[FAILED]:Config ERROR!" | tee -a $LOGFILE
cat /dev/null > $confile
echo "[base]" >> $confile
echo 'name=CentOS-$releaserver - Base' >> $confile
echo "baseurl=file:///installmnt" >> $confile
echo "gpgcheck=0" >> $confile
echo "enable=1" >> $confile
echo "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6" >> $confile
#config CentOS-Media.repo
confile=/etc/yum.repos.d/CentOS-Media.repo
[ -f $confile ] && cp $confile $confile.bak || echo -e "[FAILED]:Config ERROR!" | tee -a $LOGFILE
cat /dev/null > $confile
echo "[c6-media]" >> $confile
echo 'name=CentOS-$releaserver - Media' >> $confile
echo "baseurl=file:///installmnt" >> $confile
echo "gpgcheck=0" >> $confile
echo "enable=1" >> $confile
echo "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6" >> $confile
elif [ $sysflag -eq 1 ]; then
if [ ! -f /usr/local/mountflag.tmp ]; then
df -h | grep "RHEL" >> tmpfile2
var_rhel=$(cat tmpfile2)
rm -rf tmpfile2
if [ "$var_rhel" == "" ]; then
echo -e "[ERROR]:Wrong CD-ROM,please mount Red Hat CD-ROM!" | tee -a $LOGFILE
echo -e "[INFO]:If you already click RedHat CD-ROM,you can check by <df -h> after reboot this course." | tee -a $LOGFILE
df -h
exit 1
fi
#mount rhel
umount /dev/sr0 | tee -a $LOGFILE
[ ! -d /installmnt ] && mkdir /installmnt
mount /dev/sr0 /installmnt | tee -a $LOGFILE && cat /dev/null > /usr/local/mountflag.tmp
fi
#config rhel-source.repo
confile=/etc/yum.repos.d/rhel-source.repo
[ -f $confile ] && cp $confile $confile.bak || echo -e "[FAILED]:Config ERROR!" | tee -a $LOGFILE
cat /dev/null > $confile
echo "[rhel-source]" >> $confile
echo 'name=Red Hat Enterprise Linux $releasever - $basearch - Source' >> $confile
echo "baseurl=file:///installmnt" >> $confile
echo "gpgcheck=0" >> $confile
echo "enable=1" >> $confile
echo "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" >> $confile
fi
#yum install
yum clean all | tee -a $LOGFILE
yum repolist | tee -a $LOGFILE
echo "y" | yum install xulrunner.i686 | tee -a $LOGFILE
echo "y" | yum install libXtst.i686 | tee -a $LOGFILE
if [ $? -eq 0 ]; then
echo -e "[INFO]:yum install complete." | tee -a $LOGFILE
else
echo -e "[ERROR]:yum install failed. Please check!" | tee -a $LOGFILE
exit 1
fi
#install xrdp and download file
read -p "Please enter Broker IP:" BrokerIP
ping -c1 -w1 $BrokerIP > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "[INFO]:The Broker($BrokerIP) is connected." | tee -a $LOGFILE
else
echo "[FATAL]:The Broker($BrokerIP) is not connected!!!" | tee -a $LOGFILE
return 1
fi
cd /usr/local
wget --no-check-certificate http://$BrokerIP:8880/version/H3CDAgent/Linux%26Ubuntu/xrdp-v0.6.1.tar.gz | tee -a $LOGFILE
wget --no-check-certificate http://$BrokerIP:8880/version/H3CDAgent/Linux%26Ubuntu/h3cdaemon.run | tee -a $LOGFILE
if [ ! -f /usr/local/xrdp-v0.6.1.tar.gz ] || [ ! -f /usr/local/h3cdaemon.run ]; then
echo -e "[FAILED]:download xrdp or h3cdaemon failed." | tee -a $LOGFILE
exit 1
fi
tar -xvzf xrdp-v0.6.1.tar.gz | tee -a $LOGFILE
cd xrdp-v0.6.1
./bootstrap | tee -a $LOGFILE
./configure | tee -a $LOGFILE
make
make install | tee -a $LOGFILE
if [ $? -ne 0 ]; then
echo -e "[ERROR]:make install failed. Please check!" | tee -a $LOGFILE
exit 1
fi
cd instfiles
./xrdp.sh start
ps aux | grep xrdp
cp xrdp.sh /etc/rc.d/init.d/xrdpd
chkconfig --level 2345 xrdpd on
chkconfig --list xrdpd
#install vnsserver
cd /installmnt/Packages
vncfile=$(find /installmnt/Packages -name tigervnc-server*)
rpm -ivh $vncfile | tee -a $LOGFILE
chkconfig --add vncserver
chkconfig vncserver on
service vncserver start
vncserver
service vncserver status
service iptables stop
chkconfig iptables off
#install agent
cd /usr/local
chmod a+x h3cdaemon.run
./h3cdaemon.run $BrokerIP | tee -a $LOGFILE
if [ $? -ne 0 ]; then
echo -e "[ERROR]:install h3cdaemon failed. Please check!" | tee -a $LOGFILE
exit 1
fi
chkconfig --add h3cdservice
chkconfig h3cdservice on
service h3cdservice status | tee -a $LOGFILE
service h3cdservice version | tee -a $LOGFILE
echo -e "install_agent=1" > /usr/local/agent_check.conf
cp $global_path/auto_install* /usr/local/auto_install.sh
rm -rf /usr/local/mountflag.tmp
else
echo -e "Start to configure the system......" | tee -a $LOGFILE
#configure network
FILE_ETH0=/etc/sysconfig/network-scripts/ifcfg-eth0
if [ ! -f $FILE_ETH0 ]; then
echo -e "[FAILED]:failed to configure eth0 "
exit 1
fi
sed -i 's/'"ONBOOT=no"'/'"ONBOOT=yes"'/g' $FILE_ETH0
sed -i 's/'"NM_CONTROLLED=yes"'/'"NM_CONTROLLED=no"'/g' $FILE_ETH0
sed -i '/HWADDR/d' $FILE_ETH0 #if no ‘-i’,will not modify FILE_ETH0
sleep 1
cd /etc/udev/rules.d/
rm -rf 70-persistent-net.rules
sleep 1
#configure user
PASSWD_FILE=/etc/passwd
[ ! -f $PASSWD_FILE.back ] && cp $PASSWD_FILE $PASSWD_FILE.back
lastconf=$(cat /etc/passwd | sed -n '$p')
USERNAME=$(sed -n '$p' $PASSWD_FILE | cut -d ":" -f 1) #‘-d’ (Specify separator),‘-f’ (Specify which character to print)
tmpconf=$(sed -n '$p' $PASSWD_FILE | cut -d ":" -f 3-19) #Specify ‘:’ to be separator,print 3-19 part
oldconf=$(sed -n '$p' $PASSWD_FILE)
newconf="$USERNAME:"":$tmpconf"
[ "$lastconf" != "$newconf" ] && sed -i 's!'"$oldconf"'!'"$newconf"'!g' $PASSWD_FILE
#auto login
CUSTOM_FILE=/etc/gdm/custom.conf
if [ ! -f $CUSTOM_FILE.back ]; then
cp $CUSTOM_FILE $CUSTOM_FILE.back
autouserconf="AutomaticLogin=$USERNAME"
timeuserconf="TimedLogin=$USERNAME"
sed -i '3a AutomaticLoginEnable=True' $CUSTOM_FILE
sed -i "4a $autouserconf" $CUSTOM_FILE
sed -i '5a TimedLoginEnable=true' $CUSTOM_FILE
sed -i "6a $timeuserconf" $CUSTOM_FILE
sed -i '7a TimedLoginDelay=7' $CUSTOM_FILE
fi
echo -e "Configure the system complete." | tee -a $LOGFILE
[ -f /usr/local/castool_check.conf ] && exit 0
echo -e "Start to install Castool......" | tee -a $LOGFILE
df -h | grep "castools" >> tmpfile3
df -h | grep "castools" | awk 'print $6'>> tmpfile4
var_castool=$(cat tmpfile3)
var_castool_path=$(cat tmpfile4)
rm tmpfile3 tmpfile4
if [ "$var_castool" == "" ]; then
echo -e "[ERROR]:Wrong CD-ROM,please mount Castools CD-ROM!" | tee -a $LOGFILE
echo -e "[INFO]:If you already mount castools CD-ROM,you can check by <df -h> after reboot this course." | tee -a $LOGFILE
df -h
exit 1
fi
#Castool installing
cd $var_castool_path/linux
./CAS_tools_install.sh
[ $? -eq 0 ] && echo -e "[INFO]:Castool install complete!" || echo -e "[ERROR]:Castool install failed!" | tee -a $LOGFILE
overflag=1
echo -e "install_castool=1" > /usr/local/castool_check.conf
fi
#done
echo -e "############################################################################################"
echo -e "[INFO]:You can change broker IP from /opt/h3c/h3cdaemon/conf/imf.cfg" | tee -a $LOGFILE
[ $overflag -eq 0 ] && echo -e "[INFO]:Please continue with this script(/usr/local/auto_install.sh) before mount Castools CD-ROM." | tee -a $LOGFILE
[ $overflag -eq 0 ] && echo -e "[INFO]:If you already click Castools CD-ROM,you can check by <df -h> after reboot this course." | tee -a $LOGFILE
[ $overflag -eq 1 ] && echo -e "[INFO]:Install complete!" | tee -a $LOGFILE
echo -e "############################################################################################"
function readMe ()
echo -e "[Error]:Your option is not exist. Please check!!!" | tee -a $LOGFILE
exit 1
function workMain ()
loadscipt
echo "##################################################################"
echo "Please enter your options(number only, 1 or 2)"
echo " options 1: Auto Install Ubuntu course"
echo " options 2: Auto Install CentOS/RedHat course"
echo "##################################################################"
read -p "Please enter your options: " choice
# check enter number
flag=true
isnum=0
while $flag
do
#expr $choice + 0 &>/dev/null
#[ $? -eq 0 ] && [ flag=false ] || [ read -p "Please enter number only:(1<=options<= 2)" choice ]
expr $choice + 10 &>/dev/null
[ $? -eq 0 ] && isnum=1 || read -p "Number only,please enter again:" choice
if [ $isnum -eq 1 ] && [ $choice -le 2 -a $choice -ge 1 ]; then
flag=false
elif [ $isnum -eq 1 ]; then
isnum=0
read -p "Out of range,please enter again(1<=options<=2):" "choice"
fi
done
if [ $choice -eq 1 ]; then
installUbuntu
elif [ $choice -eq 2 ]; then
installCentOSRedHat
else
readMe
fi
return 0
workMain
exit 0
以上是关于Linux通过Shell自动部署springboot的主要内容,如果未能解决你的问题,请参考以下文章