From fef3dc1c824b95b0b756f49ed6221c363f9b9311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AD=90=E9=BE=99?= Date: Mon, 17 Jul 2023 17:18:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=84=9A=E6=9C=AC=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/conf/options.go | 1 + src/service/host_manage.go | 2 +- src/service/task_manage.go | 43 +++++++++++++++++++++++++++----------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/common/conf/options.go b/src/common/conf/options.go index 6ce92f4..7b3e71c 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 3a3f5eb..2ee1498 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 09c9f0e..c6b58ce 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 } -- 2.26.0