Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
so-operation-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
smart-operation
so-operation-api
Commits
39624e4b
Commit
39624e4b
authored
Jun 27, 2023
by
魏灿
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ansible任务执行
parent
395b624a
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
93 additions
and
1 deletion
+93
-1
src/bean/vo/request/task_manage.go
src/bean/vo/request/task_manage.go
+7
-0
src/controller/task_manage.go
src/controller/task_manage.go
+20
-0
src/pkg/beagle/resp/code.go
src/pkg/beagle/resp/code.go
+2
-0
src/router/automatedmaintenrouter.go
src/router/automatedmaintenrouter.go
+1
-1
src/service/task_manage.go
src/service/task_manage.go
+63
-0
No files found.
src/bean/vo/request/task_manage.go
View file @
39624e4b
...
@@ -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"`
//执行脚本
}
src/controller/task_manage.go
View file @
39624e4b
...
@@ -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
)
}
src/pkg/beagle/resp/code.go
View file @
39624e4b
...
@@ -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
:
"文件执行失败"
}
)
)
src/router/automatedmaintenrouter.go
View file @
39624e4b
...
@@ -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"
)
...
...
src/service/task_manage.go
View file @
39624e4b
...
@@ -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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment