• 自动化部署shell


    yum install curl policycoreutils openssh-server openssh-clients postfix

    [root@linux-node2 ~]# yum install curl policycoreutils openssh-server openssh-clients postfix
    [root@linux-node2 ~]# systemctl start postfix
    
    
    curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh| sudo bash
    
    curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh| bash
    
    yum install -y gitlab-ce
    
    
    [root@linux-node2 ~]# vim /etc/yum.repos.d/gitlab-ce.repo
    [gitlab-ce]
    name=gitlab-ce
    baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
    repo_gpgcheck=0
    gpgcheck=0
    enabled=1
    gpgkey=https://packages.gitlab.com/gpg.key
    [root@linux-node2 ~]# yum makecache
    [root@linux-node2 ~]# yum install gitlab-ce
    
    
    /etc/gitlab
    [root@node2 gitlab]# egrep -v "^$|^#" gitlab.rb
    external_url 'http://192.168.3.4.com'
    
    gitlab-ctl reconfigure
    
    gitlab-ctl status
    
    
    
    关闭gitlab:[root@linux-node2 ~]# gitlab-ctl stop
    启动gitlab:[root@linux-node2 ~]# gitlab-ctl start
    重启gitlab:[root@linux-node2 ~]# gitlab-ctl restart
    
    
    
    
    
    最终脚本
    
    #!/bin/bash
    
    # Dir List
    # mkdir -p /deploy/code/web-demo
    # mkdir -p /deploy/config/web-demo/base
    # mkdir -p /deploy/config/web-demo/other
    # mkdir -p /deploy/tar
    # mkdir -p /deploy/tmp
    # mkdir -p /opt/webroot
    # mkdir -p /webroot
    # chown -R www.www /deploy
    # chown -R www.www /opt/webroot
    # chown -R www.www /webroot
    
    # Node List
    PRE_LIST="192.168.3.3"
    GROUP2_LIST="192.168.3.4"
    NODE_LIST="192.168.3.3 192.168.3.4"
    ROLLBACK_LIST="192.168.3.3 192.168.3.4"
    
    # Date/Time Veriables
    LOG_CTIME=$(date "+%H-%M-%S")
    LOG_CDATE=$(date "+%Y-%m-%d")
    CTIME="date +%Y-%m-%d"
    CDATE="date +%H-%M-%S"
    
    
    # Shell Env
    
    PRO_NAME="web-demo"
    SHELL_NAME="deploy.sh"
    SHELL_DIR="/home/www/"
    SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"
    LOCK_FILE="/tmp/deploy.lock"
    
    
    # Code Env
    
    CODE_DIR="/deploy/code/web-demo"
    CONFIG_DIR="/deploy/config/web-demo"
    TMP_DIR="/deploy/tmp"
    TAR_DIR="/deploy/tar"
    
    url_test(){
        URL=$1
        curl -s --head $1 |grep '200 OK'
        if [ $? -ne 0 ]; then
            shell_unlock;
            writelog "test error" &&exit;
        fi
    }
    
    
    usage(){
        echo $"Usage $0 { deploy | rollback [ list | version ] }"
    }
    
    writelog(){
        LOGINFO=$1
        echo "`${CDATE}` `${CTIME}`: ${SHELL_NAME} : ${LOGINFO} " >> ${SHELL_LOG}
    }
    
    shell_lock(){
        touch ${LOCK_FILE}
    }
    
    shell_unlock(){
        rm -f ${LOCK_FILE}
    }
    
    code_get(){
        echo code_get
        writelog "code_get"
        cd ${CODE_DIR} && git pull
        cp -rp ${CODE_DIR} ${TMP_DIR}/
        API_VERL=`git show|grep commit|cut -d ' ' -f2`
        API_VER=${API_VERL:0:6}
    
    }
    
    code_build(){
        echo code_build
    }
    
    code_config(){
        echo code_config
        writelog "code_config"
        /bin/cp -rp ${CONFIG_DIR}/base/*  ${TMP_DIR}/${PRO_NAME}
        PKG_NAME="${PRO_NAME}"_"${API_VER}"-"`$CDATE`-`${CTIME}`"
        cd ${TMP_DIR}  && mv ${PRO_NAME} ${PKG_NAME}
    
    }
    
    code_tar(){
        echo code_tar
        writelog "code_tar"
        cd ${TMP_DIR} && tar czf ${PKG_NAME}.tar.gz ${PKG_NAME}
        writelog "${PKG_NAME}.tar.gz"
    }
    
    code_scp(){
        echo code_scp
        writelog "code_scp"
        for node in $PRE_LIST ;do
            scp ${TMP_DIR}/${PKG_NAME}.tar.gz $node:/opt/webroot/
        done
        for node in $GROUP2_LIST ;do
            scp ${TMP_DIR}/${PKG_NAME}.tar.gz $node:/opt/webroot/
        done
    }
    
    cluster_node_remove(){
        writelog "cluster_node_remove"
        echo cluster_node_remove
    }
    
    pre_deploy(){
        writelog "remove from cluster pre"
        echo code_deploy
        for node in $PRE_LIST ;do
            ssh $node "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
            ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
        done
    }
    
    pre_test(){
        url_test "http://${PRE_LIST}/index.html"
        echo "add to cluster pre"
    }
    
    group2_deploy(){
        writelog "remove from cluster 2"
        for node in $GROUP2_LIST ;do
            ssh $node "cd /opt/webroot && tar zxf ${PKG_NAME}.tar.gz"
            ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
        done
        scp ${CONFIG_DIR}/other/192.168.3.4.crontab.xml 192.168.3.4:/webroot/web-demo/crontab.xml
    }
    
    group2_test(){
        for node in $GROUP2_LIST ;do
            url_test "http://${node}/index.html"
        done
        echo "add to cluster group2"
    }
    
    config_diff(){
        echo config_diff
    
    }
    
    
    code_test(){
        echo code_test
    }
    
    cluster_node_in(){
        echo cluster_node_in
    }
    
    rollback_fun(){
        [ -z $1 ] && echo "input rollback version" &&return
        for node in $ROLLBACK_LIST ;do
            ssh $node "[ -d /opt/webroot/$1 ] && rm -f /webroot/web-demo && ln -s /opt/webroot/$1 /webroot/web-demo"
        done
    
    }
    
    rollback (){
        case $1 in
            list)
                ls -l /opt/webroot/*.tar.gz
                ;;
            *)
                rollback_fun $1
        esac
    
    }
    
    main(){
        if [ -f $LOCK_FILE ]; then
            echo "Deploy is running" && exit;
        fi
        shell_lock
        DEPLOY_METHOD=$1
        ROLLBACK_VER=$2
        case $DEPLOY_METHOD in
            deploy)
                code_get;
                code_build;
                code_config;
                code_tar;
                code_scp;
                pre_deploy;
                pre_test;
                group2_deploy;
                group2_test;
                config_diff;
                code_test;
                cluster_node_in;
                ;;
            rollback)
                rollback $ROLLBACK_VER
                ;;
    
            *)
                usage;
        esac
        shell_unlock
    }
    
    main $1 $2
  • 相关阅读:
    pymssql连接Azure SQL Database
    Python升级后ssl模块不可用问题解决和浅析
    CentOS 7升级Python到3.6.6后yum出错问题解决总结
    Python监控SQL Server数据库服务器磁盘使用情况
    fastjason常用方法
    类型擦除真的能完全擦除一切信息吗?java 泛型揭秘
    spring boot打包成war包的页面该放到哪里?
    为什么delete后磁盘空间没有释放而truncate会释放?
    leetcode 977. Squares of a Sorted Array
    leetcode 844. Backspace String Compare
  • 原文地址:https://www.cnblogs.com/WisWang/p/6537290.html
Copyright © 2020-2023  润新知