Commit 39624e4b authored by 魏灿's avatar 魏灿

ansible任务执行

parent 395b624a
...@@ -30,3 +30,10 @@ type ListTaskManageReq struct { ...@@ -30,3 +30,10 @@ type ListTaskManageReq struct {
CreateDateTo string `json:"createDateTo" form:"createDateTo"` //创建时间至 CreateDateTo string `json:"createDateTo" form:"createDateTo"` //创建时间至
Pagination Pagination
} }
type ExecScriptReq struct {
HostGroupId int `json:"host_group_id" vd:"$>0;msg:'请输入主机分组id'"` //主机分组id
Type int `json:"type"` //脚本额外变量类型1yaml 2json
Value string `json:"value"` //脚本额外变量值
Script string `json:"script"` //执行脚本
}
...@@ -116,3 +116,23 @@ func ListTaskManage(c *gin.Context) { ...@@ -116,3 +116,23 @@ func ListTaskManage(c *gin.Context) {
} }
SendJsonPageResponse(c, resp.OK, list, total) SendJsonPageResponse(c, resp.OK, list, total)
} }
func ExecScript(c *gin.Context) {
var req request.ExecScriptReq
if err := c.ShouldBindJSON(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
//参数校验
if err := util.ValidateSimple(req, "HostManageId"); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
taskManageSvc := service.TaskManageSvc{}
data, err := taskManageSvc.ExecScript(req)
if err != nil {
SendJsonResponse(c, err, nil)
return
}
SendJsonResponse(c, nil, data)
}
...@@ -28,4 +28,6 @@ var ( ...@@ -28,4 +28,6 @@ var (
EsConnectError = Resp{Code: 5010009, Msg: "es连接失败"} EsConnectError = Resp{Code: 5010009, Msg: "es连接失败"}
AddSheetError = Resp{Code: 5010010, Msg: "新建sheet失败"} AddSheetError = Resp{Code: 5010010, Msg: "新建sheet失败"}
UnableAccountLock = Resp{Code: 5010011, Msg: "暂无账号锁定"} UnableAccountLock = Resp{Code: 5010011, Msg: "暂无账号锁定"}
CmdExecError = Resp{Code: 5010012, Msg: "执行shell命令失败"}
FileExecError = Resp{Code: 5010013, Msg: "文件执行失败"}
) )
...@@ -18,7 +18,7 @@ func InitAutomatedMaintenRouter(e *gin.Engine) { ...@@ -18,7 +18,7 @@ func InitAutomatedMaintenRouter(e *gin.Engine) {
task.DELETE("/del", controller.DelTaskManage) // 删除 task.DELETE("/del", controller.DelTaskManage) // 删除
task.GET("/details", controller.DetailsTaskManage) // 详情 task.GET("/details", controller.DetailsTaskManage) // 详情
task.GET("/list", controller.ListTaskManage) // 列表 task.GET("/list", controller.ListTaskManage) // 列表
task.POST("/exec/script") // 立即执行 task.POST("/exec/script", controller.ExecScript) // 立即执行
} }
//主机管理 //主机管理
host := so.Group("/hostManage") host := so.Group("/hostManage")
......
...@@ -7,6 +7,8 @@ import ( ...@@ -7,6 +7,8 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"os"
"os/exec"
"time" "time"
) )
...@@ -142,3 +144,64 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int ...@@ -142,3 +144,64 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
} }
return return
} }
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{}, err error) {
//获取主机IP
var ipList []string
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
if err := db.Table("host_manage_list").Select("ip").Where("host_group_id = ?", req.HostGroupId).Find(&ipList); err != nil {
err = resp.DbSelectError.WithError(err)
return
}
//写入主机组ip
f, err := os.Open("/etc/ansible/hosts")
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)
}
}
//写入执行脚本
f2, err := os.Create("/etc/ansible/ansible.yml")
if err != nil {
err = resp.FileExecError.WithError(err)
return
}
defer f2.Close()
_, err = f2.Write([]byte(req.Script))
if err != nil {
return nil, resp.FileExecError.WithError(err)
}
//写入额外yml参数
if req.Type == 1 {
//写入执行脚本
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 {
return nil, resp.FileExecError.WithError(err)
}
req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml")
}
cmd := exec.Command("ansible", "-i", "/etc/ansible/hosts", "/etc/ansible/ansible.yml", "--extra-vars", req.Value)
output, err := cmd.Output()
if err != nil {
err = resp.CmdExecError.WithError(err)
return
}
fmt.Println(string(output))
return string(output), 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