shell自动部署linux环境下的组件安装配置

编程入门 行业动态 更新时间:2024-10-20 06:33:43

shell自动部署linux环境下的<a href=https://www.elefans.com/category/jswz/34/1771375.html style=组件安装配置"/>

shell自动部署linux环境下的组件安装配置

为了业务部署的便利,将手动在linux中执行的组件部署任务,系统配置任务,编写shell实现自动安装配置

本文shell参考的就是附件链接中文档的“4 Linux课程镜像的创建”章节编码,同时该链接也上传了该脚本

阿里云盘链接:「部署课程脚本」 

 代码如下

#! /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' ]; thenecho -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/H3CDServerif [ -f /etc/cas_cvk-version ] || [ -f /etc/cas_cvk-version/cas_cvk-version ]; thenif [ -d $CLOUDCLASS_SSD_DIR ] || [ -d $CLOUDCLASS_HDD_DIR ]; thenglobal_iscloudclass=1 #CloudClasselif [ -d $LearningSpace_CVK_DIR ] || [ -d $LearningSpace_CVM_DIR ]; thenglobal_iscloudclass=0 #Learning Spaceelse global_iscloudclass=2 #just cas systemfi
else global_iscloudclass=2 #other system
fifilename=$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 ]
thenmkdir -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 ]; thenecho "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
fifunction loadscipt (){ if [ -d /opt/H3C/H3CDServer/nginx/version/ ]; thencp $global_path/auto_install.sh /opt/H3C/H3CDServer/nginx/version/auto_install.shecho -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 1fi
}
function installUbuntu (){ if [ "$(cat /etc/issue | awk 'NR==1{print $1}')" != "Ubuntu" ] || [ "$(uname -i)" != "x86_64" ] || [ $global_iscloudclass -ne 2 ]; thenecho -e "[FATAL]:System does not meet the requirements, please check!!!" | tee -a ${LOGFILE}exit 1fiif [ ! -b /dev/sr0 ]; thenecho -e "[FAILED]:There is no CD-ROM, please check!!!" | tee -a ${LOGFILE}return 1fidf -h | grep "castools" >> tmpfile1df -h | grep "castools" | awk '{print $6}'>> tmpfile2var_castool=$(cat tmpfile1)var_castool_path=$(cat tmpfile2)rm tmpfile1 tmpfile2if [ "$var_castool" == "" ]; thenecho -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 -hexit 1elsecd $var_castool_path/linuxfiread -p "Please enter Broker IP:" BrokerIPping -c1 -w2 $BrokerIP > /dev/null 2>&1if [ $? -eq 0 ]; thenecho "[INFO]:The Broker($BrokerIP) is connected." | tee -a ${LOGFILE}elseecho "[FATAL]:The Broker($BrokerIP) is not connected!!!" | tee -a ${LOGFILE}return 1fi./CAS_tools_install.sh | tee -a ${LOGFILE}cd /usr/local/var_version="ubuntu14.04-64bit"version_number=$(cat /etc/issue | awk 'NR==1{print $2}')if [ "$version_number" == "14.04.2" ]; thenvar_version="ubuntu14.04-64bit"elif [ "$version_number" == "12.04.5" ]; thenvar_version="ubuntu12.04-64bit"fiwget --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 ]; thentar -xvf mydebs.tar.bz2echo -e "[INFO]:Download mydebs.tar.bz2 complete" | tee -a ${LOGFILE}sleep 1elseecho -e "[FAILED]:Download mydebs.tar.bz2 failed, you can manually download it to /usr/local/, and continue this script。" | tee -a ${LOGFILE}exit 1ficp /etc/apt/sources.list /etc/apt/sources.list.bakcat /dev/null > /etc/apt/sources.listecho "deb file:///usr/local/mydebs ./" > /etc/apt/sources.listapt-get update | tee -a ${LOGFILE}sleep 1apt-get install h3cdagenth3cdagent $BrokerIP#edit grub.cfgcp /boot/grub/grub.cfg /boot/grub/grub.cfg.baksed -i 's/'"set timeout=30"'/'"set timeout=0"'/g' /boot/grub/grub.cfgsed -i 's/'"set timeout=-1"'/'"set timeout=0"'/g' /boot/grub/grub.cfgservice h3cdservice status
}function installCentOSRedHat (){ local overflagoverflag=0sysflag=100if [ "$(cat /etc/issue | awk 'NR==1{print $1}')" == "CentOS" ]; thensysflag=0elif [ "$(cat /etc/issue | awk 'NR==1{print $1}')" == "Red" ]; thensysflag=1elseecho -e "[FATAL]:System does not meet the requirements, please check!!!" | tee -a ${LOGFILE}exit 1fiif [ "$(uname -i)" != "x86_64" ] || [ $global_iscloudclass -ne 2 ]; thenecho -e "[FATAL]:System does not meet the requirements, please check!!!" | tee -a ${LOGFILE}exit 1fiif [ ! -b /dev/sr0 ]; thenecho -e "[FAILED]:There is no CD-ROM, please check!!!" | tee -a ${LOGFILE}return 1fi[ ! -d /usr/local ] && mkdir -p /usr/localif [ ! -f /usr/local/agent_check.conf ]; thenif [ $sysflag -eq 0 ]; thenif [ ! -f /usr/local/mountflag.tmp ]; thendf -h | grep "CentOS" >> tmpfile1var_centos=$(cat tmpfile1)rm -rf tmpfile1if [ "$var_centos" == "" ]; thenecho -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 -hexit 1fi#mount centosumount /dev/sr0 | tee -a ${LOGFILE}[ ! -d /installmnt ] && mkdir /installmntmount /dev/sr0 /installmnt | tee -a ${LOGFILE} && cat /dev/null > /usr/local/mountflag.tmpfi#config CentOS-Base.repoconfile=/etc/yum.repos.d/CentOS-Base.repo[ -f $confile ] && cp $confile $confile.bak || echo -e "[FAILED]:Config ERROR!" | tee -a ${LOGFILE}cat /dev/null > $confileecho "[base]" >> $confileecho 'name=CentOS-$releaserver - Base' >> $confileecho "baseurl=file:///installmnt" >> $confileecho "gpgcheck=0" >> $confileecho "enable=1" >> $confileecho "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6" >> $confile#config CentOS-Media.repoconfile=/etc/yum.repos.d/CentOS-Media.repo[ -f $confile ] && cp $confile $confile.bak || echo -e "[FAILED]:Config ERROR!" | tee -a ${LOGFILE}cat /dev/null > $confileecho "[c6-media]" >> $confileecho 'name=CentOS-$releaserver - Media' >> $confileecho "baseurl=file:///installmnt" >> $confileecho "gpgcheck=0" >> $confileecho "enable=1" >> $confileecho "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6" >> $confileelif [ $sysflag -eq 1 ]; thenif [ ! -f /usr/local/mountflag.tmp ]; thendf -h | grep "RHEL" >> tmpfile2var_rhel=$(cat tmpfile2)rm -rf tmpfile2if [ "$var_rhel" == "" ]; thenecho -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 -hexit 1fi#mount rhelumount /dev/sr0 | tee -a ${LOGFILE}[ ! -d /installmnt ] && mkdir /installmntmount /dev/sr0 /installmnt | tee -a ${LOGFILE} && cat /dev/null > /usr/local/mountflag.tmpfi#config rhel-source.repoconfile=/etc/yum.repos.d/rhel-source.repo[ -f $confile ] && cp $confile $confile.bak || echo -e "[FAILED]:Config ERROR!" | tee -a ${LOGFILE}cat /dev/null > $confileecho "[rhel-source]" >> $confileecho 'name=Red Hat Enterprise Linux $releasever - $basearch - Source' >> $confileecho "baseurl=file:///installmnt" >> $confileecho "gpgcheck=0" >> $confileecho "enable=1" >> $confileecho "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" >> $confilefi#yum installyum 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 ]; thenecho -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 fileread -p "Please enter Broker IP:" BrokerIPping -c1 -w1 $BrokerIP > /dev/null 2>&1if [ $? -eq 0 ]; thenecho "[INFO]:The Broker($BrokerIP) is connected." | tee -a ${LOGFILE}elseecho "[FATAL]:The Broker($BrokerIP) is not connected!!!" | tee -a ${LOGFILE}return 1ficd /usr/localwget --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 ]; thenecho -e "[FAILED]:download xrdp or h3cdaemon failed."  | tee -a ${LOGFILE}exit 1fitar -xvzf xrdp-v0.6.1.tar.gz | tee -a ${LOGFILE}cd xrdp-v0.6.1./bootstrap | tee -a ${LOGFILE}./configure | tee -a ${LOGFILE}makemake install | tee -a ${LOGFILE}if [ $? -ne 0 ]; thenecho -e "[ERROR]:make install failed. Please check!" | tee -a ${LOGFILE}exit 1 ficd instfiles./xrdp.sh startps aux | grep xrdpcp xrdp.sh /etc/rc.d/init.d/xrdpdchkconfig --level 2345 xrdpd onchkconfig --list xrdpd#install vnsservercd /installmnt/Packagesvncfile=$(find /installmnt/Packages -name tigervnc-server*)rpm -ivh $vncfile | tee -a ${LOGFILE}chkconfig --add vncserverchkconfig vncserver onservice vncserver startvncserverservice vncserver statusservice iptables stopchkconfig iptables off#install agentcd /usr/localchmod a+x h3cdaemon.run./h3cdaemon.run $BrokerIP | tee -a ${LOGFILE}if [ $? -ne 0 ]; thenecho -e "[ERROR]:install h3cdaemon failed. Please check!" | tee -a ${LOGFILE}exit 1 fichkconfig --add h3cdservicechkconfig h3cdservice on service h3cdservice status | tee -a ${LOGFILE}service h3cdservice version | tee -a ${LOGFILE}echo -e "install_agent=1" > /usr/local/agent_check.confcp $global_path/auto_install* /usr/local/auto_install.sh rm -rf /usr/local/mountflag.tmpelseecho -e "Start to configure the system......" | tee -a ${LOGFILE}#configure networkFILE_ETH0=/etc/sysconfig/network-scripts/ifcfg-eth0if [ ! -f $FILE_ETH0 ]; thenecho -e "[FAILED]:failed to configure eth0 "exit 1fised -i 's/'"ONBOOT=no"'/'"ONBOOT=yes"'/g' $FILE_ETH0sed -i 's/'"NM_CONTROLLED=yes"'/'"NM_CONTROLLED=no"'/g' $FILE_ETH0sed -i '/HWADDR/d' $FILE_ETH0 #if no ‘-i’,will not modify FILE_ETH0sleep 1cd /etc/udev/rules.d/rm -rf 70-persistent-net.rulessleep 1#configure userPASSWD_FILE=/etc/passwd[ ! -f $PASSWD_FILE.back ] && cp $PASSWD_FILE $PASSWD_FILE.backlastconf=$(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 partoldconf=$(sed -n '$p' $PASSWD_FILE)newconf="$USERNAME:"":$tmpconf"[ "$lastconf" != "$newconf" ] && sed -i 's!'"$oldconf"'!'"$newconf"'!g' $PASSWD_FILE#auto loginCUSTOM_FILE=/etc/gdm/custom.confif [ ! -f $CUSTOM_FILE.back ]; thencp $CUSTOM_FILE $CUSTOM_FILE.backautouserconf="AutomaticLogin=$USERNAME"timeuserconf="TimedLogin=$USERNAME"sed -i '3a AutomaticLoginEnable=True' $CUSTOM_FILEsed -i "4a $autouserconf" $CUSTOM_FILEsed -i '5a TimedLoginEnable=true' $CUSTOM_FILEsed -i "6a $timeuserconf" $CUSTOM_FILEsed -i '7a TimedLoginDelay=7' $CUSTOM_FILEfiecho -e "Configure the system complete." | tee -a ${LOGFILE}[ -f /usr/local/castool_check.conf ] && exit 0echo -e "Start to install Castool......" | tee -a ${LOGFILE}df -h | grep "castools" >> tmpfile3df -h | grep "castools" | awk '{print $6}'>> tmpfile4var_castool=$(cat tmpfile3)var_castool_path=$(cat tmpfile4)rm tmpfile3 tmpfile4if [ "$var_castool" == "" ]; thenecho -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 -hexit 1		fi#Castool installingcd $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=1echo -e "install_castool=1" > /usr/local/castool_check.conffi#doneecho -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 () {loadsciptecho  "##################################################################"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=trueisnum=0while $flagdo#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:"  choiceif [ $isnum -eq 1 ] && [ $choice -le 2 -a $choice -ge 1 ]; thenflag=falseelif [ $isnum -eq 1 ]; thenisnum=0read -p "Out of range,please enter again(1<=options<=2):" "choice"fidoneif [ $choice -eq 1 ]; theninstallUbuntuelif [ $choice -eq 2 ]; theninstallCentOSRedHatelsereadMefireturn 0
}
workMain
exit 0

更多推荐

shell自动部署linux环境下的组件安装配置

本文发布于:2024-02-26 15:47:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1703035.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   环境   shell   linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!