Skip to content

Commit

Permalink
extract fileshares function
Browse files Browse the repository at this point in the history
Signed-off-by: Joana Hrotko <[email protected]>
  • Loading branch information
jhrotko committed Oct 25, 2024
1 parent 5b8a8b0 commit c430b99
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,58 +198,59 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
}
}

err := func() error {
if s.manageDesktopFileSharesEnabled(ctx) {
// collect all the bind mount paths and try to set up file shares in
// Docker Desktop for them
var paths []string
for _, svcName := range project.ServiceNames() {
svc := project.Services[svcName]
for _, vol := range svc.Volumes {
if vol.Type != string(mount.TypeBind) {
continue
}
p := filepath.Clean(vol.Source)
if !filepath.IsAbs(p) {
return fmt.Errorf("file share path is not absolute: %s", p)
}
if fi, err := os.Stat(p); errors.Is(err, fs.ErrNotExist) {
// actual directory will be implicitly created when the
// file share is initialized if it doesn't exist, so
// need to filter out any that should not be auto-created
if vol.Bind != nil && !vol.Bind.CreateHostPath {
logrus.Debugf("Skipping creating file share for %q: does not exist and `create_host_path` is false", p)
continue
}
} else if err != nil {
// if we can't read the path, we won't be able to make
// a file share for it
logrus.Debugf("Skipping creating file share for %q: %v", p, err)
continue
} else if !fi.IsDir() {
// ignore files & special types (e.g. Unix sockets)
logrus.Debugf("Skipping creating file share for %q: not a directory", p)
err := s.configureFileShares(ctx, project)
if err != nil {
progress.ContextWriter(ctx).TailMsgf("Failed to prepare Synchronized file shares: %v", err)
}
return nil
}

func (s *composeService) configureFileShares(ctx context.Context, project *types.Project) error {
if s.manageDesktopFileSharesEnabled(ctx) {
// collect all the bind mount paths and try to set up file shares in
// Docker Desktop for them
var paths []string
for _, svcName := range project.ServiceNames() {
svc := project.Services[svcName]
for _, vol := range svc.Volumes {
if vol.Type != string(mount.TypeBind) {
continue
}
p := filepath.Clean(vol.Source)
if !filepath.IsAbs(p) {
return fmt.Errorf("file share path is not absolute: %s", p)
}
if fi, err := os.Stat(p); errors.Is(err, fs.ErrNotExist) {
// actual directory will be implicitly created when the
// file share is initialized if it doesn't exist, so
// need to filter out any that should not be auto-created
if vol.Bind != nil && !vol.Bind.CreateHostPath {
logrus.Debugf("Skipping creating file share for %q: does not exist and `create_host_path` is false", p)
continue
}

paths = append(paths, p)
} else if err != nil {
// if we can't read the path, we won't be able to make
// a file share for it
logrus.Debugf("Skipping creating file share for %q: %v", p, err)
continue
} else if !fi.IsDir() {
// ignore files & special types (e.g. Unix sockets)
logrus.Debugf("Skipping creating file share for %q: not a directory", p)
continue
}
}

// remove duplicate/unnecessary child paths and sort them for predictability
paths = pathutil.EncompassingPaths(paths)
sort.Strings(paths)

fileShareManager := desktop.NewFileShareManager(s.desktopCli, project.Name, paths)
if err := fileShareManager.EnsureExists(ctx); err != nil {
return fmt.Errorf("initializing file shares: %w", err)
paths = append(paths, p)
}
}
return nil
}()

if err != nil {
progress.ContextWriter(ctx).TailMsgf("Failed to prepare Synchronized file shares: %v", err)
// remove duplicate/unnecessary child paths and sort them for predictability
paths = pathutil.EncompassingPaths(paths)
sort.Strings(paths)

fileShareManager := desktop.NewFileShareManager(s.desktopCli, project.Name, paths)
if err := fileShareManager.EnsureExists(ctx); err != nil {
return fmt.Errorf("initializing file shares: %w", err)
}
}
return nil
}
Expand All @@ -267,7 +268,7 @@ func (s *composeService) recreateVolume(ctx context.Context, project *types.Proj
err := s.Remove(ctx, project.Name, api.RemoveOptions{
Project: project,
Services: selectedService,
Volumes: true, // this does not work in the apiClient???
Volumes: true, // this does not work in the apiClient (?)
Force: true,
Stop: true,
})
Expand Down

0 comments on commit c430b99

Please sign in to comment.