Commit fef3dc1c authored by 陈子龙's avatar 陈子龙

执行脚本优化

parent 0707f02b
...@@ -57,4 +57,5 @@ const ( ...@@ -57,4 +57,5 @@ const (
CookieNameLastLogin string = "lastLogin" CookieNameLastLogin string = "lastLogin"
LocalDateTimeFormat string = "2006-01-02 15:04:05" LocalDateTimeFormat string = "2006-01-02 15:04:05"
FinalHeartBeatUnixKey = "FinalHeartBeatUnix" FinalHeartBeatUnixKey = "FinalHeartBeatUnix"
AutoExecHistory = "AutoExecHistory"
) )
...@@ -384,7 +384,7 @@ func (h *HostManageSvc) PageListHostManage(req request.ListHostManageReq) (total ...@@ -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 "+ "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 "+ "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"). "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 { if err != nil {
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
return return
......
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"time" "time"
) )
...@@ -171,7 +172,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int ...@@ -171,7 +172,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
finder.OrderBy("tm.id") finder.OrderBy("tm.id")
//查询任务 //查询任务
total, err = finder.Select("tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "+ 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) Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskManageListRes)
if err != nil { if err != nil {
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
...@@ -353,9 +354,8 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id ...@@ -353,9 +354,8 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
return return
} }
// 执行脚本 // ExecAnsible 执行ansible命令
func ExecAnsible(id, taskId int, value string) { func ExecAnsible(id, taskId int, value string) {
//执行ansible命令
var cmd *exec.Cmd var cmd *exec.Cmd
if value != "" { 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) 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) { ...@@ -389,6 +389,11 @@ func ExecAnsible(id, taskId int, value string) {
var out, outErr int var out, outErr int
var execLog string var execLog string
redis, err := client.GetRedisClient()
if err != nil {
zap.L().Error(err.Error())
}
for { for {
//逐行输出日志 //逐行输出日志
...@@ -398,10 +403,16 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -398,10 +403,16 @@ func ExecAnsible(id, taskId int, value string) {
} else if out == 0 { } else if out == 0 {
//存储执行日志 //存储执行日志
execLog = execLog + lineOut + " \n " execLog = execLog + lineOut + " \n "
err = UpdateExecHistory(request.UpdateExecHistory{ //err = UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id, // TaskHistoryId: id,
ExecLog: execLog, // 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 { if err != nil {
conf.Logger.Error("Store Execution Log", zap.Error(err)) conf.Logger.Error("Store Execution Log", zap.Error(err))
//return //return
...@@ -414,12 +425,18 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -414,12 +425,18 @@ func ExecAnsible(id, taskId int, value string) {
} else if outErr == 0 { } else if outErr == 0 {
//存储异常执行日志 //存储异常执行日志
execLog = execLog + lineErr + " \n " execLog = execLog + lineErr + " \n "
err = UpdateExecHistory(request.UpdateExecHistory{ //err = UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id, // TaskHistoryId: id,
ExecLog: execLog, // 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 { if err != nil {
conf.Logger.Error("Store abnormal execution logs", zap.Error(err)) conf.Logger.Error("Store Execution Log", zap.Error(err))
//return //return
} }
} }
...@@ -441,6 +458,7 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -441,6 +458,7 @@ func ExecAnsible(id, taskId int, value string) {
conf.Logger.Error("Modify Execution Status", zap.Error(err)) conf.Logger.Error("Modify Execution Status", zap.Error(err))
//return //return
} }
redis.Del(strconv.Itoa(id))
} else { } else {
//任务执行失败 //任务执行失败
err = UpdateExecHistory(request.UpdateExecHistory{ err = UpdateExecHistory(request.UpdateExecHistory{
...@@ -452,6 +470,7 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -452,6 +470,7 @@ func ExecAnsible(id, taskId int, value string) {
conf.Logger.Error("Modify Execution Status", zap.Error(err)) conf.Logger.Error("Modify Execution Status", zap.Error(err))
//return //return
} }
redis.Del(strconv.Itoa(id))
} }
//return //return
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment