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

执行脚本优化

parent 0dc7a0c7
...@@ -813,6 +813,44 @@ func CronStatusDetection() { ...@@ -813,6 +813,44 @@ func CronStatusDetection() {
return return
} }
//读取hosts中的主机组
hosts, err := tools.HostsToJson()
if err != nil {
fmt.Println("CronStatusDetection err:", err.Error())
return
}
if _, ok := hosts["["+AnsibleGroup+"]"]; !ok {
// 不存在
hosts["["+AnsibleGroup+"]"] = nil
}
for _, v := range hostManageList {
hostsIp := ""
if v.VoucherType == 0 {
hostsIp = fmt.Sprintf("%s ansible_ssh_host=%s ansible_ssh_port=%s ansible_ssh_user=\"%s\" ansible_ssh_pass=\"%s\" ansible_host_key_checking=false",
AnsibleIp+v.Ip, v.Ip, v.Port, v.UserName, v.Password)
} else {
hostsIp = fmt.Sprintf("%s ansible_ssh_host=%s ansible_ssh_port=%s ansible_ssh_user=\"%s\" ansible_ssh_private_key_file=/root/.ssh/id_rsa ansible_host_key_checking=false",
AnsibleIp+v.Ip, v.Ip, v.Port, v.UserName)
}
flag := 0
for _, v1 := range hosts["["+AnsibleGroup+"]"] {
if v1 == hostsIp {
flag = 1
}
}
if flag == 0 {
hosts["["+AnsibleGroup+"]"] = append(hosts["["+AnsibleGroup+"]"], hostsIp)
}
}
//写入hosts
err = tools.MapToSaveHosts(hosts)
if err != nil {
fmt.Println("CronStatusDetection err:", err.Error())
return
}
for _, v := range hostManageList { for _, v := range hostManageList {
connStatus := StatusDetection(v.Ip) connStatus := StatusDetection(v.Ip)
//修改状态 //修改状态
......
...@@ -8,7 +8,9 @@ import ( ...@@ -8,7 +8,9 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"go.uber.org/zap"
"io" "io"
"os" "os"
"os/exec" "os/exec"
...@@ -268,31 +270,118 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id ...@@ -268,31 +270,118 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
if err != nil { if err != nil {
return return
} }
go ExecAnsible(id, req.TaskId, req.Value)
////执行ansible命令
//var cmd *exec.Cmd
//if req.Value != "" {
// cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts_"+fmt.Sprintf("%d", req.TaskId), "/etc/ansible/ansible_"+fmt.Sprintf("%d", req.TaskId)+".yml", "--extra-vars", req.Value)
//} else {
// cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts_"+fmt.Sprintf("%d", req.TaskId), "/etc/ansible/ansible_"+fmt.Sprintf("%d", req.TaskId)+".yml")
//}
////ansible-playbook -i /tmp/hosts --list-hosts debug.yml
////捕获正常日志
//stdout, err := cmd.StdoutPipe()
//if err != nil {
// err = resp.CmdExecError.WithError(err)
// return
//}
////捕获异常日志
//stderr, err := cmd.StderrPipe()
//if err != nil {
// err = resp.CmdExecError.WithError(err)
// return
//}
////执行cmd命令
//if err = cmd.Start(); err != nil {
// err = resp.CmdExecError.WithError(err)
// return
//}
////获取 正常/异常 输出流
//outputBuf := bufio.NewReader(stdout)
//readerr := bufio.NewReader(stderr)
//
//var out, outErr int
//var execLog string
//for {
//
// //逐行输出日志
// lineOut, err1 := outputBuf.ReadString('\n')
// if (err1 != nil || io.EOF == err1) && out == 0 {
// out = 1
// } else if out == 0 {
// //存储执行日志
// execLog = execLog + lineOut + " \n "
// UpdateExecHistory(request.UpdateExecHistory{
// TaskHistoryId: id,
// ExecLog: execLog,
// })
// }
//
// lineErr, err2 := readerr.ReadString('\n')
// if (err2 != nil || io.EOF == err2) && outErr == 0 {
// outErr = 1
// } else if outErr == 0 {
// //存储异常执行日志
// execLog = execLog + lineErr + " \n "
// UpdateExecHistory(request.UpdateExecHistory{
// TaskHistoryId: id,
// ExecLog: execLog,
// })
// }
//
// if out == 1 && outErr == 1 {
// break
// }
//}
//cmd.Wait()
//
//if cmd.ProcessState.Success() {
// //任务执行成功
// UpdateExecHistory(request.UpdateExecHistory{
// TaskHistoryId: id,
// ExecLog: execLog,
// State: 1,
// })
//} else {
// //任务执行失败
// UpdateExecHistory(request.UpdateExecHistory{
// TaskHistoryId: id,
// ExecLog: execLog,
// State: 2,
// })
//}
return
}
// 执行脚本
func ExecAnsible(id, taskId int, value string) {
//执行ansible命令 //执行ansible命令
var cmd *exec.Cmd var cmd *exec.Cmd
if req.Value != "" { if value != "" {
cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts_"+fmt.Sprintf("%d", req.TaskId), "/etc/ansible/ansible_"+fmt.Sprintf("%d", req.TaskId)+".yml", "--extra-vars", req.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)
} else { } else {
cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts_"+fmt.Sprintf("%d", req.TaskId), "/etc/ansible/ansible_"+fmt.Sprintf("%d", req.TaskId)+".yml") cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts_"+fmt.Sprintf("%d", taskId), "/etc/ansible/ansible_"+fmt.Sprintf("%d", taskId)+".yml")
} }
//ansible-playbook -i /tmp/hosts --list-hosts debug.yml //ansible-playbook -i /tmp/hosts --list-hosts debug.yml
//捕获正常日志 //捕获正常日志
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
err = resp.CmdExecError.WithError(err) err = resp.CmdExecError.WithError(err)
return conf.Logger.Error("Capture normal logs", zap.Error(err))
//return
} }
//捕获异常日志 //捕获异常日志
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if err != nil { if err != nil {
err = resp.CmdExecError.WithError(err) err = resp.CmdExecError.WithError(err)
return conf.Logger.Error("Capture exception logs", zap.Error(err))
//return
} }
//执行cmd命令 //执行cmd命令
if err = cmd.Start(); err != nil { if err = cmd.Start(); err != nil {
err = resp.CmdExecError.WithError(err) err = resp.CmdExecError.WithError(err)
return conf.Logger.Error("Execute cmd command", zap.Error(err))
//return
} }
//获取 正常/异常 输出流 //获取 正常/异常 输出流
outputBuf := bufio.NewReader(stdout) outputBuf := bufio.NewReader(stdout)
...@@ -309,11 +398,14 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id ...@@ -309,11 +398,14 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
} else if out == 0 { } else if out == 0 {
//存储执行日志 //存储执行日志
execLog = execLog + lineOut + " \n " execLog = execLog + lineOut + " \n "
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
}
} }
lineErr, err2 := readerr.ReadString('\n') lineErr, err2 := readerr.ReadString('\n')
...@@ -322,10 +414,14 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id ...@@ -322,10 +414,14 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
} else if outErr == 0 { } else if outErr == 0 {
//存储异常执行日志 //存储异常执行日志
execLog = execLog + lineErr + " \n " execLog = execLog + lineErr + " \n "
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
}
} }
if out == 1 && outErr == 1 { if out == 1 && outErr == 1 {
...@@ -336,18 +432,26 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id ...@@ -336,18 +432,26 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
if cmd.ProcessState.Success() { if cmd.ProcessState.Success() {
//任务执行成功 //任务执行成功
UpdateExecHistory(request.UpdateExecHistory{ err = UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id, TaskHistoryId: id,
ExecLog: execLog, ExecLog: execLog,
State: 1, State: 1,
}) })
if err != nil {
conf.Logger.Error("Modify Execution Status", zap.Error(err))
//return
}
} else { } else {
//任务执行失败 //任务执行失败
UpdateExecHistory(request.UpdateExecHistory{ err = UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id, TaskHistoryId: id,
ExecLog: execLog, ExecLog: execLog,
State: 2, State: 2,
}) })
if err != nil {
conf.Logger.Error("Modify Execution Status", zap.Error(err))
//return
}
} }
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