首页 » Python » 基础进阶 » 正文

Python3_模拟生产环境 AB组 切换流量上线

#########python基础第二周作业需求
模拟生产环境 AB 切换流量上线 ,基于nginx 反向代理
配置文件 在目录中,请讲执行文件放在 bin目录下

##需求:
1:通过 python交互 执行 启动程序 (input方式)
2:通过函数处理每一步操作
3:流程
    3.1 先进行问询, 提供几个选择,然后根据选择 执行相对的代码,比如
        现在要做的是:()
            A.A组切上去
            B.A组切下来
            C.B组切上去
            D.B组切下来
            E.重启Nginx服务
            F.完成迭代
            G.更新代码 (可以写成伪代码 print('代码已更新完成'))

    3.1 先进行 A   或者  B组的 上线,
        每组上线 都需要  将那个组对应的主机 nginx配置中切下来 (也就是把权重调整为0,或者注释掉)
        然后重启Nginx服务
        然后更新代码
        然后将那个组  对应的主机  nginx配置文件中 切上来
        然后重启Nginx服务

        另外一个组重复上面这个操作
        然后完成更新迭代

4.修改 readme文件, 将你自己的逻辑写在这个文件下面

代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018-12-1 13:27
# @Author  : zhdya@zhdya.cn
# @File    : ops_nginx.py


groupA = ['10.0.0.1:8080', '10.0.0.2:8080']
groupB = ['192.168.0.1:8080', '192.168.0.2:8080']

def startA():
    with open('../conf/nginx.conf', 'r', encoding='utf-8') as f:
        datas = f.readlines()
    with open('../conf/nginx.conf', 'w', encoding='utf-8') as fw:
        for line in datas:
            if line.strip().startswith('server') and line.strip().split()[1] in groupA:
                print("ERROR!! the {0} is Running now!!".format(line.strip().split()[1]))
            elif line.strip().startswith('#server') and line.strip().split()[1] in groupA:
                line = line.replace('#server','server')
                print("Start {0} successful!!".format(line.strip().split()[1]))
            fw.write(line)


def shutdownA():
    with open('../conf/nginx.conf', 'r', encoding='utf-8') as f:
        datas = f.readlines()
    with open('../conf/nginx.conf', 'w', encoding='utf-8') as fw:
        for line in datas:
            if line.strip().startswith('#server') and line.strip().split()[1] in groupA:
                print("ERROR!! The {0} Already shutdown!!".format(line.strip().split()[1]))
            elif line.strip().startswith('#server') and line.strip().split()[1] in groupB:
                print("WARNING!! The {0} is Shutdown now!!".format(line.strip().split()[1]))
            elif line.strip().startswith('server') and line.strip().split()[1] in groupA:
                line = line.replace('server','#server')
                print("Shutdown {0} successful!!".format(line.strip().split()[1]))
            fw.write(line)


def startB():
    with open('../conf/nginx.conf', 'r', encoding='utf-8') as f:
        datas = f.readlines()
    with open('../conf/nginx.conf', 'w', encoding='utf-8') as fw:
        for line in datas:
            if line.strip().startswith('server') and line.strip().split()[1] in groupB:
                print("ERROR!! the {0} is Running now!!".format(line.strip().split()[1]))
            elif line.strip().startswith('#server') and line.strip().split()[1] in groupB:
                line = line.replace('#server','server')
                print("Start {0} successful!!".format(line.strip().split()[1]))
            fw.write(line)


def shutdownB():
    with open('../conf/nginx.conf', 'r', encoding='utf-8') as f:
        datas = f.readlines()
    with open('../conf/nginx.conf', 'w', encoding='utf-8') as fw:
        for line in datas:
            if line.strip().startswith('#server') and line.strip().split()[1] in groupB:
                print("ERROR!! The {0} Already shutdown!!".format(line.strip().split()[1]))
            elif line.strip().startswith('#server') and line.strip().split()[1] in groupA:
                print("WARNING!! The {0} is Shutdown now!!".format(line.strip().split()[1]))
            elif line.strip().startswith('server') and line.strip().split()[1] in groupB:
                line = line.replace('server','#server')
                print("Shutdown {0} successful!!".format(line.strip().split()[1]))
            fw.write(line)


def restartNginx():
    print("Restart Nginx successful!!")


def updateCode():
    print("Update Code successful!!")


if __name__ == '__main__':
    print('1:start nginx_A\n2:Shutdown nginx_A\n3:start nginx_B\n4:shutdown nginx_B\n5:restart Nginx\n6:update code')
    while 1:
        input_num = input('Pls choice the number [quit for exit]: ')
        if input_num.isdigit():
            input_num = int(input_num)
            if(input_num==1):
                startA()
            elif(input_num==2):
                shutdownA()
            elif(input_num==3):
                startB()
            elif(input_num==4):
                shutdownB()
            elif (input_num == 5):
                restartNginx()
            elif(input_num==6):
                updateCode()
            else:
                print("pls just input 1, 2, 3, 4!!")
                continue
        elif (input_num == "quit"):
            break
        else:
            print("you input was wrong!!")
            continue

功能简介:

#############################################
###基本功能:
- 程序已经实现可以通过交互方式选择需要的功能;
    - 一旦运行就会一直循环,但是可以输入“quit 退出;
    - 一旦输入非数字或者非菜单栏指定数字会根据输入去判断并提示;

###程序功能:
- 可以实现将各组负载批量更新,并提示哪些负载已经更新“Start XXX successful!!
- 可以实现将各组负载批量下线,并提示哪些负载已经更新“Shutdown XXX successful!!

###程序特色:
- 当负载已经在运行中,再次去启动会有提示“ERROR!! the XXX is Running now!!
- 当负载已经停止,再次去停止会有提示“ERROR!! The XXX Already shutdown!!
- 模拟真实线上机器(一旦两组负载都下掉会出现线上故障),故当负载B已经停止了,再次去停止A,会有提示“WARNING!! The <B group ip> is Shutdown now!!

###程序演示执行结果:
1:start nginx_A
2:Shutdown nginx_A
3:start nginx_B
4:shutdown nginx_B
5:restart Nginx
6:update code
Pls choice the number [quit for exit]: 1
Start 10.0.0.1:8080 successful!!
Start 10.0.0.2:8080 successful!!
Pls choice the number [quit for exit]: 3
Start 192.168.0.1:8080 successful!!
Start 192.168.0.2:8080 successful!!
Pls choice the number [quit for exit]: 1
ERROR!! the 10.0.0.1:8080 is Running now!!
ERROR!! the 10.0.0.2:8080 is Running now!!
Pls choice the number [quit for exit]: 2
Shutdown 10.0.0.1:8080 successful!!
Shutdown 10.0.0.2:8080 successful!!
Pls choice the number [quit for exit]: 4
WARNING!! The 10.0.0.1:8080 is Shutdown now!!
WARNING!! The 10.0.0.2:8080 is Shutdown now!!
Shutdown 192.168.0.1:8080 successful!!
Shutdown 192.168.0.2:8080 successful!!
Pls choice the number [quit for exit]: 2
ERROR!! The 10.0.0.1:8080 Already shutdown!!
ERROR!! The 10.0.0.2:8080 Already shutdown!!
WARNING!! The 192.168.0.1:8080 is Shutdown now!!
WARNING!! The 192.168.0.2:8080 is Shutdown now!!
Pls choice the number [quit for exit]: 3
Start 192.168.0.1:8080 successful!!
Start 192.168.0.2:8080 successful!!
Pls choice the number [quit for exit]: 1
Start 10.0.0.1:8080 successful!!
Start 10.0.0.2:8080 successful!!
Pls choice the number [quit for exit]: quit
赞 (5)

发表评论