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
743b94a7
Commit
743b94a7
authored
Jul 03, 2023
by
陈子龙
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev-czl' into dev
parents
3a56f2f8
0966d83f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
100 additions
and
27 deletions
+100
-27
src/bean/vo/request/task_manage.go
src/bean/vo/request/task_manage.go
+1
-1
src/common/tools/hosts
src/common/tools/hosts
+11
-1
src/common/tools/hosts.go
src/common/tools/hosts.go
+11
-3
src/controller/task_manage.go
src/controller/task_manage.go
+1
-0
src/service/host_manage.go
src/service/host_manage.go
+76
-22
No files found.
src/bean/vo/request/task_manage.go
View file @
743b94a7
...
@@ -36,5 +36,5 @@ type ExecScriptReq struct {
...
@@ -36,5 +36,5 @@ type ExecScriptReq struct {
HostGroupId
int
`form:"host_group_id" vd:"$>0;msg:'请输入主机分组id'"`
//主机分组id
HostGroupId
int
`form:"host_group_id" vd:"$>0;msg:'请输入主机分组id'"`
//主机分组id
Type
int
`form:"type"`
//脚本额外变量类型1yaml 2json
Type
int
`form:"type"`
//脚本额外变量类型1yaml 2json
Value
string
`form:"value"`
//脚本额外变量值
Value
string
`form:"value"`
//脚本额外变量值
Script
string
`form:"script"`
//执行脚本
Script
string
`form:"script"`
//执行脚本
/文件名
}
}
src/common/tools/hosts
View file @
743b94a7
[HostGroup]
HostIp192.168.0.1 ansible_ssh_host=192.168.0.1 ansible_ssh_port=22 ansible_ssh_user="admin" ansible_ssh_pass="123456"
HostIp192.186.10.1 ansible_ssh_host=192.186.10.1 ansible_ssh_port=22 ansible_ssh_user="admin" ansible_ssh_pass="123456"
HostIp101.168.0.1 ansible_ssh_host=101.168.0.1 ansible_ssh_port=22 ansible_ssh_user="root" ansible_ssh_pass=""
HostIp101.186.10.1 ansible_ssh_host=101.186.10.1 ansible_ssh_port=22 ansible_ssh_user="root" ansible_ssh_pass=""
[webGroup1]
[webGroup1]
192.168.0.11 ansible_ssh_host=192.168.0.11 ansible_ssh_user=root ansible_ssh_pass=123.com ansible_ssh_port=3333
192.168.0.11 ansible_ssh_host=192.168.0.11 ansible_ssh_user=root ansible_ssh_pass=123.com ansible_ssh_port=3333
192.168.0.12 ansible_ssh_host=192.168.0.12 ansible_ssh_user=root ansible_ssh_pass=1234.com ansible_ssh_port=2222
192.168.0.12 ansible_ssh_host=192.168.0.12 ansible_ssh_user=root ansible_ssh_pass=1234.com ansible_ssh_port=2222
[webGroup2]
[webGroup2]
192.168.1.11 ansible_ssh_host=192.168.1.11 ansible_ssh_user=admin ansible_ssh_pass=ccc.com ansible_ssh_port=11
192.168.1.11 ansible_ssh_host=192.168.1.11 ansible_ssh_user=admin ansible_ssh_pass=ccc.com ansible_ssh_port=11
192.168.1.12 ansible_ssh_host=192.168.1.12 ansible_ssh_user=admin ansible_ssh_pass=111.com ansible_ssh_port=22
192.168.1.12 ansible_ssh_host=192.168.1.12 ansible_ssh_user=admin ansible_ssh_pass=111.com ansible_ssh_port=22
\ No newline at end of file
[HostGroup15]
HostIp22 ansible_ssh_host=192.168.0.1 ansible_ssh_port=22 ansible_ssh_user="admin" ansible_ssh_pass="123456"
HostIp23 ansible_ssh_host=192.186.10.1 ansible_ssh_port=22 ansible_ssh_user="admin" ansible_ssh_pass="123456"
HostIp24 ansible_ssh_host=101.168.0.1 ansible_ssh_port=22 ansible_ssh_user="root" ansible_ssh_pass=""
HostIp25 ansible_ssh_host=101.186.10.1 ansible_ssh_port=22 ansible_ssh_user="root" ansible_ssh_pass=""
src/common/tools/hosts.go
View file @
743b94a7
...
@@ -8,7 +8,9 @@ import (
...
@@ -8,7 +8,9 @@ import (
)
)
func
HostsToJson
()
(
data
map
[
string
][]
string
,
err
error
)
{
func
HostsToJson
()
(
data
map
[
string
][]
string
,
err
error
)
{
f
,
err
:=
os
.
Open
(
`/etc/ansible/hosts`
)
//f, err := os.Open(`/etc/ansible/hosts`)
f
,
err
:=
os
.
Open
(
`D:\work\goWork\智能运维平台\so-operation-api\src\common\tools\hosts`
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -35,13 +37,15 @@ func HostsToJson() (data map[string][]string, err error) {
...
@@ -35,13 +37,15 @@ func HostsToJson() (data map[string][]string, err error) {
}
}
func
MapToSaveHosts
(
data
map
[
string
][]
string
)
error
{
func
MapToSaveHosts
(
data
map
[
string
][]
string
)
error
{
f
,
err
:=
os
.
OpenFile
(
"/etc/ansible/hosts"
,
os
.
O_APPEND
|
os
.
O_CREATE
|
os
.
O_RDWR
|
os
.
O_TRUNC
,
0777
)
//f, err := os.OpenFile("/etc/ansible/hosts", os.O_APPEND|os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
f
,
err
:=
os
.
OpenFile
(
"D:/work/goWork/智能运维平台/so-operation-api/src/common/tools/hosts"
,
os
.
O_APPEND
|
os
.
O_CREATE
|
os
.
O_RDWR
|
os
.
O_TRUNC
,
0777
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
defer
f
.
Close
()
defer
f
.
Close
()
for
k
,
v
:=
range
data
{
for
k
,
v
:=
range
data
{
_
,
err
:
=
f
.
Write
([]
byte
(
fmt
.
Sprintf
(
"%s
\n
"
,
k
)))
_
,
err
=
f
.
Write
([]
byte
(
fmt
.
Sprintf
(
"%s
\n
"
,
k
)))
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -49,6 +53,10 @@ func MapToSaveHosts(data map[string][]string) error {
...
@@ -49,6 +53,10 @@ func MapToSaveHosts(data map[string][]string) error {
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
_
,
err
=
f
.
Write
([]
byte
(
"
\n
"
))
if
err
!=
nil
{
return
err
}
}
}
return
nil
return
nil
}
}
src/controller/task_manage.go
View file @
743b94a7
...
@@ -123,6 +123,7 @@ func ExecScript(c *gin.Context) {
...
@@ -123,6 +123,7 @@ func ExecScript(c *gin.Context) {
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
WithError
(
err
),
nil
)
return
return
}
}
taskManageSvc
:=
service
.
TaskManageSvc
{}
taskManageSvc
:=
service
.
TaskManageSvc
{}
data
,
err
:=
taskManageSvc
.
ExecScript
(
req
)
data
,
err
:=
taskManageSvc
.
ExecScript
(
req
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
src/service/host_manage.go
View file @
743b94a7
...
@@ -11,8 +11,8 @@ import (
...
@@ -11,8 +11,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/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/tools"
"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"
"os/exec"
"strings"
"strings"
"time"
"time"
...
@@ -22,7 +22,8 @@ type HostManageSvc struct {
...
@@ -22,7 +22,8 @@ type HostManageSvc struct {
User
*
entity
.
SystemUser
User
*
entity
.
SystemUser
}
}
const
AnsibleGroup
string
=
"HostManage"
const
AnsibleGroup
string
=
"HostGroup"
const
AnsibleIp
string
=
"HostIp"
// AddHostManage 新增主机
// AddHostManage 新增主机
func
(
h
*
HostManageSvc
)
AddHostManage
(
req
request
.
AddHostManageReq
)
(
err
error
)
{
func
(
h
*
HostManageSvc
)
AddHostManage
(
req
request
.
AddHostManageReq
)
(
err
error
)
{
...
@@ -84,12 +85,22 @@ func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq) (err error)
...
@@ -84,12 +85,22 @@ func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq) (err error)
return
return
}
}
//读取hosts中的主机组
hosts
,
err
:=
tools
.
HostsToJson
()
if
err
!=
nil
{
err
=
resp
.
MarshalError
.
WithError
(
err
)
session
.
Rollback
()
return
}
hosts
[
"["
+
AnsibleGroup
+
fmt
.
Sprintf
(
"%d"
,
hostManage
.
Id
)
+
"]"
]
=
nil
//新增主机分组列表
//新增主机分组列表
for
_
,
v
:=
range
hostManageLists
{
for
_
,
v
:=
range
hostManageLists
{
port
:=
"22"
port
:=
"22"
if
v
.
Port
!=
""
{
if
v
.
Port
!=
""
{
port
=
v
.
Port
port
=
v
.
Port
}
}
hostManageList
:=
entity
.
HostManageList
{
hostManageList
:=
entity
.
HostManageList
{
Ip
:
v
.
Ip
,
Ip
:
v
.
Ip
,
Port
:
port
,
Port
:
port
,
...
@@ -106,6 +117,18 @@ func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq) (err error)
...
@@ -106,6 +117,18 @@ func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq) (err error)
session
.
Rollback
()
session
.
Rollback
()
return
return
}
}
hostsIp
:=
fmt
.
Sprintf
(
"%s ansible_ssh_host=%s ansible_ssh_port=%s ansible_ssh_user=
\"
%s
\"
ansible_ssh_pass=
\"
%s
\"
"
,
AnsibleIp
+
fmt
.
Sprintf
(
"%d"
,
hostManageList
.
Id
),
v
.
Ip
,
v
.
Port
,
v
.
UserName
,
v
.
Password
)
hosts
[
"["
+
AnsibleGroup
+
fmt
.
Sprintf
(
"%d"
,
hostManage
.
Id
)
+
"]"
]
=
append
(
hosts
[
"["
+
AnsibleGroup
+
fmt
.
Sprintf
(
"%d"
,
hostManage
.
Id
)
+
"]"
],
hostsIp
)
}
//写入hosts
err
=
tools
.
MapToSaveHosts
(
hosts
)
if
err
!=
nil
{
err
=
resp
.
MarshalError
.
WithError
(
err
)
session
.
Rollback
()
return
}
}
session
.
Commit
()
session
.
Commit
()
...
@@ -166,6 +189,14 @@ func (h *HostManageSvc) EditHostManage(req request.EditHostManageReq) (err error
...
@@ -166,6 +189,14 @@ func (h *HostManageSvc) EditHostManage(req request.EditHostManageReq) (err error
return
return
}
}
//读取hosts中的主机组
hosts
,
err
:=
tools
.
HostsToJson
()
if
err
!=
nil
{
err
=
resp
.
MarshalError
.
WithError
(
err
)
return
}
hosts
[
"["
+
AnsibleGroup
+
fmt
.
Sprintf
(
"%d"
,
req
.
Id
)
+
"]"
]
=
nil
//新增主机分组列表
//新增主机分组列表
for
_
,
v
:=
range
hostManageLists
{
for
_
,
v
:=
range
hostManageLists
{
port
:=
"22"
port
:=
"22"
...
@@ -188,8 +219,19 @@ func (h *HostManageSvc) EditHostManage(req request.EditHostManageReq) (err error
...
@@ -188,8 +219,19 @@ func (h *HostManageSvc) EditHostManage(req request.EditHostManageReq) (err error
session
.
Rollback
()
session
.
Rollback
()
return
return
}
}
hostsIp
:=
fmt
.
Sprintf
(
"%s ansible_ssh_host=%s ansible_ssh_port=%s ansible_ssh_user=
\"
%s
\"
ansible_ssh_pass=
\"
%s
\"
"
,
AnsibleIp
+
fmt
.
Sprintf
(
"%d"
,
hostManageList
.
Id
),
v
.
Ip
,
v
.
Port
,
v
.
UserName
,
v
.
Password
)
hosts
[
"["
+
AnsibleGroup
+
fmt
.
Sprintf
(
"%d"
,
req
.
Id
)
+
"]"
]
=
append
(
hosts
[
"["
+
AnsibleGroup
+
fmt
.
Sprintf
(
"%d"
,
req
.
Id
)
+
"]"
],
hostsIp
)
}
}
//写入hosts
err
=
tools
.
MapToSaveHosts
(
hosts
)
if
err
!=
nil
{
err
=
resp
.
MarshalError
.
WithError
(
err
)
return
}
session
.
Commit
()
session
.
Commit
()
return
return
}
}
...
@@ -393,7 +435,7 @@ func (h *HostManageSvc) SaveStateHostManage(hostManageList []request.HostManageL
...
@@ -393,7 +435,7 @@ func (h *HostManageSvc) SaveStateHostManage(hostManageList []request.HostManageL
}
}
//处理ip列表
//处理ip列表
ipGroup
:=
1
ipGroup
:=
1
H
ostManageListCaches
:=
make
([]
entity
.
HostManageListCache
,
0
)
h
ostManageListCaches
:=
make
([]
entity
.
HostManageListCache
,
0
)
for
_
,
v
:=
range
hostManageList
{
for
_
,
v
:=
range
hostManageList
{
port
:=
"22"
port
:=
"22"
if
v
.
Port
!=
""
{
if
v
.
Port
!=
""
{
...
@@ -410,40 +452,52 @@ func (h *HostManageSvc) SaveStateHostManage(hostManageList []request.HostManageL
...
@@ -410,40 +452,52 @@ func (h *HostManageSvc) SaveStateHostManage(hostManageList []request.HostManageL
Password
:
v
.
Password
,
Password
:
v
.
Password
,
IpGroup
:
ipGroup
,
IpGroup
:
ipGroup
,
}
}
HostManageListCaches
=
append
(
H
ostManageListCaches
,
HostManageListCache
)
hostManageListCaches
=
append
(
h
ostManageListCaches
,
HostManageListCache
)
}
}
ipGroup
+=
1
ipGroup
+=
1
}
}
//
往hosts文件中写入主机组ip
//
读取hosts中的主机组
f
,
err
:=
os
.
Open
(
"/etc/ansible/hosts"
)
hosts
,
err
:=
tools
.
HostsToJson
(
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
resp
.
FileExec
Error
.
WithError
(
err
)
err
=
resp
.
Marshal
Error
.
WithError
(
err
)
return
return
}
}
defer
f
.
Close
()
if
_
,
ok
:=
hosts
[
"["
+
AnsibleGroup
+
"]"
];
!
ok
{
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
)))
hosts
[
"["
+
AnsibleGroup
+
"]"
]
=
nil
if
err
!=
nil
{
err
=
resp
.
FileExecError
.
WithError
(
err
)
return
}
}
}
//状态检测
for
_
,
v
:=
range
hostManageListCaches
{
id
=
uuid
.
New
()
.
String
()
hostsIp
:=
fmt
.
Sprintf
(
"%s ansible_ssh_host=%s ansible_ssh_port=%s ansible_ssh_user=
\"
%s
\"
ansible_ssh_pass=
\"
%s
\"
"
,
AnsibleIp
+
v
.
Ip
,
v
.
Ip
,
v
.
Port
,
v
.
UserName
,
v
.
Password
)
flag
:=
0
for
_
,
v1
:=
range
hosts
[
"["
+
AnsibleGroup
+
"]"
]
{
if
v1
==
hostsIp
{
flag
=
1
}
}
if
flag
==
0
{
hosts
[
"["
+
AnsibleGroup
+
"]"
]
=
append
(
hosts
[
"["
+
AnsibleGroup
+
"]"
],
hostsIp
)
}
}
//写入hosts
err
=
tools
.
MapToSaveHosts
(
hosts
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
resp
.
FAIL
.
WithError
(
err
)
err
=
resp
.
MarshalError
.
WithError
(
err
)
return
return
}
}
for
k
,
v
:=
range
HostManageListCaches
{
//状态检测
id
=
uuid
.
New
()
.
String
()
for
k
,
v
:=
range
hostManageListCaches
{
//调用状态检测函数
//调用状态检测函数
H
ostManageListCaches
[
k
]
.
ConnStatus
=
StatusDetection
(
v
.
Ip
)
h
ostManageListCaches
[
k
]
.
ConnStatus
=
StatusDetection
(
v
.
Ip
)
H
ostManageListCaches
[
k
]
.
Id
=
id
h
ostManageListCaches
[
k
]
.
Id
=
id
}
}
//存入数据库
//存入数据库
_
,
err
=
db
.
Table
(
"host_manage_list_cache"
)
.
Insert
(
&
H
ostManageListCaches
)
_
,
err
=
db
.
Table
(
"host_manage_list_cache"
)
.
Insert
(
&
h
ostManageListCaches
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
resp
.
DbInsertError
.
WithError
(
err
)
err
=
resp
.
DbInsertError
.
WithError
(
err
)
return
return
...
@@ -509,7 +563,7 @@ func (h *HostManageSvc) SaveIpExceptionList(req request.HostIpExceptionListReq)
...
@@ -509,7 +563,7 @@ func (h *HostManageSvc) SaveIpExceptionList(req request.HostIpExceptionListReq)
// StatusDetection 状态检测
// StatusDetection 状态检测
func
StatusDetection
(
ip
string
)
(
ipConn
int
)
{
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"
,
AnsibleIp
+
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
))
...
...
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