github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/builder/dockerfile/clientsession.go (about)

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