jenkins server using python script rabbitmqadmin and shell on the target server management

jenkins server using python and shell scripts rabbitmqadmin manage target servers 

preparations: 
1 .jenkins server, install rabbitmqadmi command 
rabbitmqadmin management script Download: HTTPS: // raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.7.8/ bin / rabbitmqadmin 
directly copied to the python script / usr / bin / under rabbitmqadmin, empowering 
 the chmod + X / usr / bin / rabbitmqadmin 


# test 
# SU Apache 
[Apache Jenkins @: / the root] -H rabbitmqadmin $ 1.1 . 1.1 -P 15672 -u -p chinasoft2018 CHINASOFT -f tsv - q List Queues 


2 . jumper_server and get through ssh jenkins, so jenkins can automatically log stepping stones 

windows springboard and backend also need to open up, the back-end windows install opensshfor windows and configured to trust, ramp function automatically log windows machines 

3 . require variables to modify, obtain the correct key value 
 rabbitmq_queue = CHINASOFT * 

# detection Rabbitmq number of message queue if the number of the message queue is greater than 200 within a queue message is deleted 
rabbitmq_host = 1.1 . 1.1 
rabbitmq_port = 15672 
rabbitmq_username = CHINASOFT 
rabbitmq_password = Pass 
rabbitmq_queue = CHINASOFT * 
rabbitmq_queue_max = 200 
drop_queue = () 

rabbitmqadmin -H $ rabbitmq_host -P $ rabbitmq_port $ rabbitmq_username -p -u -f $ rabbitmq_password tsv -q List Queues | the while read queue message_total ; do  if [ "$queue" == "$rabbitmq_queue" ] && [ "$message_total" -gt $rabbitmq_queue_max ]; then rabbitmqadmin -H $rabbitmq_host -P $rabbitmq_port -u $rabbitmq_username -p $rabbitmq_password delete queue name=$queue; drop_queue+=($queue);  fi  ; done

echo ${drop_queue[@]}

if [ ${#drop_queue[*]} -gt 0 ] 
then
    echo "warning 启动构建的服务"
    dir_name=bak.$(date +%Y-%m-%d)
    cp -a /data/www/vhosts/chinasoft.com /data/data_backup/chinasoft.com_$dir_name
    push_dir=/data/www/vhosts/chinasoft.com/httpdocs
    mkdir -p $push_dir

    ###生产环境的变量

    ENVIRONMENT_NAME="convert_env"
    ENVIRONMENT_VARIABLE="prod"
    WIN_ROOT="/cygdrive/d"
    WIN_RESTART_SLEEP=20
    WIN_ACCOUNT="Administrator"

    win_back_ips=('172.17.0.33' '172.17.0.32')
    execute_jump_ip="1.1.1.1"
    middle_server_ip="172.17.0.36"
    back_server_ip="172.17.0.35"
    restart_middle_group_name="online2:"
    restart_back_group_name=("chinasoft_back:" "chinasoft_server:" )

    # $1 - jump_ip
    # $2 - win_account
    # $3 - win_ip
    # $4 - win_root
    # $5 - push_dir
    # $6 - sleep
    function rsync_windows_server()
    {
        __jump_ip="$1"
        __win_account="$2"
        __win_ip="$3"
        __win_root="$4"
        __push_dir="$5"
        __sleep=$6

        shift
        shift
        shift
        shift
        shift
        shift

        __exclude_params=()
        for exclude_dir in "$@"; do
            __exclude_params+=(--exclude="$exclude_dir")
        done
        ssh -p2008 root@$execute_jump_ip "ssh -p 2008 $__win_account@$__win_ip \"export $ENVIRONMENT_NAME=$ENVIRONMENT_VARIABLE; $__win_root$__push_dir/converter_db/scripts/back_stop_consumer.bat\""
        rsync -avzP --bwlimit=1000 "${__exclude_params[@]}" $__push_dir/converter_db root@$__win_ip::root --chmod=ugo=rwX
        ssh -p2008 root@$execute_jump_ip  "ssh -p 2008 $__win_account@$__win_ip shutdown -r -t 0"
        sleep $__sleep
    } 


    rsync -vau --delete --exclude=.git/ --exclude=.gitignore/ --exclude=node_modules/ $WORKSPACE/ $push_dir
    #chmod -R ugo+r $push_dir
    chmod +x -R $push_dir/converter_db/scripts
    chmod +x -R $push_dir/converter_db/core_api

    __linux_exclude_params=()
    __linux_exclude_params+=(--exclude="converter_db/core_api/Windows")

    __windows_exclude_dir=()
    __windows_exclude_dir+=("converter_db/core_api/Linux")

    if [ "$PASS_CORE" == "true" ];then
        __linux_exclude_params+=(--exclude="converter_db/core_api")
        __windows_exclude_dir+=("converter_db/core_api"-vaRzP --progress --bwlimit =
    RsyncI)
    
1000 "${__linux_exclude_params[@]}" --password-file=/usr/local/rsync/etc/pe_rsyncd.passwd $push_dir/converter_middle apache@$middle_server_ip::apache

    if [ "$BUILD_TASK_SERVICE" == "true" ];then
        # stop consumer & rsync & restart windows
        for server_ip in "${win_back_ip[@]}"; do
            rsync_windows_server "$execute_jump_ip" "$WIN_ACCOUNT" $server_ip "$WIN_ROOT" "$push_dir" "$WIN_RESTART_SLEEP" "${__windows_exclude_dir[@]}"
        done

        rsync -vaRzP --progress --bwlimit=1000 "${__linux_exclude_params[@]}" --password-file=/usr/local/rsync/etc/pe_rsyncd.passwd $push_dir/converter_db apache@$middle_server_ip::apache
        rsync -vaRzP --progress --bwlimit=1000 "${__linux_exclude_params[@]}" --password-file= / usr / local / rsync / etc / pe_rsyncd. passwd $ push_dir / converter_db the Apache :: @ $ back_server_ip the Apache | to true 
        SSH -p2008 root @ $ execute_jump_ip   " SSH -p 2008 root @ $ middle_server_ip supervisorctl restart $ middle_celery_group " 
        SSH -p2008 @ $ execute_jump_ip the root   " SSH the root -p @ 2008 $ {$ back_server_ip supervisorctl the restart back_celery_group [@]} " 
    Fi 
Fi 



######################### #### 
# The first part: hard restart 
# PE.ali_www.chinasoft.com_monitor - new new 
############################# 

# rabbitmq about every two hours to check, monitor chinasoft.com of rabbitmq queue, if it exceeds the threshold value of 200 is triggered to restart the action 


Build PERIODICALLY
 
two hours to perform a
HH/ 2 * * * the whoami 
# detect the number of message queue Rabbitmq If the message queue number is greater than 200 within a queue message deletion 
rabbitmq_host = 1.1 . 1.2 
rabbitmq_port = 15672 
rabbitmq_username = CHINASOFT 
rabbitmq_password = Pass 
rabbitmq_queue = online_ * 
rabbitmq_queue_max = 200 is 
drop_queue = () 
rabbitmqadmin $ rabbitmq_host -P $ rabbitmq_port -H -u $ rabbitmq_username -p $ rabbitmq_password -f -q List Queues tsv | the while the Read Queue message_total; do IF [ "


  $queue" == "$rabbitmq_queue" ] && [ "$message_total" -gt $rabbitmq_queue_max ]; then rabbitmqadmin -H $rabbitmq_host -P $rabbitmq_port -u $rabbitmq_username -p $rabbitmq_password delete queue name=$queue; drop_queue+=($queue);  fi  ; done

echo ${drop_queue[@]}

if [ ${#drop_queue[*]} -gt 0 ] 
then
    echo "warning 启动重启windows服务器的脚本"
    push_dir="/data/www/vhosts/chinasoft.com/httpdocs"=
    ENVIRONMENT_NAME
    ### production environment variable
"convert_env"
    ENVIRONMENT_VARIABLE="prod"
    WIN_ROOT="/cygdrive/d"
    WIN_RESTART_SLEEP=20
    WIN_ACCOUNT="Administrator"

    win_back_ips=('172.17.0.33' '172.17.0.32')
    execute_jump_ip="1.1.1.1"
    middle_server_ip="172.17.0.36"
    back_server_ip="172.17.0.35"
    restart_middle_group_name="online2:"
    restart_back_group_name=("chinasoft_back:" "chinasoft_server:" )

    # $1 - jump_ip
    # $2 - win_account
    # $3 - win_ip
    # $4 - win_root
    # $5 - push_dir
    # $6 - sleep
    function rsync_windows_server()
    {
        __jump_ip="$1"
        __win_account="$2"
        __win_ip="$3"
        __win_root="$4"
        __push_dir="$5"
        __sleep=$6

        shift
        shift
        shift
        shift
        shift
        shift

        __exclude_params=()
        for exclude_dir in "$@"; do
            __exclude_params+=(--exclude="$exclude_dir")
        done
        ssh -p2008 root@$execute_jump_ip "ssh -p 2008 $__win_account@$__win_ip \"export $ENVIRONMENT_NAME=$ENVIRONMENT_VARIABLE; $__win_root$__push_dir/converter_db/scripts/back_stop_consumer.bat\""
        ssh -p2008 root@$execute_jump_ip  "ssh -p 2008 $__win_account@$__win_ip shutdown -r -t 0"
        sleep $__sleep
    } 


    #chmod -R ugo+r $push_dir
    chmod +x -R $push_dir/converter_db/scripts
    chmod +x -R $push_dir/converter_db/core_api

    __linux_exclude_params=()
    __linux_exclude_params+=(--exclude="converter_db/core_api/Windows")

    __windows_exclude_dir=()
    __windows_exclude_dir+=("converter_db/core_api/Linux")

    if [ "$PASS_CORE" == "true" ];then
        __linux_exclude_params+=(--exclude="converter_db/core_api")
        __windows_exclude_dir+=("converter_db/core_api")
    fi


    if [ "$BUILD_TASK_SERVICE" == "true" ];then
        # stop consumer & rsync & restart windows
        for server_ip in "${win_back_ip[@]}"; do
            rsync_windows_server "$execute_jump_ip" "$WIN_ACCOUNT" $server_ip "$WIN_ROOT" "$push_dir" "$WIN_RESTART_SLEEP" "${__windows_exclude_dir[@]}"
        done

        SSH -p2008 execute_jump_ip the root @ $   " SSH the root -p @ $ 2008 $ middle_celery_group middle_server_ip supervisorctl the restart " 
        SSH -p2008 execute_jump_ip the root @ $   " SSH the root -p @ 2008 $ {$ back_server_ip supervisorctl the restart back_celery_group [@]} " 
    Fi 
Fi 

# ############################################ 

# soft reset, hot update 

dir_name = BAK. $ ( DATE + M-%%% Y- D)
 CP -a /data/www/vhosts/chinasoft.com / Data / data_backup / chinasoft.com_ $ dir_name 
push_dir = / Data / WWW / vhosts / CHINASOFT .com / the root of httpdocs
 mkdir - the p-$ push_dir 

### production environment variable

ENVIRONMENT_NAME="convert_env"
ENVIRONMENT_VARIABLE="prod"
WIN_ROOT="/cygdrive/d"
WIN_RESTART_SLEEP=20
WIN_ACCOUNT="Administrator"

win_back_ips=('172.17.0.33' '172.17.0.32')
execute_jump_ip="1.1.1.1"
middle_server_ip="172.17.0.36"
back_server_ip="172.17.0.35"
restart_middle_group_name="online2:"
restart_back_group_name=("chinasoft_back:" "chinasoft_server:" )

# $1 - jump_ip
# $2 - win_account
# $3 - win_ip
# $4 - win_root
# $5 - push_dir
# $6 - sleep
function rsync_windows_server()
{
    __jump_ip="$1"
    __win_account="$2"
    __win_ip="$3"
    __win_root="$4"
    __push_dir="$5"
    __sleep=$6

    shift
    shift
    shift
    shift
    shift
    shift

    __exclude_params=()
    for exclude_dir in "$@"; do
        __exclude_params+=(--exclude="$exclude_dir")
    done
    ssh -p2008 root@$execute_jump_ip "ssh -p 2008 $__win_account@$__win_ip \"export $ENVIRONMENT_NAME=$ENVIRONMENT_VARIABLE; $__win_root$__push_dir/converter_db/scripts/back_stop_consumer.bat\""
    rsync -avzP --bwlimit=1000 "${__exclude_params[@]}" $__push_dir/converter_db root@$__win_ip::root --chmod=ugo=rwX
    ssh -p2008 root@$execute_jump_ip  "ssh -p 2008 $__win_account@$__win_ip shutdown -r -t 0"
    sleep $__sleep
} 


rsync -vau --delete --exclude=.git/ --exclude=.gitignore/ --exclude=node_modules/ $WORKSPACE/ $push_dir
#chmod -R ugo+r $push_dir
chmod +x -R $push_dir/converter_db/scripts
chmod +x -R $push_dir/converter_db/core_api

__linux_exclude_params=()
__linux_exclude_params+=(--exclude="converter_db/core_api/Windows")

__windows_exclude_dir=()
__windows_exclude_dir+=("converter_db/core_api/Linux")

if [ "$PASS_CORE" == "true" ];then
    __linux_exclude_params+=(--exclude="converter_db/core_api")
    __windows_exclude_dir+=("converter_db/core_api")
fi

rsync -vaRzP --progress --bwlimit=1000 "${__linux_exclude_params[@]}" --password-file=/usr/local/rsync/etc/pe_rsyncd.passwd $push_dir/converter_middle apache@$middle_server_ip::apache

if [ "$BUILD_TASK_SERVICE" == "true" ];then
    # stop consumer & rsync & restart windows
    for server_ip in "${win_back_ip[@]}"; do
        rsync_windows_server "$execute_jump_ip" "$WIN_ACCOUNT" $server_ip "$WIN_ROOT" "$push_dir" "$WIN_RESTART_SLEEP" "${__windows_exclude_dir[@]}"
    done

    rsync -vaRzP --progress --bwlimit=1000 "${__linux_exclude_params[@]}" --password-file=/usr/local/rsync/etc/pe_rsyncd.passwd $push_dir/converter_db apache@$middle_server_ip::apache
    rsync -vaRzP --progress --bwlimit=1000 "${__linux_exclude_params[@]}" --password-file=/usr/local/rsync/etc/pe_rsyncd.passwd $push_dir/converter_db apache@$back_server_ip::apache |true
    ssh -p2008 root@$execute_jump_ip  "ssh -p 2008 root@$middle_server_ip supervisorctl restart $middle_celery_group"
    ssh -p2008 root@$execute_jump_ip  "ssh -p 2008 root@$back_server_ip supervisorctl restart ${back_celery_group[@]}"
fi
fi

After the release code # to delete cache operation
# To configure http://jenkins2.chinasoft.cn/jenkins/view/pe.chinasoft/job/web_html/configure delete nginx cache
ssh -p2008 [email protected] "sudo rm -rf / dev / shm / nginx / hipdf / cache / * "

Guess you like

Origin www.cnblogs.com/reblue520/p/12053131.html