jenkins服务器使用python脚本rabbitmqadmin和shell对目标服务器进行管理

准备工作:
.jenkins服务器,安装rabbitmqadmi命令
rabbitmqadmin管理脚本下载地址:https://raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.7.8/bin/rabbitmqadmin
直接将python脚本拷贝到 /usr/bin/rabbitmqadmin下,赋权
chmod +x /usr/bin/rabbitmqadmin # 测试
# su apache
[apache@jenkins:/root]$ rabbitmqadmin -H 1.1.1.1 -P -u chinasoft -p chinasoft2018 -f tsv -q list queues .打通jenkins和jumper_server的ssh,让jenkins能自动登录跳板机 跳板机和后端的windows也需要打通,后端windows安装openssh for windows并且配置互信,跳板机能自动登录windows机器 .需要修改的变量,获取正确的key值
rabbitmq_queue=chinasoft* # 检测Rabbitmq 消息队列数量 如果消息队列内数量大于200 则删除队列内消息
rabbitmq_host=1.1.1.1
rabbitmq_port=
rabbitmq_username=chinasoft
rabbitmq_password=pass
rabbitmq_queue=chinasoft*
rabbitmq_queue_max=
drop_queue=() rabbitmqadmin -H $rabbitmq_host -P $rabbitmq_port -u $rabbitmq_username -p $rabbitmq_password -f tsv -q list queues | 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 ]
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=
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:" ) # $ - jump_ip
# $ - win_account
# $ - win_ip
# $ - win_root
# $ - push_dir
# $ - sleep
function rsync_windows_server()
{
__jump_ip="$1"
__win_account="$2"
__win_ip="$3"
__win_root="$4"
__push_dir="$5"
__sleep=$ 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= "${__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= "${__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= "${__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= "${__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 #############################
#第一部分:硬重启
#PE.ali_www.chinasoft.com_monitor-new
############################# # 每隔两小时检查一下rabbitmq,监控chinasoft.com的rabbitmq队列,如果超过200的阈值,则触发重启动作 Build periodically 两小时执行一次
H H/ * * * whoami
# 检测Rabbitmq 消息队列数量 如果消息队列内数量大于200 则删除队列内消息
rabbitmq_host=1.1.1.2
rabbitmq_port=
rabbitmq_username=chinasoft
rabbitmq_password=pass
rabbitmq_queue=online_*
rabbitmq_queue_max=
drop_queue=() rabbitmqadmin -H $rabbitmq_host -P $rabbitmq_port -u $rabbitmq_username -p $rabbitmq_password -f tsv -q list queues | 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 ]
then
echo "warning 启动重启windows服务器的脚本"
push_dir="/data/www/vhosts/chinasoft.com/httpdocs"
###生产环境的变量 ENVIRONMENT_NAME="convert_env"
ENVIRONMENT_VARIABLE="prod"
WIN_ROOT="/cygdrive/d"
WIN_RESTART_SLEEP=
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:" ) # $ - jump_ip
# $ - win_account
# $ - win_ip
# $ - win_root
# $ - push_dir
# $ - sleep
function rsync_windows_server()
{
__jump_ip="$1"
__win_account="$2"
__win_ip="$3"
__win_root="$4"
__push_dir="$5"
__sleep=$ 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 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 ############################################# # 软重启,热更新 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=
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:" ) # $ - jump_ip
# $ - win_account
# $ - win_ip
# $ - win_root
# $ - push_dir
# $ - sleep
function rsync_windows_server()
{
__jump_ip="$1"
__win_account="$2"
__win_ip="$3"
__win_root="$4"
__push_dir="$5"
__sleep=$ 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= "${__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= "${__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= "${__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= "${__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

# 发布代码以后删除缓存操作
# 要配置http://jenkins2.chinasoft.cn/jenkins/view/pe.chinasoft/job/web_html/configure  删除nginx的缓存
ssh -p2008 devadmin@1.1.1.1 "sudo rm -rf /dev/shm/nginx/hipdf/cache/*"

jenkins服务器使用python脚本rabbitmqadmin和shell对目标服务器进行管理的更多相关文章

  1. Jenkins自动执行python脚本输出测试报告

    前言 在用python做自动化测试时,我们写好代码,然后需要执行才能得到测试报告,这时我们可以通过 Jenkins 来进一步完成自动化工作. 借助Jenkins,我们可以结合 Git/SVN 自动拉取 ...

  2. 【jenkins】jenkins实时显示python脚本输出

    jenkins在构建shell脚本时可以实时输出结果,但是在构建python脚本时,是等到python执行完成以后,才显示结果,这个对于我们判断脚本执行状态非常不利 这里介绍一种方法,能够实时显示py ...

  3. Python——scapy模块实现tcp探测目标服务器路由轨迹

      scapy模块的安装 484 yum install tcpdump graphviz ImageMagick -y 485 wget http://www.secdev.org/projects ...

  4. shell如何向python传递参数,shell如何接受python的返回值

    1.shell如何向python传递参数 shell脚本 python $sendmailCommandPath $optDate python脚本 lastDateFormat = sys.argv ...

  5. python脚本运行的几种方式

    1.脚本式编程 将如下代码拷贝至 hello.py文件中: print ("Hello, Python!"); 通过以下命令执行该脚本: $ python ./hello.py h ...

  6. XShell通过中转服务器直接连接目标服务器

    最近由于公司生产环境的变化,使得我们不能使用自己的机器连接到生产环境去,而是要通过跳板机中转才可以连接.于是今天尝试使用 XShell 通过跳板机直接转接到生产环境. 一.使用代理方式 首先填写连接信 ...

  7. SqlServer作业指定目标服务器

    用SSMS生成数据库作业的创建脚本的时候,有一步是sp_add_jobserver操作: EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = ...

  8. jenkins发布程序触发shell调用python脚本刷新akamai cdn api

    刷新cdn的流程:jenkins获取git中的代码,触发脚本推送到生产环境中(即cdn的源站) --> 触发脚本获取git工作目录的更新列表,将更新列表拼凑成带域名信息的url,写入到目录中 - ...

  9. 【原】Gradle调用shell脚本和python脚本并传参

    最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结. Pre build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenki ...

随机推荐

  1. 常用内置模块(三)--subprocess、re

    一.subprocess模块 进程:一个正在运行的程序 子进程:在父进程运行的过程中在其内部又开启了一个进程,即子进程. 作用:用于执行系统命令 os.system也可以获取当前的进程信息,但是它只能 ...

  2. centos 服务器 nginx 负载均衡服务安装

    yum -y install gcc gcc-c++ autoconf automake libtool make cmake yum -y install zlib zlib-devel opens ...

  3. [2019.05.09]Linux 学习笔记(3)

    最近的心得: CLI真好用,GUI就是渣渣 1. Bash 里面的命令是可以起别名的,起一个别名的方法是 alias [Alias]=[command] command可以是任意长的别名,比如 ali ...

  4. pandas用法总结

    pandas用法总结 2018年06月07日 10:49:03 一夜了 阅读数 38705更多 分类专栏: 杂项   一.生成数据表 1.首先导入pandas库,一般都会用到numpy库,所以我们先导 ...

  5. win10如何找回自带的照片查看器

    1.接着我们在打开的记事本中一字不差的输入以下代码 Windows Registry Editor Version 5.00 ; Change Extension's File Type [HKEY_ ...

  6. G6 学习资料

    G6 学习资料 网址 G6 1.x API 文档 http://antvis.github.io/g6/doc/index.html 官方demo列表 https://github.com/antvi ...

  7. python --装饰器内容讲解

    python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能. 3.1 定义装饰器 ...

  8. python--递归函数讲解

    递归算法是一种直接或间接调用自身算法的过程. 递归算法解决问题的特点: (1)递归就是在过程或函数里调用自身 (2)在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口. (3)递归算法解题通 ...

  9. Pandas模块 -- 实操练习

    如果对序列进行数学函数的运算,首选numpy模块: 如果对序列做统计运算,首选序列的“方法”,因为序列的“方法”更加丰富,如计算序列的偏度.峰度等,而Numpy模块是没有这样的函数. 手工构造数据框D ...

  10. Python错误 -- try/except/finally 、调用栈、记录错误、抛出错误

    Bug:程序编写有问题造成的错误,称之为Bug.    debug:调试 注意:bug是程序本身有问题.有缺陷.系统漏洞 异常:完全无法在程序运行中预测的错误,例如写入文件的时候,磁盘满了,写不进去了 ...