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
e2f7dd18
Commit
e2f7dd18
authored
Jun 29, 2023
by
陈子龙
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
自动化运维 fix
parent
d75d0acb
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
406 additions
and
119 deletions
+406
-119
src/bean/entity/host_manage.go
src/bean/entity/host_manage.go
+13
-1
src/bean/vo/request/host_manage.go
src/bean/vo/request/host_manage.go
+19
-10
src/bean/vo/response/host_manage.go
src/bean/vo/response/host_manage.go
+14
-0
src/controller/host_manage.go
src/controller/host_manage.go
+110
-60
src/pkg/beagle/resp/code.go
src/pkg/beagle/resp/code.go
+1
-0
src/router/automatedmaintenrouter.go
src/router/automatedmaintenrouter.go
+7
-6
src/service/host_manage.go
src/service/host_manage.go
+220
-20
src/service/task_manage.go
src/service/task_manage.go
+22
-22
No files found.
src/bean/entity/host_manage.go
View file @
e2f7dd18
...
...
@@ -22,5 +22,17 @@ type HostManageList struct {
HostGroupId
int
`json:"hostGroupId" xorm:"host_group_id"`
// 主机分组id
HostFileUrl
string
`json:"hostFileUrl" xorm:"host_file_url"`
// 主机文件url
IsDelete
int
`json:"isDelete" xorm:"is_delete" `
// 是否删除(0 未删除 1已删除)
ConnStatus
int
`json:"connStatus" xorm:"conn_status" `
// 连接状态(0异常 1正常)
ConnStatus
int
`json:"connStatus" xorm:"conn_status" `
// 连接状态(0正常 1异常)
IpGroup
int
`json:"ipGroup" xorm:"ip_group" `
// ip分组
}
type
HostManageListCache
struct
{
Id
string
`json:"id" xorm:"id" `
// id
Ip
string
`json:"ip" xorm:"ip"`
// ip
Port
string
`json:"port" xorm:"port"`
// 端口
VoucherType
int
`json:"voucherType" xorm:"voucher_type"`
// 凭证类型(0密码验证 密钥验证)
UserName
string
`json:"userName" xorm:"user_name"`
// 用户名
Password
string
`json:"password" xorm:"password"`
// 密码
ConnStatus
int
`json:"connStatus" xorm:"conn_status" `
// 连接状态(0正常 1异常)
IpGroup
int
`json:"ipGroup" xorm:"ip_group" `
// ip分组
}
src/bean/vo/request/host_manage.go
View file @
e2f7dd18
package
request
type
AddHostManageReq
struct
{
HostName
string
`form:"hostName" binding:"required"`
// 主机分组名称
HostType
int
`form:"hostType" binding:"oneof=0 1"`
//主机上传类型(0列表维护 1文件上传)
HostFileUrl
string
`form:"hostFileUrl"`
// 主机文件url
//HostFile string `form:"hostFile"` // 主机文件
HostManageList
string
`form:"hostManageList"`
//主机列表
HostName
string
`json:"hostName" binding:"required"`
// 主机分组名称
HostFileUrl
string
`json:"hostFileUrl"`
// 主机文件url
HostManageList
[]
HostManageList
`json:"hostManageList"`
//主机列表
}
type
HostManageList
struct
{
...
...
@@ -14,13 +12,13 @@ type HostManageList struct {
VoucherType
int
`json:"voucherType"`
// 凭证类型(0密码验证 1密钥验证)
UserName
string
`json:"userName"`
// 用户名
Password
string
`json:"password"`
// 密码
IpGroup
int
`json:"ipGroup"`
// ip分组
}
type
EditHostManageReq
struct
{
Id
int
`form:"id" binding:"required"`
// 主键ID
HostType
int
`form:"hostType" binding:"oneof=0 1"`
//主机上传类型(0列表维护 1文件上传)
HostFileUrl
string
`form:"hostFileUrl"`
// 主机文件url
HostManageList
string
`form:"hostManageList"`
//主机列表
Id
int
`json:"id" binding:"required"`
// 主键ID
HostFileUrl
string
`json:"hostFileUrl"`
// 主机文件url
HostManageList
[]
HostManageList
`json:"hostManageList"`
//主机列表
}
type
DelHostManageReq
struct
{
...
...
@@ -34,7 +32,18 @@ type ListHostManageReq struct {
Pagination
}
// StateHostManageReq 状态检测
type
StateHostManageReq
struct
{
Id
int
`json:"id" form:"id" binding:"required"`
//主键ID
DetectionType
int
`json:"detectionType" form:"detectionType" binding:"oneof=1 2 3"`
//1主页列表手动检测 2输入ip列表保存时检测 3上传ip列表文件保存时检测
Id
int
`json:"id" form:"id"`
//主机分组ID
//HostFile string `form:"hostFile"` // 主机文件
HostManageList
string
`json:"hostManageList" form:"hostManageList"`
//主机ip列表
}
// HostIpExceptionListReq 异常列表
type
HostIpExceptionListReq
struct
{
DetectionType
int
`json:"detectionType" binding:"oneof=1 2"`
//1主页异常ip列表 2保存检测时异常列表
Id
int
`json:"id"`
//主机分组ID
Uuid
string
`json:"uuid"`
//临时缓存uuid
Pagination
}
src/bean/vo/response/host_manage.go
View file @
e2f7dd18
...
...
@@ -57,3 +57,17 @@ type HostManageListRes struct {
UserName
string
`json:"userName"`
// 用户名
Password
string
`json:"password"`
// 密码
}
type
HostManageListCacheRes
struct
{
AormalHost
[]
HostManageListCache
`json:"normalHost"`
// 正常ip
AbnormalHost
[]
HostManageListCache
`json:"abnormalHost"`
// 异常ip
}
type
HostManageListCache
struct
{
Ip
string
`json:"ip"`
// ip
Port
string
`json:"port"`
// 端口
VoucherType
int
`json:"voucherType"`
// 凭证类型(0密码验证 密钥验证)
UserName
string
`json:"userName"`
// 用户名
Password
string
`json:"password"`
// 密码
IpGroup
int
`json:"ipGroup"`
// ip分组
}
src/controller/host_manage.go
View file @
e2f7dd18
...
...
@@ -28,6 +28,11 @@ func ParsingHostFiles(c *gin.Context) (hostManageList []request.HostManageList,
//读取工作簿
rows
:=
xlsxData
.
GetRows
(
"Sheet1"
)
if
len
(
rows
)
>
1001
{
err
=
resp
.
FailedToParseFile
.
WithError
(
errors
.
New
(
"ip数量不可超过1000"
))
return
}
for
i
:=
0
;
i
<
len
(
rows
);
i
++
{
//默认跳过第一行
if
i
<
1
{
...
...
@@ -57,45 +62,21 @@ func AddHostManage(c *gin.Context) {
req
request
.
AddHostManageReq
err
error
)
if
err
=
c
.
ShouldBind
(
&
req
);
err
!=
nil
{
if
err
=
c
.
ShouldBind
JSON
(
&
req
);
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
return
}
hostManageList
:=
make
([]
request
.
HostManageList
,
0
)
if
req
.
HostType
==
0
{
err
=
json
.
Unmarshal
([]
byte
(
req
.
HostManageList
),
&
hostManageList
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
return
}
}
else
{
if
req
.
HostFileUrl
==
""
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机文件url为空"
)),
nil
)
return
}
//解析主机文件
hostManageList
,
err
=
ParsingHostFiles
(
c
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
ReadFileError
.
WithError
(
errors
.
New
(
"主机文件url为空"
)),
nil
)
return
}
}
//参数校验
if
req
.
HostName
==
""
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机分组名称为空"
)),
nil
)
return
}
if
len
(
h
ostManageList
)
==
0
{
if
len
(
req
.
H
ostManageList
)
==
0
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机分组数量为0"
)),
nil
)
return
}
if
len
(
hostManageList
)
>
5
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机分组数量超过5条"
)),
nil
)
return
}
for
_
,
v
:=
range
hostManageList
{
for
_
,
v
:=
range
req
.
HostManageList
{
if
v
.
Ip
==
""
||
v
.
UserName
==
""
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"ip或用户名为空"
)),
nil
)
return
...
...
@@ -107,7 +88,7 @@ func AddHostManage(c *gin.Context) {
}
hostManageSvc
:=
service
.
HostManageSvc
{}
err
=
hostManageSvc
.
AddHostManage
(
req
,
hostManageList
)
err
=
hostManageSvc
.
AddHostManage
(
req
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
err
,
nil
)
return
...
...
@@ -121,40 +102,17 @@ func EditHostManage(c *gin.Context) {
req
request
.
EditHostManageReq
err
error
)
if
err
:
=
c
.
ShouldBind
(
&
req
);
err
!=
nil
{
if
err
=
c
.
ShouldBind
(
&
req
);
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
return
}
hostManageList
:=
make
([]
request
.
HostManageList
,
0
)
if
req
.
HostType
==
0
{
err
=
json
.
Unmarshal
([]
byte
(
req
.
HostManageList
),
&
hostManageList
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
return
}
}
else
{
if
req
.
HostFileUrl
==
""
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机文件url为空"
)),
nil
)
return
}
//解析主机文件
hostManageList
,
err
=
ParsingHostFiles
(
c
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
ReadFileError
.
WithError
(
errors
.
New
(
"主机文件url为空"
)),
nil
)
return
}
}
//参数校验
if
len
(
h
ostManageList
)
==
0
{
if
len
(
req
.
H
ostManageList
)
==
0
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机分组数量为0"
)),
nil
)
return
}
if
len
(
hostManageList
)
>
5
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机分组数量超过5条"
)),
nil
)
return
}
for
_
,
v
:=
range
hostManageList
{
for
_
,
v
:=
range
req
.
HostManageList
{
if
v
.
Ip
==
""
||
v
.
UserName
==
""
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"ip或用户名为空"
)),
nil
)
return
...
...
@@ -166,7 +124,7 @@ func EditHostManage(c *gin.Context) {
}
hostManageSvc
:=
service
.
HostManageSvc
{}
err
=
hostManageSvc
.
EditHostManage
(
req
,
hostManageList
)
err
=
hostManageSvc
.
EditHostManage
(
req
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
err
,
nil
)
return
...
...
@@ -239,17 +197,109 @@ func ListHostManage(c *gin.Context) {
// StateHostManage 状态检测
func
StateHostManage
(
c
*
gin
.
Context
)
{
var
req
request
.
StateHostManageReq
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
var
(
req
request
.
StateHostManageReq
err
error
)
if
err
=
c
.
ShouldBind
(
&
req
);
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
return
}
hostManageList
:=
make
([]
request
.
HostManageList
,
0
)
hostManageSvc
:=
service
.
HostManageSvc
{}
total
,
list
,
err
:=
hostManageSvc
.
StateHostManage
(
req
)
switch
req
.
DetectionType
{
case
1
:
//列表点击状态检测
err
=
hostManageSvc
.
ListStateHostManage
(
req
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
err
,
nil
)
return
}
SendJsonResponse
(
c
,
resp
.
OK
,
nil
)
return
case
2
:
//解析主机列表
err
=
json
.
Unmarshal
([]
byte
(
req
.
HostManageList
),
&
hostManageList
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
return
}
case
3
:
//解析主机文件
hostManageList
,
err
=
ParsingHostFiles
(
c
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
ReadFileError
.
WithError
(
errors
.
New
(
"解析主机文件失败"
)),
nil
)
return
}
}
//参数校验
if
len
(
hostManageList
)
==
0
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机分组数量为0"
)),
nil
)
return
}
if
len
(
hostManageList
)
>
5
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"主机分组ip数量超过5条"
)),
nil
)
return
}
for
_
,
v
:=
range
hostManageList
{
if
v
.
Ip
==
""
||
v
.
UserName
==
""
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"ip或用户名为空"
)),
nil
)
return
}
if
v
.
VoucherType
==
0
&&
v
.
Password
==
""
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"密码为空"
)),
nil
)
return
}
}
//保存时状态检测
id
,
err
:=
hostManageSvc
.
SaveStateHostManage
(
hostManageList
)
if
err
!=
nil
{
SendJson
PageResponse
(
c
,
err
,
nil
,
0
)
SendJson
Response
(
c
,
err
,
id
)
return
}
SendJsonPageResponse
(
c
,
resp
.
OK
,
list
,
total
)
SendJsonResponse
(
c
,
resp
.
OK
,
id
)
}
// HostIpExceptionList 主机ip异常列表
func
HostIpExceptionList
(
c
*
gin
.
Context
)
{
var
req
request
.
HostIpExceptionListReq
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
return
}
hostManageSvc
:=
service
.
HostManageSvc
{}
switch
req
.
DetectionType
{
case
1
:
//列表页异常列表
if
req
.
Id
==
0
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"id为空"
)),
nil
)
return
}
//列表点击状态检测
total
,
list
,
err
:=
hostManageSvc
.
HostIpExceptionList
(
req
)
if
err
!=
nil
{
SendJsonPageResponse
(
c
,
err
,
nil
,
0
)
return
}
SendJsonPageResponse
(
c
,
err
,
list
,
total
)
case
2
:
//保存时异常列表
if
req
.
Uuid
==
""
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
errors
.
New
(
"uuid为空"
)),
nil
)
return
}
//保存时状态检测异常列表
total
,
list
,
err
:=
hostManageSvc
.
SaveIpExceptionList
(
req
)
if
err
!=
nil
{
SendJsonPageResponse
(
c
,
err
,
nil
,
0
)
return
}
SendJsonPageResponse
(
c
,
err
,
list
,
total
)
}
}
src/pkg/beagle/resp/code.go
View file @
e2f7dd18
...
...
@@ -35,4 +35,5 @@ var (
UploadFileError
=
Resp
{
Code
:
6000001
,
Msg
:
"文件上传失败"
}
GetFileStreamError
=
Resp
{
Code
:
6000002
,
Msg
:
"获取文件流失败"
}
ReadFileError
=
Resp
{
Code
:
6000003
,
Msg
:
"读取文件失败"
}
FailedToParseFile
=
Resp
{
Code
:
6000004
,
Msg
:
"解析文件失败"
}
)
src/router/automatedmaintenrouter.go
View file @
e2f7dd18
...
...
@@ -23,11 +23,12 @@ func InitAutomatedMaintenRouter(e *gin.Engine) {
//主机管理
host
:=
so
.
Group
(
"/hostManage"
)
{
host
.
POST
(
"/add"
,
controller
.
AddHostManage
)
// 新增
host
.
PUT
(
"/edit"
,
controller
.
EditHostManage
)
// 编辑
host
.
DELETE
(
"/del"
,
controller
.
DelHostManage
)
// 删除
host
.
GET
(
"/details"
,
controller
.
DetailsHostManage
)
// 详情
host
.
GET
(
"/list"
,
controller
.
ListHostManage
)
// 列表
host
.
GET
(
"/state"
,
controller
.
StateHostManage
)
// 状态检测
host
.
POST
(
"/add"
,
controller
.
AddHostManage
)
// 新增
host
.
PUT
(
"/edit"
,
controller
.
EditHostManage
)
// 编辑
host
.
DELETE
(
"/del"
,
controller
.
DelHostManage
)
// 删除
host
.
GET
(
"/details"
,
controller
.
DetailsHostManage
)
// 详情
host
.
GET
(
"/list"
,
controller
.
ListHostManage
)
// 列表
host
.
GET
(
"/state"
,
controller
.
StateHostManage
)
// 状态检测
host
.
GET
(
"/ipExceptionList"
,
controller
.
HostIpExceptionList
)
// 主机ip异常列表
}
}
src/service/host_manage.go
View file @
e2f7dd18
...
...
@@ -2,11 +2,16 @@ package service
import
(
"errors"
"fmt"
"github.com/google/uuid"
"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"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"os"
"os/exec"
"strings"
"time"
)
...
...
@@ -15,7 +20,7 @@ type HostManageSvc struct {
}
// AddHostManage 新增主机
func
(
h
*
HostManageSvc
)
AddHostManage
(
req
request
.
AddHostManageReq
,
hostManageLists
[]
request
.
HostManageList
)
(
err
error
)
{
func
(
h
*
HostManageSvc
)
AddHostManage
(
req
request
.
AddHostManageReq
)
(
err
error
)
{
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
err
=
resp
.
DbConnectError
.
WithError
(
err
)
...
...
@@ -54,8 +59,9 @@ func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq, hostManageLi
session
.
Rollback
()
return
}
//新增主机分组列表
for
_
,
v
:=
range
hostManageLists
{
for
_
,
v
:=
range
req
.
HostManageList
{
port
:=
"22"
if
v
.
Port
!=
""
{
port
=
v
.
Port
...
...
@@ -68,6 +74,7 @@ func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq, hostManageLi
Password
:
v
.
Password
,
HostGroupId
:
hostManage
.
Id
,
HostFileUrl
:
req
.
HostFileUrl
,
IpGroup
:
v
.
IpGroup
,
}
_
,
err
=
session
.
Table
(
"host_manage_list"
)
.
Insert
(
&
hostManageList
)
if
err
!=
nil
{
...
...
@@ -82,7 +89,7 @@ func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq, hostManageLi
}
// EditHostManage 编辑
func
(
h
*
HostManageSvc
)
EditHostManage
(
req
request
.
EditHostManageReq
,
hostManageLists
[]
request
.
HostManageList
)
(
err
error
)
{
func
(
h
*
HostManageSvc
)
EditHostManage
(
req
request
.
EditHostManageReq
)
(
err
error
)
{
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
err
=
resp
.
DbConnectError
.
WithError
(
err
)
...
...
@@ -118,7 +125,7 @@ func (h *HostManageSvc) EditHostManage(req request.EditHostManageReq, hostManage
}
//新增主机分组列表
for
_
,
v
:=
range
hostManageLists
{
for
_
,
v
:=
range
req
.
HostManageList
{
port
:=
"22"
if
v
.
Port
!=
""
{
port
=
v
.
Port
...
...
@@ -131,6 +138,7 @@ func (h *HostManageSvc) EditHostManage(req request.EditHostManageReq, hostManage
Password
:
v
.
Password
,
HostGroupId
:
hostManage
.
Id
,
HostFileUrl
:
req
.
HostFileUrl
,
IpGroup
:
v
.
IpGroup
,
}
_
,
err
=
session
.
Table
(
"host_manage_list"
)
.
Insert
(
&
hostManageList
)
if
err
!=
nil
{
...
...
@@ -157,6 +165,21 @@ func (h *HostManageSvc) DelHostManage(req request.DelHostManageReq) (err error)
defer
session
.
Close
()
session
.
Begin
()
//检测是否可删除
var
taskCnt
int
_
,
err
=
session
.
Table
(
"task_manage"
)
.
In
(
"host_group_id"
,
req
.
Id
)
.
Where
(
"is_delete = 0"
)
.
Select
(
"count(*) AS task_cnt"
)
.
Get
(
&
taskCnt
)
if
taskCnt
>
0
{
err
=
resp
.
DbDeleteError
.
WithError
(
errors
.
New
(
"此主机分组下有关联任务,不可删除"
))
session
.
Rollback
()
return
}
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
session
.
Rollback
()
return
}
//删除主机分组
_
,
err
=
session
.
Table
(
"host_manage"
)
.
In
(
"id"
,
req
.
Id
)
.
Cols
(
"is_delete"
)
.
Update
(
&
entity
.
HostManageList
{
IsDelete
:
1
,
...
...
@@ -210,20 +233,21 @@ func (h *HostManageSvc) DetailsHostManage(id int) (hostManageRes response.HostMa
return
}
for
_
,
v
:=
range
hostList
{
if
v
.
HostFileUrl
!=
""
{
hostManageRes
.
HostFileUrl
=
v
.
HostFileUrl
}
}
hostManageRes
.
Id
=
hostManage
.
Id
hostManageRes
.
HostName
=
hostManage
.
HostName
hostManageRes
.
TaskCnt
=
len
(
hostManageRes
.
T
askList
)
hostManageRes
.
IpCnt
=
len
(
host
ManageRes
.
Host
List
)
hostManageRes
.
TaskCnt
=
len
(
t
askList
)
hostManageRes
.
IpCnt
=
len
(
hostList
)
hostManageRes
.
CreateUser
=
hostManage
.
CreateUser
hostManageRes
.
CreateTime
=
hostManage
.
CreateTime
hostManageRes
.
UpdateUser
=
hostManage
.
UpdateUser
hostManageRes
.
UpdateTime
=
hostManage
.
UpdateTime
for
_
,
v
:=
range
hostList
{
if
v
.
HostFileUrl
!=
""
{
hostManageRes
.
HostFileUrl
=
v
.
HostFileUrl
break
}
}
hostManageRes
.
HostList
=
hostList
hostManageRes
.
TaskList
=
taskList
...
...
@@ -262,24 +286,200 @@ func (h *HostManageSvc) ListHostManage(req request.ListHostManageReq) (total int
return
}
//
StateHostManage
状态检测
func
(
h
*
HostManageSvc
)
StateHostManage
(
req
request
.
StateHostManageReq
)
(
total
int64
,
hostManageListRes
[]
response
.
HostManageListRes
,
err
error
)
{
//
ListStateHostManage 列表
状态检测
func
(
h
*
HostManageSvc
)
ListStateHostManage
(
req
request
.
StateHostManageReq
)
(
err
error
)
{
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
err
=
resp
.
DbConnectError
.
WithError
(
err
)
return
}
//状态检测。。
// 开启事务
session
:=
db
.
NewSession
()
defer
session
.
Close
()
session
.
Begin
()
//查询异常列表
finder
:=
db
.
Table
(
"host_manage_list"
)
.
Where
(
"is_delete = 0 AND conn_status = 0 AND host_group_id = ?"
,
req
.
Id
)
finder
.
OrderBy
(
"id"
)
//查询任务
total
,
err
=
finder
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
hostManageListRes
)
//查询主机IP列表
hostManageList
:=
make
([]
response
.
HostManageListRes
,
0
)
finder
:=
session
.
Table
(
"host_manage_list"
)
.
Where
(
"is_delete = 0 AND host_group_id = ?"
,
req
.
Id
)
err
=
finder
.
Find
(
&
hostManageList
)
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
return
}
//往hosts文件中写入主机组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
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
,
})
if
err
!=
nil
{
err
=
resp
.
DbUpdateError
.
WithError
(
err
)
session
.
Rollback
()
return
}
}
session
.
Commit
()
return
}
// SaveStateHostManage 保存时状态检测
func
(
h
*
HostManageSvc
)
SaveStateHostManage
(
hostManageList
[]
request
.
HostManageList
)
(
id
string
,
err
error
)
{
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
err
=
resp
.
DbConnectError
.
WithError
(
err
)
return
}
//处理ip列表
ipGroup
:=
1
HostManageListCaches
:=
make
([]
entity
.
HostManageListCache
,
0
)
for
_
,
v
:=
range
hostManageList
{
port
:=
"22"
if
v
.
Port
!=
""
{
port
=
v
.
Port
}
ipList
:=
strings
.
Split
(
v
.
Ip
,
","
)
for
_
,
v1
:=
range
ipList
{
HostManageListCache
:=
entity
.
HostManageListCache
{
Ip
:
v1
,
Port
:
port
,
VoucherType
:
v
.
VoucherType
,
UserName
:
v
.
UserName
,
Password
:
v
.
Password
,
IpGroup
:
ipGroup
,
}
HostManageListCaches
=
append
(
HostManageListCaches
,
HostManageListCache
)
}
ipGroup
+=
1
}
//状态检测
id
=
uuid
.
New
()
.
String
()
if
err
!=
nil
{
err
=
resp
.
FAIL
.
WithError
(
err
)
return
}
for
k
,
_
:=
range
HostManageListCaches
{
//调用状态检测函数
cc
:=
true
if
cc
{
//正常
HostManageListCaches
[
k
]
.
ConnStatus
=
0
}
else
{
//异常
HostManageListCaches
[
k
]
.
ConnStatus
=
1
}
HostManageListCaches
[
k
]
.
Id
=
id
}
//存入数据库
_
,
err
=
db
.
Table
(
"host_manage_list_cache"
)
.
Insert
(
&
HostManageListCaches
)
if
err
!=
nil
{
err
=
resp
.
DbInsertError
.
WithError
(
err
)
return
}
return
}
// HostIpExceptionList 列表点击状态检测
func
(
h
*
HostManageSvc
)
HostIpExceptionList
(
req
request
.
HostIpExceptionListReq
)
(
total
int64
,
hostManageListRes
[]
response
.
HostManageListRes
,
err
error
)
{
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
err
=
resp
.
DbConnectError
.
WithError
(
err
)
return
}
finder
:=
db
.
Table
(
"host_manage_list"
)
.
Where
(
"conn_status = 1 AND is_delete = 0 AND host_group_id = ?"
,
req
.
Id
)
//查询
total
,
err
=
finder
.
Select
(
"id,ip,port,voucher_type,user_name,password"
)
.
OrderBy
(
"id"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
hostManageListRes
)
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
return
}
return
}
// SaveIpExceptionList 保存时状态检测异常列表
func
(
h
*
HostManageSvc
)
SaveIpExceptionList
(
req
request
.
HostIpExceptionListReq
)
(
total
int64
,
hostManageListCacheRes
response
.
HostManageListCacheRes
,
err
error
)
{
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
err
=
resp
.
DbConnectError
.
WithError
(
err
)
return
}
//异常ip列表-分页
finder
:=
db
.
Table
(
"host_manage_list_cache"
)
.
Where
(
"conn_status = 1 AND id = ?"
,
req
.
Uuid
)
//查询
total
,
err
=
finder
.
OrderBy
(
"ip_group"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
hostManageListCacheRes
.
AbnormalHost
)
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
return
}
//正常ip列表
finder1
:=
db
.
Table
(
"host_manage_list_cache"
)
.
Where
(
"conn_status = 0 AND id = ?"
,
req
.
Uuid
)
//查询
err
=
finder1
.
OrderBy
(
"ip_group"
)
.
Find
(
&
hostManageListCacheRes
.
AormalHost
)
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
return
}
//删除临时数据
_
,
err
=
db
.
Table
(
"host_manage_list_cache"
)
.
Where
(
"id = ?"
,
req
.
Uuid
)
.
Delete
()
if
err
!=
nil
{
err
=
resp
.
DbDeleteError
.
WithError
(
err
)
return
}
return
}
// StatusDetection 状态检测
func
StatusDetection
(
ip
string
)
(
ipConn
bool
)
{
var
cmd
*
exec
.
Cmd
cmd
=
exec
.
Command
(
"ansible"
,
ip
,
"-m"
,
"ping"
)
output
,
err
:=
cmd
.
Output
()
if
err
!=
nil
{
err
=
resp
.
CmdExecError
.
WithError
(
err
)
return
}
fmt
.
Println
(
string
(
output
))
//return string(output), nil
return
}
src/service/task_manage.go
View file @
e2f7dd18
...
...
@@ -147,28 +147,28 @@ 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
(
"
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
)
}
}
//
//
获取主机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)
//
}
//
}
//写入执行脚本
f2
,
err
:=
os
.
Create
(
"/etc/ansible/ansible.yml"
)
if
err
!=
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