From 17ef66aa49acd24067904a8f2a1a02f06fe42cc2 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:36:53 +0800 Subject: [PATCH] feat(init): add support for international language pack initialization (#7315) --- backend/app/service/snapshot_create.go | 1 + backend/app/service/snapshot_recover.go | 1 + backend/app/service/upgrade.go | 4 ++ backend/init/hook/hook.go | 74 +++++++++++++++++++++++++ cmd/server/cmd/restore.go | 1 + 5 files changed, 81 insertions(+) diff --git a/backend/app/service/snapshot_create.go b/backend/app/service/snapshot_create.go index 65ea464b3..c5979c77d 100644 --- a/backend/app/service/snapshot_create.go +++ b/backend/app/service/snapshot_create.go @@ -50,6 +50,7 @@ func snapPanel(snap snapHelper, targetDir string) { if err := common.CopyFile("/usr/local/bin/1pctl", targetDir); err != nil { status = err.Error() } + _, _ = cmd.Execf("cp -r /usr/local/bin/lang %s", targetDir) if err := common.CopyFile("/etc/systemd/system/1panel.service", targetDir); err != nil { status = err.Error() diff --git a/backend/app/service/snapshot_recover.go b/backend/app/service/snapshot_recover.go index 83c943c04..6d835a888 100644 --- a/backend/app/service/snapshot_recover.go +++ b/backend/app/service/snapshot_recover.go @@ -104,6 +104,7 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b updateRecoverStatus(snap.ID, isRecover, "1PctlBinary", constant.StatusFailed, err.Error()) return } + _, _ = cmd.Execf("cp -r %s %s", path.Join(snapFileDir, "1panel/lang"), "/usr/local/bin/") global.LOG.Debug("recover 1pctl from snapshot file successful!") req.IsNew = true } diff --git a/backend/app/service/upgrade.go b/backend/app/service/upgrade.go index b2d2fdad9..2b357ce2d 100644 --- a/backend/app/service/upgrade.go +++ b/backend/app/service/upgrade.go @@ -143,6 +143,7 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error { u.handleRollback(originalDir, 2) return } + _, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(tmpDir, "lang")) if _, err := cmd.Execf("sed -i -e 's#BASE_DIR=.*#BASE_DIR=%s#g' /usr/local/bin/1pctl", global.CONF.System.BaseDir); err != nil { global.LOG.Errorf("upgrade basedir in 1pctl failed, err: %v", err) u.handleRollback(originalDir, 2) @@ -175,6 +176,7 @@ func (u *UpgradeService) handleBackup(fileOp files.FileOp, originalDir string) e if err := fileOp.Copy("/etc/systemd/system/1panel.service", originalDir); err != nil { return err } + _, _ = cmd.Execf("cp -r /usr/local/bin/lang %s", originalDir) checkPointOfWal() if err := handleTar(path.Join(global.CONF.System.BaseDir, "1panel/db"), originalDir, "db.tar.gz", "db/1Panel.db-*", ""); err != nil { return err @@ -205,6 +207,8 @@ func (u *UpgradeService) handleRollback(originalDir string, errStep int) { if err := common.CopyFile(path.Join(originalDir, "1pctl"), "/usr/local/bin"); err != nil { global.LOG.Errorf("rollback 1panel failed, err: %v", err) } + _, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(originalDir, "lang")) + if errStep == 2 { return } diff --git a/backend/init/hook/hook.go b/backend/init/hook/hook.go index b0334966f..857bfda1b 100644 --- a/backend/init/hook/hook.go +++ b/backend/init/hook/hook.go @@ -3,8 +3,10 @@ package hook import ( "encoding/base64" "encoding/json" + "fmt" "os" "path" + "sort" "strings" "github.com/1Panel-dev/1Panel/backend/app/dto" @@ -15,6 +17,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/encrypt" + "github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/xpack" ) @@ -85,6 +88,8 @@ func Init() { handleSnapStatus() loadLocalDir() initDir() + + go initLang() } func handleSnapStatus() { @@ -260,3 +265,72 @@ func handleCronJobAlert(cronjob *model.Cronjob) { return } } + +func initLang() { + fileOp := files.NewFileOp() + upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade") + if fileOp.Stat("/usr/local/bin/lang/zh.sh") { + return + } + tmpPath, err := loadRestorePath(upgradePath) + files, _ := os.ReadDir(path.Join(upgradePath, tmpPath, "downloads")) + if len(files) == 0 { + tmpPath = "no such file" + } else { + for _, item := range files { + if item.IsDir() && strings.HasPrefix(item.Name(), "1panel-") { + tmpPath = path.Join(upgradePath, tmpPath, "downloads", item.Name(), "lang") + break + } + } + } + + if err != nil || tmpPath == "no such file" || !fileOp.Stat(tmpPath) { + path := fmt.Sprintf("%s/language/lang.tar.gz", global.CONF.System.RepoUrl) + if err := fileOp.DownloadFileWithProxy(path, "/usr/local/bin/lang.tar.gz"); err != nil { + global.LOG.Errorf("download lang.tar.gz failed, err: %v", err) + return + } + if !fileOp.Stat("/usr/local/bin/lang.tar.gz") { + global.LOG.Errorf("download lang.tar.gz failed, no such file, err: %v", err) + return + } + std, err := cmd.Execf("tar zxvfC %s %s", "/usr/local/bin/lang.tar.gz", "/usr/local/bin/") + if err != nil { + fmt.Printf("decompress lang.tar.gz failed, std: %s, err: %v", std, err) + return + } + _ = os.Remove("/usr/local/bin/lang.tar.gz") + global.LOG.Info("init lang for 1pctl successful") + return + } + std, err := cmd.Execf("cp -r %s %s", tmpPath, "/usr/local/bin/") + if err != nil { + fmt.Printf("load lang from package failed, std: %s, err: %v", std, err) + return + } + global.LOG.Info("init lang for 1pctl successful") +} + +func loadRestorePath(upgradeDir string) (string, error) { + if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) { + return "no such file", nil + } + files, err := os.ReadDir(upgradeDir) + if err != nil { + return "", err + } + var folders []string + for _, file := range files { + if file.IsDir() { + folders = append(folders, file.Name()) + } + } + if len(folders) == 0 { + return "no such file", nil + } + sort.Slice(folders, func(i, j int) bool { + return folders[i] > folders[j] + }) + return folders[0], nil +} diff --git a/cmd/server/cmd/restore.go b/cmd/server/cmd/restore.go index 7b9da5351..391a00094 100644 --- a/cmd/server/cmd/restore.go +++ b/cmd/server/cmd/restore.go @@ -53,6 +53,7 @@ var restoreCmd = &cobra.Command{ if err := common.CopyFile(path.Join(tmpPath, "1pctl"), "/usr/local/bin"); err != nil { return err } + _, _ = cmdUtils.Execf("cp -r %s /usr/local/bin", path.Join(tmpPath, "lang")) fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep3")) if err := common.CopyFile(path.Join(tmpPath, "1panel.service"), "/etc/systemd/system"); err != nil { return err