From b9cf891b68d7b1d34d187f2c2d335eeed964104c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=8F=E7=81=BF?= Date: Fri, 30 Jun 2023 18:01:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=84=9A=E6=9C=ACfix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pkg/beagle/resp/code.go | 2 ++ src/service/host_manage.go | 42 +++++++++------------------------ src/service/task_manage.go | 46 ++++++++++++++++++------------------- 3 files changed, 36 insertions(+), 54 deletions(-) diff --git a/src/pkg/beagle/resp/code.go b/src/pkg/beagle/resp/code.go index 3a85585..492dadf 100644 --- a/src/pkg/beagle/resp/code.go +++ b/src/pkg/beagle/resp/code.go @@ -30,6 +30,8 @@ var ( UnableAccountLock = Resp{Code: 5010011, Msg: "暂无账号锁定"} CmdExecError = Resp{Code: 5010012, Msg: "执行shell命令失败"} FileExecError = Resp{Code: 5010013, Msg: "文件执行失败"} + YamlAnalysisError = Resp{Code: 5010014, Msg: "yaml解析错误"} + MarshalError = Resp{Code: 5010015, Msg: "文件解析错误"} // 文件上传 UploadFileError = Resp{Code: 6000001, Msg: "文件上传失败"} diff --git a/src/service/host_manage.go b/src/service/host_manage.go index c72e3c7..b38e0d0 100644 --- a/src/service/host_manage.go +++ b/src/service/host_manage.go @@ -22,6 +22,8 @@ type HostManageSvc struct { User *entity.SystemUser } +const AnsibleGroup string = "HostManage" + // AddHostManage 新增主机 func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq) (err error) { db, err := client.GetDbClient() @@ -355,29 +357,21 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err return } defer f.Close() + _, err = f.Write([]byte(fmt.Sprintf("%s%d\n", AnsibleGroup, req.Id))) + if err != nil { + return resp.FileExecError.WithError(err) + } for _, v := range hostManageList { _, err = f.Write([]byte(fmt.Sprintf("%s ansible_ssh_host=%s ansible_ssh_port=%s ansible_ssh_user=\"%s\" ansible_ssh_pass=\"%s\"\n", v.Ip, v.Ip, v.Port, v.UserName, v.Password))) if err != nil { return resp.FileExecError.WithError(err) } } - for _, v := range hostManageList { - //状态检测 - ipConn := StatusDetection(v.Ip) - var state int - if ipConn { - //连接正常 - state = 0 - } else { - //连接异常 - state = 1 - } - //修改状态 _, err = session.Table("host_manage_list").Where("is_delete = 0 AND id = ?", v.Id). Cols("conn_status").Update(&entity.HostManageList{ - ConnStatus: state, + ConnStatus: StatusDetection(v.Ip), }) if err != nil { err = resp.DbUpdateError.WithError(err) @@ -385,7 +379,6 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err return } } - session.Commit() return } @@ -444,15 +437,7 @@ func (h *HostManageSvc) SaveStateHostManage(hostManageList []request.HostManageL } for k, v := range HostManageListCaches { //调用状态检测函数 - ipConn := StatusDetection(v.Ip) - if ipConn { - //连接正常 - HostManageListCaches[k].ConnStatus = 0 - } else { - //连接异常 - HostManageListCaches[k].ConnStatus = 1 - } - + HostManageListCaches[k].ConnStatus = StatusDetection(v.Ip) HostManageListCaches[k].Id = id } @@ -521,21 +506,16 @@ func (h *HostManageSvc) SaveIpExceptionList(req request.HostIpExceptionListReq) } // StatusDetection 状态检测 -func StatusDetection(ip string) (ipConn bool) { - +func StatusDetection(ip string) (ipConn int) { var cmd *exec.Cmd - cmd = exec.Command("ansible", fmt.Sprintf("%s", ip), "-m", "ping") - output, err := cmd.Output() if err != nil { fmt.Println("ping:", string(output)) - return false + return 1 } fmt.Println("ping:", string(output)) - //return string(output), nil - - return true + return 0 } // ExportIpStr 结果导出 diff --git a/src/service/task_manage.go b/src/service/task_manage.go index f8c9c70..04738e0 100644 --- a/src/service/task_manage.go +++ b/src/service/task_manage.go @@ -3,6 +3,8 @@ package service import ( "errors" "fmt" + "github.com/ghodss/yaml" + json "github.com/json-iterator/go" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" @@ -147,28 +149,27 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int } func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{}, err error) { - ////获取主机IP - //var ipList []string - //db, err := client.GetDbClient() - //if err != nil { - // return nil, resp.DbConnectError.WithError(err) - //} - //if err := db.Table("host_manage_list").Select("ip").Where("is_delete = 0 AND host_group_id = ?", req.HostGroupId).Find(&ipList); err != nil { - // return nil, resp.DbSelectError.WithError(err) - //} - ////写入主机组ip - //f, err := os.OpenFile("/etc/ansible/hosts", os.O_APPEND|os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777) - //if err != nil { - // err = resp.FileExecError.WithError(err) - // return - //} - //defer f.Close() - //for _, v := range ipList { - // _, err := f.Write([]byte(fmt.Sprintf("%s\n", v))) - // if err != nil { - // return nil, resp.FileExecError.WithError(err) - // } - //} + var script map[string]interface{} + j2, err := yaml.YAMLToJSON([]byte(req.Script)) + if err != nil { + return nil, resp.YamlAnalysisError.WithError(err) + } + err = json.Unmarshal(j2, &script) + if err != nil { + return nil, resp.MarshalError.WithError(err) + } + if script["host"] == "all" { + script["host"] = fmt.Sprintf("%s%d", AnsibleGroup, req.HostGroupId) + } + j, err := json.Marshal(script) + if err != nil { + return nil, resp.MarshalError.WithError(err) + } + y, err := yaml.JSONToYAML(j) + if err != nil { + return nil, resp.YamlAnalysisError.WithError(err) + } + req.Script = string(y) //写入执行脚本 f2, err := os.Create("/etc/ansible/ansible.yml") if err != nil { @@ -193,7 +194,6 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{}, } req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml") } - var cmd *exec.Cmd if req.Value != "" { cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts", "/etc/ansible/ansible.yml", "--extra-vars", req.Value) -- 2.26.0