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
09afcbb6
Commit
09afcbb6
authored
Jul 17, 2023
by
陈子龙
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev-czl' into dev
parents
852043e2
fef3dc1c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
13 deletions
+33
-13
src/common/conf/options.go
src/common/conf/options.go
+1
-0
src/service/host_manage.go
src/service/host_manage.go
+1
-1
src/service/task_manage.go
src/service/task_manage.go
+31
-12
No files found.
src/common/conf/options.go
View file @
09afcbb6
...
@@ -57,4 +57,5 @@ const (
...
@@ -57,4 +57,5 @@ const (
CookieNameLastLogin
string
=
"lastLogin"
CookieNameLastLogin
string
=
"lastLogin"
LocalDateTimeFormat
string
=
"2006-01-02 15:04:05"
LocalDateTimeFormat
string
=
"2006-01-02 15:04:05"
FinalHeartBeatUnixKey
=
"FinalHeartBeatUnix"
FinalHeartBeatUnixKey
=
"FinalHeartBeatUnix"
AutoExecHistory
=
"AutoExecHistory"
)
)
src/service/host_manage.go
View file @
09afcbb6
...
@@ -384,7 +384,7 @@ func (h *HostManageSvc) PageListHostManage(req request.ListHostManageReq) (total
...
@@ -384,7 +384,7 @@ func (h *HostManageSvc) PageListHostManage(req request.ListHostManageReq) (total
"task_manage tm WHERE tm.is_delete = 0 AND tm.host_group_id = hm.ID) AS task_cnt,(SELECT COUNT(*) FROM "
+
"task_manage tm WHERE tm.is_delete = 0 AND tm.host_group_id = hm.ID) AS task_cnt,(SELECT COUNT(*) FROM "
+
"host_manage_list hml WHERE hml.is_delete = 0 AND hml.conn_status = 1 AND hml.host_group_id = hm.ID) AS "
+
"host_manage_list hml WHERE hml.is_delete = 0 AND hml.conn_status = 1 AND hml.host_group_id = hm.ID) AS "
+
"ip_cnt_err,(SELECT COUNT(*) FROM host_manage_list hml WHERE hml.is_delete = 0 AND hml.host_group_id = hm.ID) AS ip_cnt"
)
.
"ip_cnt_err,(SELECT COUNT(*) FROM host_manage_list hml WHERE hml.is_delete = 0 AND hml.host_group_id = hm.ID) AS ip_cnt"
)
.
OrderBy
(
"hm.create_time
desc
"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
hostManageListRes
)
OrderBy
(
"hm.create_time"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
hostManageListRes
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
err
=
resp
.
DbSelectError
.
WithError
(
err
)
return
return
...
...
src/service/task_manage.go
View file @
09afcbb6
...
@@ -14,6 +14,7 @@ import (
...
@@ -14,6 +14,7 @@ import (
"io"
"io"
"os"
"os"
"os/exec"
"os/exec"
"strconv"
"strings"
"strings"
"time"
"time"
)
)
...
@@ -171,7 +172,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
...
@@ -171,7 +172,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
finder
.
OrderBy
(
"tm.id"
)
finder
.
OrderBy
(
"tm.id"
)
//查询任务
//查询任务
total
,
err
=
finder
.
Select
(
"tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "
+
total
,
err
=
finder
.
Select
(
"tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "
+
"where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time"
)
.
OrderBy
(
"tm.create_time
desc
"
)
.
"where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time"
)
.
OrderBy
(
"tm.create_time"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
taskManageListRes
)
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
taskManageListRes
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
err
=
resp
.
DbSelectError
.
WithError
(
err
)
...
@@ -353,9 +354,8 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
...
@@ -353,9 +354,8 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
return
return
}
}
//
执行脚本
//
ExecAnsible 执行ansible命令
func
ExecAnsible
(
id
,
taskId
int
,
value
string
)
{
func
ExecAnsible
(
id
,
taskId
int
,
value
string
)
{
//执行ansible命令
var
cmd
*
exec
.
Cmd
var
cmd
*
exec
.
Cmd
if
value
!=
""
{
if
value
!=
""
{
cmd
=
exec
.
Command
(
"ansible-playbook"
,
"-i"
,
"/etc/ansible/hosts_"
+
fmt
.
Sprintf
(
"%d"
,
taskId
),
"/etc/ansible/ansible_"
+
fmt
.
Sprintf
(
"%d"
,
taskId
)
+
".yml"
,
"--extra-vars"
,
value
)
cmd
=
exec
.
Command
(
"ansible-playbook"
,
"-i"
,
"/etc/ansible/hosts_"
+
fmt
.
Sprintf
(
"%d"
,
taskId
),
"/etc/ansible/ansible_"
+
fmt
.
Sprintf
(
"%d"
,
taskId
)
+
".yml"
,
"--extra-vars"
,
value
)
...
@@ -389,6 +389,11 @@ func ExecAnsible(id, taskId int, value string) {
...
@@ -389,6 +389,11 @@ func ExecAnsible(id, taskId int, value string) {
var
out
,
outErr
int
var
out
,
outErr
int
var
execLog
string
var
execLog
string
redis
,
err
:=
client
.
GetRedisClient
()
if
err
!=
nil
{
zap
.
L
()
.
Error
(
err
.
Error
())
}
for
{
for
{
//逐行输出日志
//逐行输出日志
...
@@ -398,10 +403,16 @@ func ExecAnsible(id, taskId int, value string) {
...
@@ -398,10 +403,16 @@ func ExecAnsible(id, taskId int, value string) {
}
else
if
out
==
0
{
}
else
if
out
==
0
{
//存储执行日志
//存储执行日志
execLog
=
execLog
+
lineOut
+
"
\n
"
execLog
=
execLog
+
lineOut
+
"
\n
"
err
=
UpdateExecHistory
(
request
.
UpdateExecHistory
{
//err = UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId
:
id
,
// TaskHistoryId: id,
ExecLog
:
execLog
,
// ExecLog: execLog,
})
//})
//if err != nil {
// conf.Logger.Error("Store Execution Log", zap.Error(err))
// //return
//}
// TODO 写缓存
err
=
redis
.
HSet
(
conf
.
AutoExecHistory
,
strconv
.
Itoa
(
id
),
execLog
)
if
err
!=
nil
{
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"Store Execution Log"
,
zap
.
Error
(
err
))
conf
.
Logger
.
Error
(
"Store Execution Log"
,
zap
.
Error
(
err
))
//return
//return
...
@@ -414,12 +425,18 @@ func ExecAnsible(id, taskId int, value string) {
...
@@ -414,12 +425,18 @@ func ExecAnsible(id, taskId int, value string) {
}
else
if
outErr
==
0
{
}
else
if
outErr
==
0
{
//存储异常执行日志
//存储异常执行日志
execLog
=
execLog
+
lineErr
+
"
\n
"
execLog
=
execLog
+
lineErr
+
"
\n
"
err
=
UpdateExecHistory
(
request
.
UpdateExecHistory
{
//err = UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId
:
id
,
// TaskHistoryId: id,
ExecLog
:
execLog
,
// ExecLog: execLog,
})
//})
//if err != nil {
// conf.Logger.Error("Store abnormal execution logs", zap.Error(err))
// //return
//}
// TODO 写缓存
err
=
redis
.
HSet
(
conf
.
AutoExecHistory
,
strconv
.
Itoa
(
id
),
execLog
)
if
err
!=
nil
{
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"Store
abnormal execution logs
"
,
zap
.
Error
(
err
))
conf
.
Logger
.
Error
(
"Store
Execution Log
"
,
zap
.
Error
(
err
))
//return
//return
}
}
}
}
...
@@ -441,6 +458,7 @@ func ExecAnsible(id, taskId int, value string) {
...
@@ -441,6 +458,7 @@ func ExecAnsible(id, taskId int, value string) {
conf
.
Logger
.
Error
(
"Modify Execution Status"
,
zap
.
Error
(
err
))
conf
.
Logger
.
Error
(
"Modify Execution Status"
,
zap
.
Error
(
err
))
//return
//return
}
}
redis
.
Del
(
strconv
.
Itoa
(
id
))
}
else
{
}
else
{
//任务执行失败
//任务执行失败
err
=
UpdateExecHistory
(
request
.
UpdateExecHistory
{
err
=
UpdateExecHistory
(
request
.
UpdateExecHistory
{
...
@@ -452,6 +470,7 @@ func ExecAnsible(id, taskId int, value string) {
...
@@ -452,6 +470,7 @@ func ExecAnsible(id, taskId int, value string) {
conf
.
Logger
.
Error
(
"Modify Execution Status"
,
zap
.
Error
(
err
))
conf
.
Logger
.
Error
(
"Modify Execution Status"
,
zap
.
Error
(
err
))
//return
//return
}
}
redis
.
Del
(
strconv
.
Itoa
(
id
))
}
}
//return
//return
}
}
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