def file_data_check(request): log.info("do file data check.") deploy_type = request.get('deploy_type') file_path = request.get('dest_path') file_md5 = request.get('md5') is_exec = request.get('isexcute') curr_file_md5 = '' if not os.path.exists(file_path): #还未接收到文件数据 log.warn('file %s is not exists, skip check.' % file_path) file_state = -1 else: curr_file_md5 = comm_lib.getmd5(file_path) if curr_file_md5 == file_md5: log.info( 'file %s data check success.can call(or not:is_exec[%s]) it now.' % (file_path, is_exec)) file_state = 2 request.update({ 'request': 'file_data_check_response', 'send_state': file_state }) return comm_lib.send_socket_data(sk.s, request)
def client_handle(data): type = data.get('method') client_sh = curr_path + os.sep + 'sh' + os.sep + "client.sh" if type not in ['stop', 'start', 'restart', 'update']: return log.info('client %s .' % type) if not os.path.exists(client_sh): return log.info('can not find %s , skip.' % client_sh) cmd = ''' dos2unix %s >/dev/null 2>&1;chmod u+x %s;sh %s %s''' % ( client_sh, client_sh, client_sh, type) dt = {} dt['request'] = "client_handle_respose" dt['method'] = type ret, detail = commands.getstatusoutput(cmd) if ret >> 8 != 0: log.info(detail) log.info('client %s falied.' % type) dt['status'] = "failed" else: dt['status'] = "success" log.info('client %s success.' % type) return comm_lib.send_socket_data(sk.s, dt)
def find_file_path(data): role = data.get('role') if not role: return False roletmp = '' for i in role.split(os.sep): if i: if not re.match(r'.*[a-zA-Z0-9-_]+.*', i): break roletmp += os.sep + str(i) cmd = ''' getfacl %s -R 2>/dev/null|grep -P "%s"|awk '{a=a"/"$NF"::::::"}END{print a}' ''' % ( roletmp, re.sub('^/+', '', role)) ret, detail = commands.getstatusoutput(cmd) if ret >> 8 != 0: filelist = '' else: filelist = detail data.update({ 'request': 'find_file_path_response', 'filelist': filelist.split('::::::') }) return comm_lib.send_socket_data(sk.s, data)
def download_file(data): file = data.get('file') filedata = '' if not os.path.exists(file): filedata = -1 with open(file, 'rb') as f: filedata = f.read() data.update({'request': 'download_file_response', 'filedata': filedata}) return comm_lib.send_socket_data(sk.s, data)
def get_task_log(request): task_name = request.get('task_name') filename = request.get('filename') download = request.get('download') request['request'] = "task_log_response" request['response'] = '' log_file = curr_path + os.sep + task_log_path + os.sep + task_name + os.sep + filename.split( os.sep)[-1] + ".log" log.info('get task log[%s]' % log_file) if not os.path.exists(log_file): request['response'] = 'log file[%s] is not exists' % log_file return comm_lib.send_socket_data(sk.s, request) with open(log_file, 'rb') as f: #下载日志时候读取后直接返回 if download: dt = f.read() request['response'] = dt return comm_lib.send_socket_data(sk.s, request) count = int() while 1: request['response'] = '' lines = f.readlines() if not lines: count += 1 #1分钟没新的数据写到日志则退出 if count >= 60: return time.sleep(1) else: request['response'] = lines if re.match(r'^:::task[ \t]+call[ \t]+done[ \t]+.*[0-9]+', lines[-1]): request.update({'logdone': 'yes'}) comm_lib.send_socket_data(sk.s, request) if request.get('logdone') == 'yes': return
def execute_task_cmd(taskdata): if taskdata.get('object') == "file_data_check": return file_data_check(taskdata) task_name = taskdata.get('task_name') cmd_obj = taskdata.get('object') dotype = taskdata.get('dotype') collecttemplate = taskdata.get('collecttemplate') taskdata.update({'request': 'task_response', 'task_status': ''}) #任务使用预处理脚本调用 script_path = curr_path + os.sep + 'sh/task_call_prehandle.sh' if not os.path.exists(script_path): return log.err('can not find task_call_prehandle.sh, skip.') #一次接收一个任务调用 if isinstance(cmd_obj, list): cmd_obj = cmd_obj[0] exc_failed = False if not cmd_obj: return log.err('task[%s] execute failed. cmd obj is None.') cmd = cmd_obj taskdata['task_status'] = "running" cmd_type = dotype if not cmd_type: cmd_type = 'run' elif cmd_type == "cancel": cmd_type = 'stop' elif cmd_type == "restart" or cmd_type == "single_restart": cmd_type = 'stop' log.info('call task_call_prehandle.sh[%s %s %s %s] begin.' % (script_path, cmd_type, task_name, cmd)) exec_cmd = 'dos2unix %s >/dev/null 2>&1;chmod u+x %s;%s %s %s %s' % ( script_path, script_path, script_path, cmd_type, task_name, cmd) commands.getstatusoutput(exec_cmd) cmd_type = 'run' exec_cmd = 'dos2unix %s >/dev/null 2>&1;chmod u+x %s;%s %s %s %s' % ( script_path, script_path, script_path, cmd_type, task_name, cmd) log.info('call task_call_prehandle.sh[%s %s %s %s] begin.' % (script_path, cmd_type, task_name, cmd)) ret, detail = commands.getstatusoutput(exec_cmd) if ret >> 8 != 0: exc_failed = True taskdata['task_status'] = "failed" log.err('call task_call_prehandle.sh err.\n%s' % detail) else: if dotype == "cancel": taskdata['task_status'] = "cancel" else: taskdata['task_status'] = "success" if collecttemplate: taskdata.update({'collectdata': detail}) log.info('task %s call done.' % str(cmd)) if exc_failed: log.warn('task %s can not execute done.' % cmd_obj) else: log.info('call %s done.' % task_name) return comm_lib.send_socket_data(sk.s, taskdata)