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
a9ef0990
Commit
a9ef0990
authored
Jul 19, 2023
by
陈子龙
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev-czl' into dev
parents
49660de9
25560e75
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
19 deletions
+65
-19
src/service/host_manage.go
src/service/host_manage.go
+1
-2
src/service/task_history.go
src/service/task_history.go
+29
-0
src/service/task_manage.go
src/service/task_manage.go
+32
-14
src/service/work_order.go
src/service/work_order.go
+3
-3
No files found.
src/service/host_manage.go
View file @
a9ef0990
...
...
@@ -378,13 +378,12 @@ func (h *HostManageSvc) PageListHostManage(req request.ListHostManageReq) (total
if
req
.
CreateDateTo
!=
""
{
finder
.
Where
(
"hm.create_time <= ?"
,
req
.
CreateDateTo
)
}
finder
.
OrderBy
(
"hm.id"
)
//查询任务
total
,
err
=
finder
.
Select
(
"hm.id,hm.host_name,hm.create_user,hm.create_time,(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 "
+
"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"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
hostManageListRes
)
OrderBy
(
"hm.create_time
desc
"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
hostManageListRes
)
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
return
...
...
src/service/task_history.go
View file @
a9ef0990
...
...
@@ -2,12 +2,16 @@ package service
import
(
"fmt"
json
"github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/spf13/cast"
"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/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"go.uber.org/zap"
"time"
)
...
...
@@ -117,6 +121,7 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo
)
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"db err"
,
zap
.
Error
(
err
))
err
=
resp
.
DbConnectError
.
WithError
(
err
)
return
}
...
...
@@ -130,6 +135,30 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo
return
}
if
taskExecLogRes
.
State
==
0
{
redis
,
err
:=
client
.
GetRedisClient
()
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"redis err"
,
zap
.
Error
(
err
))
err
=
resp
.
RedisConnectError
.
ErrorDetail
(
err
)
return
}
value
,
err
:=
redis
.
HGet
(
conf
.
AutoExecHistory
,
cast
.
ToString
(
id
))
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"redis HGet err"
,
zap
.
Error
(
err
))
err
=
resp
.
RedisExecError
.
ErrorDetail
(
err
)
return
}
var
execCache
ExecCache
err
=
json
.
Unmarshal
([]
byte
(
value
),
&
execCache
)
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"redis value Unmarshal err"
,
zap
.
Error
(
err
))
err
=
resp
.
FAIL
.
WithError
(
err
)
return
}
taskExecLogRes
.
ExecLog
=
execCache
.
ExecLog
taskExecLogRes
.
ExecEndTime
=
execCache
.
ExecEndTime
}
//获取执行耗时
start
,
err
=
time
.
Parse
(
"2006-01-02 15:04:05"
,
taskExecLogRes
.
ExecStartTime
.
Format
(
"2006-01-02 15:04:05"
))
if
err
!=
nil
{
...
...
src/service/task_manage.go
View file @
a9ef0990
...
...
@@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
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"
...
...
@@ -169,10 +170,9 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
if
req
.
HostGroupId
!=
0
{
finder
.
Where
(
"tm.host_group_id = ?"
,
req
.
HostGroupId
)
}
finder
.
OrderBy
(
"tm.id"
)
//查询任务
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"
)
.
"where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time"
)
.
OrderBy
(
"tm.create_time
desc
"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
taskManageListRes
)
if
err
!=
nil
{
err
=
resp
.
DbSelectError
.
WithError
(
err
)
...
...
@@ -354,6 +354,11 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
return
}
type
ExecCache
struct
{
ExecLog
string
`json:"exec_log"`
ExecEndTime
time
.
Time
`json:"exec_end_time"`
// 执行结束时间
}
// ExecAnsible 执行ansible命令
func
ExecAnsible
(
id
,
taskId
int
,
value
string
)
{
var
cmd
*
exec
.
Cmd
...
...
@@ -388,11 +393,12 @@ func ExecAnsible(id, taskId int, value string) {
readerr
:=
bufio
.
NewReader
(
stderr
)
var
out
,
outErr
int
var
execLog
string
//var execLog string
var
execObj
[]
byte
var
execCache
ExecCache
redis
,
err
:=
client
.
GetRedisClient
()
if
err
!=
nil
{
zap
.
L
()
.
Error
(
err
.
Error
(
))
conf
.
Logger
.
Error
(
"redis err"
,
zap
.
Error
(
err
))
}
for
{
//逐行输出日志
...
...
@@ -401,11 +407,17 @@ func ExecAnsible(id, taskId int, value string) {
out
=
1
}
else
if
out
==
0
{
//存储执行日志
execLog
=
execLog
+
lineOut
+
"
\n
"
//execLog = execLog + lineOut + " \n "
execCache
.
ExecLog
=
execCache
.
ExecLog
+
lineOut
+
"
\n
"
execCache
.
ExecEndTime
=
time
.
Now
()
execObj
,
err
=
json
.
Marshal
(
execCache
)
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"lineOut Marshal execCache err"
,
zap
.
Error
(
err
))
}
// 写缓存
err
=
redis
.
HSet
(
conf
.
AutoExecHistory
,
strconv
.
Itoa
(
id
),
execLog
)
err
=
redis
.
HSet
(
conf
.
AutoExecHistory
,
strconv
.
Itoa
(
id
),
fmt
.
Sprintf
(
"%s"
,
execObj
)
)
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"Store Execution Log"
,
zap
.
Error
(
err
))
conf
.
Logger
.
Error
(
"Store Execution Log
err
"
,
zap
.
Error
(
err
))
}
}
...
...
@@ -414,11 +426,17 @@ func ExecAnsible(id, taskId int, value string) {
outErr
=
1
}
else
if
outErr
==
0
{
//存储异常执行日志
execLog
=
execLog
+
lineErr
+
"
\n
"
//execLog = execLog + lineErr + " \n "
execCache
.
ExecLog
=
execCache
.
ExecLog
+
lineErr
+
"
\n
"
execCache
.
ExecEndTime
=
time
.
Now
()
execObj
,
err
=
json
.
Marshal
(
execCache
)
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"lineErr Marshal execCache err"
,
zap
.
Error
(
err
))
}
// 写缓存
err
=
redis
.
HSet
(
conf
.
AutoExecHistory
,
strconv
.
Itoa
(
id
),
execLog
)
err
=
redis
.
HSet
(
conf
.
AutoExecHistory
,
strconv
.
Itoa
(
id
),
fmt
.
Sprintf
(
"%s"
,
execObj
)
)
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"Store Execution Log"
,
zap
.
Error
(
err
))
conf
.
Logger
.
Error
(
"Store Execution Log
err
"
,
zap
.
Error
(
err
))
}
}
if
out
==
1
&&
outErr
==
1
{
...
...
@@ -431,7 +449,7 @@ func ExecAnsible(id, taskId int, value string) {
//任务执行成功
err
=
UpdateExecHistory
(
request
.
UpdateExecHistory
{
TaskHistoryId
:
id
,
ExecLog
:
execLog
,
ExecLog
:
exec
Cache
.
Exec
Log
,
State
:
1
,
})
if
err
!=
nil
{
...
...
@@ -443,11 +461,11 @@ func ExecAnsible(id, taskId int, value string) {
//任务执行失败
err
=
UpdateExecHistory
(
request
.
UpdateExecHistory
{
TaskHistoryId
:
id
,
ExecLog
:
execLog
,
ExecLog
:
exec
Cache
.
Exec
Log
,
State
:
2
,
})
if
err
!=
nil
{
conf
.
Logger
.
Error
(
"Modify Execution Status"
,
zap
.
Error
(
err
))
conf
.
Logger
.
Error
(
"Modify Execution Status
err
"
,
zap
.
Error
(
err
))
//return
}
redis
.
HDel
(
conf
.
AutoExecHistory
,
strconv
.
Itoa
(
id
))
...
...
src/service/work_order.go
View file @
a9ef0990
...
...
@@ -263,7 +263,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderManage(req request.ListWorkOrderManage
if
req
.
CreateDateTo
!=
""
{
finder
.
Where
(
"create_time <= ?"
,
req
.
CreateDateTo
)
}
finder
.
OrderBy
(
"create_time"
)
finder
.
OrderBy
(
"create_time
desc
"
)
//查询任务
total
,
err
=
finder
.
Select
(
"id,order_name,order_level,order_cnt,push_obj,timing_type,timing_state,create_user,create_time"
)
.
Limit
(
req
.
PageSize
,
(
req
.
Page
-
1
)
*
req
.
PageSize
)
.
FindAndCount
(
&
workOrderListRes
)
...
...
@@ -456,7 +456,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderIssuance(req request.ListWorkOrderReq)
if
req
.
Id
!=
0
{
finder
.
Where
(
"wom.id = ?"
,
req
.
Id
)
}
finder
.
OrderBy
(
"woi.create_time"
)
finder
.
OrderBy
(
"woi.create_time
desc
"
)
//查询任务
total
,
err
=
finder
.
Select
(
"woi.
\"
id
\"
,woi.order_id,wom.order_name,wom.order_level,woi.order_state,woi.push_obj,woi.create_user,woi.create_time,woi.complete_time,"
+
"(select count(1) from work_order_me wome where wome.order_issuance_id = woi.id and wome.order_state = woi.order_state) as state_cnt,"
+
...
...
@@ -615,7 +615,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderMe(req request.ListWorkOrderReq) (tota
if
req
.
CompleteTimeTo
!=
""
{
finder
.
Where
(
"wome.complete_time <= ?"
,
req
.
CompleteTimeTo
)
}
finder
.
OrderBy
(
"woi.create_time"
)
finder
.
OrderBy
(
"woi.create_time
desc
"
)
//查询任务
total
,
err
=
finder
.
Select
(
"wome.
\"
id
\"
,wome.order_id,wome.order_issuance_id,wom.order_name,wom.order_level,"
+
"wome.order_state,woi.push_obj,woi.create_user,woi.create_time,wome.complete_time"
)
.
...
...
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