Commit b9cf891b authored by 魏灿's avatar 魏灿

执行脚本fix

parent 51eebe6a
......@@ -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: "文件上传失败"}
......
......@@ -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 结果导出
......
......@@ -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)
......
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