clientsession.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. sharedKey string
  37. uuid string
  38. }
  39. // NewClientSessionSourceIdentifier returns new ClientSessionSourceIdentifier instance
  40. func NewClientSessionSourceIdentifier(ctx context.Context, sg SessionGetter, uuid string) (*ClientSessionSourceIdentifier, error) {
  41. csi := &ClientSessionSourceIdentifier{
  42. uuid: uuid,
  43. }
  44. caller, err := sg.Get(ctx, uuid)
  45. if err != nil {
  46. return nil, errors.Wrapf(err, "failed to get session for %s", uuid)
  47. }
  48. csi.caller = caller
  49. return csi, nil
  50. }
  51. // Transport returns transport identifier for remote identifier
  52. func (csi *ClientSessionSourceIdentifier) Transport() string {
  53. return remotecontext.ClientSessionRemote
  54. }
  55. // SharedKey returns shared key for remote identifier. Shared key is used
  56. // for finding the base for a repeated transfer.
  57. func (csi *ClientSessionSourceIdentifier) SharedKey() string {
  58. return csi.caller.SharedKey()
  59. }
  60. // Key returns unique key for remote identifier. Requests with same key return
  61. // same data.
  62. func (csi *ClientSessionSourceIdentifier) Key() string {
  63. return csi.uuid
  64. }