clientsession.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dockerfile
  2. import (
  3. "time"
  4. "github.com/docker/docker/builder/fscache"
  5. "github.com/docker/docker/builder/remotecontext"
  6. "github.com/moby/buildkit/session"
  7. "github.com/moby/buildkit/session/filesync"
  8. "github.com/pkg/errors"
  9. "golang.org/x/net/context"
  10. )
  11. const sessionConnectTimeout = 5 * time.Second
  12. // ClientSessionTransport is a transport for copying files from docker client
  13. // to the daemon.
  14. type ClientSessionTransport struct{}
  15. // NewClientSessionTransport returns new ClientSessionTransport instance
  16. func NewClientSessionTransport() *ClientSessionTransport {
  17. return &ClientSessionTransport{}
  18. }
  19. // Copy data from a remote to a destination directory.
  20. func (cst *ClientSessionTransport) Copy(ctx context.Context, id fscache.RemoteIdentifier, dest string, cu filesync.CacheUpdater) error {
  21. csi, ok := id.(*ClientSessionSourceIdentifier)
  22. if !ok {
  23. return errors.New("invalid identifier for client session")
  24. }
  25. return filesync.FSSync(ctx, csi.caller, filesync.FSSendRequestOpt{
  26. IncludePatterns: csi.includePatterns,
  27. DestDir: dest,
  28. CacheUpdater: cu,
  29. })
  30. }
  31. // ClientSessionSourceIdentifier is an identifier that can be used for requesting
  32. // files from remote client
  33. type ClientSessionSourceIdentifier struct {
  34. includePatterns []string
  35. caller session.Caller
  36. uuid string
  37. }
  38. // NewClientSessionSourceIdentifier returns new ClientSessionSourceIdentifier instance
  39. func NewClientSessionSourceIdentifier(ctx context.Context, sg SessionGetter, uuid string) (*ClientSessionSourceIdentifier, error) {
  40. csi := &ClientSessionSourceIdentifier{
  41. uuid: uuid,
  42. }
  43. caller, err := sg.Get(ctx, uuid)
  44. if err != nil {
  45. return nil, errors.Wrapf(err, "failed to get session for %s", uuid)
  46. }
  47. csi.caller = caller
  48. return csi, nil
  49. }
  50. // Transport returns transport identifier for remote identifier
  51. func (csi *ClientSessionSourceIdentifier) Transport() string {
  52. return remotecontext.ClientSessionRemote
  53. }
  54. // SharedKey returns shared key for remote identifier. Shared key is used
  55. // for finding the base for a repeated transfer.
  56. func (csi *ClientSessionSourceIdentifier) SharedKey() string {
  57. return csi.caller.SharedKey()
  58. }
  59. // Key returns unique key for remote identifier. Requests with same key return
  60. // same data.
  61. func (csi *ClientSessionSourceIdentifier) Key() string {
  62. return csi.uuid
  63. }