From 10145da2f974cf8ffcac66025e702014545fe73f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Fri, 14 Jun 2024 10:59:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4=E9=83=A8=E5=88=86?= =?UTF-8?q?=E8=BD=AF=E4=BB=B6=E6=9C=8D=E5=8A=A1=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/fail2ban_controller.go | 50 --------------- .../controllers/plugins/mysql_controller.go | 52 ++------------- .../plugins/supervisor_controller.go | 64 ++++--------------- routes/plugin.go | 26 +------- 4 files changed, 20 insertions(+), 172 deletions(-) diff --git a/app/http/controllers/plugins/fail2ban_controller.go b/app/http/controllers/plugins/fail2ban_controller.go index 67652249..129ecc72 100644 --- a/app/http/controllers/plugins/fail2ban_controller.go +++ b/app/http/controllers/plugins/fail2ban_controller.go @@ -26,56 +26,6 @@ func NewFail2banController() *Fail2banController { } } -// Status 获取运行状态 -func (r *Fail2banController) Status(ctx http.Context) http.Response { - status, err := tools.ServiceStatus("fail2ban") - if err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败") - } - - return controllers.Success(ctx, status) -} - -// Reload 重载配置 -func (r *Fail2banController) Reload(ctx http.Context) http.Response { - if err := tools.ServiceReload("fail2ban"); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "重载配置失败") - } - - return controllers.Success(ctx, nil) -} - -// Restart 重启服务 -func (r *Fail2banController) Restart(ctx http.Context) http.Response { - if err := tools.ServiceRestart("fail2ban"); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "重启服务失败") - } - - return controllers.Success(ctx, nil) -} - -// Start 启动服务 -func (r *Fail2banController) Start(ctx http.Context) http.Response { - if err := tools.ServiceStart("fail2ban"); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "启动服务失败") - } - - return controllers.Success(ctx, nil) -} - -// Stop 停止服务 -func (r *Fail2banController) Stop(ctx http.Context) http.Response { - if err := tools.ServiceStop("fail2ban"); err != nil { - return nil - } - status, err := tools.ServiceStatus("fail2ban") - if err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败") - } - - return controllers.Success(ctx, !status) -} - // List 所有 Fail2ban 规则 func (r *Fail2banController) List(ctx http.Context) http.Response { page := ctx.Request().QueryInt("page", 1) diff --git a/app/http/controllers/plugins/mysql_controller.go b/app/http/controllers/plugins/mysql_controller.go index 362de216..b5e70a9c 100644 --- a/app/http/controllers/plugins/mysql_controller.go +++ b/app/http/controllers/plugins/mysql_controller.go @@ -28,52 +28,6 @@ func NewMySQLController() *MySQLController { } } -// Status 获取运行状态 -func (r *MySQLController) Status(ctx http.Context) http.Response { - status, err := tools.ServiceStatus("mysqld") - if err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") - } - - return controllers.Success(ctx, status) -} - -// Reload 重载配置 -func (r *MySQLController) Reload(ctx http.Context) http.Response { - if err := tools.ServiceReload("mysqld"); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "重载MySQL配置失败") - } - - return controllers.Success(ctx, nil) -} - -// Restart 重启服务 -func (r *MySQLController) Restart(ctx http.Context) http.Response { - if err := tools.ServiceRestart("mysqld"); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "重启MySQL服务失败") - } - - return controllers.Success(ctx, nil) -} - -// Start 启动服务 -func (r *MySQLController) Start(ctx http.Context) http.Response { - if err := tools.ServiceStart("mysqld"); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "启动MySQL服务失败") - } - - return controllers.Success(ctx, nil) -} - -// Stop 停止服务 -func (r *MySQLController) Stop(ctx http.Context) http.Response { - if err := tools.ServiceStop("mysqld"); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "停止MySQL服务失败") - } - - return controllers.Success(ctx, nil) -} - // GetConfig 获取配置 func (r *MySQLController) GetConfig(ctx http.Context) http.Response { config, err := tools.Read("/www/server/mysql/conf/my.cnf") @@ -95,7 +49,11 @@ func (r *MySQLController) SaveConfig(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, "写入MySQL配置失败") } - return r.Restart(ctx) + if err := tools.ServiceReload("mysqld"); err != nil { + return controllers.Error(ctx, http.StatusInternalServerError, "重载MySQL失败") + } + + return controllers.Success(ctx, nil) } // Load 获取负载 diff --git a/app/http/controllers/plugins/supervisor_controller.go b/app/http/controllers/plugins/supervisor_controller.go index 6e7304db..695557a3 100644 --- a/app/http/controllers/plugins/supervisor_controller.go +++ b/app/http/controllers/plugins/supervisor_controller.go @@ -1,6 +1,7 @@ package plugins import ( + "fmt" "strconv" "strings" @@ -11,66 +12,25 @@ import ( ) type SupervisorController struct { - ServiceName string + service string } func NewSupervisorController() *SupervisorController { - var serviceName string + var service string if tools.IsRHEL() { - serviceName = "supervisord" + service = "supervisord" } else { - serviceName = "supervisor" + service = "supervisor" } return &SupervisorController{ - ServiceName: serviceName, + service: service, } } -// Status 状态 -func (r *SupervisorController) Status(ctx http.Context) http.Response { - status, err := tools.ServiceStatus(r.ServiceName) - if err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "获取Supervisor状态失败") - } - - return controllers.Success(ctx, status) -} - -// Start 启动 -func (r *SupervisorController) Start(ctx http.Context) http.Response { - if err := tools.ServiceStart(r.ServiceName); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "启动Supervisor失败") - } - - return controllers.Success(ctx, nil) -} - -// Stop 停止 -func (r *SupervisorController) Stop(ctx http.Context) http.Response { - if err := tools.ServiceStop(r.ServiceName); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "停止Supervisor失败") - } - - return controllers.Success(ctx, nil) -} - -// Restart 重启 -func (r *SupervisorController) Restart(ctx http.Context) http.Response { - if err := tools.ServiceRestart(r.ServiceName); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "重启Supervisor失败") - } - - return controllers.Success(ctx, nil) -} - -// Reload 重载 -func (r *SupervisorController) Reload(ctx http.Context) http.Response { - if err := tools.ServiceReload(r.ServiceName); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "重载Supervisor失败") - } - - return controllers.Success(ctx, nil) +// Service 获取服务名称 +func (r *SupervisorController) Service(ctx http.Context) http.Response { + return controllers.Success(ctx, r.service) } // Log 日志 @@ -123,7 +83,11 @@ func (r *SupervisorController) SaveConfig(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) } - return r.Restart(ctx) + if err = tools.ServiceRestart(r.service); err != nil { + return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("重启 %s 服务失败", r.service)) + } + + return controllers.Success(ctx, nil) } // Processes 进程列表 diff --git a/routes/plugin.go b/routes/plugin.go index 9011ecd0..213c1508 100644 --- a/routes/plugin.go +++ b/routes/plugin.go @@ -26,11 +26,6 @@ func Plugin() { }) r.Prefix("mysql57").Group(func(route route.Router) { mySQLController := plugins.NewMySQLController() - route.Get("status", mySQLController.Status) - route.Post("reload", mySQLController.Reload) - route.Post("start", mySQLController.Start) - route.Post("stop", mySQLController.Stop) - route.Post("restart", mySQLController.Restart) route.Get("load", mySQLController.Load) route.Get("config", mySQLController.GetConfig) route.Post("config", mySQLController.SaveConfig) @@ -56,11 +51,6 @@ func Plugin() { }) r.Prefix("mysql80").Group(func(route route.Router) { mySQLController := plugins.NewMySQLController() - route.Get("status", mySQLController.Status) - route.Post("reload", mySQLController.Reload) - route.Post("start", mySQLController.Start) - route.Post("stop", mySQLController.Stop) - route.Post("restart", mySQLController.Restart) route.Get("load", mySQLController.Load) route.Get("config", mySQLController.GetConfig) route.Post("config", mySQLController.SaveConfig) @@ -86,11 +76,6 @@ func Plugin() { }) r.Prefix("mysql84").Group(func(route route.Router) { mySQLController := plugins.NewMySQLController() - route.Get("status", mySQLController.Status) - route.Post("reload", mySQLController.Reload) - route.Post("start", mySQLController.Start) - route.Post("stop", mySQLController.Stop) - route.Post("restart", mySQLController.Restart) route.Get("load", mySQLController.Load) route.Get("config", mySQLController.GetConfig) route.Post("config", mySQLController.SaveConfig) @@ -304,11 +289,7 @@ func Plugin() { }) r.Prefix("supervisor").Group(func(route route.Router) { supervisorController := plugins.NewSupervisorController() - route.Get("status", supervisorController.Status) - route.Post("start", supervisorController.Start) - route.Post("stop", supervisorController.Stop) - route.Post("restart", supervisorController.Restart) - route.Post("reload", supervisorController.Reload) + route.Get("service", supervisorController.Service) route.Get("log", supervisorController.Log) route.Post("clearLog", supervisorController.ClearLog) route.Get("config", supervisorController.Config) @@ -327,11 +308,6 @@ func Plugin() { }) r.Prefix("fail2ban").Group(func(route route.Router) { fail2banController := plugins.NewFail2banController() - route.Get("status", fail2banController.Status) - route.Post("start", fail2banController.Start) - route.Post("stop", fail2banController.Stop) - route.Post("restart", fail2banController.Restart) - route.Post("reload", fail2banController.Reload) route.Get("jails", fail2banController.List) route.Post("jails", fail2banController.Add) route.Delete("jails", fail2banController.Delete)