Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ build_frontend:

build_core_on_linux:
cd $(CORE_PATH) \
&& GOOS=$(GOOS) GOARCH=$(GOARCH) $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(CORE_NAME) $(CORE_MAIN)
&& CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(CORE_NAME) $(CORE_MAIN)

build_agent_on_linux:
cd $(AGENT_PATH) \
&& GOOS=$(GOOS) GOARCH=$(GOARCH) $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(AGENT_NAME) $(AGENT_MAIN)
&& CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(AGENT_NAME) $(AGENT_MAIN)

build_core_on_darwin:
cd $(CORE_PATH) \
&& GOOS=linux GOARCH=amd64 $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(CORE_NAME) $(CORE_MAIN)
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(CORE_NAME) $(CORE_MAIN)

build_agent_on_darwin:
cd $(AGENT_PATH) \
&& GOOS=linux GOARCH=amd64 $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(AGENT_NAME) $(AGENT_MAIN)
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -trimpath -ldflags '-s -w' -o $(BUILD_PATH)/$(AGENT_NAME) $(AGENT_MAIN)

build_all: build_frontend build_core_on_linux build_agent_on_linux

Expand Down
25 changes: 19 additions & 6 deletions agent/app/service/snapshot_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/i18n"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
"github.com/1Panel-dev/1Panel/agent/utils/common"
"github.com/1Panel-dev/1Panel/agent/utils/controller"
"github.com/1Panel-dev/1Panel/agent/utils/copier"
"github.com/1Panel-dev/1Panel/agent/utils/files"
"gorm.io/gorm"
Expand Down Expand Up @@ -285,6 +286,10 @@ func loadDbConn(snap *snapHelper, targetDir string, req dto.SnapshotCreate) erro
func snapBaseData(snap snapHelper, targetDir string, withDockerConf bool) error {
snap.Task.Log("---------------------- 2 / 8 ----------------------")
snap.Task.LogStart(i18n.GetMsgByKey("SnapBaseInfo"))
svcScriptBakPath := path.Join(targetDir, "scriptbak")
if _, err := os.Stat(svcScriptBakPath); err != nil {
_ = os.MkdirAll(svcScriptBakPath, os.ModePerm)
}

if global.IsMaster {
err := snap.FileOp.CopyFile("/usr/local/bin/1panel-core", targetDir)
Expand All @@ -306,19 +311,27 @@ func snapBaseData(snap snapHelper, targetDir string, withDockerConf bool) error
}

if global.IsMaster {
err = snap.FileOp.CopyFile("/etc/systemd/system/1panel-core.service", targetDir)
snap.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-core.service"), err)
svcCorePath, _ := controller.GetServicePath("1panel-core")
err = snap.FileOp.CopyFile(svcCorePath, svcScriptBakPath)
snap.Task.LogWithStatus(i18n.GetWithName("SnapCopy", svcCorePath), err)
if err != nil {
return err
}
}

err = snap.FileOp.CopyFile("/etc/systemd/system/1panel-agent.service", targetDir)
snap.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-agent.service"), err)
svcAgentName, _ := controller.GetServicePath("1panel-agent")
err = snap.FileOp.CopyFile(svcAgentName, svcScriptBakPath)
snap.Task.LogWithStatus(i18n.GetWithName("SnapCopy", svcAgentName), err)
if err != nil {
return err
}

initScriptPath := path.Join(global.Dir.ResourceDir, "initscript")
if _, err := os.Stat(initScriptPath); err == nil {
err = snap.FileOp.CopyDirWithNewName(initScriptPath, svcScriptBakPath, ".")
snap.Task.LogWithStatus(i18n.GetWithName("SnapCopy", initScriptPath), err)
if err != nil {
return err
}
}
if withDockerConf {
if snap.FileOp.Stat(constant.DaemonJsonPath) {
err = snap.FileOp.CopyFile(constant.DaemonJsonPath, targetDir)
Expand Down
54 changes: 42 additions & 12 deletions agent/app/service/snapshot_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ import (
"github.com/pkg/errors"
)

var (
svcBasePath, _ = controller.GetServicePath("")
svcCoreName, _ = controller.LoadServiceName("1panel-core")
selCoreName, _ = controller.SelectInitScript("1panel-core")
scriptCoreName, _ = controller.GetScriptName("1panel-core")
svcAgentName, _ = controller.LoadServiceName("1panel-agent")
selAgentName, _ = controller.SelectInitScript("1panel-agent")
scriptAgentName, _ = controller.GetScriptName("1panel-agent")
)

type snapRecoverHelper struct {
FileOp files.FileOp
Task *task.Task
Expand Down Expand Up @@ -259,7 +269,15 @@ func backupBeforeRecover(name string, itemHelper *snapRecoverHelper) error {
if _, err := os.Stat(baseDir); err != nil {
_ = os.MkdirAll(baseDir, os.ModePerm)
}

svcScriptBakPath := path.Join(baseDir, "scriptbak")
initScriptPath := path.Join(global.Dir.ResourceDir, "initscript")
if _, err := os.Stat(initScriptPath); err == nil {
err = itemHelper.FileOp.CopyDirWithNewName(initScriptPath, svcScriptBakPath, ".")
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", initScriptPath), err)
if err != nil {
return err
}
}
err := itemHelper.FileOp.CopyDirWithExclude(global.Dir.DataDir, rootDir, []string{"cache", "tmp"})
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", global.Dir.DataDir), err)
if err != nil {
Expand Down Expand Up @@ -294,8 +312,9 @@ func backupBeforeRecover(name string, itemHelper *snapRecoverHelper) error {
if err != nil {
return err
}
err = itemHelper.FileOp.CopyFile("/etc/systemd/system/1panel-core.service", baseDir)
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-core.service"), err)
svcCorePath, _ := controller.GetServicePath("1panel-core")
err = itemHelper.FileOp.CopyFile(svcCorePath, svcScriptBakPath)
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", svcCorePath), err)
if err != nil {
return err
}
Expand All @@ -305,8 +324,9 @@ func backupBeforeRecover(name string, itemHelper *snapRecoverHelper) error {
if err != nil {
return err
}
err = itemHelper.FileOp.CopyFile("/etc/systemd/system/1panel-agent.service", baseDir)
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-agent.service"), err)
svcAgentPath, _ := controller.GetServicePath("1panel-agent")
err = itemHelper.FileOp.CopyFile(svcAgentPath, svcScriptBakPath)
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", svcAgentPath), err)
if err != nil {
return err
}
Expand Down Expand Up @@ -363,7 +383,7 @@ func recoverAppData(src string, itemHelper *snapRecoverHelper) error {
func recoverBaseData(src string, itemHelper *snapRecoverHelper) error {
itemHelper.Task.Log("---------------------- 6 / 11 ----------------------")
itemHelper.Task.LogStart(i18n.GetMsgByKey("SnapBaseInfo"))

svcScriptBakPath := path.Join(src, "scriptbak")
if global.IsMaster {
err := itemHelper.FileOp.CopyFile(path.Join(src, "1pctl"), "/usr/local/bin")
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/usr/local/bin/1pctl"), err)
Expand All @@ -375,21 +395,31 @@ func recoverBaseData(src string, itemHelper *snapRecoverHelper) error {
if err != nil {
return err
}
err = itemHelper.FileOp.CopyFile(path.Join(src, "1panel-core.service"), "/etc/systemd/system")
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-core.service"), err)
svcCoreName, _ := controller.LoadServiceName("1panel-core")
err = itemHelper.FileOp.CopyFile(path.Join(svcScriptBakPath, svcCoreName), svcBasePath)
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", path.Join(svcBasePath, svcCoreName)), err)
if err != nil {
return err
err = itemHelper.FileOp.CopyFile(path.Join(svcScriptBakPath, selCoreName), path.Join(svcBasePath, scriptCoreName))
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", path.Join(svcBasePath, scriptCoreName)), err)
if err != nil {
return err
}
}
}
err := itemHelper.FileOp.CopyFile(path.Join(src, "1panel-agent"), "/usr/local/bin")
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/usr/local/bin/1panel-agent"), err)
if err != nil {
return err
}
err = itemHelper.FileOp.CopyFile(path.Join(src, "1panel-agent.service"), "/etc/systemd/system")
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-agent.service"), err)
svcAgentName, _ := controller.LoadServiceName("1panel-agent")
err = itemHelper.FileOp.CopyFile(path.Join(svcScriptBakPath, svcAgentName), svcBasePath)
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", path.Join(svcBasePath, svcAgentName)), err)
if err != nil {
return err
err = itemHelper.FileOp.CopyFile(path.Join(svcScriptBakPath, selAgentName), path.Join(svcBasePath, scriptAgentName))
itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", path.Join(svcBasePath, scriptAgentName)), err)
if err != nil {
return err
}
}

if !itemHelper.FileOp.Stat(path.Join(src, "daemon.json")) {
Expand Down
9 changes: 5 additions & 4 deletions agent/app/service/snapshot_rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error {
go func() {
rootDir := fmt.Sprintf("%s/1panel_original/original_%s", global.Dir.BaseDir, snap.Name)
baseDir := path.Join(rootDir, "base")
svcScriptBakPath := path.Join(baseDir, "scriptbak")

FileOp := files.NewFileOp()
taskItem.AddSubTask(
Expand Down Expand Up @@ -61,17 +62,17 @@ func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error {
)
if global.IsMaster {
taskItem.AddSubTask(
i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-core.service"),
i18n.GetWithName("SnapCopy", path.Join(svcBasePath, svcCoreName)),
func(t *task.Task) error {
return FileOp.CopyFile(path.Join(baseDir, "1panel-core.service"), "/etc/systemd/system")
return FileOp.CopyFile(path.Join(svcScriptBakPath, svcCoreName), svcBasePath)
},
nil,
)
}
taskItem.AddSubTask(
i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-agent.service"),
i18n.GetWithName("SnapCopy", path.Join(svcBasePath, svcAgentName)),
func(t *task.Task) error {
return FileOp.CopyFile(path.Join(baseDir, "1panel-agent.service"), "/etc/systemd/system")
return FileOp.CopyFile(path.Join(svcScriptBakPath, svcAgentName), svcBasePath)
},
nil,
)
Expand Down
92 changes: 92 additions & 0 deletions agent/utils/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"

Expand Down Expand Up @@ -196,3 +197,94 @@ func loadFromPredefined(mgr Controller, keyword string) string {
}
return ""
}

// GetServicePath returns the configuration file path for the specified service.
// If serviceName is empty, it returns the default directory path based on the system's service manager.
// For non-empty serviceName, it retrieves the exact service file path.
// Parameters:
// - serviceName: Name of the service. If empty, returns the default directory.
//
// Returns:
// - string: The service configuration file path.
// - error: Error if the service manager is unsupported or command execution fails.
func GetServicePath(serviceName string) (string, error) {
if serviceName == "" {
client, err := New()
if err != nil {
return "", err
}
switch client.Name() {
case "systemd":
return "/etc/systemd/system/", nil
case "openrc", "sysvinit":
return "/etc/init.d/", nil
default:
return "", fmt.Errorf("unsupported manager: %s", client.Name())
}
}
service, err := LoadServiceName(serviceName)
if err != nil {
return "", err
}
client, err := New()
if err != nil {
return "", err
}
switch client.Name() {
case "systemd":
stdout, err := exec.Command("systemctl", "show", "-p", "FragmentPath", service).Output()
if err != nil {
return "", err
}
parts := strings.SplitN(string(stdout), "=", 2)
if len(parts) != 2 {
return "", fmt.Errorf("unexpected output: %s", string(stdout))
}
return strings.TrimSpace(parts[1]), nil
case "openrc", "sysvinit":
return fmt.Sprintf("/etc/init.d/%s", service), nil
default:
return "", fmt.Errorf("unsupported manager: %s", client.Name())
}
}

func SelectInitScript(keyword string) (string, error) {
client, err := New()
if err != nil {
return "", err
}
switch client.Name() {
case "systemd":
keyword = strings.TrimSuffix(keyword, ".service") + ".service"
case "openrc":
keyword = strings.TrimSuffix(keyword, ".service") + ".openrc"
case "sysvinit":
if _, err := os.Stat("/etc/rc.common"); err == nil {
keyword = strings.TrimSuffix(keyword, ".service") + ".prod"
} else {
keyword = strings.TrimSuffix(keyword, ".service") + ".init"
}
default:
return "", fmt.Errorf("unsupported manager: %s", client.Name())
}
return keyword, nil
}

func GetScriptName(keyword string) (string, error) {
client, err := New()
if err != nil {
return "", err
}
switch client.Name() {
case "systemd":
keyword = strings.TrimSuffix(keyword, ".service") + ".service"
case "openrc", "sysvinit":
lastDotIdx := strings.LastIndex(keyword, ".")
if lastDotIdx != -1 {
keyword = keyword[:lastDotIdx]
}
default:
return "", fmt.Errorf("unsupported manager: %s", client.Name())
}
return keyword, nil
}
4 changes: 3 additions & 1 deletion agent/utils/controller/manager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func handlerErr(out string, err error) error {
}

func run(name string, args ...string) (string, error) {
global.LOG.Debugf("handle with controller `%s %s`", name, strings.Join(args, " "))
if global.LOG != nil {
global.LOG.Debugf("handle with controller `%s %s`", name, strings.Join(args, " "))
}
return cmd.NewCommandMgr(cmd.WithTimeout(10*time.Second)).RunWithStdoutBashCf("LANGUAGE=en_US:en %s %s", name, strings.Join(args, " "))
}
3 changes: 3 additions & 0 deletions agent/utils/files/file_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ func (f FileOp) CopyAndReName(src, dst, name string, cover bool) error {
}

func (f FileOp) CopyDirWithNewName(src, dst, newName string) error {
if newName == "." || newName == "" {
return cmd.RunDefaultBashCf(`cp -rf '%s'/. '%s'`, src, dst)
}
dstDir := filepath.Join(dst, newName)
return cmd.RunDefaultBashCf(`cp -rf '%s' '%s'`, src, dstDir)
}
Expand Down
Loading
Loading