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

立即执行逻辑优化 fix

parent 7ede4b8a
...@@ -143,7 +143,7 @@ func ExecScript(c *gin.Context) { ...@@ -143,7 +143,7 @@ func ExecScript(c *gin.Context) {
} }
if taskManage.YamlDesc == "" { if taskManage.YamlDesc == "" {
//解析文件 //解析minio文件
ymlUrl := strings.Split(taskManage.YamlUrl, "/") ymlUrl := strings.Split(taskManage.YamlUrl, "/")
minioClient, err2 := client.GetMinioConnect() minioClient, err2 := client.GetMinioConnect()
if err2 != nil { if err2 != nil {
...@@ -163,27 +163,7 @@ func ExecScript(c *gin.Context) { ...@@ -163,27 +163,7 @@ func ExecScript(c *gin.Context) {
taskManage.YamlDesc = string(obj) taskManage.YamlDesc = string(obj)
} }
//解析yml文件 //写入额外yml变量
//if req.YmlFileName != "" && req.Value == "" {
// minioClient, err := client.GetMinioConnect()
// if err != nil {
// SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("执行脚本为空")), nil)
// return
// }
// object, err := minioClient.GetObject(c, conf.Options.MinioBucket, req.YmlFileName, minio.GetObjectOptions{})
// if err != nil {
// SendJsonResponse(c, resp.GetFileStreamError.WithError(err), nil)
// return
// }
// obj, err := io.ReadAll(object)
// if err != nil {
// SendJsonResponse(c, resp.ReadFileError.WithError(err), nil)
// return
// }
// req.Script = string(obj)
//}
//写入额外yml参数
if req.Type == 1 { if req.Type == 1 {
var value map[string]interface{} var value map[string]interface{}
j2, err3 := yaml.YAMLToJSON([]byte(req.Value)) j2, err3 := yaml.YAMLToJSON([]byte(req.Value))
...@@ -196,7 +176,6 @@ func ExecScript(c *gin.Context) { ...@@ -196,7 +176,6 @@ func ExecScript(c *gin.Context) {
err = resp.InvalidParam.WithError(errors.New("变量配置错误")) err = resp.InvalidParam.WithError(errors.New("变量配置错误"))
return return
} }
value["hosts"] = fmt.Sprintf("%s%d", service.AnsibleGroup, taskManage.HostGroupId)
j, err3 := json.Marshal(value) j, err3 := json.Marshal(value)
if err3 != nil { if err3 != nil {
...@@ -204,20 +183,6 @@ func ExecScript(c *gin.Context) { ...@@ -204,20 +183,6 @@ func ExecScript(c *gin.Context) {
return return
} }
req.Value = fmt.Sprintf("%s", j) 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")
} }
id, err := taskManageSvc.ExecScript(req, taskManage.YamlDesc) id, err := taskManageSvc.ExecScript(req, taskManage.YamlDesc)
......
...@@ -130,12 +130,13 @@ func initAnsibleHosts() { ...@@ -130,12 +130,13 @@ func initAnsibleHosts() {
defer f.Close() defer f.Close()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} else {
_, err := f.Write([]byte("[web]\n"))
if err != nil {
fmt.Println(err.Error())
}
} }
//else {
// _, err := f.Write([]byte("[web]\n"))
// if err != nil {
// fmt.Println(err.Error())
// }
//}
} }
func initTempDirPrefix() { func initTempDirPrefix() {
......
...@@ -401,7 +401,7 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err ...@@ -401,7 +401,7 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err
defer session.Close() defer session.Close()
session.Begin() session.Begin()
//查询主机IP列表 // 查询主机IP列表
hostManageList := make([]response.HostManageListRes, 0) hostManageList := make([]response.HostManageListRes, 0)
finder := session.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", req.Id) finder := session.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", req.Id)
err = finder.Find(&hostManageList) err = finder.Find(&hostManageList)
...@@ -410,13 +410,14 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err ...@@ -410,13 +410,14 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err
return return
} }
//读取hosts中的主机组 // 读取hosts中的主机组
hosts, err := tools.HostsToJson() hosts, err := tools.HostsToJson()
if err != nil { if err != nil {
err = resp.MarshalError.WithError(err) err = resp.MarshalError.WithError(err)
session.Rollback() session.Rollback()
return return
} }
if _, ok := hosts["["+AnsibleGroup+fmt.Sprintf("%d", req.Id)+"]"]; !ok { if _, ok := hosts["["+AnsibleGroup+fmt.Sprintf("%d", req.Id)+"]"]; !ok {
// 不存在 // 不存在
hosts["["+AnsibleGroup+fmt.Sprintf("%d", req.Id)+"]"] = nil hosts["["+AnsibleGroup+fmt.Sprintf("%d", req.Id)+"]"] = nil
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"strings"
"time" "time"
) )
...@@ -202,29 +203,55 @@ func (t *TaskManageSvc) GetTaskManage(id int) (getTaskManage response.GetTaskMan ...@@ -202,29 +203,55 @@ func (t *TaskManageSvc) GetTaskManage(id int) (getTaskManage response.GetTaskMan
} }
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id int, err error) { func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id int, err error) {
//var scripts []map[string]interface{} //查询主机信息
//var script map[string]interface{} db, err := client.GetDbClient()
//j2, err := yaml.YAMLToJSON([]byte(req.Script)) if err != nil {
//if err != nil { err = resp.DbConnectError.WithError(err)
// return nil, resp.YamlAnalysisError.WithError(err) return
//} }
//err = json.Unmarshal(j2, &scripts)
//if err != nil { //查询任务详情
// return nil, resp.MarshalError.WithError(errors.New("yaml格式错误")) hostManageList := make([]response.HostManageListRes, 0)
//} finder := db.Table("task_manage").Alias("tm").
//script = scripts[0] Join("INNER", "host_manage hm", "tm.host_group_id = hm.id").
//script["host"] = fmt.Sprintf("%s%d", AnsibleGroup, req.HostGroupId) Join("INNER", "host_manage_list hml", "hm.id = hml.host_group_id").
//j, err := json.Marshal(script) Where("tm.is_delete = 0 AND hm.is_delete = 0 AND hml.is_delete = 0 AND tm.id = ?", req.TaskId)
//if err != nil { err = finder.Select("hml.ip,hml.ip,hml.port,hml.voucher_type,hml.user_name,hml.password").Find(&hostManageList)
// return nil, resp.MarshalError.WithError(err) if err != nil {
//} err = resp.DbSelectError.WithError(err)
//y, err := yaml.JSONToYAML(j) return
//if err != nil { }
// return nil, resp.YamlAnalysisError.WithError(err)
//} //新增主机分组列表
//req.Script = string(y) var hosts []string
for _, v := range hostManageList {
hostsIp := ""
if v.VoucherType == 0 {
hostsIp = fmt.Sprintf("%s:%s ansible_ssh_user=\"%s\" ansible_ssh_pass=\"%s\" ansible_host_key_checking=false\n",
v.Ip, v.Port, v.UserName, v.Password)
} else {
hostsIp = fmt.Sprintf("%s:%s ansible_ssh_user=\"%s\" ansible_ssh_private_key_file=/root/.ssh/id_rsa ansible_host_key_checking=false\n",
v.Ip, v.Port, v.UserName)
}
hosts = append(hosts, hostsIp)
}
//写入主机信息
hostsIp := strings.Replace(strings.Trim(fmt.Sprint(hosts), "[]"), " ", "\n", -1)
hostsGroup, err := os.Create("/etc/ansible/hosts_" + fmt.Sprintf("%d", req.TaskId))
if err != nil {
err = resp.FileExecError.WithError(err)
return
}
defer hostsGroup.Close()
_, err = hostsGroup.Write([]byte(hostsIp))
if err != nil {
err = resp.FileExecError.WithError(err)
return
}
//写入执行脚本 //写入执行脚本
f2, err := os.Create("/etc/ansible/ansible.yml") f2, err := os.Create("/etc/ansible/ansible_" + fmt.Sprintf("%d", req.TaskId) + ".yml")
if err != nil { if err != nil {
err = resp.FileExecError.WithError(err) err = resp.FileExecError.WithError(err)
return return
...@@ -248,10 +275,11 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id ...@@ -248,10 +275,11 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
//执行ansible命令 //执行ansible命令
var cmd *exec.Cmd var cmd *exec.Cmd
if req.Value != "" { if req.Value != "" {
cmd = exec.Command("ansible-playbook", "/etc/ansible/ansible.yml", "--extra-vars", 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 { } else {
cmd = exec.Command("ansible-playbook", "/etc/ansible/ansible.yml", "--extra-vars", "{\"hosts\":\""+"cc"+"\"}") 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() stdout, err := cmd.StdoutPipe()
if err != nil { if err != 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