Skip to content

Commit b59ccc5

Browse files
feat: 增加进程守护管理 (#1786)
增加 Supervisor 状态读取 初始化 启动 重启 设置 日志 功能 Refs #1754 Refs #1409 Refs #1388 Refs #379 Refs #353 Refs #331
1 parent f9c8aa0 commit b59ccc5

File tree

30 files changed

+2347
-10
lines changed

30 files changed

+2347
-10
lines changed

Makefile

-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ build_backend_on_darwin:
2929
cd $(SERVER_PATH) \
3030
&& GOOS=linux GOARCH=amd64 $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(APP_NAME) $(MAIN)
3131

32-
build_backend_on_archlinux:
33-
cd $(SERVER_PATH) \
34-
&& GOOS=$(GOOS) GOARCH=$(GOARCH) $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(APP_NAME) $(MAIN)
35-
3632
build_all: build_frontend build_backend_on_linux
3733

3834
build_on_local: clean_assets build_frontend build_backend_on_darwin upx_bin

backend/app/api/v1/entry.go

+2
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ var (
5151

5252
runtimeService = service.NewRuntimeService()
5353
processService = service.NewIProcessService()
54+
55+
hostToolService = service.NewIHostToolService()
5456
)

backend/app/api/v1/host_tool.go

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package v1
2+
3+
import (
4+
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
5+
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
6+
"github.com/1Panel-dev/1Panel/backend/constant"
7+
"github.com/1Panel-dev/1Panel/backend/global"
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
// @Tags Host tool
12+
// @Summary Get tool
13+
// @Description 获取主机工具状态
14+
// @Accept json
15+
// @Param request body request.HostToolReq true "request"
16+
// @Success 200
17+
// @Security ApiKeyAuth
18+
// @Router /host/tool [post]
19+
func (b *BaseApi) GetToolStatus(c *gin.Context) {
20+
var req request.HostToolReq
21+
if err := c.ShouldBindJSON(&req); err != nil {
22+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
23+
return
24+
}
25+
if err := global.VALID.Struct(req); err != nil {
26+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
27+
return
28+
}
29+
30+
config, err := hostToolService.GetToolStatus(req)
31+
if err != nil {
32+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
33+
return
34+
}
35+
helper.SuccessWithData(c, config)
36+
}
37+
38+
// @Tags Host tool
39+
// @Summary Create Host tool Config
40+
// @Description 创建主机工具配置
41+
// @Accept json
42+
// @Param request body request.HostToolCreate true "request"
43+
// @Success 200
44+
// @Security ApiKeyAuth
45+
// @Router /host/tool/create [post]
46+
// @x-panel-log {"bodyKeys":["type"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"创建 [type] 配置","formatEN":"create [type] config"}
47+
func (b *BaseApi) InitToolConfig(c *gin.Context) {
48+
var req request.HostToolCreate
49+
if err := c.ShouldBindJSON(&req); err != nil {
50+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
51+
return
52+
}
53+
if err := global.VALID.Struct(req); err != nil {
54+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
55+
return
56+
}
57+
58+
if err := hostToolService.CreateToolConfig(req); err != nil {
59+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
60+
return
61+
}
62+
helper.SuccessWithOutData(c)
63+
}
64+
65+
// @Tags Host tool
66+
// @Summary Operate tool
67+
// @Description 操作主机工具
68+
// @Accept json
69+
// @Param request body request.HostToolReq true "request"
70+
// @Success 200
71+
// @Security ApiKeyAuth
72+
// @Router /host/tool/operate [post]
73+
// @x-panel-log {"bodyKeys":["operate","type"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"[operate] [type] ","formatEN":"[operate] [type]"}
74+
func (b *BaseApi) OperateTool(c *gin.Context) {
75+
var req request.HostToolReq
76+
if err := c.ShouldBindJSON(&req); err != nil {
77+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
78+
return
79+
}
80+
if err := global.VALID.Struct(req); err != nil {
81+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
82+
return
83+
}
84+
85+
err := hostToolService.OperateTool(req)
86+
if err != nil {
87+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
88+
return
89+
}
90+
helper.SuccessWithOutData(c)
91+
}
92+
93+
// @Tags Host tool
94+
// @Summary Get tool config
95+
// @Description 操作主机工具配置文件
96+
// @Accept json
97+
// @Param request body request.HostToolConfig true "request"
98+
// @Success 200
99+
// @Security ApiKeyAuth
100+
// @Router /host/tool/config [post]
101+
// @x-panel-log {"bodyKeys":["operate"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"[operate] 主机工具配置文件 ","formatEN":"[operate] tool config"}
102+
func (b *BaseApi) OperateToolConfig(c *gin.Context) {
103+
var req request.HostToolConfig
104+
if err := c.ShouldBindJSON(&req); err != nil {
105+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
106+
return
107+
}
108+
if err := global.VALID.Struct(req); err != nil {
109+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
110+
return
111+
}
112+
113+
config, err := hostToolService.OperateToolConfig(req)
114+
if err != nil {
115+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
116+
return
117+
}
118+
helper.SuccessWithData(c, config)
119+
}
120+
121+
// @Tags Host tool
122+
// @Summary Get tool
123+
// @Description 获取主机工具日志
124+
// @Accept json
125+
// @Param request body request.HostToolLogReq true "request"
126+
// @Success 200
127+
// @Security ApiKeyAuth
128+
// @Router /host/tool/log [post]
129+
func (b *BaseApi) GetToolLog(c *gin.Context) {
130+
var req request.HostToolLogReq
131+
if err := c.ShouldBindJSON(&req); err != nil {
132+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
133+
return
134+
}
135+
if err := global.VALID.Struct(req); err != nil {
136+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
137+
return
138+
}
139+
140+
logContent, err := hostToolService.GetToolLog(req)
141+
if err != nil {
142+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
143+
return
144+
}
145+
helper.SuccessWithData(c, logContent)
146+
}
147+
148+
// @Tags Host tool
149+
// @Summary Create Supervisor process
150+
// @Description 操作守护进程
151+
// @Accept json
152+
// @Param request body request.SupervisorProcessConfig true "request"
153+
// @Success 200
154+
// @Security ApiKeyAuth
155+
// @Router /host/tool/supervisor/process [post]
156+
// @x-panel-log {"bodyKeys":["operate"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"[operate] 守护进程 ","formatEN":"[operate] process"}
157+
func (b *BaseApi) OperateProcess(c *gin.Context) {
158+
var req request.SupervisorProcessConfig
159+
if err := c.ShouldBindJSON(&req); err != nil {
160+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
161+
return
162+
}
163+
if err := global.VALID.Struct(req); err != nil {
164+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
165+
return
166+
}
167+
168+
err := hostToolService.OperateSupervisorProcess(req)
169+
if err != nil {
170+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
171+
return
172+
}
173+
helper.SuccessWithOutData(c)
174+
}

backend/app/dto/request/host_tool.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package request
2+
3+
type HostToolReq struct {
4+
Type string `json:"type" validate:"required,oneof=supervisord"`
5+
Operate string `json:"operate" validate:"oneof=status restart start stop"`
6+
}
7+
8+
type HostToolCreate struct {
9+
Type string `json:"type" validate:"required"`
10+
SupervisorConfig
11+
}
12+
13+
type SupervisorConfig struct {
14+
ConfigPath string `json:"configPath"`
15+
}
16+
17+
type HostToolLogReq struct {
18+
Type string `json:"type" validate:"required,oneof=supervisord"`
19+
}
20+
21+
type HostToolConfig struct {
22+
Type string `json:"type" validate:"required,oneof=supervisord"`
23+
Operate string `json:"operate" validate:"oneof=get set"`
24+
Content string `json:"content"`
25+
}
26+
27+
type SupervisorProcessConfig struct {
28+
Name string `json:"name"`
29+
Operate string `json:"operate"`
30+
Command string `json:"command"`
31+
User string `json:"user"`
32+
Dir string `json:"dir"`
33+
Numprocs string `json:"numprocs"`
34+
}

backend/app/dto/response/host_tool.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package response
2+
3+
type HostToolRes struct {
4+
Type string `json:"type"`
5+
Config interface{} `json:"config"`
6+
}
7+
8+
type Supervisor struct {
9+
ConfigPath string `json:"configPath"`
10+
IncludeDir string `json:"includeDir"`
11+
LogPath string `json:"logPath"`
12+
IsExist bool `json:"isExist"`
13+
Init bool `json:"init"`
14+
Msg string `json:"msg"`
15+
Version string `json:"version"`
16+
Status string `json:"status"`
17+
CtlExist bool `json:"ctlExist"`
18+
}
19+
20+
type HostToolConfig struct {
21+
Content string `json:"content"`
22+
}

0 commit comments

Comments
 (0)