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

自动化运维 立即执行 fix

parent 159e45b0
......@@ -33,7 +33,7 @@ type ListTaskManageReq struct {
}
type ExecScriptReq struct {
TaskId int `form:"task_id" binding:"required"` //主机分组id
TaskId int `form:"task_id" binding:"required"` //任务id
HostGroupId int `form:"host_group_id" binding:"required"` //主机分组id
Type int `form:"type" binding:"oneof=1 2"` //脚本额外变量类型1yaml 2json
Value string `form:"value"` //脚本额外变量值
......
package controller
import (
"encoding/json"
"errors"
"fmt"
"github.com/ghodss/yaml"
"github.com/gin-gonic/gin"
"github.com/minio/minio-go/v7"
"github.com/spf13/cast"
......@@ -153,11 +156,50 @@ func ExecScript(c *gin.Context) {
req.Script = string(obj)
}
//写入额外yml参数
if req.Type == 1 {
//var scripts []map[string]interface{}
var value map[string]interface{}
j2, err := yaml.YAMLToJSON([]byte(req.Script))
if err != nil {
err = resp.YamlAnalysisError.WithError(err)
return
}
err = json.Unmarshal(j2, &value)
if err != nil {
err = resp.InvalidParam.WithError(errors.New("yaml格式错误"))
return
}
value["hosts"] = fmt.Sprintf("%s%d", service.AnsibleGroup, req.HostGroupId)
j, err := json.Marshal(value)
if err != nil {
err = resp.MarshalError.WithError(err)
return
}
req.Value = fmt.Sprintf("%s", j)
//写入执行脚本
//f3, err := os.Create("/etc/ansible/ansible_extra.yml")
//if err != nil {
// err = resp.FileExecError.WithError(err)
// return
//}
//defer f3.Close()
//_, err = f3.Write([]byte(req.Value))
//if err != nil {
// err = resp.FileExecError.WithError(err)
// return
//}
//req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml")
}
taskManageSvc := service.TaskManageSvc{}
err := taskManageSvc.ExecScript(req)
id, err := taskManageSvc.ExecScript(req)
if err != nil {
SendJsonResponse(c, err, nil)
return
}
SendJsonResponse(c, nil, nil)
SendJsonResponse(c, nil, id)
}
......@@ -182,7 +182,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
return
}
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) {
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (id int, err error) {
//var scripts []map[string]interface{}
//var script map[string]interface{}
//j2, err := yaml.YAMLToJSON([]byte(req.Script))
......@@ -207,30 +207,18 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) {
//写入执行脚本
f2, err := os.Create("/etc/ansible/ansible.yml")
if err != nil {
return resp.FileExecError.WithError(err)
err = resp.FileExecError.WithError(err)
return
}
defer f2.Close()
_, err = f2.Write([]byte(req.Script))
if err != nil {
return resp.FileExecError.WithError(err)
}
//写入额外yml参数
if req.Type == 1 {
//写入执行脚本
f3, err := os.Create("/etc/ansible/ansible_extra.yml")
if err != nil {
return resp.FileExecError.WithError(err)
}
defer f3.Close()
_, err = f3.Write([]byte(req.Value))
if err != nil {
return resp.FileExecError.WithError(err)
}
req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml")
err = resp.FileExecError.WithError(err)
return
}
//新增任务历史
id, err := AddExecHistory(request.AddExecHistory{
id, err = AddExecHistory(request.AddExecHistory{
TaskId: req.TaskId,
CreateUser: "",
})
......@@ -241,23 +229,26 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (err error) {
//执行ansible命令
var cmd *exec.Cmd
if req.Value != "" {
cmd = exec.Command("ansible-playbook", "/etc/ansible/ansible.yml", "--extra-vars", req.Value) //2>/dev/null 忽略警告日志
cmd = exec.Command("ansible-playbook", "/etc/ansible/ansible.yml", "--extra-vars", req.Value)
} else {
cmd = exec.Command("ansible-playbook", "/etc/ansible/ansible.yml")
}
//捕获正常日志
stdout, err := cmd.StdoutPipe()
if err != nil {
return resp.CmdExecError.WithError(err)
err = resp.CmdExecError.WithError(err)
return
}
//捕获异常日志
stderr, err := cmd.StderrPipe()
if err != nil {
return resp.CmdExecError.WithError(err)
err = resp.CmdExecError.WithError(err)
return
}
//执行cmd命令
if err = cmd.Start(); err != nil {
return resp.CmdExecError.WithError(err)
err = resp.CmdExecError.WithError(err)
return
}
//获取 正常/异常 输出流
outputBuf := bufio.NewReader(stdout)
......
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