@@ -2,6 +2,7 @@ package service
22
33import (
44 "bufio"
5+ "context"
56 "errors"
67 "fmt"
78 "os"
@@ -24,7 +25,6 @@ import (
2425 "github.com/1Panel-dev/1Panel/agent/utils/docker"
2526 "github.com/docker/docker/api/types/container"
2627 "github.com/docker/docker/api/types/filters"
27- "golang.org/x/net/context"
2828)
2929
3030const composeProjectLabel = "com.docker.compose.project"
@@ -233,14 +233,15 @@ func (u *ContainerService) ComposeOperation(req dto.ComposeOperation) error {
233233 return err
234234 }
235235 if req .WithFile {
236- _ = os .RemoveAll (path .Dir (req .Path ))
236+ for _ , item := range strings .Split (req .Path , "," ) {
237+ if len (item ) != 0 {
238+ _ = os .RemoveAll (path .Dir (item ))
239+ }
240+ }
237241 }
238242 _ = composeRepo .DeleteRecord (repo .WithByName (req .Name ))
239243 return nil
240244 }
241- if _ , err := os .Stat (req .Path ); err != nil {
242- return fmt .Errorf ("load file with path %s failed, %v" , req .Path , err )
243- }
244245 if req .Operation == "up" {
245246 if stdout , err := compose .Up (req .Path ); err != nil {
246247 return fmt .Errorf ("docker-compose up failed, std: %s, err: %v" , stdout , err )
@@ -257,11 +258,11 @@ func (u *ContainerService) ComposeUpdate(req dto.ComposeUpdate) error {
257258 if cmd .CheckIllegal (req .Name , req .Path ) {
258259 return buserr .New ("ErrCmdIllegal" )
259260 }
260- oldFile , err := os .ReadFile (req .Path )
261+ oldFile , err := os .ReadFile (req .DetailPath )
261262 if err != nil {
262- return fmt .Errorf ("load file with path %s failed, %v" , req .Path , err )
263+ return fmt .Errorf ("load file with path %s failed, %v" , req .DetailPath , err )
263264 }
264- file , err := os .OpenFile (req .Path , os .O_WRONLY | os .O_TRUNC , 0640 )
265+ file , err := os .OpenFile (req .DetailPath , os .O_WRONLY | os .O_TRUNC , 0640 )
265266 if err != nil {
266267 return err
267268 }
@@ -270,8 +271,8 @@ func (u *ContainerService) ComposeUpdate(req dto.ComposeUpdate) error {
270271 _ , _ = write .WriteString (req .Content )
271272 write .Flush ()
272273
273- global .LOG .Infof ("docker-compose.yml %s has been replaced, now start to docker-compose restart" , req .Path )
274- if err := newComposeEnv (req .Path , req .Env ); err != nil {
274+ global .LOG .Infof ("docker-compose.yml %s has been replaced, now start to docker-compose restart" , req .DetailPath )
275+ if err := newComposeEnv (req .DetailPath , req .Env ); err != nil {
275276 return err
276277 }
277278
@@ -361,11 +362,8 @@ func (u *ContainerService) loadPath(req *dto.ComposeCreate) error {
361362}
362363
363364func removeContainerForCompose (composeName , composePath string ) error {
364- if _ , err := os .Stat (composePath ); err == nil {
365- if stdout , err := compose .Operate (composePath , "down" ); err != nil {
366- return errors .New (stdout )
367- }
368- return nil
365+ if stdout , err := compose .Operate (composePath , "down" ); err != nil {
366+ return errors .New (stdout )
369367 }
370368 var options container.ListOptions
371369 options .All = true
@@ -405,7 +403,11 @@ func recreateCompose(content, path string) error {
405403
406404func loadEnv (list []dto.ComposeInfo ) []dto.ComposeInfo {
407405 for i := 0 ; i < len (list ); i ++ {
408- envFilePath := path .Join (path .Dir (list [i ].Path ), ".env" )
406+ tmpPath := list [i ].Path
407+ if strings .Contains (list [i ].Path , "," ) {
408+ tmpPath = strings .Split (list [i ].Path , "," )[0 ]
409+ }
410+ envFilePath := path .Join (path .Dir (tmpPath ), ".env" )
409411 file , err := os .ReadFile (envFilePath )
410412 if err != nil {
411413 continue
0 commit comments