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

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

运行主程序:./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
        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
}

install_postgres(){
    #start to install postgres
    cd postgres
    status=0
    rpm -ivh --force pgdg-centos96-9.6-3.noarch.rpm
    let status+=$?
    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

原文地址:https://www.cnblogs.com/zyy98877/p/9254451.html

时间: 2024-10-09 01:30:12

自动化部署脚本--linux执行sh脚本的相关文章

RF脚本里面执行sh脚本时,sh有java调用时异常

测试过程中遇到问题: 调用的脚本内容如下: cat /home/csqinwj/test/zidh/BDCSC-basis-KVV1/ct_user_acct_mask_m/bdcscCli.sh#!/bin/bashHOME=$(cd "$(dirname "$0")"; pwd)java -Xmx120m -Xms120m -XX:PermSize=40M -XX:MaxPermSize=40M -XX:+DisableExplicitGC -cp $HOME/

执行sh脚本文件下载Github上的代码(雷霄骅的ffmpeg示例代码)

   今天想重新学习下ffmpeg,于是又来到了雷晓骅的博客,先下载了他的所有代码,这里记录一下在Windows上使用sh脚本下载GitHub上代码的过程. CygWin(最后并没有用到)    可以在Windows系统上使用Unix终端的工具,在运行Windows的同时也可以使用vi,bash,tar等Unix下的工具. 使用CygWin执行sh脚本提示'\r'未找到的命令,因为在dos/window下按一次回车键实际上输入的是"回车(CR)"和"换行(LF)",

Linux执行.sh文件,提示No such file or directory的问题的解决方法

Linux执行.sh文件,提示No such file or directory的问题在window平台下,写好shell脚本文件,迁移到linux平台,赋过可执行权限,执行该sh文件,却提示No such file or directory.ls 了下,确实有该文件,怎么会事呢,难道是文件格式兼容性问题?用vim打开该sh文件,输入:[plain]:set ff 回车,显示fileformat=dos,重新设置下文件格式:[plain]:set ff=unix 保存退出:[plain]:wq 

linux执行sh报错:$’\r’: 未找到命令的解决

背景 执行.sh脚本时出现$'\r': 未找到命令, 原因 是因为命令直接从windows 复制过来导致的 解决 yum install dos2unix dos2unix **.sh 进行转换 再次执行即可

Linux自动执行sh脚本

crontab Linux 用户时程表 crontab -l 查看时程表 crontab -e 编辑时程表 crontab -r 删除时程表 一般crontab 服务是自动启用的. service crond status 命令,查看crontab 是否启用 事例:在指定时间执行/opt/start.sh,在指定时间执行/opt/stop.sh crontab -e 进入vi编辑界面 */2 * * * * sh /opt/start.sh #每隔2分钟执行一次start.sh脚本 */4 *

linux执行jmeter脚本解决响应数据为空

Linux服务器用命令执行了jmeter脚本,在本地查看结果时发现结果树种的"请求.响应数据"都显示为空,有错误日志中也看不出所以然,请看演示! 先执行脚本:执行成功(...end of run),但是发现有两个错误 从服务器到处rmw_*.jtl结果放在本地jmeter中查看,发现数据都显示空 再查看jmeter.log日志,发现失败的请求并未有错误日志(当然验证的参数错误,并非配置等错误) 接下来怎么办呢?在jmeter.properties文件中修改配置 jmeter.save.

linux下sh脚本定时删除文件

一.sh脚本如下: #!/bin/sh location="/home/sharedir/" find $location -mtime +30 -type f |xargs rm -f 保存命名为del.sh 另外两种查找相关文件的方法如下: 1)find  /home/sharedir/  -mtime +30 -type f -name *.gz 2)find  /home/sharedir/  -mtime +30 -type f |xargs 二.使用crontab -e 定

shell脚本中执行python脚本并接收其返回值的例子

1.在shell脚本执行python脚本时,需要通过python脚本的返回值来判断后面程序要执行的命令 例:有两个py程序  hello.py 复制代码代码如下: def main():    print "Hello" if __name__=='__main__':    main()world.py def main():    print "Hello" if __name__=='__main__':    main() shell 脚本 test.sh

Linux执行.sh文件Permission denied

执行sh文件报没有权限: 看下该文件的权限: 缺少执行的权限,直接加上吧: chmod 777 test.sh 权限加上去了,可以执行sh文件了 若用 chmod 4755 filename 可使此程序具有root的权限 原文地址:https://www.cnblogs.com/lyc94620/p/10008873.html