fix(updater): 忽略容器路径并优先使用 Compose 配置目录
CI / verify (push) Canceled after 0s

This commit is contained in:
2026-09-28 12:01:39 +08:00
parent 63538ec5b7
commit 49d4af27ca
4 changed files with 40 additions and 3 deletions
+18 -3
View File
@@ -230,7 +230,7 @@ func (s *Server) composeProjectName(ctx context.Context) (string, error) {
}
func (s *Server) discoverHostCompose(ctx context.Context) (string, string, error) {
if s.cfg.HostProjectDir != "" {
if s.cfg.HostProjectDir != "" && !isContainerPath(s.cfg.HostProjectDir, s.cfg.ProjectDir) {
if !filepath.IsAbs(s.cfg.HostProjectDir) {
return "", "", fmt.Errorf("FONU_UPDATE_HOST_DIR 必须是绝对路径")
}
@@ -241,17 +241,32 @@ func (s *Server) discoverHostCompose(ctx context.Context) (string, string, error
hostDir := strings.TrimSpace(workingDir)
hostCompose := chooseComposeFile(configFiles, filepath.Base(s.cfg.ComposeFile))
if hostCompose == "" {
return "", "", fmt.Errorf("无法从容器标签读取 Compose 文件")
if hostDir == "" || isContainerPath(hostDir, s.cfg.ProjectDir) {
return "", "", fmt.Errorf("无法从容器标签读取宿主机 Compose 目录")
}
hostCompose = filepath.Join(hostDir, filepath.Base(s.cfg.ComposeFile))
}
if hostDir == "" {
if hostDir == "" || isContainerPath(hostDir, s.cfg.ProjectDir) {
hostDir = filepath.Dir(hostCompose)
}
if !filepath.IsAbs(hostDir) || !filepath.IsAbs(hostCompose) {
return "", "", fmt.Errorf("容器标签中的 Compose 路径不是绝对路径")
}
if isContainerPath(hostDir, s.cfg.ProjectDir) {
return "", "", fmt.Errorf("容器标签只提供了容器内路径 %s,无法定位宿主机 Compose 目录", hostDir)
}
return filepath.Clean(hostDir), filepath.Clean(hostCompose), nil
}
func isContainerPath(path, containerDir string) bool {
path = filepath.Clean(strings.TrimSpace(path))
containerDir = filepath.Clean(strings.TrimSpace(containerDir))
if path == "" || containerDir == "" || path == "." {
return false
}
return path == containerDir || strings.HasPrefix(path, containerDir+string(filepath.Separator))
}
func (s *Server) dockerLabel(ctx context.Context, label string) (string, error) {
format := fmt.Sprintf(`{{ index .Config.Labels %q }}`, label)
checkCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
+17
View File
@@ -85,3 +85,20 @@ func TestResolveComposePlanUsesHostProjectDir(t *testing.T) {
t.Fatalf("helper compose 路径不正确: %s", plan.helperComposePath)
}
}
func TestIsContainerPath(t *testing.T) {
cases := []struct {
path string
want bool
}{
{"/workspace", true},
{"/workspace/docker-compose.yml", true},
{"/vol1/1000/Docker/mafrp", false},
{"/vol1/1000/Docker/mafrp/docker-compose.yml", false},
}
for _, tc := range cases {
if got := isContainerPath(tc.path, "/workspace"); got != tc.want {
t.Fatalf("isContainerPath(%q) = %v, want %v", tc.path, got, tc.want)
}
}
}