Esempio n. 1
0
def insertnewzone(alist):
    """新插入一个元素(list 操作)
    """
    listitems = gamezones()
    dbnum = alist['db']

    res = getresbynum(dbnum)
    if res:
        #res = res.replace("'",'').lstrip('[(').rstrip(')]').split(',')
        res = eval(res)[0]
    else:
        write_logger('error', "CSServer return Num error!!")
        sys.exit(100)

    dbvalue = res[2].strip()
    dbname = res[3].strip()
    alist = argvToAlist(dbnum, dbvalue, dbname)

    listitems.append(alist)
    astr = "update iswork=1 dbnum=" + str(dbnum)

    res = getres(astr, host=csip)

    write_xml_alist(sortitems(listitems))
    sys.exit()
Esempio n. 2
0
def writexmlbyxml(alist):
    """根据alist生成xml,然后让函数  **write_xml** 写入。
    """
    global filetopath
    domaiversion = getversion()
    root = ET.fromstring("<configs> </configs>")
    add_child_subelement(root, create_element("config", {'name': 'server_path', 'value': '/data/release/sgonline'},''))
    add_child_subelement(root, create_element("config", {'name': 'sversion', 'value': domaiversion}, ''))

    if len(alist) > 0:
        dbnum = int(alist[0]['db'])
        res = getresbynum(dbnum)
        if res :
            #res = res.replace("'",'').lstrip('[(').rstrip(')]').split(',')
            res = eval(res)[0]
        else :
            write_logger('error',"CSServer return Num error!!")
            sys.exit(100)

        ip = res[1].strip()

        add_child_subelement(root, create_element("config", {'name':ip,'db':alist[0]['db'],'value': alist[0]['value']},''))

    for line in alist:
        add_child_subelement(root, create_element("config", line,''))
    indent(root)
    write_xml(ET.ElementTree(root),filetopath)
Esempio n. 3
0
def  insertnewzone(alist):
    """新插入一个元素(list 操作)
    """
    listitems = gamezones()
    dbnum = alist['db']

    res = getresbynum(dbnum)
    if res:
        #res = res.replace("'",'').lstrip('[(').rstrip(')]').split(',')
        res = eval(res)[0]
    else :
        write_logger('error',"CSServer return Num error!!")
        sys.exit(100)

    dbvalue = res[2].strip()
    dbname = res[3].strip()
    alist = argvToAlist(dbnum,dbvalue,dbname)

    listitems.append(alist)
    astr = "update iswork=1 dbnum=" + str(dbnum)

    res = getres(astr,host=csip)

    write_xml_alist(sortitems(listitems))
    sys.exit()
Esempio n. 4
0
def kaixinqu(alist):
    """开新区程序的操作非常的简单,操作如下::

        1. 参数判断,必须是两个参数
        2. 是否为新区判断
        3. 开区脚本执行

    需要注意的是::

        1. 参数中第一个为 *dbnum* ,第二个为 *time* ,时间参数的格式必须为"20150101"(8位)


    """
    # 因为只有一个正确结果,所以只需要删除两边的'[('和')]'
    #res = getres("findbydb " + str(num)).lstrip('[(').rstrip(')]').split(',')
    # 20151126 追加: 可以使用eval函数进行处理

    if len(alist) != 2:
        write_logger('error',"len(cmd) error")
        return [False, "len(cmd) error!!"]
    num = int(alist[0])
    time = alist[1]
    if len(time) !=  8:
        write_logger('error',"len(time)(required: 8)  error")
        return [False,"Time error!!,len(time)(required: 8) error"]


    res = getresbynum(num)
    print res
    if res :
        res = eval(res)[0]
    else :
        write_logger('error',"CSServer return  None!!")
        return [False,"CSServer return Num error!!"]

    # tag 用来判断是否是新区
    print res
    tag = int(res[4])
    if tag != 0 :
        write_logger('error',"Not new zone")
        return [False,"Not new zone"]



    # 2, 进行开区文件copy,重新启动cgi
    command = "sh commands/shelltools/CreateNewZone.sh" + " " +  str(num) +" "+ time
    #command = "echo $PATH"
    subp = subprores(command)
    if not subp:
        write_logger('error',"sh: CreateNewZone.sh, ERROR occurred!!!")
        return [False,"sh: CreateNewZone.sh, ERROR occurred!!!"]

    return [True,"Execution OK"]
Esempio n. 5
0
def kaixinqu(alist):
    """开新区程序的操作非常的简单,操作如下::

        1. 参数判断,必须是两个参数
        2. 是否为新区判断
        3. 开区脚本执行

    需要注意的是::

        1. 参数中第一个为 *dbnum* ,第二个为 *time* ,时间参数的格式必须为"20150101"(8位)


    """
    # 因为只有一个正确结果,所以只需要删除两边的'[('和')]'
    #res = getres("findbydb " + str(num)).lstrip('[(').rstrip(')]').split(',')
    # 20151126 追加: 可以使用eval函数进行处理

    if len(alist) != 2:
        write_logger('error', "len(cmd) error")
        return [False, "len(cmd) error!!"]
    num = int(alist[0])
    time = alist[1]
    if len(time) != 8:
        write_logger('error', "len(time)(required: 8)  error")
        return [False, "Time error!!,len(time)(required: 8) error"]

    res = getresbynum(num)
    print res
    if res:
        res = eval(res)[0]
    else:
        write_logger('error', "CSServer return  None!!")
        return [False, "CSServer return Num error!!"]

    # tag 用来判断是否是新区
    print res
    tag = int(res[4])
    if tag != 0:
        write_logger('error', "Not new zone")
        return [False, "Not new zone"]

    # 2, 进行开区文件copy,重新启动cgi
    command = "sh commands/shelltools/CreateNewZone.sh" + " " + str(
        num) + " " + time
    #command = "echo $PATH"
    subp = subprores(command)
    if not subp:
        write_logger('error', "sh: CreateNewZone.sh, ERROR occurred!!!")
        return [False, "sh: CreateNewZone.sh, ERROR occurred!!!"]

    return [True, "Execution OK"]
Esempio n. 6
0
def getIpAndDBnum(alist):
    """ 获得ip和合区的列表。有两种形式::

         1. 给了迁区的ip,则直接返回ip和dbnumlist。
         2. 没有给出ip,则获得dbnumlist的第一个区的ip,然后返回。

    这里面的ip为迁区到的ip。理论上迁区在任何服务器上都可以。

    """
    ipcheck  = str(type(alist[0]))
    if 'int' not in ipcheck :
        ip = alist[0].split('=')[1]
        dbnumlist=alist[1:]
    else:
        #ip=getresbynum(alist[0]).replace("'",'').lstrip('[(').rstrip(')]').split(',')[1].strip()
        ip = eval(getresbynum(alist[0]))[0][1]
        dbnumlist=alist
    return ip,dbnumlist
Esempio n. 7
0
def getIpAndDBnum(alist):
    """ 获得ip和合区的列表。有两种形式::

         1. 给了迁区的ip,则直接返回ip和dbnumlist。
         2. 没有给出ip,则获得dbnumlist的第一个区的ip,然后返回。

    这里面的ip为迁区到的ip。理论上迁区在任何服务器上都可以。

    """
    ipcheck = str(type(alist[0]))
    if 'int' not in ipcheck:
        ip = alist[0].split('=')[1]
        dbnumlist = alist[1:]
    else:
        #ip=getresbynum(alist[0]).replace("'",'').lstrip('[(').rstrip(')]').split(',')[1].strip()
        ip = eval(getresbynum(alist[0]))[0][1]
        dbnumlist = alist
    return ip, dbnumlist
Esempio n. 8
0
def writexmlbytext(alist):
    global filetopath
    domaiversion = getversion()

    ###################
    domai_xml_lines = []
    domai_xml_lines.append(r'<?xml version="1.0" encoding="UTF-8" ?>')
    domai_xml_lines.append('\n')
    domai_xml_lines.append(r'<configs>')
    domai_xml_lines.append('\n')
    domai_xml_lines.append(
        '\t' + r'<config name="server_path" value="/data/release/sgonline" />')
    domai_xml_lines.append('\n')
    domai_xml_lines.append('\t' + r'<config name="sversion" value="' +
                           str(domaiversion) + r'" />')
    domai_xml_lines.append('\n')
    #############

    if len(alist) > 0:
        dbnum = int(alist[0]['db'])
        res = getresbynum(dbnum)
        if res:
            res = eval(res)[0]
        else:
            write_logger('error', "CSServer return Num error!!")
            sys.exit(100)

        ip = res[1].strip()
        domai_xml_lines.append('\t' + r'<config name="' + str(ip) +
                               r'" value="' + str(alist[0]['value']) +
                               r'" db="' + str(alist[0]['db']) + '" />')
        domai_xml_lines.append('\n')

    for line in alist:
        domai_xml_lines.append('\t' + r'<config name="' + str(line['name']) +
                               r'" value="' + str(line['value']) + r'" db="' +
                               str(line['db']) + '" />')
        domai_xml_lines.append('\n')
    domai_xml_lines.append(r'</configs>')
    domai_xml_lines.append('\n')

    fp = open(filetopath, 'w')
    fp.writelines(domai_xml_lines)
    fp.close()
Esempio n. 9
0
def writexmlbyxml(alist):
    """根据alist生成xml,然后让函数  **write_xml** 写入。
    """
    global filetopath
    domaiversion = getversion()
    root = ET.fromstring("<configs> </configs>")
    add_child_subelement(
        root,
        create_element("config", {
            'name': 'server_path',
            'value': '/data/release/sgonline'
        }, ''))
    add_child_subelement(
        root,
        create_element("config", {
            'name': 'sversion',
            'value': domaiversion
        }, ''))

    if len(alist) > 0:
        dbnum = int(alist[0]['db'])
        res = getresbynum(dbnum)
        if res:
            #res = res.replace("'",'').lstrip('[(').rstrip(')]').split(',')
            res = eval(res)[0]
        else:
            write_logger('error', "CSServer return Num error!!")
            sys.exit(100)

        ip = res[1].strip()

        add_child_subelement(
            root,
            create_element("config", {
                'name': ip,
                'db': alist[0]['db'],
                'value': alist[0]['value']
            }, ''))

    for line in alist:
        add_child_subelement(root, create_element("config", line, ''))
    indent(root)
    write_xml(ET.ElementTree(root), filetopath)
Esempio n. 10
0
def arch_zone(alist):
    """根据合区的区号进行归档,得到ip-numlist 的对应关系,生成字典。
    """
    adict = {}
    for dbnum in alist:
        res = getresbynum(dbnum)
        if res :
            #res = res.replace("'",'').lstrip('[(').rstrip(')]').split(',')
            res = eval(res)[0]
        else :
            write_logger('error',"CSServer return  None!!")
            return [False,"CSServer return Num error!!"]

        ip =  res[1].strip()
        if ip in adict.keys():
            adict[ip] = adict[ip] + [dbnum,]
        else:
            adict[ip] = [dbnum,]

    return  adict
Esempio n. 11
0
def writexmlbytext(alist):
    global filetopath
    domaiversion = getversion()

    ###################
    domai_xml_lines=[]
    domai_xml_lines.append(r'<?xml version="1.0" encoding="UTF-8" ?>')
    domai_xml_lines.append('\n')
    domai_xml_lines.append(r'<configs>')
    domai_xml_lines.append('\n')
    domai_xml_lines.append('\t' + r'<config name="server_path" value="/data/release/sgonline" />')
    domai_xml_lines.append('\n')
    domai_xml_lines.append('\t' + r'<config name="sversion" value="' + str(domaiversion)+r'" />')
    domai_xml_lines.append('\n')
    #############

    if len(alist) > 0:
        dbnum = int(alist[0]['db'])
        res = getresbynum(dbnum)
        if res :
            res = eval(res)[0]
        else :
            write_logger('error',"CSServer return Num error!!")
            sys.exit(100)

        ip = res[1].strip()
        domai_xml_lines.append('\t' + r'<config name="' + str(ip) + r'" value="' + str(alist[0]['value']) + r'" db="'+ str(alist[0]['db'])+'" />')
        domai_xml_lines.append('\n')

    for line in alist:
        domai_xml_lines.append('\t' + r'<config name="' + str(line['name']) + r'" value="' + str(line['value']) + r'" db="'+ str(line['db']) + '" />')
        domai_xml_lines.append('\n')
    domai_xml_lines.append(r'</configs>')
    domai_xml_lines.append('\n')

    fp = open(filetopath,'w')
    fp.writelines(domai_xml_lines)
    fp.close()
Esempio n. 12
0
def hequ(alist):
    """ 合区的核心程序。
    目前涉及的几个步骤如下::

        1. 获取ip,numlist 并进行归档。
        2. 根据判断ip是否为本地进行分类操作

             * ip为本地ip,停服。
             * ip不是本机ip,则向该ip发送 **tarzone** 命令

        3. CSserver的数据库更改。主要是更改 *ip* 和 *value*
        4. 由于打包过来的文件日志目录缺失,增加日志目录
        5. 合区脚本执行
        6. 本地domai.conf配置增加。在 *第二步* 的时候将在domai.conf中本地的要合区的dbnum进行了删除。

    需要注意的是::
        1. *ip* 并不是必须存在的,如果存在合区到 *ip* 所在服务器上,如果不存在则合区到第一个 *dbnum* 所在的服务器上。

    """
    localip,dbnumlist = getIpAndDBnum(alist)

    for dbnum in dbnumlist:
        res = getresbynum(dbnum)
        print res
        if res :
            #res = res.replace("'",'').lstrip('[(').rstrip(')]').split(',')
            res = eval(res)[0]
        else :
            write_logger('error',"CSServer return  None!!")
            return [False,"CSServer return Num error!!"]

        # tag 用来判断是否是新区
        print res
        tag = int(res[4])
        if tag != 1 :
            write_logger('error',"Not old zone, can't hequ")
            return [False,"Not old zone . can't hequ"]

    zonedict = arch_zone(dbnumlist)
    print zonedict


    exittag = False
    for ip in zonedict.keys():
        if localip != ip :
            astr = "tarzone " + " ".join([str(x) for x in zonedict[ip]])
            write_logger('info', "sh: %s Ok" % astr)
            getres(astr,host=ip, port=1012)
            # 下载解压区服
            command = "sh commands/shelltools/scp.sh  " +  ip
            subp = subprores(command)
            if not subp:
                write_logger('error',"%s ERROR" % command)
                exittag = True
                return [False,"%s  ERROR occurred!!!" % command ]
            write_logger('info',"%s EXE  OK" % command)
        else:
            # 设置iswork=0,并重启cgi
            astr = " ".join([str(x) for  x in zonedict[ip]])
            command = "sh commands/shelltools/changeZoneConf.sh  delete " + astr
            subp = subprores(command)
            if not subp:
                write_logger('error',"%s ERROR" % command)
                exittag = True
                return [False,"changeZoneConf.sh  delete ERROR occurred!!!"]
            write_logger('info',"%s EXE  OK" % command)

    if exittag == True :
        return [False, "Error happened"]
    # 更改所有dbnum的ip为合区ip
    changezoneipandvalue(dbnumlist,localip)
    write_logger('info', "func: changezoneipandvalue happened!!!!")

    # 创建日志文件
    astr = " ".join([str(x) for x in dbnumlist])

    write_logger('debug','createZoneLog.sh start.....................')
    command = "sh commands/shelltools/createZoneLog.sh " + astr
    subp = subprores(command)
    if not subp:
        write_logger('error',"sh: %s ERROR occurred!!!" % command)
        return [False,"createZoneLog.sh  ERROR occurred!!!"]
    write_logger('debug','createZoneLog.sh EXE Ok!!!!!!!!!!!!!')

    # 执行合区脚本
    write_logger('debug',"hequ.sh  start .............")
    command = "sh commands/shelltools/hequ.sh " + astr
    subp = subprores(command)
    if not subp:
        write_logger('error',"sh %s Error occurred!!!" % command)
        return [False,"hequ.sh ERROR occurred!!!"]
    write_logger('debug',"hequ.sh EXE OK!!!!!!!!!!!!!!!")

    # 更改所有区的iswork=1,重启cgi ,over
    write_logger('debug',"changeZoneConf.sh add starting...............")
    command = "sh commands/shelltools/changeZoneConf.sh  add " + astr
    subp = subprores(command)
    if not subp:
        write_logger('error',"sh %s Error occurred!!!" % command)
        return [False,"changeZoneConf.sh add ERROR occurred!!!"]
    write_logger('debug',"changeZoneConf.sh add EXE OK!!!!!!!!!!!!!!!!!!")

    return [True, "hequ OK!!!!"]