def add_host(id, hosts, user, port, passwd, sudopasswd): async_set(id, "progressMsg", "begin to run") async_set(id, "progress", "10") time.sleep(2) async_set(id, "progressMsg", "ssh to") async_set(id, "progress", "20") time.sleep(2) async_set(id, "progressMsg", "get info") async_set(id, "progress", "80") time.sleep(2) async_set(id, "progressMsg", "finish") async_set(id, "progress", "100") #保留10秒后清除 session = database.getSession() for hostName in hosts: host = Host(hostName) host.ip = "192.168.1.1" host.cpu = "fade8core" host.mem = "fade100G" host.disk = "fade100T" host.rack = "unknow" session.add(host) session.commit() time.sleep(10) async_remove(id)
def add_host(id,hosts,user,port,passwd,sudopasswd): async_set(id,"progressMsg","begin to run") async_set(id,"progress","10") time.sleep(2) async_set(id,"progressMsg","ssh to") async_set(id,"progress","20") time.sleep(2) async_set(id,"progressMsg","get info") async_set(id,"progress","80") time.sleep(2) async_set(id,"progressMsg","finish") async_set(id,"progress","100") #保留10秒后清除 session=database.getSession() for hostName in hosts: host = Host(hostName) host.ip="192.168.1.1" host.cpu="fade8core" host.mem="fade100G" host.disk="fade100T" host.rack="unknow" session.add(host) session.commit() time.sleep(10) async_remove(id)
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()
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()