自动化部署脚本文件目录:

自动化部署脚本--linux执行sh脚本-LMLPHP

运行主程序:./install.sh

#!/bin/bash

SCRIPTPATH=$(cd "$(dirname "$0")"; pwd)#

这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这个目录来定位所要运行程序的相对位置(绝对位置除外)。

Install_log=$SCRIPTPATH/install_log.txt

echo "***start install***" > $Install_log
echo " workspace is $SCRIPTPATH" | tee -a "$Install_log"#写入日志文件日志

show_license() {
    clear
    confirm='Do you agree with this license? [y/n]: '
    more license.txt

lic_agreed=''

while read -p 'Do you agree with this license? [y/n]: ' lic_agreed
        do
                case "$lic_agreed" in
                        y | Y ) break;;
                        n | N ) echo 'License refused - exiting.' ;exit 1;;
                        * )     ;;
                esac
        done
}

install_expect(){
    # start install expect
    rpm -ivh --force expect/tcl-8.5.13-8.el7.x86_64.rpm
    if [[ 0 != $? ]] ; then
        echo "tcl install Failed !!!!!!"   | tee -a "$Install_log"
        exit 1
    else
        echo "tcl install Success !!!!!!"   | tee -a "$Install_log"
    fi
        
    rpm -ivh --force expect/expect-5.45-14.el7_1.x86_64.rpm
    if [[ 0 != $? ]] ; then
        echo "expect install Failed !!!!!!"   | tee -a "$Install_log"
        exit 1
    else
        echo "expect install Success !!!!!!"   | tee -a "$Install_log"
    fi
    
    # expect install finish
}

install_unzip(){
    # start unzip
    rpm -ivh --force unzip/unzip-6.0-19.el7.x86_64.rpm
    if [[ 0 != $? ]] ; then#$? 是上一个程序执行是否成功的标志,如果执行成功则$? 为0,否则 不为0

echo "unzip install Failed !!!!!!"   | tee -a "$Install_log"
        exit 1
    else
        echo "unzip install Success !!!!!!"   | tee -a "$Install_log"
    fi
    #unzip install finish
}

install_killall(){
    # start killall
    rpm -ivh --force killall/psmisc-22.20-15.el7.x86_64.rpm
    if [[ 0 != $? ]] ; then
        echo "killall psmisc install Failed !!!!!!"   | tee -a "$Install_log"
    else
        echo "killall psmisc install Success !!!!!!"   | tee -a "$Install_log"
    fi

#killall install finish
}

install_snmp(){
    snmpstatus=0
    rpm -ivh --force snmplib/net-snmp-libs-5.7.2-33.el7_5.2.x86_64.rpm
    let snmpstatus+=$?
    sleep 2
    rpm -ivh --force snmplib/net-snmp-utils-5.7.2-33.el7_5.2.x86_64.rpm
    let snmpstatus+=$?
    if [[ 0 != $snmpstatus ]] ; then
        echo "snmp  install Failed !!!!!!"   | tee -a "$Install_log"
    else
        echo "snmp  install Success !!!!!!"   | tee -a "$Install_log"
    fi
}

install_java(){
    #start to install java
    rpm -ivh --force java/jdk-8u144-linux-x64.rpm
    if [[ 0 != $? ]] ; then
        echo "java  install Failed !!!!!!"   | tee -a "$Install_log"
        exit 1
    else
        echo "java  install Success !!!!!!"   | tee -a "$Install_log"
    fi
    export JAVA_HOME=/usr/java/jdk1.8.0_144
    export JRE_HOME=$JAVA_HOME/jre
    export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
    export CLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib/dt.jar
    #java install finish
}

install_activemq(){
    #start to install activemq
    tar zxvf activemq/apache-activemq-5.13.0.tar.gz | tee -a "$Install_log"
    $SCRIPTPATH/apache-activemq-5.13.0/bin/linux-x86-64/activemq start &
    sleep 5
    echo "activemq install finish" | tee -a "$Install_log"
    #activemq install finish
}

install_redis(){
    #start to install redis
    tar zxvf redis/redis-3.2.9.tar.gz
    $SCRIPTPATH/redis-3.2.9/src/redis-server $SCRIPTPATH/redis-3.2.9/redis.conf &
    sleep 5
    echo "redis install finish" | tee -a "$Install_log"
    #redis install finish
}

install_systemdlib(){
    libstatus=0
    rpm -ivh --force libsystemd/lz4-1.7.5-2.el7.x86_64.rpm
    let libstatus+=$?
    sleep 2
    rpm -ivh --force libsystemd/systemd-libs-219-57.el7.x86_64.rpm
    let libstatus+=$?
    if [[ 0 != $libstatus ]] ; then
        echo "systemdlib  install Failed !!!!!!"   | tee -a "$Install_log"
        exit 1
    else
        echo "systemdlib  install Success !!!!!!"   | tee -a "$Install_log"
    fi
}

#下面这个函数,由于最小安装模式下缺少依赖包,所以将安装成功的文件进行an并集

install_postgres(){
    #start to install postgres
    cd postgres
    status=0
    rpm -ivh --force pgdg-centos96-9.6-3.noarch.rpm
    let status+=$?# $?取上一个安装结果,安装成功为0,安装失败为1

sleep 2
    rpm -ivh --force postgresql96-libs-9.6.5-1PGDG.rhel7.x86_64.rpm
    let status+=$?
    sleep 2
    rpm -ivh --force postgresql96-9.6.5-1PGDG.rhel7.x86_64.rpm
    let status+=$?
    sleep 2
    rpm -ivh --force libxslt-1.1.28-5.el7.x86_64.rpm
    let status+=$?
    sleep 2
    rpm -ivh --force postgresql96-contrib-9.6.5-1PGDG.rhel7.x86_64.rpm
    let status+=$?
    sleep 2
    rpm -ivh --force postgresql96-devel-9.6.5-1PGDG.rhel7.x86_64.rpm
    let status+=$?
    sleep 2
    rpm -ivh --force postgresql96-server-9.6.5-1PGDG.rhel7.x86_64.rpm
    let status+=$?
    sleep 2
    if [[ 0 != $status ]] ; then
        echo "postgres  install Failed !!!!!!"   | tee -a "$Install_log"
        exit 1
    else
        echo "postgres  install Success !!!!!!"   | tee -a "$Install_log"
    fi
    sleep 2
    cd $SCRIPTPATH
    #postgres install finish
}

init_postgresql(){n
    #init postgresql
    adduser uxsino
    /usr/pgsql-9.6/bin/postgresql96-setup initdb
    service postgresql-9.6 start
    $SCRIPTPATH/init-data.sh | tee -a "$Install_log"
    sleep 10
    mv -f $SCRIPTPATH/postgresql.conf /var/lib/pgsql/9.6/data/
    mv -f $SCRIPTPATH/pg_hba.conf /var/lib/pgsql/9.6/data/
    sleep 5
    service postgresql-9.6 restart
    sleep 10
    echo "postgresql init finish" | tee -a "$Install_log"
    #postgresql init finish
}

stop_firewalld(){
    #stop & disable firewalld
    
    systemctl stop firewalld.service | tee -a "$Install_log"
    sleep 10
    systemctl disable firewalld.service | tee -a "$Install_log"
    
    echo "stop & disable firewalld finish" | tee -a "$Install_log"
    #stop & disable firewalld finish
}

auto_run_config(){
    echo "export JAVA_HOME=/usr/java/jdk1.8.0_144" > $SCRIPTPATH/autostart.sh
    echo "export JRE_HOME=$JAVA_HOME/jre" >> $SCRIPTPATH/autostart.sh
    echo "export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin" >> $SCRIPTPATH/autostart.sh
    echo "export CLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib/dt.jar" >> $SCRIPTPATH/autostart.sh
    echo "service postgresql-9.6 restart" >> $SCRIPTPATH/autostart.sh
    echo "sleep 30" >> $SCRIPTPATH/autostart.sh
    echo "$SCRIPTPATH/redis-3.2.9/src/redis-server $SCRIPTPATH/redis-3.2.9/redis.conf &" >> $SCRIPTPATH/autostart.sh
    echo "sleep 10" >> $SCRIPTPATH/autostart.sh
    echo "$SCRIPTPATH/apache-activemq-5.13.0/bin/linux-x86-64/activemq start &" >> $SCRIPTPATH/autostart.sh
    echo "sleep 10" >> $SCRIPTPATH/autostart.sh
    echo "systemctl stop firewalld.service" >> $SCRIPTPATH/autostart.sh
    echo "sleep 10" >> $SCRIPTPATH/autostart.sh
    echo "$SCRIPTPATH/simo/scripts/runlib" >> $SCRIPTPATH/autostart.sh
    chmod 777 $SCRIPTPATH/autostart.sh
    confirm='Do you want set SIMO auto run? [y/n]: '
    auto_agreed=''
    
        while read -p 'Do you want set SIMO auto run? [y/n]: ' auto_agreed
        do
                case "$auto_agreed" in
                        y | Y )
                        #set simo auto run
                        echo "$SCRIPTPATH/autostart.sh" >> /etc/rc.d/rc.local  #linux系统自启动文件需要配置的文件路径
                        chmod +x /etc/rc.d/rc.local
                        break;;
                        n | N ) echo 'Do not set SIMO auto run' ;break;;
                        * )     ;;
                esac
        done
}

#unzip simo and config simo
unzip_simo(){
    unzip $SCRIPTPATH/simo-source/simo.zip
    cp $SCRIPTPATH/application.properties $SCRIPTPATH/simo/scripts/
    #enable/disable servicecenter
    echo "$SCRIPTPATH/simo/scripts/runmodule simo-servicecenter 512m &" > $SCRIPTPATH/runlib
    echo "sleep 5" >> $SCRIPTPATH/runlib
    #enable/disable collector
    echo "$SCRIPTPATH/simo/scripts/runmodule simo-collector 1024 &" >> $SCRIPTPATH/runlib
    echo "sleep 5" >> $SCRIPTPATH/runlib
    #enable/disable business
    echo "$SCRIPTPATH/simo/scripts/runmodule simo-business 512m  &" >> $SCRIPTPATH/runlib
    echo "sleep 5" >> $SCRIPTPATH/runlib
    #enable/disable itsm
    echo "$SCRIPTPATH/simo/scripts/runmodule simo-itsm 1024m &" >> $SCRIPTPATH/runlib
    echo "sleep 5" >> $SCRIPTPATH/runlib
    #enable/disable cmdb
    echo "$SCRIPTPATH/simo/scripts/runmodule simo-cmdb 512m &" >> $SCRIPTPATH/runlib
    echo "sleep 5" >> $SCRIPTPATH/runlib
    #enable/disable workflow
    echo "$SCRIPTPATH/simo/scripts/runmodule simo-workflow 512m &" >> $SCRIPTPATH/runlib
    echo "sleep 10" >> $SCRIPTPATH/runlib
    #enable/disable mc
    echo "$SCRIPTPATH/simo/scripts/runmodule simo-mc 2048m &" >> $SCRIPTPATH/runlib
    mv -f $SCRIPTPATH/runlib $SCRIPTPATH/simo/scripts/
}

run_simo(){
    chmod 777 $SCRIPTPATH/simo/scripts/runlib
    $SCRIPTPATH/simo/scripts/runlib
}

show_license
    install_expect
    install_unzip
    install_killall
    install_snmp
    install_java
    install_activemq
    install_redis
    install_systemdlib
    install_postgres
    init_postgresql
    stop_firewalld
    auto_run_config
    unzip_simo
    run_simo
    
    
exit 0

init-data.sh脚本如下:

#!/usr/bin/expect

set timeout 900
set user postgres
spawn su $user
expect "bash-4.2*"
send "psql\r"
expect "postgres=#"
send "alter user postgres with password 'postgres';\r"
expect "postgres=#"
send "create user uxsino with password 'Uxs!nO10';\r"
expect "postgres=#"
send "create database simo_dev owner uxsino;\r"
expect "postgres=#"
send "GRANT ALL PRIVILEGES ON DATABASE simo_dev to uxsino;\r"
expect "postgres=#"
send "\\q\r"
expect "bash-4.2*"
send "exit\r"
expect eof

05-06 12:07