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

feat: 将面板配置固定保存

This commit is contained in:
耗子
2024-10-12 18:11:34 +08:00
parent 6ec5ebb18d
commit 3c1c4e92b9
9 changed files with 31 additions and 81 deletions

View File

@@ -19,6 +19,6 @@ jobs:
run: sudo apt-get install -y curl jq
- name: Set up environment
run: |
cp config/config.example.yml config/config.yml
cp config.example.yml config.yml
- name: Run tests
run: go test ./...

2
.gitignore vendored
View File

@@ -33,7 +33,7 @@ _cgo_export.*
*.log
*.sqlite
*.db
config/config.yml
config.yml
# 临时文件
tmp/

View File

@@ -51,5 +51,5 @@ archives:
strip_binary_directory: true
files:
- LICENSE
- config.example.yml
- storage/*
- config/*

View File

@@ -72,7 +72,7 @@
如果你无法重装系统,请以`root`用户登录服务器,执行以下命令卸载面板:
```shell
curl -fsLm 10 -o uninstall_panel.sh https://dl.cdn.haozi.net/panel/uninstall_panel.sh && bash uninstall_panel.sh
curl -fsLm 10 -o uninstall.sh https://dl.cdn.haozi.net/panel/uninstall.sh && bash uninstall.sh
```
卸载面板前请务必备份好所有数据,提前卸载面板全部应用。卸载后数据将**无法恢复**

View File

@@ -72,7 +72,7 @@ Recommended to back up data and reinstall the system first, so that the system c
If you are unable to reinstall the system, log in to the server as the `root` user and execute the following command to uninstall the panel:
```shell
curl -fsLm 10 -o uninstall_panel.sh https://dl.cdn.haozi.net/panel/uninstall_panel.sh && bash uninstall_panel.sh
curl -fsLm 10 -o uninstall.sh https://dl.cdn.haozi.net/panel/uninstall.sh && bash uninstall.sh
```
Before uninstalling the panel, please be sure to back up all data and uninstall all panel plugins in advance. The data will **not be recoverable** after uninstallation!

View File

@@ -2,9 +2,6 @@ package bootstrap
import (
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/knadh/koanf/parsers/yaml"
@@ -12,26 +9,17 @@ import (
"github.com/knadh/koanf/v2"
"github.com/TheTNB/panel/internal/app"
"github.com/TheTNB/panel/pkg/io"
)
func initConf() {
executable, err := os.Executable()
if err != nil {
log.Fatalf("failed to get executable: %v", err)
}
res, err := filepath.EvalSymlinks(filepath.Dir(executable))
if err != nil {
log.Fatalf("failed to get executable path: %v", err)
}
if isTesting() || isAir() || isDirectlyRun() {
res, err = os.Getwd()
if err != nil {
log.Fatalf("failed to get working directory: %v", err)
}
config := "/usr/local/etc/panel/config.yml"
if !io.Exists(config) {
config = "config.yml"
}
app.Conf = koanf.New(".")
if err = app.Conf.Load(file.Provider(filepath.Join(res, "config/config.yml")), yaml.Parser()); err != nil {
if err := app.Conf.Load(file.Provider(config), yaml.Parser()); err != nil {
log.Fatalf("failed to load config: %v", err)
}
}
@@ -48,32 +36,3 @@ func initGlobal() {
}
time.Local = loc
}
// isTesting checks if the application is running in testing mode.
func isTesting() bool {
for _, arg := range os.Args {
if strings.Contains(arg, "-test.") {
return true
}
}
return false
}
// isAir checks if the application is running using Air.
func isAir() bool {
for _, arg := range os.Args {
if strings.Contains(filepath.ToSlash(arg), "/storage/temp") {
return true
}
}
return false
}
// isDirectlyRun checks if the application is running using go run.
func isDirectlyRun() bool {
executable, _ := os.Executable()
return strings.Contains(filepath.Base(executable), os.TempDir()) ||
(strings.Contains(filepath.ToSlash(executable), "/var/folders") && strings.Contains(filepath.ToSlash(executable), "/T/go-build")) // macOS
}

View File

@@ -126,7 +126,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P
restartFlag := false
config := new(types.PanelConfig)
cm := yaml.CommentMap{}
raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml"))
raw, err := io.Read("/usr/local/etc/panel/config.yml")
if err != nil {
return false, err
}
@@ -143,7 +143,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P
if err != nil {
return false, err
}
if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0644); err != nil {
if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0644); err != nil {
return false, err
}
if raw != string(encoded) {
@@ -179,7 +179,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
color.Greenln("文件名: ", name)
color.Greenln("前置检查...")
if io.Exists("/tmp/panel-storage.zip") || io.Exists("/tmp/panel-config.zip") {
if io.Exists("/tmp/panel-storage.zip") {
return errors.New("检测到 /tmp 存在临时文件,可能是上次更新失败导致的,请排除后重试")
}
@@ -193,11 +193,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
color.Redln("备份面板数据失败:", err)
return err
}
if err := io.Compress([]string{filepath.Join(app.Root, "panel/config")}, "/tmp/panel-config.zip", io.Zip); err != nil {
color.Redln("备份面板配置失败:", err)
return err
}
if !io.Exists("/tmp/panel-storage.zip") || !io.Exists("/tmp/panel-config.zip") {
if !io.Exists("/tmp/panel-storage.zip") {
return errors.New("已备份面板数据检查失败")
}
color.Greenln("备份完成")
@@ -242,10 +238,6 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
if !io.Exists(filepath.Join(app.Root, "panel", "web")) {
return errors.New("更新失败,可能是下载过程中出现了问题")
}
if err = io.Mv(filepath.Join(app.Root, "panel", "cli"), "/usr/local/sbin/panel-cli"); err != nil {
color.Redln("移动面板命令行工具失败:", err)
return err
}
color.Greenln("更新完成")
color.Greenln("恢复面板数据...")
@@ -253,10 +245,6 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
color.Redln("恢复面板数据失败:", err)
return err
}
if err = io.UnCompress("/tmp/panel-config.zip", filepath.Join(app.Root, "panel"), io.Zip); err != nil {
color.Redln("恢复面板配置失败:", err)
return err
}
if !io.Exists(filepath.Join(app.Root, "panel/storage/app.db")) {
return errors.New("恢复面板数据失败")
}
@@ -275,10 +263,14 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
color.Redln("写入面板版本号失败:", err)
return err
}
if err = io.Mv(filepath.Join(app.Root, "panel/cli"), "/usr/local/sbin/panel-cli"); err != nil {
color.Redln("移动面板命令行工具失败:", err)
return err
}
color.Greenln("设置面板文件权限...")
_ = io.Chmod("/usr/local/sbin/panel-cli", 0700)
_ = io.Chmod("/etc/systemd/system/panel.servic", 0700)
_ = io.Chmod("/etc/systemd/system/panel.service", 0700)
_ = io.Chmod(filepath.Join(app.Root, "panel"), 0700)
color.Greenln("设置完成")
@@ -286,7 +278,6 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
_, _ = shell.Execf("systemctl daemon-reload")
_ = io.Remove("/tmp/panel-storage.zip")
_ = io.Remove("/tmp/panel-config.zip")
return nil
}

View File

@@ -210,7 +210,7 @@ func (s *CliService) UserPassword(ctx context.Context, cmd *cli.Command) error {
func (s *CliService) HTTPSOn(ctx context.Context, cmd *cli.Command) error {
config := new(types.PanelConfig)
cm := yaml.CommentMap{}
raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml"))
raw, err := io.Read("/usr/local/etc/panel/config.yml")
if err != nil {
return err
}
@@ -225,7 +225,7 @@ func (s *CliService) HTTPSOn(ctx context.Context, cmd *cli.Command) error {
return err
}
if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil {
if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil {
return err
}
@@ -235,7 +235,7 @@ func (s *CliService) HTTPSOn(ctx context.Context, cmd *cli.Command) error {
func (s *CliService) HTTPSOff(ctx context.Context, cmd *cli.Command) error {
config := new(types.PanelConfig)
cm := yaml.CommentMap{}
raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml"))
raw, err := io.Read("/usr/local/etc/panel/config.yml")
if err != nil {
return err
}
@@ -250,7 +250,7 @@ func (s *CliService) HTTPSOff(ctx context.Context, cmd *cli.Command) error {
return err
}
if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil {
if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil {
return err
}
@@ -260,7 +260,7 @@ func (s *CliService) HTTPSOff(ctx context.Context, cmd *cli.Command) error {
func (s *CliService) EntranceOn(ctx context.Context, cmd *cli.Command) error {
config := new(types.PanelConfig)
cm := yaml.CommentMap{}
raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml"))
raw, err := io.Read("/usr/local/etc/panel/config.yml")
if err != nil {
return err
}
@@ -275,7 +275,7 @@ func (s *CliService) EntranceOn(ctx context.Context, cmd *cli.Command) error {
return err
}
if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil {
if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil {
return err
}
@@ -285,7 +285,7 @@ func (s *CliService) EntranceOn(ctx context.Context, cmd *cli.Command) error {
func (s *CliService) EntranceOff(ctx context.Context, cmd *cli.Command) error {
config := new(types.PanelConfig)
cm := yaml.CommentMap{}
raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml"))
raw, err := io.Read("/usr/local/etc/panel/config.yml")
if err != nil {
return err
}
@@ -300,7 +300,7 @@ func (s *CliService) EntranceOff(ctx context.Context, cmd *cli.Command) error {
return err
}
if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil {
if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil {
return err
}
@@ -315,7 +315,7 @@ func (s *CliService) Port(ctx context.Context, cmd *cli.Command) error {
config := new(types.PanelConfig)
cm := yaml.CommentMap{}
raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml"))
raw, err := io.Read("/usr/local/etc/panel/config.yml")
if err != nil {
return err
}
@@ -330,7 +330,7 @@ func (s *CliService) Port(ctx context.Context, cmd *cli.Command) error {
return err
}
if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil {
if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil {
return err
}
@@ -515,7 +515,7 @@ func (s *CliService) Init(ctx context.Context, cmd *cli.Command) error {
config := new(types.PanelConfig)
cm := yaml.CommentMap{}
raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml"))
raw, err := io.Read("/usr/local/etc/panel/config.yml")
if err != nil {
return err
}
@@ -530,7 +530,7 @@ func (s *CliService) Init(ctx context.Context, cmd *cli.Command) error {
if err != nil {
return err
}
if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil {
if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil {
return err
}