mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 09:13:49 +08:00
chore: fix lint
This commit is contained in:
@@ -75,7 +75,9 @@ func (receiver *Monitoring) Handle(console.Context) error {
|
||||
Info: info,
|
||||
})
|
||||
if err != nil {
|
||||
facades.Log().Infof("[面板] 系统监控保存失败: %s", err.Error())
|
||||
facades.Log().Tags("面板", "系统监控").With(map[string]any{
|
||||
"error": err.Error(),
|
||||
}).Infof("保存失败")
|
||||
color.Red().Printfln(translate.Get("commands.panel:monitoring.fail")+": %s", err.Error())
|
||||
return nil
|
||||
}
|
||||
@@ -86,7 +88,9 @@ func (receiver *Monitoring) Handle(console.Context) error {
|
||||
return nil
|
||||
}
|
||||
if _, err = facades.Orm().Query().Where("created_at < ?", carbon.Now().SubDays(days).ToDateTimeString()).Delete(&models.Monitor{}); err != nil {
|
||||
facades.Log().Infof("[面板] 系统监控删除过期数据失败: %s", err.Error())
|
||||
facades.Log().Tags("面板", "系统监控").With(map[string]any{
|
||||
"error": err.Error(),
|
||||
}).Infof("删除过期数据失败")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ func (r *SshController) Session(ctx http.Context) http.Response {
|
||||
}
|
||||
defer ws.Close()
|
||||
|
||||
config := ssh.SSHClientConfigPassword(
|
||||
config := ssh.ClientConfigPassword(
|
||||
r.setting.Get(models.SettingKeySshHost)+":"+r.setting.Get(models.SettingKeySshPort),
|
||||
r.setting.Get(models.SettingKeySshUser),
|
||||
r.setting.Get(models.SettingKeySshPassword),
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"github.com/TheTNB/panel/pkg/tools"
|
||||
"github.com/goravel/framework/facades"
|
||||
|
||||
"github.com/TheTNB/panel/app/models"
|
||||
@@ -21,41 +20,61 @@ func (receiver *ProcessTask) Signature() string {
|
||||
func (receiver *ProcessTask) Handle(args ...any) error {
|
||||
taskID, ok := args[0].(uint)
|
||||
if !ok {
|
||||
facades.Log().Info("[面板][ProcessTask] 任务ID参数错误")
|
||||
facades.Log().Tags("面板", "异步任务").With(map[string]any{
|
||||
"args": args,
|
||||
}).Infof("参数错误")
|
||||
return nil
|
||||
}
|
||||
|
||||
var task models.Task
|
||||
if err := facades.Orm().Query().Where("id = ?", taskID).Get(&task); err != nil {
|
||||
facades.Log().Infof("[面板][ProcessTask] 获取任务%d失败: %s", taskID, err.Error())
|
||||
facades.Log().Tags("面板", "异步任务").With(map[string]any{
|
||||
"task_id": taskID,
|
||||
"error": err.Error(),
|
||||
}).Infof("获取任务失败")
|
||||
return nil
|
||||
}
|
||||
|
||||
task.Status = models.TaskStatusRunning
|
||||
if err := facades.Orm().Query().Save(&task); err != nil {
|
||||
facades.Log().Infof("[面板][ProcessTask] 更新任务%d失败: %s", taskID, err.Error())
|
||||
facades.Log().Tags("面板", "异步任务").With(map[string]any{
|
||||
"task_id": taskID,
|
||||
"error": err.Error(),
|
||||
}).Infof("更新任务失败")
|
||||
return nil
|
||||
}
|
||||
|
||||
facades.Log().Infof("[面板][ProcessTask] 开始执行任务%d", taskID)
|
||||
cmd := exec.Command("bash", "-c", task.Shell)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
facades.Log().Tags("面板", "异步任务").With(map[string]any{
|
||||
"task_id": taskID,
|
||||
}).Infof("开始执行任务")
|
||||
|
||||
if _, err := tools.Exec(task.Shell); err != nil {
|
||||
task.Status = models.TaskStatusFailed
|
||||
if err := facades.Orm().Query().Save(&task); err != nil {
|
||||
facades.Log().Infof("[面板][ProcessTask] 更新任务%d失败: %s", taskID, err.Error())
|
||||
facades.Log().Tags("面板", "异步任务").With(map[string]any{
|
||||
"task_id": taskID,
|
||||
"error": err.Error(),
|
||||
}).Infof("更新任务失败")
|
||||
return nil
|
||||
}
|
||||
facades.Log().Infof("[面板][ProcessTask] 任务%d执行失败: %s", taskID, err.Error())
|
||||
facades.Log().Tags("面板", "异步任务").With(map[string]any{
|
||||
"task_id": taskID,
|
||||
"error": err.Error(),
|
||||
}).Infof("执行任务失败")
|
||||
return nil
|
||||
}
|
||||
|
||||
task.Status = models.TaskStatusSuccess
|
||||
if err := facades.Orm().Query().Save(&task); err != nil {
|
||||
facades.Log().Infof("[面板][ProcessTask] 更新任务%d失败: %s", taskID, err.Error())
|
||||
facades.Log().Tags("面板", "异步任务").With(map[string]any{
|
||||
"task_id": taskID,
|
||||
"error": err.Error(),
|
||||
}).Infof("更新任务失败")
|
||||
return nil
|
||||
}
|
||||
|
||||
facades.Log().Infof("[面板][ProcessTask] 任务%d执行成功", taskID)
|
||||
facades.Log().Tags("面板", "异步任务").With(map[string]any{
|
||||
"task_id": taskID,
|
||||
}).Infof("执行任务成功")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/TheTNB/panel/app/models"
|
||||
@@ -17,43 +19,39 @@ func NewCronImpl() *CronImpl {
|
||||
// AddToSystem 添加到系统
|
||||
func (r *CronImpl) AddToSystem(cron models.Cron) error {
|
||||
if tools.IsRHEL() {
|
||||
if _, err := tools.Exec("echo \"" + cron.Time + " " + cron.Shell + " >> " + cron.Log + " 2>&1\" >> /var/spool/cron/root"); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tools.Exec("systemctl restart crond"); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if _, err := tools.Exec("echo \"" + cron.Time + " " + cron.Shell + " >> " + cron.Log + " 2>&1\" >> /var/spool/cron/crontabs/root"); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tools.Exec("systemctl restart cron"); err != nil {
|
||||
if _, err := tools.Exec(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 nil
|
||||
if tools.IsDebian() {
|
||||
if _, err := tools.Exec(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 errors.New("不支持的系统")
|
||||
}
|
||||
|
||||
// DeleteFromSystem 从系统中删除
|
||||
func (r *CronImpl) DeleteFromSystem(cron models.Cron) error {
|
||||
// 需要转义Shell路径的/为\/
|
||||
// 需要转义 shell 路径的/为\/
|
||||
cron.Shell = strings.ReplaceAll(cron.Shell, "/", "\\/")
|
||||
if tools.IsRHEL() {
|
||||
if _, err := tools.Exec("sed -i '/" + cron.Shell + "/d' /var/spool/cron/root"); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tools.Exec("systemctl restart crond"); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return tools.ServiceRestart("crond")
|
||||
}
|
||||
|
||||
if tools.IsDebian() {
|
||||
if _, err := tools.Exec("sed -i '/" + cron.Shell + "/d' /var/spool/cron/crontabs/root"); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tools.Exec("systemctl restart cron"); err != nil {
|
||||
return err
|
||||
}
|
||||
return tools.ServiceRestart("cron")
|
||||
}
|
||||
|
||||
return nil
|
||||
return errors.New("不支持的系统")
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ func (r *PluginImpl) All() []types.Plugin {
|
||||
return plugins
|
||||
}
|
||||
|
||||
// GetBySlug 根据slug获取插件
|
||||
// GetBySlug 根据 slug 获取插件
|
||||
func (r *PluginImpl) GetBySlug(slug string) types.Plugin {
|
||||
for _, item := range r.All() {
|
||||
if item.Slug == slug {
|
||||
@@ -69,7 +69,7 @@ func (r *PluginImpl) GetBySlug(slug string) types.Plugin {
|
||||
return types.Plugin{}
|
||||
}
|
||||
|
||||
// GetInstalledBySlug 根据slug获取已安装的插件
|
||||
// GetInstalledBySlug 根据 slug 获取已安装的插件
|
||||
func (r *PluginImpl) GetInstalledBySlug(slug string) models.Plugin {
|
||||
var plugin models.Plugin
|
||||
if err := facades.Orm().Query().Where("slug", slug).Get(&plugin); err != nil {
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"github.com/TheTNB/panel/app/models"
|
||||
"github.com/goravel/framework/facades"
|
||||
|
||||
"github.com/TheTNB/panel/app/models"
|
||||
"github.com/TheTNB/panel/pkg/tools"
|
||||
)
|
||||
|
||||
type SettingImpl struct {
|
||||
@@ -17,19 +19,11 @@ func NewSettingImpl() *SettingImpl {
|
||||
func (r *SettingImpl) Get(key string, defaultValue ...string) string {
|
||||
var setting models.Setting
|
||||
if err := facades.Orm().Query().Where("key", key).FirstOrFail(&setting); err != nil {
|
||||
if len(defaultValue) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return defaultValue[0]
|
||||
return tools.FirstElement(defaultValue)
|
||||
}
|
||||
|
||||
if len(setting.Value) == 0 {
|
||||
if len(defaultValue) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return defaultValue[0]
|
||||
return tools.FirstElement(defaultValue)
|
||||
}
|
||||
|
||||
return setting.Value
|
||||
|
||||
@@ -460,8 +460,7 @@ func (r *WebsiteImpl) SaveConfig(config requests.SaveConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tools.Exec("systemctl reload openresty")
|
||||
return err
|
||||
return tools.ServiceReload("openresty")
|
||||
}
|
||||
|
||||
// Delete 删除网站
|
||||
@@ -495,8 +494,7 @@ func (r *WebsiteImpl) Delete(id uint) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := tools.Exec("systemctl reload openresty")
|
||||
return err
|
||||
return tools.ServiceReload("openresty")
|
||||
}
|
||||
|
||||
// GetConfig 获取网站配置
|
||||
|
||||
@@ -15,7 +15,7 @@ const (
|
||||
PUBLICKEY
|
||||
)
|
||||
|
||||
type SSHClientConfig struct {
|
||||
type ClientConfig struct {
|
||||
AuthMethod AuthMethod
|
||||
HostAddr string
|
||||
User string
|
||||
@@ -24,8 +24,8 @@ type SSHClientConfig struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func SSHClientConfigPassword(hostAddr, user, Password string) *SSHClientConfig {
|
||||
return &SSHClientConfig{
|
||||
func ClientConfigPassword(hostAddr, user, Password string) *ClientConfig {
|
||||
return &ClientConfig{
|
||||
Timeout: time.Second * 5,
|
||||
AuthMethod: PASSWORD,
|
||||
HostAddr: hostAddr,
|
||||
@@ -34,8 +34,8 @@ func SSHClientConfigPassword(hostAddr, user, Password string) *SSHClientConfig {
|
||||
}
|
||||
}
|
||||
|
||||
func SSHClientConfigPulicKey(hostAddr, user, keyPath string) *SSHClientConfig {
|
||||
return &SSHClientConfig{
|
||||
func ClientConfigPublicKey(hostAddr, user, keyPath string) *ClientConfig {
|
||||
return &ClientConfig{
|
||||
Timeout: time.Second * 5,
|
||||
AuthMethod: PUBLICKEY,
|
||||
HostAddr: hostAddr,
|
||||
@@ -44,7 +44,7 @@ func SSHClientConfigPulicKey(hostAddr, user, keyPath string) *SSHClientConfig {
|
||||
}
|
||||
}
|
||||
|
||||
func NewSSHClient(conf *SSHClientConfig) (*ssh.Client, error) {
|
||||
func NewSSHClient(conf *ClientConfig) (*ssh.Client, error) {
|
||||
config := &ssh.ClientConfig{
|
||||
Timeout: conf.Timeout,
|
||||
User: conf.User,
|
||||
|
||||
Reference in New Issue
Block a user