def appTaskTest(ansible_host_appmgnt_return_filepath, ansible_host_appmgnt_applist_path, app_id, app_action, opr_user, applog_id): cmdbApp = CmdbApp.objects.get(app_id=app_id) applog = CmdbAppLog.objects.get(id=applog_id) #执行结果文件返回路径 file_path_return = ansible_host_appmgnt_return_filepath #执行结果文件文件名 file_name_return = opr_user + "_app_oneopr_return_" + app_action + applog.app_operation_time.strftime( "%Y%m%d%H%M%S") conntarget = "Ansible" sshd = ssh_connect(conntarget) dir_cmd = "mkdir " + file_path_return ssh_exec_cmd(sshd, dir_cmd) cmd = "sh appopr.sh " + " |tee -a " + file_path_return + file_name_return + ".log" #cmd = "./clusterManagement.sh "+app_action+" "+file_name+" |tee -a " + file_path_return + file_name_return + ".log" stdin, stdout, stderr = ssh_exec_cmd(sshd, cmd) err_list = stderr.readlines() if len(err_list) > 0: print('Start failed:' + err_list[0]) returnmsg = "False" if app_action != 'status': cmdbApp.app_lastopr_result = "失败" applog.app_opr_result = "失败" cmdbApp.save() else: cmdbApp.app_lastopr_result = "执行中" applog.app_opr_result = "执行中" cmdbApp.app_status = "4" print('Start success.') returnmsg = "True" cmdbApp.save() applog.save() sshd.close() return returnmsg
def remote_scp1(remote_path_file,local_path): t, sftp = sftpconnect("CMIOT") try: get(sftp,remote_path_file, local_path) t.close() sshd = ssh_connect("Ansible") #获得该文件后直接删除该文件,而不是删除该目录下的文件 cmd = "rm -f " + remote_path_file ssh_exec_cmd(sshd, cmd) ssh_close(sshd) except IOError: print("not exist")
def taskOneAction(bg_id, bg_action, opr_user, bg_log_id, bg_old_status): log_info = "执行情况\n" taskManagement = BgTaskManagement.objects.get(bg_id=bg_id) file_name = taskManagement.bg_module + "_" + taskManagement.bg_domain + \ "_" + bg_action + "_" + datetime.now().strftime("%Y%m%d%H%M%S") conntarget = "Ansible" sshd = ssh_connect(conntarget) dir_cmd = "mkdir " + file_name ssh_exec_cmd(sshd, dir_cmd) if bg_action == 'start': cmd = taskManagement.bg_task_start + " |tee -a " + file_name + "/" + file_name + ".log" elif bg_action == 'stop': cmd = taskManagement.bg_task_stop + " |tee -a " + file_name + "/" + file_name + ".log" elif bg_action == 'restart': cmd = taskManagement.bg_task_restart + " |tee -a " + file_name + "/" + file_name + ".log" elif bg_action == 'query': cmd = taskManagement.bg_task_query + " |tee -a " + file_name + "/" + file_name + ".log" else: return redirect("taskManagement") stdin, stdout, stderr = ssh_exec_cmd(sshd, cmd) err_list = stderr.readlines() if len(err_list) > 0: print('Start failed:' + err_list[0]) returnmsg = "False" if bg_action != 'query': taskManagement.bg_lastopr_result = "失败" taskManagement.bg_status = bg_old_status taskManagement.save() bg_log = BgTaskLog.objects.get(bg_log_id=bg_log_id) bg_log.bg_opr_result = "失败" # 写入日志文件 bg_log_msg = str(bg_log.bg_operation_time) + ":" + \ bg_log.bg_operation_user + bg_log.bg_operation + bg_log.bg_opr_result + "\n执行情况:\n" + err_list[0] path = config.get('TaskManagement', 'log_path') + file_name + '.log' bg_log.bg_log_dir = path file = open(path, 'a+') file.write(bg_log_msg) file.close() bg_log.save() else: print('Start success.') bg_log = BgTaskLog.objects.get(bg_log_id=bg_log_id) bg_log.bg_opr_result = "执行中" bg_log_msg = str(bg_log.bg_operation_time) + ":" + \ bg_log.bg_operation_user + bg_log.bg_operation + '\n' + log_info # path = config.get('TaskManagement','log_path') + file_name + '.log' path = file_name + "/" + file_name + ".log" bg_log.bg_log_dir = path bg_log.save() returnmsg = "True" sshd.close() return returnmsg
def batchRefreshClusterStatusTask(ansible_cmdb_appmgnt_clusterReturn_filepath, user_name): returnmsg = "False" file_path_return = ansible_cmdb_appmgnt_clusterReturn_filepath file_name_return = user_name + "_refresh_cluster_status_return" conntarget = "Ansible" sshd = ssh_connect(conntarget) dir_cmd = "mkdir " + file_path_return ssh_exec_cmd(sshd, dir_cmd) cmd = "./clusterManagement.sh status" + " >> " + file_path_return + "/" + file_name_return + ".log" stdin, stdout, stderr = ssh_exec_cmd(sshd, cmd) err_list = stderr.readlines() if len(err_list) > 0: print('Refresh failed:' + err_list[0]) else: print('Start success.') returnmsg = "True" sshd.close() return returnmsg
def taskAction(id, DB_Name, tablespace_name, start_time, end_time, alarm_threshold): conntarget = "Ansible_bg" sshd = ssh_connect(conntarget) # 待补充脚本参数 cmd = "python /home/zhaocy/tasktest/timeprint.py " stdin, stdout, stderr = ssh_exec_cmd(sshd, cmd) err_list = stderr.readlines() aiopsDetectLog = models.AiopsDetectLog.objects.get(id=id) if len(err_list) > 0: print('Start failed:' + err_list[0]) returnmsg = "False" aiopsDetectLog.status = "失败" else: aiopsDetectLog.status = "生成中" print('Start success.') aiopsDetectLog.save() returnmsg = "True" sshd.close() return returnmsg
def batchRefreshHostStatusTask(ansible_host_hostmgnt_return_filepath, user_name, ansible_host_hostmgnt_scrideploy_path): returnmsg = "False" file_path_return = ansible_host_hostmgnt_return_filepath file_name_return = user_name + "_refresh_host_status_return" conntarget = "Ansible" sshd = ssh_connect(conntarget) dir_cmd = "mkdir " + file_path_return ssh_exec_cmd(sshd, dir_cmd) #cmd = "python3 osproject/bin/mach_get.py " + " >> " + file_path_return + "/" + file_name_return + ".log" cmd = "python " + ansible_host_hostmgnt_scrideploy_path + "mach_get.py " + " >> " + file_path_return + "/" + file_name_return + ".log" stdin, stdout, stderr = ssh_exec_cmd(sshd, cmd) err_list = stderr.readlines() if len(err_list) > 0: print('Refresh failed:' + err_list[0]) else: print('Start success.') returnmsg = "True" sshd.close() return returnmsg
def batchRefreshClusterStatusTask(ansible_cmdb_appmgnt_clusterReturn_filepath, user_name, logId): print("开始执行---------------------") cmdbAppLog = CmdbAppLog.objects.get(id=logId) returnmsg = "False" file_path_return = ansible_cmdb_appmgnt_clusterReturn_filepath nowTime = datetime.now().strftime("%Y%m%d%H%M%S") file_name_return = "refresh_cluster_status_return_" + str(logId) conntarget = "Ansible" sshd = ssh_connect(conntarget) cmd = "sh appbatchopr.sh " + " >> " + file_path_return + "/" + file_name_return + ".log" #cmd = "./clusterManagement.sh status" + " >> " + file_path_return + "/" + file_name_return + ".log" stdin, stdout, stderr = ssh_exec_cmd(sshd, cmd) err_list = stderr.readlines() if len(err_list) > 0: print('Refresh failed:' + err_list[0]) else: cmdbAppLog.app_opr_result = "集群刷新执行中" cmdbAppLog.save() print('Start success.') returnmsg = "True" sshd.close() return returnmsg
def appTaskOne(ansible_host_appmgnt_return_filepath, file_name, app_id, app_action, opr_user): cmdbApp = CmdbApp.objects.get(app_id=app_id) file_path_return = ansible_host_appmgnt_return_filepath file_name_return = opr_user + "_app_oneopr_return" conntarget = "Ansible" sshd = ssh_connect(conntarget) dir_cmd = "mkdir " + file_path_return ssh_exec_cmd(sshd, dir_cmd) cmd = "aaa.sh " + file_name + " action=" + app_action + " |tee -a " + file_path_return + file_name_return + ".log" stdin, stdout, stderr = ssh_exec_cmd(sshd, cmd) err_list = stderr.readlines() if len(err_list) > 0: print('Start failed:' + err_list[0]) returnmsg = "False" if app_action != 'status': cmdbApp.bg_lastopr_result = "失败" cmdbApp.save() else: print('Start success.') returnmsg = "True" sshd.close() return returnmsg