diff --git a/src/common/conf/options.go b/src/common/conf/options.go index 6ce92f4ac5135bd3766b80fef094f524af868615..7b3e71c05dc3122be377c681ca288ebf6b70517b 100644 --- a/src/common/conf/options.go +++ b/src/common/conf/options.go @@ -57,4 +57,5 @@ const ( CookieNameLastLogin string = "lastLogin" LocalDateTimeFormat string = "2006-01-02 15:04:05" FinalHeartBeatUnixKey = "FinalHeartBeatUnix" + AutoExecHistory = "AutoExecHistory" ) diff --git a/src/service/host_manage.go b/src/service/host_manage.go index 3a3f5ebedd00dbf77159035af6f000c649278790..2ee14983633982e11864b510f5dbcc96a1fc1d35 100644 --- a/src/service/host_manage.go +++ b/src/service/host_manage.go @@ -384,7 +384,7 @@ func (h *HostManageSvc) PageListHostManage(req request.ListHostManageReq) (total "task_manage tm WHERE tm.is_delete = 0 AND tm.host_group_id = hm.ID) AS task_cnt,(SELECT COUNT(*) FROM "+ "host_manage_list hml WHERE hml.is_delete = 0 AND hml.conn_status = 1 AND hml.host_group_id = hm.ID) AS "+ "ip_cnt_err,(SELECT COUNT(*) FROM host_manage_list hml WHERE hml.is_delete = 0 AND hml.host_group_id = hm.ID) AS ip_cnt"). - OrderBy("hm.create_time desc").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListRes) + OrderBy("hm.create_time").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListRes) if err != nil { err = resp.DbSelectError.WithError(err) return diff --git a/src/service/task_manage.go b/src/service/task_manage.go index 09c9f0e458f2d7f219ee875192918d4a49c7ceca..c6b58cef82f466ab21642aeb38a0f4516baec4a8 100644 --- a/src/service/task_manage.go +++ b/src/service/task_manage.go @@ -14,6 +14,7 @@ import ( "io" "os" "os/exec" + "strconv" "strings" "time" ) @@ -171,7 +172,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int finder.OrderBy("tm.id") //查询任务 total, err = finder.Select("tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "+ - "where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time").OrderBy("tm.create_time desc"). + "where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time").OrderBy("tm.create_time"). Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskManageListRes) if err != nil { err = resp.DbSelectError.WithError(err) @@ -353,9 +354,8 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id return } -// 执行脚本 +// ExecAnsible 执行ansible命令 func ExecAnsible(id, taskId int, value string) { - //执行ansible命令 var cmd *exec.Cmd if value != "" { cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts_"+fmt.Sprintf("%d", taskId), "/etc/ansible/ansible_"+fmt.Sprintf("%d", taskId)+".yml", "--extra-vars", value) @@ -389,6 +389,11 @@ func ExecAnsible(id, taskId int, value string) { var out, outErr int var execLog string + + redis, err := client.GetRedisClient() + if err != nil { + zap.L().Error(err.Error()) + } for { //逐行输出日志 @@ -398,10 +403,16 @@ func ExecAnsible(id, taskId int, value string) { } else if out == 0 { //存储执行日志 execLog = execLog + lineOut + " \n " - err = UpdateExecHistory(request.UpdateExecHistory{ - TaskHistoryId: id, - ExecLog: execLog, - }) + //err = UpdateExecHistory(request.UpdateExecHistory{ + // TaskHistoryId: id, + // ExecLog: execLog, + //}) + //if err != nil { + // conf.Logger.Error("Store Execution Log", zap.Error(err)) + // //return + //} + // TODO 写缓存 + err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), execLog) if err != nil { conf.Logger.Error("Store Execution Log", zap.Error(err)) //return @@ -414,12 +425,18 @@ func ExecAnsible(id, taskId int, value string) { } else if outErr == 0 { //存储异常执行日志 execLog = execLog + lineErr + " \n " - err = UpdateExecHistory(request.UpdateExecHistory{ - TaskHistoryId: id, - ExecLog: execLog, - }) + //err = UpdateExecHistory(request.UpdateExecHistory{ + // TaskHistoryId: id, + // ExecLog: execLog, + //}) + //if err != nil { + // conf.Logger.Error("Store abnormal execution logs", zap.Error(err)) + // //return + //} + // TODO 写缓存 + err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), execLog) if err != nil { - conf.Logger.Error("Store abnormal execution logs", zap.Error(err)) + conf.Logger.Error("Store Execution Log", zap.Error(err)) //return } } @@ -441,6 +458,7 @@ func ExecAnsible(id, taskId int, value string) { conf.Logger.Error("Modify Execution Status", zap.Error(err)) //return } + redis.Del(strconv.Itoa(id)) } else { //任务执行失败 err = UpdateExecHistory(request.UpdateExecHistory{ @@ -452,6 +470,7 @@ func ExecAnsible(id, taskId int, value string) { conf.Logger.Error("Modify Execution Status", zap.Error(err)) //return } + redis.Del(strconv.Itoa(id)) } //return }