예제 #1
0
파일: async.py 프로젝트: emmerichLuang/uhp
def add_host(asyncId, hosts, login):
    #保留10秒后清除
    session = database.getSession()
    idMap = {}
    progress = 0
    progressStep = int((100 - 10) / len(hosts))

    #创建任务
    for hostName in hosts:
        taskId = database.build_task(session, "async", "add_host", hostName,
                                     "machine", "connect")
        idMap[hostName] = taskId

    async_push(asyncId, "progressMsg", "build task")
    progress = 10
    async_set(asyncId, "progress", progress)

    #交给ansible运行
    async_push(
        asyncId, "progressMsg",
        "ansible connect and install yum python json.As the yum update automatically,it could be long..."
    )
    progress = 20
    async_set(asyncId, "progress", progress)
    newlogin = get_default_login(session, login)
    #     (user,port,passwd,sudopasswd)
    for host in hosts:
        taskId = idMap[host]
        database.update_task(session, taskId, Task.STATUS_RUNNING,
                             Task.RESULT_UNFINISH, "")

    #即使不实际执行ansible
    #也可以获取相关的信息
    #要求信息是准确可用的
    import ansible_lib
    ret = ansible_lib.connect_host(hosts, newlogin[0], newlogin[1],
                                   newlogin[2], newlogin[3])
    #假连接
    #ret=fade_connect_host(hosts,newlogin[0],newlogin[1],newlogin[2],newlogin[3])
    app_log.info("connect with" + str(hosts) + " with :" + str(newlogin))

    #处理结果
    success = []
    failed = []
    prepareTaskids = []
    for hostName in ret:
        taskId = idMap[hostName]
        (result, msg, info) = ret[hostName]
        if result:
            #成功连接,添加到host表

            success.append(hostName)
            host = Host(hostName, Host.STATUS_CONNECTED)
            host.ip = info['ip']
            host.cpu = info['cpu']
            host.mem = info['mem']
            host.disk = info['disk']
            host.rack = info['rack']
            session.add(host)
            session.commit()
            if config.install_manager:
                #提交一个prepare任务,假如运行成功将修改状态到ready
                prepareTaskid = database.build_task(session, "ansible",
                                                    "prepare", hostName,
                                                    "prepare", "prepare")
                prepareTaskids.append(prepareTaskid)
                callback_lib.add_callback(session, prepareTaskid,
                                          "changeHostToReady")
            #假如登录信息跟默认的不一致,保存这些登录信息到数据库
            save_login(session, hostName, login)

            #更新任务状态
            database.update_task(session, taskId, Task.STATUS_FINISH,
                                 Task.RESULT_SUCCESS, "")
            progress += progressStep
            async_push(asyncId, "progressMsg", "finish connect " + hostName)
            async_set(asyncId, "progress", progress)
        else:
            failed.append(hostName)
            database.update_task(session, taskId, Task.STATUS_FINISH,
                                 Task.RESULT_FAILED, msg)

    #全部完成
    async_push(asyncId, "progressMsg",
               "success:" + ",".join(success) + " failed:" + ",".join(failed))
    async_set(asyncId, "progress", "100")
    #发送消息到worker
    retMsg = ""
    msg = ','.join([str(id) for id in prepareTaskids])
    if not mm.send(msg):
        retMsg = "send message to worker error"
    #10秒后清除内存任务
    time.sleep(10)
    async_remove(asyncId)
    session.close()
예제 #2
0
파일: async.py 프로젝트: caimaoy/uhp
def add_host(asyncId,hosts,login):  
    #保留10秒后清除
    session=database.getSession()
    idMap = {}
    progress = 0 ;
    progressStep = int( (100-10)/len(hosts) )
    
    #创建任务
    for hostName in hosts:
        taskId = database.build_task(session,"async","add_host",hostName,"machine","connect")
        idMap[hostName] = taskId
    
    async_push(asyncId,"progressMsg","build task")
    progress = 10
    async_set(asyncId,"progress",progress) 

    #交给ansible运行
    async_push(asyncId,"progressMsg","ansible connect and install yum python json.As the yum update automatically,it could be long...")
    progress = 20
    async_set(asyncId,"progress",progress) 
    newlogin = get_default_login(session, login)
#     (user,port,passwd,sudopasswd) 
    for host in hosts:
        taskId = idMap[host] 
        database.update_task(session,taskId,Task.STATUS_RUNNING,Task.RESULT_UNFINISH,"") 
    
    if config.fade_windows:
        ret=fade_connect_host(hosts,newlogin[0],newlogin[1],newlogin[2],newlogin[3])
    else:
        import ansible_lib
        ret=ansible_lib.connect_host(hosts,newlogin[0],newlogin[1],newlogin[2],newlogin[3])
    app_log.info("connect with"+str(hosts)+" with :"+str(newlogin) )
        
    #处理结果
    success=[]
    failed=[]
    prepareTaskids=[]
    for hostName in ret:
        taskId = idMap[hostName]
        (result,msg,info) = ret[hostName]
        if result:
            #成功连接,添加到host表
            
            success.append(hostName)
            host = Host(hostName,Host.STATUS_CONNECTED)
            host.ip=info['ip']
            host.cpu=info['cpu']
            host.mem=info['mem']
            host.disk=info['disk']
            host.rack=info['rack']
            session.add(host)
            session.commit()
            if config.install_manager:
                #提交一个prepare任务,假如运行成功将修改状态到ready
                prepareTaskid = database.build_task(session,"ansible","prepare",hostName,"prepare","prepare")
                prepareTaskids.append(prepareTaskid)
                callback_lib.add_callback(session,prepareTaskid,"changeHostToReady")
            #假如登录信息跟默认的不一致,保存这些登录信息到数据库
            save_login(session, hostName, login)
            
            #更新任务状态
            database.update_task(session,taskId,Task.STATUS_FINISH,Task.RESULT_SUCCESS,"")
            progress += progressStep
            async_push(asyncId,"progressMsg","finish connect "+hostName)
            async_set(asyncId,"progress",progress)
        else:
            failed.append(hostName)
            database.update_task(session,taskId,Task.STATUS_FINISH,Task.RESULT_FAILED,msg)

    #全部完成
    async_push(asyncId,"progressMsg","success:"+",".join(success)+" failed:"+",".join(failed))
    async_set(asyncId,"progress","100")   
    #发送消息到worker
    retMsg = ""
    msg = ','.join([str(id) for id in prepareTaskids])
    if not mm.send(msg):
        retMsg = "send message to worker error"
    #10秒后清除内存任务
    time.sleep(10)
    async_remove(asyncId)
    session.close()
예제 #3
0
    def deal_alarm_list(self, alarm_list):
        new_key_word, old_key_word = self.diff_key_word(alarm_list)
        mail_center.push_key_word_map(self.key_word_map)

        session = database.getSession()
        # 更新数据库的alarm_now表
        for alarm_now in session.query(AlarmNow):
            session.delete(alarm_now)

        for (key_word, info) in self.key_word_map.items():
            end = key_word.find("(")
            host = key_word[0:end]
            session.add(AlarmNow(key_word, host, info["msg"], "ERROR", info["count"], int(time.time())))

        session.commit()

        # 根据连续告警次数尝试进行修复动作
        try:
            fix_list = []
            for auto_fix in session.query(AlarmAutofix):
                fix_list.append(auto_fix.format())

            for (key_word, count) in self.key_word_map.items():
                for auto_fix in fix_list:
                    match = auto_fix["pattern"].match(key_word)
                    if match and (count == auto_fix["count"] or count == auto_fix["count"] * 2):
                        end = key_word.find("(")
                        host = key_word[0:end]
                        log.info(" build the auto fix tasks: %s %s %s " % (host, auto_fix["role"], auto_fix["task"]))
                        database.build_task(
                            session, "auto_fix", auto_fix["service"], host, auto_fix["role"], auto_fix["task"]
                        )
        except:
            log.exception("autofix catch exception")

        try:
            # 发出邮件 记录动作
            ignore_key_word = session.query(AlarmAssist).filter(AlarmAssist.name == "ignore_key_word").first()
            ignore_list = []
            if ignore_key_word != None:
                # ignore_list = ignore_key_word.value.split(",")
                for str in ignore_key_word.value.split(","):
                    if len(str) > 1:
                        ignore_list.append(re.compile(str))
            # log.info(ignore_list)

            for alarm_state in new_key_word:
                key_word = alarm_state["key_word"]
                session.add(AlarmList(alarm_state["key_word"], "", alarm_state["msg"], "ERROR", int(time.time())))
                if self.is_match(ignore_list, key_word):
                    log.info("ignore %s" % key_word)
                else:
                    self._callback(alarm_state)

            for alarm_state in old_key_word:
                key_word = alarm_state["key_word"]
                session.add(AlarmList(alarm_state["key_word"], "", alarm_state["msg"], "INFO", int(time.time())))
                if self.is_match(ignore_list, key_word):
                    log.info("ignore %s" % key_word)
                else:
                    self._callback(alarm_state)

            session.commit()
        except:
            log.exception("deal callback catch exception")
        session.close()
예제 #4
0
    def deal_alarm_list(self, alarm_list):
        new_key_word, old_key_word = self.diff_key_word(alarm_list)
        mail_center.push_key_word_map(self.key_word_map)

        session = database.getSession()
        #更新数据库的alarm_now表
        for alarm_now in session.query(AlarmNow):
            session.delete(alarm_now)

        for (key_word, info) in self.key_word_map.items():
            end = key_word.find('(')
            host = key_word[0:end]
            session.add(
                AlarmNow(key_word, host, info['msg'], "ERROR", info['count'],
                         int(time.time())))

        session.commit()

        #根据连续告警次数尝试进行修复动作
        try:
            fix_list = []
            for auto_fix in session.query(AlarmAutofix):
                fix_list.append(auto_fix.format())

            for (key_word, count) in self.key_word_map.items():
                for auto_fix in fix_list:
                    match = auto_fix['pattern'].match(key_word)
                    if match and (count == auto_fix['count']
                                  or count == auto_fix['count'] * 2):
                        end = key_word.find('(')
                        host = key_word[0:end]
                        log.info(" build the auto fix tasks: %s %s %s " %
                                 (host, auto_fix['role'], auto_fix['task']))
                        database.build_task(session, "auto_fix",
                                            auto_fix['service'], host,
                                            auto_fix['role'], auto_fix['task'])
        except:
            log.exception("autofix catch exception")

        try:
            #发出邮件 记录动作
            ignore_key_word = session.query(AlarmAssist).filter(
                AlarmAssist.name == "ignore_key_word").first()
            ignore_list = []
            if ignore_key_word != None:
                #ignore_list = ignore_key_word.value.split(",")
                for str in ignore_key_word.value.split(","):
                    if len(str) > 1:
                        ignore_list.append(re.compile(str))
            #log.info(ignore_list)

            for alarm_state in new_key_word:
                key_word = alarm_state['key_word']
                session.add(
                    AlarmList(alarm_state['key_word'], "", alarm_state['msg'],
                              "ERROR", int(time.time())))
                if self.is_match(ignore_list, key_word):
                    log.info("ignore %s" % key_word)
                else:
                    self._callback(alarm_state)

            for alarm_state in old_key_word:
                key_word = alarm_state['key_word']
                session.add(
                    AlarmList(alarm_state['key_word'], "", alarm_state['msg'],
                              "INFO", int(time.time())))
                if self.is_match(ignore_list, key_word):
                    log.info("ignore %s" % key_word)
                else:
                    self._callback(alarm_state)

            session.commit()
        except:
            log.exception("deal callback catch exception")
        session.close()