diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 44b88ac00..8232b3f28 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -492,23 +492,6 @@ func upgradeInstall(installId uint, detailId uint, backup bool) error { detailDir = path.Join(constant.ResourceDir, "apps", "local", strings.TrimPrefix(install.App.Key, "local"), detail.Version) } - images, err := composeV2.GetDockerComposeImages([]byte(detail.DockerCompose)) - if err != nil { - upErr = err - return - } - dockerCli, err := composeV2.NewClient() - if err != nil { - upErr = err - return - } - for _, image := range images { - if err = dockerCli.PullImage(image, true); err != nil { - upErr = buserr.WithNameAndErr("ErrDockerPullImage", "", err) - return - } - } - command := exec.Command("/bin/bash", "-c", fmt.Sprintf("cp -rn %s/* %s || true", detailDir, install.GetPath())) stdout, _ := command.CombinedOutput() if stdout != nil { @@ -592,6 +575,29 @@ func upgradeInstall(installId uint, detailId uint, backup bool) error { return } + content, err := fileOp.GetContent(install.GetEnvPath()) + if err != nil { + upErr = err + return + } + + images, err := composeV2.GetDockerComposeImages(install.Name, content, []byte(detail.DockerCompose)) + if err != nil { + upErr = err + return + } + dockerCli, err := composeV2.NewClient() + if err != nil { + upErr = err + return + } + for _, image := range images { + if err = dockerCli.PullImage(image, true); err != nil { + upErr = buserr.WithNameAndErr("ErrDockerPullImage", "", err) + return + } + } + if upErr = fileOp.WriteFile(install.GetComposePath(), strings.NewReader(install.DockerCompose), 0775); upErr != nil { return } diff --git a/backend/utils/docker/compose.go b/backend/utils/docker/compose.go index e0f859730..c6e804190 100644 --- a/backend/utils/docker/compose.go +++ b/backend/utils/docker/compose.go @@ -6,7 +6,6 @@ import ( "github.com/compose-spec/compose-go/types" "github.com/docker/compose/v2/pkg/api" "github.com/joho/godotenv" - "gopkg.in/yaml.v3" "path" "regexp" "strings" @@ -116,15 +115,32 @@ type Service struct { Image string `yaml:"image"` } -func GetDockerComposeImages(data []byte) ([]string, error) { - var dc ComposeProject - err := yaml.Unmarshal(data, &dc) +func GetDockerComposeImages(projectName string, env, yml []byte) ([]string, error) { + var ( + configFiles []types.ConfigFile + images []string + ) + configFiles = append(configFiles, types.ConfigFile{ + Filename: "docker-compose.yml", + Content: yml}, + ) + envMap, err := godotenv.UnmarshalBytes(env) if err != nil { return nil, err } + details := types.ConfigDetails{ + ConfigFiles: configFiles, + Environment: envMap, + } - var images []string - for _, service := range dc.Services { + project, err := loader.LoadWithContext(context.Background(), details, func(options *loader.Options) { + options.SetProjectName(projectName, true) + options.ResolvePaths = true + }) + if err != nil { + return nil, err + } + for _, service := range project.AllServices() { images = append(images, service.Image) } return images, nil