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
b9cf891b
Commit
b9cf891b
authored
Jun 30, 2023
by
魏灿
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
执行脚本fix
parent
51eebe6a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
54 deletions
+36
-54
src/pkg/beagle/resp/code.go
src/pkg/beagle/resp/code.go
+2
-0
src/service/host_manage.go
src/service/host_manage.go
+11
-31
src/service/task_manage.go
src/service/task_manage.go
+23
-23
No files found.
src/pkg/beagle/resp/code.go
View file @
b9cf891b
...
...
@@ -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
:
"文件上传失败"
}
...
...
src/service/host_manage.go
View file @
b9cf891b
...
...
@@ -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 结果导出
...
...
src/service/task_manage.go
View file @
b9cf891b
...
...
@@ -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
)
...
...
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