mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 23:27:17 +08:00
* refactor: 重构部分完成 * fix: 添加.gitkeep * fix: build * fix: lint * fix: lint * chore(deps): Update module github.com/go-playground/validator/v10 to v10.22.1 (#162) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): Update module gorm.io/gorm to v1.25.12 (#161) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): Update module golang.org/x/net to v0.29.0 (#159) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * workflow: 更新工作流 * workflow: test new download * feat: merge frontend project * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: fix frontend build * workflow: update to ubuntu-24.04 * workflow: rename build-* * workflow: 修改fetch-depth * chore(deps): Update dependency eslint to v9 (#164) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(frontend): update dependences * chore(frontend): fix lint * chore(frontend): fix lint * workflow: add govulncheck * workflow: disable nilaway * feat: 使用新的压缩解压库 * fix: 测试 * fix: 测试 * fix: 测试 * feat: 添加ntp包 * chore(deps): Lock file maintenance (#168) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): Update module github.com/go-resty/resty/v2 to v2.15.0 (#167) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): Update dependency @iconify/json to v2.2.249 (#169) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * feat: 添加限流器 * feat: 调整登录限流 * feat: 证书 * fix: lint * feat: 证书dns * feat: 证书acme账号 * fix: 修改UserID导致的一系列问题 * feat: 低配版任务队列 * feat: 队列完成 * fix: lint * fix: lint * fix: swagger和前端路由 * fix: 去掉ntp测试 * feat: 完成插件接口 * feat: 完成cron * feat: 完成safe * chore(deps): Update dependency vue to v3.5.6 (#170) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): Update dependency @vueuse/core to v11.1.0 (#171) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): Update dependency vite to v5.4.6 (#173) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): Update unocss monorepo to v0.62.4 (#172) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore: update renovate config * feat: 新的firewall客户端 * fix: lint * feat: firewall完成 * feat: ssh完成 * feat: 容器完成1/2 * feat: 容器完成 * feat: 文件完成 * feat: systemctl及设置 * fix: windows编译 * fix: session not work * fix: migrate not work * feat: 前端路由 * feat: 初步支持cli --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
254 lines
5.7 KiB
Go
254 lines
5.7 KiB
Go
package data
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"github.com/golang-module/carbon/v2"
|
|
|
|
"github.com/TheTNB/panel/internal/app"
|
|
"github.com/TheTNB/panel/internal/biz"
|
|
"github.com/TheTNB/panel/internal/http/request"
|
|
"github.com/TheTNB/panel/pkg/io"
|
|
"github.com/TheTNB/panel/pkg/os"
|
|
"github.com/TheTNB/panel/pkg/shell"
|
|
"github.com/TheTNB/panel/pkg/str"
|
|
"github.com/TheTNB/panel/pkg/systemctl"
|
|
)
|
|
|
|
type cronRepo struct {
|
|
settingRepo biz.SettingRepo
|
|
}
|
|
|
|
func NewCronRepo() biz.CronRepo {
|
|
return &cronRepo{
|
|
settingRepo: NewSettingRepo(),
|
|
}
|
|
}
|
|
|
|
func (r *cronRepo) Count() (int64, error) {
|
|
var count int64
|
|
if err := app.Orm.Model(&biz.Cron{}).Count(&count).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return count, nil
|
|
}
|
|
|
|
func (r *cronRepo) List(page, limit uint) ([]*biz.Cron, int64, error) {
|
|
var cron []*biz.Cron
|
|
var total int64
|
|
err := app.Orm.Model(&biz.Cert{}).Order("id desc").Count(&total).Offset(int((page - 1) * limit)).Limit(int(limit)).Find(&cron).Error
|
|
return cron, total, err
|
|
}
|
|
|
|
func (r *cronRepo) Get(id uint) (*biz.Cron, error) {
|
|
cron := new(biz.Cron)
|
|
if err := app.Orm.Where("id = ?", id).First(cron).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cron, nil
|
|
}
|
|
|
|
func (r *cronRepo) Create(req *request.CronCreate) error {
|
|
if !regexp.MustCompile(`^((\*|\d+|\d+-\d+|\d+/\d+|\d+-\d+/\d+|\*/\d+)(,(\*|\d+|\d+-\d+|\d+/\d+|\d+-\d+/\d+|\*/\d+))*\s?){5}$`).MatchString(req.Time) {
|
|
return errors.New("时间格式错误")
|
|
}
|
|
|
|
var script string
|
|
if req.Type == "backup" {
|
|
if len(req.BackupPath) == 0 {
|
|
req.BackupPath, _ = r.settingRepo.Get(biz.SettingKeyBackupPath)
|
|
if len(req.BackupPath) == 0 {
|
|
return errors.New("备份路径不能为空")
|
|
}
|
|
req.BackupPath = filepath.Join(req.BackupPath, req.BackupType)
|
|
}
|
|
script = fmt.Sprintf(`#!/bin/bash
|
|
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
|
|
|
|
# 耗子面板 - 数据备份脚本
|
|
|
|
type=%s
|
|
path=%s
|
|
name=%s
|
|
save=%d
|
|
|
|
# 执行备份
|
|
panel backup ${type} ${name} ${path} ${save} 2>&1
|
|
`, req.BackupType, req.BackupPath, req.Target, req.Save)
|
|
}
|
|
if req.Type == "cutoff" {
|
|
script = fmt.Sprintf(`#!/bin/bash
|
|
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
|
|
|
|
# 耗子面板 - 日志切割脚本
|
|
|
|
name=%s
|
|
save=%d
|
|
|
|
# 执行切割
|
|
panel cutoff ${name} ${save} 2>&1
|
|
`, req.Target, req.Save)
|
|
}
|
|
|
|
shellDir := fmt.Sprintf("%s/server/cron/", app.Root)
|
|
shellLogDir := fmt.Sprintf("%s/server/cron/logs/", app.Root)
|
|
if !io.Exists(shellDir) {
|
|
return errors.New("计划任务目录不存在")
|
|
}
|
|
if !io.Exists(shellLogDir) {
|
|
return errors.New("计划任务日志目录不存在")
|
|
}
|
|
shellFile := strconv.Itoa(int(carbon.Now().Timestamp())) + str.RandomString(16)
|
|
if err := io.Write(filepath.Join(shellDir, shellFile+".sh"), script, 0700); err != nil {
|
|
return errors.New(err.Error())
|
|
}
|
|
if out, err := shell.Execf("dos2unix %s%s.sh", shellDir, shellFile); err != nil {
|
|
return errors.New(out)
|
|
}
|
|
|
|
cron := new(biz.Cron)
|
|
cron.Name = req.Name
|
|
cron.Type = req.Type
|
|
cron.Status = true
|
|
cron.Time = req.Time
|
|
cron.Shell = shellDir + shellFile + ".sh"
|
|
cron.Log = shellLogDir + shellFile + ".log"
|
|
|
|
if err := app.Orm.Create(cron).Error; err != nil {
|
|
return err
|
|
}
|
|
if err := r.addToSystem(cron); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *cronRepo) Update(req *request.CronUpdate) error {
|
|
cron, err := r.Get(req.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !regexp.MustCompile(`^((\*|\d+|\d+-\d+|\d+/\d+|\d+-\d+/\d+|\*/\d+)(,(\*|\d+|\d+-\d+|\d+/\d+|\d+-\d+/\d+|\*/\d+))*\s?){5}$`).MatchString(req.Time) {
|
|
return errors.New("时间格式错误")
|
|
}
|
|
|
|
if !cron.Status {
|
|
return errors.New("计划任务已禁用")
|
|
}
|
|
|
|
cron.Time = req.Time
|
|
cron.Name = req.Name
|
|
if err = app.Orm.Save(cron).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = io.Write(cron.Shell, req.Script, 0700); err != nil {
|
|
return err
|
|
}
|
|
if out, err := shell.Execf("dos2unix %s", cron.Shell); err != nil {
|
|
return errors.New(out)
|
|
}
|
|
|
|
if err = r.deleteFromSystem(cron); err != nil {
|
|
return err
|
|
}
|
|
if cron.Status {
|
|
if err = r.addToSystem(cron); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *cronRepo) Delete(id uint) error {
|
|
cron, err := r.Get(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = r.deleteFromSystem(cron); err != nil {
|
|
return err
|
|
}
|
|
if err = io.Remove(cron.Shell); err != nil {
|
|
return err
|
|
}
|
|
|
|
return app.Orm.Delete(cron).Error
|
|
}
|
|
|
|
func (r *cronRepo) Status(id uint, status bool) error {
|
|
cron, err := r.Get(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = r.deleteFromSystem(cron); err != nil {
|
|
return err
|
|
}
|
|
if status {
|
|
return r.addToSystem(cron)
|
|
}
|
|
|
|
cron.Status = status
|
|
|
|
return app.Orm.Save(cron).Error
|
|
}
|
|
|
|
func (r *cronRepo) Log(id uint) (string, error) {
|
|
cron, err := r.Get(id)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if !io.Exists(cron.Log) {
|
|
return "", errors.New("日志文件不存在")
|
|
}
|
|
|
|
log, err := shell.Execf("tail -n 1000 '%s'", cron.Log)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return log, nil
|
|
}
|
|
|
|
// addToSystem 添加到系统
|
|
func (r *cronRepo) addToSystem(cron *biz.Cron) error {
|
|
if _, err := shell.Execf(`( crontab -l; echo "%s %s >> %s 2>&1" ) | sort - | uniq - | crontab -`, cron.Time, cron.Shell, cron.Log); err != nil {
|
|
return err
|
|
}
|
|
|
|
return r.restartCron()
|
|
}
|
|
|
|
// deleteFromSystem 从系统中删除
|
|
func (r *cronRepo) deleteFromSystem(cron *biz.Cron) error {
|
|
if _, err := shell.Execf(`( crontab -l | grep -v -F "%s %s >> %s 2>&1" ) | crontab -`, cron.Time, cron.Shell, cron.Log); err != nil {
|
|
return err
|
|
}
|
|
|
|
return r.restartCron()
|
|
}
|
|
|
|
// restartCron 重启 cron 服务
|
|
func (r *cronRepo) restartCron() error {
|
|
if os.IsRHEL() {
|
|
return systemctl.Restart("crond")
|
|
}
|
|
|
|
if os.IsDebian() || os.IsUbuntu() {
|
|
return systemctl.Restart("cron")
|
|
}
|
|
|
|
return errors.New("不支持的系统")
|
|
}
|