Commit 09afcbb6 authored by 陈子龙's avatar 陈子龙

Merge branch 'dev-czl' into dev

parents 852043e2 fef3dc1c
......@@ -57,4 +57,5 @@ const (
CookieNameLastLogin string = "lastLogin"
LocalDateTimeFormat string = "2006-01-02 15:04:05"
FinalHeartBeatUnixKey = "FinalHeartBeatUnix"
AutoExecHistory = "AutoExecHistory"
)
......@@ -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
......
......@@ -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
}
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