Skip to content

Commit

Permalink
watch: remove requirements for tar binary and for sync target to be rw
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Jan 9, 2024
1 parent f659918 commit 575f2ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
24 changes: 5 additions & 19 deletions internal/sync/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type LowLevelClient interface {
ContainersForService(ctx context.Context, projectName string, serviceName string) ([]moby.Container, error)

Exec(ctx context.Context, containerID string, cmd []string, in io.Reader) error
Untar(ctx context.Context, id string, reader io.ReadCloser) error
}

type Tar struct {
Expand Down Expand Up @@ -84,39 +85,24 @@ func (t *Tar) Sync(ctx context.Context, service types.ServiceConfig, paths []Pat
if len(pathsToDelete) != 0 {
deleteCmd = append([]string{"rm", "-rf"}, pathsToDelete...)
}
copyCmd := []string{"tar", "-v", "-C", "/", "-x", "-f", "-"}

var eg multierror.Group
writers := make([]*io.PipeWriter, len(containers))
for i := range containers {
containerID := containers[i].ID
r, w := io.Pipe()
writers[i] = w
tarReader := tarArchive(pathsToCopy)

Check warning on line 92 in internal/sync/tar.go

View check run for this annotation

Codecov / codecov/patch

internal/sync/tar.go#L91-L92

Added lines #L91 - L92 were not covered by tests
eg.Go(func() error {
if len(deleteCmd) != 0 {
if err := t.client.Exec(ctx, containerID, deleteCmd, nil); err != nil {
return fmt.Errorf("deleting paths in %s: %w", containerID, err)
}
}
if err := t.client.Exec(ctx, containerID, copyCmd, r); err != nil {

if err := t.client.Untar(ctx, containerID, tarReader); err != nil {

Check warning on line 100 in internal/sync/tar.go

View check run for this annotation

Codecov / codecov/patch

internal/sync/tar.go#L100

Added line #L100 was not covered by tests
return fmt.Errorf("copying files to %s: %w", containerID, err)
}
return nil
})
}

multiWriter := newLossyMultiWriter(writers...)
tarReader := tarArchive(pathsToCopy)
defer func() {
_ = tarReader.Close()
multiWriter.Close()
}()
_, err = io.Copy(multiWriter, tarReader)
if err != nil {
return err
}
multiWriter.Close()

return eg.Wait().ErrorOrNil()
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/compose/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str
return nil
}

func (t tarDockerClient) Untar(ctx context.Context, id string, archive io.ReadCloser) error {
return t.s.apiClient().CopyToContainer(ctx, id, "/", archive, moby.CopyToContainerOptions{
CopyUIDGID: true,
})

Check warning on line 425 in pkg/compose/watch.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/watch.go#L422-L425

Added lines #L422 - L425 were not covered by tests
}

func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Project, serviceName string, build api.BuildOptions, batch []fileEvent, syncer sync.Syncer) error {
pathMappings := make([]sync.PathMapping, len(batch))
restartService := false
Expand Down

0 comments on commit 575f2ed

Please sign in to comment.