Commit 56e8b07b authored by 陈子龙's avatar 陈子龙

自动化运维 立即执行 fix

parent 25da70e7
package service
import (
"bufio"
"context"
"errors"
"fmt"
......@@ -223,64 +222,55 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data map[string]i
if err != nil {
return nil, resp.YamlAnalysisError.WithError(err)
}
fmt.Println("11111111")
req.Script = string(y)
//写入执行脚本
f2, err := os.Create("/etc/ansible/ansible.yml")
if err != nil {
fmt.Println("22222222222:", err)
return nil, resp.FileExecError.WithError(err)
}
defer f2.Close()
_, err = f2.Write([]byte(req.Script))
if err != nil {
fmt.Println("3333333333333:", err)
return nil, resp.FileExecError.WithError(err)
}
fmt.Println("4444444444444444:")
//写入额外yml参数
if req.Type == 1 {
//写入执行脚本
f3, err := os.Create("/etc/ansible/ansible_extra.yml")
if err != nil {
fmt.Println("55555555555555555:", err)
return nil, resp.FileExecError.WithError(err)
}
defer f3.Close()
_, err = f3.Write([]byte(req.Value))
if err != nil {
fmt.Println("66666666666666666:", err)
return nil, resp.FileExecError.WithError(err)
}
req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml")
fmt.Println("77777777777777777777:")
}
fmt.Println("88888888888888888888:")
var cmd *exec.Cmd
if req.Value != "" {
cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts", "/etc/ansible/ansible.yml", "--extra-vars", req.Value)
} else {
fmt.Println("999999999999999999999999:")
cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts", "/etc/ansible/ansible.yml")
}
fmt.Println("aaaaaaaaaaaaaaa:")
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("执行出错:", err)
fmt.Println("stdout:", stdout)
return nil, resp.CmdExecError.WithError(err)
}
fmt.Println("bbbbbbbbbbbbbbbbbbbb:")
outputBuf := bufio.NewReader(stdout)
output, isPrefix, err := outputBuf.ReadLine()
fmt.Println("执行成功")
fmt.Println("stdout:", stdout)
//outputBuf := bufio.NewReader(stdout)
//output, isPrefix, err := outputBuf.ReadLine()
//output, err := cmd.Output()
//if err != nil {
// err = resp.CmdExecError.WithError(err)
// return
//}
//fmt.Println(string(output))
data["step"] = isPrefix
data["output"] = string(output)
fmt.Println("ccccccccccccccc:")
//data["step"] = isPrefix
//data["output"] = string(output)
//data["output"] = string(output)
return data, nil
}
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