2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 06:47:20 +08:00

refactor: 重构systemctl包

This commit is contained in:
耗子
2024-06-23 00:48:37 +08:00
parent 11d6b40dce
commit 6bd07251fc
21 changed files with 104 additions and 85 deletions

View File

@@ -18,6 +18,7 @@ import (
"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -105,7 +106,7 @@ func (receiver *Panel) Handle(ctx console.Context) error {
}
// 停止面板服务因为在shell中运行的和systemd的不同
_ = tools.ServiceStop("panel")
_ = systemctl.Stop("panel")
types.Status = types.StatusUpgrade
if err = tools.UpdatePanel(panel); err != nil {

View File

@@ -13,6 +13,7 @@ import (
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -109,7 +110,7 @@ func (r *InfoController) CountInfo(ctx http.Context) http.Response {
}
var databaseCount int64
if mysqlInstalled {
status, err := tools.ServiceStatus("mysqld")
status, err := systemctl.Status("mysqld")
if status && err == nil {
rootPassword := r.setting.Get(models.SettingKeyMysqlRootPassword)
type database struct {
@@ -151,7 +152,7 @@ func (r *InfoController) CountInfo(ctx http.Context) http.Response {
}
}
if postgresqlInstalled {
status, err := tools.ServiceStatus("postgresql")
status, err := systemctl.Status("postgresql")
if status && err == nil {
raw, err := shell.Execf(`echo "\l" | su - postgres -c "psql"`)
if err == nil {

View File

@@ -7,6 +7,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
requests "github.com/TheTNB/panel/app/http/requests/plugins/frp"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -63,7 +64,7 @@ func (r *FrpController) UpdateConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
if err := tools.ServiceRestart(updateRequest.Service); err != nil {
if err := systemctl.Restart(updateRequest.Service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
requests "github.com/TheTNB/panel/app/http/requests/plugins/gitea"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -54,7 +55,7 @@ func (r *GiteaController) UpdateConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
if err := tools.ServiceRestart("gitea"); err != nil {
if err := systemctl.Restart("gitea"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -50,7 +51,7 @@ func (r *MySQLController) SaveConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "写入MySQL配置失败")
}
if err := tools.ServiceReload("mysqld"); err != nil {
if err := systemctl.Reload("mysqld"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载MySQL失败")
}
@@ -64,7 +65,7 @@ func (r *MySQLController) Load(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusUnprocessableEntity, "MySQL root密码为空")
}
status, _ := tools.ServiceStatus("mysqld")
status, _ := systemctl.Status("mysqld")
if !status {
return controllers.Success(ctx, []types.NV{})
}
@@ -173,7 +174,7 @@ func (r *MySQLController) GetRootPassword(ctx http.Context) http.Response {
// SetRootPassword 设置root密码
func (r *MySQLController) SetRootPassword(ctx http.Context) http.Response {
status, err := tools.ServiceStatus("mysqld")
status, err := systemctl.Status("mysqld")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败")
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -58,7 +59,7 @@ func (r *OpenRestyController) SaveConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "保存配置失败")
}
if err := tools.ServiceReload("openresty"); err != nil {
if err := systemctl.Reload("openresty"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载服务失败")
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -86,7 +87,7 @@ func (r *PhpMyAdminController) SetPort(ctx http.Context) http.Response {
}
}
if err := tools.ServiceReload("openresty"); err != nil {
if err := systemctl.Reload("openresty"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载OpenResty失败")
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
requests "github.com/TheTNB/panel/app/http/requests/plugins/podman"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -54,7 +55,7 @@ func (r *PodmanController) UpdateRegistryConfig(ctx http.Context) http.Response
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
if err := tools.ServiceRestart("podman"); err != nil {
if err := systemctl.Restart("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
@@ -100,7 +101,7 @@ func (r *PodmanController) UpdateStorageConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
if err := tools.ServiceRestart("podman"); err != nil {
if err := systemctl.Restart("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -61,7 +62,7 @@ func (r *PostgreSQLController) SaveConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "写入PostgreSQL配置失败")
}
if err := tools.ServiceReload("postgresql"); err != nil {
if err := systemctl.Reload("postgresql"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载服务失败")
}
@@ -79,7 +80,7 @@ func (r *PostgreSQLController) SaveUserConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "写入PostgreSQL配置失败")
}
if err := tools.ServiceReload("postgresql"); err != nil {
if err := systemctl.Reload("postgresql"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载服务失败")
}
@@ -88,7 +89,7 @@ func (r *PostgreSQLController) SaveUserConfig(ctx http.Context) http.Response {
// Load 获取负载
func (r *PostgreSQLController) Load(ctx http.Context) http.Response {
status, _ := tools.ServiceStatus("postgresql")
status, _ := systemctl.Status("postgresql")
if !status {
return controllers.Success(ctx, []types.NV{})
}
@@ -230,7 +231,7 @@ func (r *PostgreSQLController) AddDatabase(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, out)
}
if err := tools.ServiceReload("postgresql"); err != nil {
if err := systemctl.Reload("postgresql"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载服务失败")
}
@@ -441,7 +442,7 @@ func (r *PostgreSQLController) AddRole(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, out)
}
if err := tools.ServiceReload("postgresql"); err != nil {
if err := systemctl.Reload("postgresql"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载服务失败")
}
@@ -464,7 +465,7 @@ func (r *PostgreSQLController) DeleteRole(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, out)
}
if err := tools.ServiceReload("postgresql"); err != nil {
if err := systemctl.Reload("postgresql"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载服务失败")
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -169,7 +170,7 @@ func (r *PureFtpdController) SetPort(ctx http.Context) http.Response {
}
}
if err := tools.ServiceRestart("pure-ftpd"); err != nil {
if err := systemctl.Restart("pure-ftpd"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -40,7 +41,7 @@ func (r *RedisController) SaveConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "写入Redis配置失败")
}
if err := tools.ServiceRestart("redis"); err != nil {
if err := systemctl.Restart("redis"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重启Redis失败")
}
@@ -49,7 +50,7 @@ func (r *RedisController) SaveConfig(ctx http.Context) http.Response {
// Load 获取负载
func (r *RedisController) Load(ctx http.Context) http.Response {
status, err := tools.ServiceStatus("redis")
status, err := systemctl.Status("redis")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取Redis状态失败")
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
requests "github.com/TheTNB/panel/app/http/requests/plugins/rsync"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -136,7 +137,7 @@ secrets file = /etc/rsyncd.secrets
return controllers.Error(ctx, http.StatusInternalServerError, out)
}
if err := tools.ServiceRestart("rsyncd"); err != nil {
if err := systemctl.Restart("rsyncd"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
@@ -182,7 +183,7 @@ func (r *RsyncController) Destroy(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
if err = tools.ServiceRestart("rsyncd"); err != nil {
if err = systemctl.Restart("rsyncd"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
@@ -243,7 +244,7 @@ secrets file = /etc/rsyncd.secrets
return controllers.Error(ctx, http.StatusInternalServerError, out)
}
if err = tools.ServiceRestart("rsyncd"); err != nil {
if err = systemctl.Restart("rsyncd"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
@@ -289,7 +290,7 @@ func (r *RsyncController) UpdateConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
if err := tools.ServiceRestart("rsyncd"); err != nil {
if err := systemctl.Restart("rsyncd"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -84,7 +85,7 @@ func (r *SupervisorController) SaveConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
if err = tools.ServiceRestart(r.service); err != nil {
if err = systemctl.Restart(r.service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("重启 %s 服务失败", r.service))
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/spf13/cast"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -38,32 +39,32 @@ func (r *SafeController) SetFirewallStatus(ctx http.Context) http.Response {
var err error
if ctx.Request().InputBool("status") {
if tools.IsRHEL() {
err = tools.ServiceStart("firewalld")
err = systemctl.Start("firewalld")
if err == nil {
err = tools.ServiceEnable("firewalld")
err = systemctl.Enable("firewalld")
}
} else {
_, err = shell.Execf("echo y | ufw enable")
if err == nil {
err = tools.ServiceStart("ufw")
err = systemctl.Start("ufw")
}
if err == nil {
err = tools.ServiceEnable("ufw")
err = systemctl.Enable("ufw")
}
}
} else {
if tools.IsRHEL() {
err = tools.ServiceStop("firewalld")
err = systemctl.Stop("firewalld")
if err == nil {
err = tools.ServiceDisable("firewalld")
err = systemctl.Disable("firewalld")
}
} else {
_, err = shell.Execf("ufw disable")
if err == nil {
err = tools.ServiceStop("ufw")
err = systemctl.Stop("ufw")
}
if err == nil {
err = tools.ServiceDisable("ufw")
err = systemctl.Disable("ufw")
}
}
}
@@ -222,9 +223,9 @@ func (r *SafeController) DeleteFirewallRule(ctx http.Context) http.Response {
func (r *SafeController) firewallStatus() bool {
var running bool
if tools.IsRHEL() {
running, _ = tools.ServiceStatus("firewalld")
running, _ = systemctl.Status("firewalld")
} else {
running, _ = tools.ServiceStatus("ufw")
running, _ = systemctl.Status("ufw")
}
return running
@@ -232,7 +233,7 @@ func (r *SafeController) firewallStatus() bool {
// GetSshStatus 获取 SSH 状态
func (r *SafeController) GetSshStatus(ctx http.Context) http.Response {
running, err := tools.ServiceStatus(r.ssh)
running, err := systemctl.Status(r.ssh)
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
@@ -243,17 +244,17 @@ func (r *SafeController) GetSshStatus(ctx http.Context) http.Response {
// SetSshStatus 设置 SSH 状态
func (r *SafeController) SetSshStatus(ctx http.Context) http.Response {
if ctx.Request().InputBool("status") {
if err := tools.ServiceEnable(r.ssh); err != nil {
if err := systemctl.Enable(r.ssh); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if err := tools.ServiceStart(r.ssh); err != nil {
if err := systemctl.Start(r.ssh); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
} else {
if err := tools.ServiceStop(r.ssh); err != nil {
if err := systemctl.Stop(r.ssh); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if err := tools.ServiceDisable(r.ssh); err != nil {
if err := systemctl.Disable(r.ssh); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
}
@@ -285,9 +286,9 @@ func (r *SafeController) SetSshPort(ctx http.Context) http.Response {
_, _ = shell.Execf("sed -i 's/#Port %s/Port %d/g' /etc/ssh/sshd_config", oldPort, port)
_, _ = shell.Execf("sed -i 's/Port %s/Port %d/g' /etc/ssh/sshd_config", oldPort, port)
status, _ := tools.ServiceStatus(r.ssh)
status, _ := systemctl.Status(r.ssh)
if status {
_ = tools.ServiceRestart(r.ssh)
_ = systemctl.Restart(r.ssh)
}
return Success(ctx, nil)

View File

@@ -5,7 +5,7 @@ import (
"github.com/goravel/framework/contracts/http"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/pkg/systemctl"
)
type SystemController struct {
@@ -32,7 +32,7 @@ func (r *SystemController) ServiceStatus(ctx http.Context) http.Response {
}
service := ctx.Request().Query("service")
status, err := tools.ServiceStatus(service)
status, err := systemctl.Status(service)
if err != nil {
return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("获取 %s 服务运行状态失败", service))
}
@@ -57,7 +57,7 @@ func (r *SystemController) ServiceIsEnabled(ctx http.Context) http.Response {
}
service := ctx.Request().Query("service")
enabled, err := tools.ServiceIsEnabled(service)
enabled, err := systemctl.IsEnabled(service)
if err != nil {
return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("获取 %s 服务启用状态失败", service))
}
@@ -82,7 +82,7 @@ func (r *SystemController) ServiceEnable(ctx http.Context) http.Response {
}
service := ctx.Request().Input("service")
if err := tools.ServiceEnable(service); err != nil {
if err := systemctl.Enable(service); err != nil {
return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("启用 %s 服务失败", service))
}
@@ -106,7 +106,7 @@ func (r *SystemController) ServiceDisable(ctx http.Context) http.Response {
}
service := ctx.Request().Input("service")
if err := tools.ServiceDisable(service); err != nil {
if err := systemctl.Disable(service); err != nil {
return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("禁用 %s 服务失败", service))
}
@@ -130,7 +130,7 @@ func (r *SystemController) ServiceRestart(ctx http.Context) http.Response {
}
service := ctx.Request().Input("service")
if err := tools.ServiceRestart(service); err != nil {
if err := systemctl.Restart(service); err != nil {
return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("重启 %s 服务失败", service))
}
@@ -154,7 +154,7 @@ func (r *SystemController) ServiceReload(ctx http.Context) http.Response {
}
service := ctx.Request().Input("service")
if err := tools.ServiceReload(service); err != nil {
if err := systemctl.Reload(service); err != nil {
return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("重载 %s 服务失败", service))
}
@@ -178,7 +178,7 @@ func (r *SystemController) ServiceStart(ctx http.Context) http.Response {
}
service := ctx.Request().Input("service")
if err := tools.ServiceStart(service); err != nil {
if err := systemctl.Start(service); err != nil {
return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("启动 %s 服务失败", service))
}
@@ -202,7 +202,7 @@ func (r *SystemController) ServiceStop(ctx http.Context) http.Response {
}
service := ctx.Request().Input("service")
if err := tools.ServiceStop(service); err != nil {
if err := systemctl.Stop(service); err != nil {
return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("停止 %s 服务失败", service))
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -577,7 +578,7 @@ server
if err := tools.Write("/www/server/vhost/rewrite"+website.Name+".conf", "", 0644); err != nil {
return nil
}
if err := tools.ServiceReload("openresty"); err != nil {
if err := systemctl.Reload("openresty"); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
@@ -643,7 +644,7 @@ func (r *WebsiteController) Status(ctx http.Context) http.Response {
if err = tools.Write("/www/server/vhost/"+website.Name+".conf", raw, 0644); err != nil {
return ErrorSystem(ctx)
}
if err = tools.ServiceReload("openresty"); err != nil {
if err = systemctl.Reload("openresty"); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}

View File

@@ -12,6 +12,7 @@ import (
requests "github.com/TheTNB/panel/app/http/requests/cert"
"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/pkg/acme"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -277,7 +278,7 @@ func (s *CertImpl) ObtainAuto(ID uint) (acme.Certificate, error) {
if err = tools.Write("/www/server/vhost/ssl/"+cert.Website.Name+".key", cert.Key, 0644); err != nil {
return acme.Certificate{}, err
}
if err = tools.ServiceReload("openresty"); err != nil {
if err = systemctl.Reload("openresty"); err != nil {
return acme.Certificate{}, err
}
}
@@ -317,7 +318,7 @@ func (s *CertImpl) ObtainManual(ID uint) (acme.Certificate, error) {
if err = tools.Write("/www/server/vhost/ssl/"+cert.Website.Name+".key", cert.Key, 0644); err != nil {
return acme.Certificate{}, err
}
if err = tools.ServiceReload("openresty"); err != nil {
if err = systemctl.Reload("openresty"); err != nil {
return acme.Certificate{}, err
}
}
@@ -402,7 +403,7 @@ func (s *CertImpl) Renew(ID uint) (acme.Certificate, error) {
if err = tools.Write("/www/server/vhost/ssl/"+cert.Website.Name+".key", cert.Key, 0644); err != nil {
return acme.Certificate{}, err
}
if err = tools.ServiceReload("openresty"); err != nil {
if err = systemctl.Reload("openresty"); err != nil {
return acme.Certificate{}, err
}
}
@@ -434,7 +435,7 @@ func (s *CertImpl) Deploy(ID, WebsiteID uint) error {
if err = tools.Write("/www/server/vhost/ssl/"+website.Name+".key", cert.Key, 0644); err != nil {
return err
}
if err = tools.ServiceReload("openresty"); err != nil {
if err = systemctl.Reload("openresty"); err != nil {
return err
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -23,14 +24,14 @@ func (r *CronImpl) AddToSystem(cron models.Cron) error {
if _, err := shell.Execf(fmt.Sprintf(`echo "%s %s >> %s 2>&1" >> /var/spool/cron/root`, cron.Time, cron.Shell, cron.Log)); err != nil {
return err
}
return tools.ServiceRestart("crond")
return systemctl.Restart("crond")
}
if tools.IsDebian() {
if _, err := shell.Execf(fmt.Sprintf(`echo "%s %s >> %s 2>&1" >> /var/spool/cron/crontabs/root`, cron.Time, cron.Shell, cron.Log)); err != nil {
return err
}
return tools.ServiceRestart("cron")
return systemctl.Restart("cron")
}
return errors.New("不支持的系统")
@@ -44,14 +45,14 @@ func (r *CronImpl) DeleteFromSystem(cron models.Cron) error {
if _, err := shell.Execf("sed -i '/" + cron.Shell + "/d' /var/spool/cron/root"); err != nil {
return err
}
return tools.ServiceRestart("crond")
return systemctl.Restart("crond")
}
if tools.IsDebian() {
if _, err := shell.Execf("sed -i '/" + cron.Shell + "/d' /var/spool/cron/crontabs/root"); err != nil {
return err
}
return tools.ServiceRestart("cron")
return systemctl.Restart("cron")
}
return errors.New("不支持的系统")

View File

@@ -14,6 +14,7 @@ import (
"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -29,7 +30,7 @@ func NewPHPImpl(version uint) *PHPImpl {
}
func (r *PHPImpl) Reload() error {
return tools.ServiceReload("php-fpm-" + r.version)
return systemctl.Reload("php-fpm-" + r.version)
}
func (r *PHPImpl) GetConfig() (string, error) {

View File

@@ -17,6 +17,7 @@ import (
"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -280,7 +281,7 @@ server
return models.Website{}, err
}
if err := tools.ServiceReload("openresty"); err != nil {
if err := systemctl.Reload("openresty"); err != nil {
return models.Website{}, err
}
@@ -298,7 +299,7 @@ server
_, _ = shell.Execf(`echo "GRANT ALL PRIVILEGES ON DATABASE ` + website.DbName + ` TO ` + website.DbUser + `;" | su - postgres -c "psql"`)
userConfig := "host " + website.DbName + " " + website.DbUser + " 127.0.0.1/32 scram-sha-256"
_, _ = shell.Execf(`echo "` + userConfig + `" >> /www/server/postgresql/data/pg_hba.conf`)
_ = tools.ServiceReload("postgresql")
_ = systemctl.Reload("postgresql")
}
return w, nil
@@ -324,7 +325,7 @@ func (r *WebsiteImpl) SaveConfig(config requests.SaveConfig) error {
if err = tools.Write("/www/server/vhost/"+website.Name+".conf", config.Raw, 0644); err != nil {
return err
}
if err = tools.ServiceReload("openresty"); err != nil {
if err = systemctl.Reload("openresty"); err != nil {
return err
}
@@ -516,7 +517,7 @@ func (r *WebsiteImpl) SaveConfig(config requests.SaveConfig) error {
return err
}
return tools.ServiceReload("openresty")
return systemctl.Reload("openresty")
}
// Delete 删除网站
@@ -550,7 +551,7 @@ func (r *WebsiteImpl) Delete(id uint) error {
return err
}
return tools.ServiceReload("openresty")
return systemctl.Reload("openresty")
}
// GetConfig 获取网站配置

View File

@@ -1,4 +1,4 @@
package tools
package systemctl
import (
"errors"
@@ -8,14 +8,14 @@ import (
"github.com/TheTNB/panel/pkg/shell"
)
// ServiceStatus 获取服务状态
func ServiceStatus(name string) (bool, error) {
// Status 获取服务状态
func Status(name string) (bool, error) {
output, err := shell.Execf("systemctl status %s | grep Active | grep -v grep | awk '{print $2}'", name)
return output == "active", err
}
// ServiceIsEnabled 服务是否启用
func ServiceIsEnabled(name string) (bool, error) {
// IsEnabled 服务是否启用
func IsEnabled(name string) (bool, error) {
cmd := exec.Command("systemctl", "is-enabled", name)
output, _ := cmd.CombinedOutput()
status := strings.TrimSpace(string(output))
@@ -36,38 +36,38 @@ func ServiceIsEnabled(name string) (bool, error) {
}
}
// ServiceStart 启动服务
func ServiceStart(name string) error {
// Start 启动服务
func Start(name string) error {
_, err := shell.Execf("systemctl start %s", name)
return err
}
// ServiceStop 停止服务
func ServiceStop(name string) error {
// Stop 停止服务
func Stop(name string) error {
_, err := shell.Execf("systemctl stop %s", name)
return err
}
// ServiceRestart 重启服务
func ServiceRestart(name string) error {
// Restart 重启服务
func Restart(name string) error {
_, err := shell.Execf("systemctl restart %s", name)
return err
}
// ServiceReload 重载服务
func ServiceReload(name string) error {
// Reload 重载服务
func Reload(name string) error {
_, err := shell.Execf("systemctl reload %s", name)
return err
}
// ServiceEnable 启用服务
func ServiceEnable(name string) error {
// Enable 启用服务
func Enable(name string) error {
_, err := shell.Execf("systemctl enable %s", name)
return err
}
// ServiceDisable 禁用服务
func ServiceDisable(name string) error {
// Disable 禁用服务
func Disable(name string) error {
_, err := shell.Execf("systemctl disable %s", name)
return err
}