Commit b9cf891b authored by 魏灿's avatar 魏灿

执行脚本fix

parent 51eebe6a
...@@ -30,6 +30,8 @@ var ( ...@@ -30,6 +30,8 @@ var (
UnableAccountLock = Resp{Code: 5010011, Msg: "暂无账号锁定"} UnableAccountLock = Resp{Code: 5010011, Msg: "暂无账号锁定"}
CmdExecError = Resp{Code: 5010012, Msg: "执行shell命令失败"} CmdExecError = Resp{Code: 5010012, Msg: "执行shell命令失败"}
FileExecError = Resp{Code: 5010013, Msg: "文件执行失败"} FileExecError = Resp{Code: 5010013, Msg: "文件执行失败"}
YamlAnalysisError = Resp{Code: 5010014, Msg: "yaml解析错误"}
MarshalError = Resp{Code: 5010015, Msg: "文件解析错误"}
// 文件上传 // 文件上传
UploadFileError = Resp{Code: 6000001, Msg: "文件上传失败"} UploadFileError = Resp{Code: 6000001, Msg: "文件上传失败"}
......
...@@ -22,6 +22,8 @@ type HostManageSvc struct { ...@@ -22,6 +22,8 @@ type HostManageSvc struct {
User *entity.SystemUser User *entity.SystemUser
} }
const AnsibleGroup string = "HostManage"
// AddHostManage 新增主机 // AddHostManage 新增主机
func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq) (err error) { func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq) (err error) {
db, err := client.GetDbClient() db, err := client.GetDbClient()
...@@ -355,29 +357,21 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err ...@@ -355,29 +357,21 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err
return return
} }
defer f.Close() 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 { 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))) _, 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 { if err != nil {
return resp.FileExecError.WithError(err) return resp.FileExecError.WithError(err)
} }
} }
for _, v := range hostManageList { 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). _, err = session.Table("host_manage_list").Where("is_delete = 0 AND id = ?", v.Id).
Cols("conn_status").Update(&entity.HostManageList{ Cols("conn_status").Update(&entity.HostManageList{
ConnStatus: state, ConnStatus: StatusDetection(v.Ip),
}) })
if err != nil { if err != nil {
err = resp.DbUpdateError.WithError(err) err = resp.DbUpdateError.WithError(err)
...@@ -385,7 +379,6 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err ...@@ -385,7 +379,6 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err
return return
} }
} }
session.Commit() session.Commit()
return return
} }
...@@ -444,15 +437,7 @@ func (h *HostManageSvc) SaveStateHostManage(hostManageList []request.HostManageL ...@@ -444,15 +437,7 @@ func (h *HostManageSvc) SaveStateHostManage(hostManageList []request.HostManageL
} }
for k, v := range HostManageListCaches { for k, v := range HostManageListCaches {
//调用状态检测函数 //调用状态检测函数
ipConn := StatusDetection(v.Ip) HostManageListCaches[k].ConnStatus = StatusDetection(v.Ip)
if ipConn {
//连接正常
HostManageListCaches[k].ConnStatus = 0
} else {
//连接异常
HostManageListCaches[k].ConnStatus = 1
}
HostManageListCaches[k].Id = id HostManageListCaches[k].Id = id
} }
...@@ -521,21 +506,16 @@ func (h *HostManageSvc) SaveIpExceptionList(req request.HostIpExceptionListReq) ...@@ -521,21 +506,16 @@ func (h *HostManageSvc) SaveIpExceptionList(req request.HostIpExceptionListReq)
} }
// StatusDetection 状态检测 // StatusDetection 状态检测
func StatusDetection(ip string) (ipConn bool) { func StatusDetection(ip string) (ipConn int) {
var cmd *exec.Cmd var cmd *exec.Cmd
cmd = exec.Command("ansible", fmt.Sprintf("%s", ip), "-m", "ping") cmd = exec.Command("ansible", fmt.Sprintf("%s", ip), "-m", "ping")
output, err := cmd.Output() output, err := cmd.Output()
if err != nil { if err != nil {
fmt.Println("ping:", string(output)) fmt.Println("ping:", string(output))
return false return 1
} }
fmt.Println("ping:", string(output)) fmt.Println("ping:", string(output))
//return string(output), nil return 0
return true
} }
// ExportIpStr 结果导出 // ExportIpStr 结果导出
......
...@@ -3,6 +3,8 @@ package service ...@@ -3,6 +3,8 @@ package service
import ( import (
"errors" "errors"
"fmt" "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/entity"
"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"
...@@ -147,28 +149,27 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int ...@@ -147,28 +149,27 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
} }
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{}, err error) { func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{}, err error) {
////获取主机IP var script map[string]interface{}
//var ipList []string j2, err := yaml.YAMLToJSON([]byte(req.Script))
//db, err := client.GetDbClient() if err != nil {
//if err != nil { return nil, resp.YamlAnalysisError.WithError(err)
// return nil, resp.DbConnectError.WithError(err) }
//} err = json.Unmarshal(j2, &script)
//if err := db.Table("host_manage_list").Select("ip").Where("is_delete = 0 AND host_group_id = ?", req.HostGroupId).Find(&ipList); err != nil { if err != nil {
// return nil, resp.DbSelectError.WithError(err) return nil, resp.MarshalError.WithError(err)
//} }
////写入主机组ip if script["host"] == "all" {
//f, err := os.OpenFile("/etc/ansible/hosts", os.O_APPEND|os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777) script["host"] = fmt.Sprintf("%s%d", AnsibleGroup, req.HostGroupId)
//if err != nil { }
// err = resp.FileExecError.WithError(err) j, err := json.Marshal(script)
// return if err != nil {
//} return nil, resp.MarshalError.WithError(err)
//defer f.Close() }
//for _, v := range ipList { y, err := yaml.JSONToYAML(j)
// _, err := f.Write([]byte(fmt.Sprintf("%s\n", v))) if err != nil {
// if err != nil { return nil, resp.YamlAnalysisError.WithError(err)
// return nil, resp.FileExecError.WithError(err) }
// } req.Script = string(y)
//}
//写入执行脚本 //写入执行脚本
f2, err := os.Create("/etc/ansible/ansible.yml") f2, err := os.Create("/etc/ansible/ansible.yml")
if err != nil { if err != nil {
...@@ -193,7 +194,6 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{}, ...@@ -193,7 +194,6 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{},
} }
req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml") req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml")
} }
var cmd *exec.Cmd var cmd *exec.Cmd
if req.Value != "" { if req.Value != "" {
cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts", "/etc/ansible/ansible.yml", "--extra-vars", req.Value) cmd = exec.Command("ansible-playbook", "-i", "/etc/ansible/hosts", "/etc/ansible/ansible.yml", "--extra-vars", req.Value)
......
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