github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/worker/volume.go (about) 1 package worker 2 3 import ( 4 "context" 5 "github.com/pf-qiu/concourse/v6/tracing" 6 "io" 7 8 "code.cloudfoundry.org/lager" 9 "github.com/concourse/baggageclaim" 10 "github.com/pf-qiu/concourse/v6/atc/db" 11 ) 12 13 //go:generate counterfeiter . Volume 14 15 type Volume interface { 16 Handle() string 17 Path() string 18 19 SetProperty(key string, value string) error 20 Properties() (baggageclaim.VolumeProperties, error) 21 22 SetPrivileged(bool) error 23 24 StreamIn(ctx context.Context, path string, encoding baggageclaim.Encoding, tarStream io.Reader) error 25 StreamOut(ctx context.Context, path string, encoding baggageclaim.Encoding) (io.ReadCloser, error) 26 27 GetStreamInP2pUrl(ctx context.Context, path string) (string, error) 28 StreamP2pOut(ctx context.Context, path string, destUrl string, encoding baggageclaim.Encoding) error 29 30 COWStrategy() baggageclaim.COWStrategy 31 32 InitializeResourceCache(db.UsedResourceCache) error 33 GetResourceCacheID() int 34 InitializeTaskCache(logger lager.Logger, jobID int, stepName string, path string, privileged bool) error 35 InitializeArtifact(name string, buildID int) (db.WorkerArtifact, error) 36 37 CreateChildForContainer(db.CreatingContainer, string) (db.CreatingVolume, error) 38 39 WorkerName() string 40 Destroy() error 41 } 42 43 type VolumeMount struct { 44 Volume Volume 45 MountPath string 46 } 47 48 type volume struct { 49 bcVolume baggageclaim.Volume 50 dbVolume db.CreatedVolume 51 volumeClient VolumeClient 52 } 53 54 type byMountPath []VolumeMount 55 56 func (p byMountPath) Len() int { 57 return len(p) 58 } 59 func (p byMountPath) Swap(i, j int) { 60 p[i], p[j] = p[j], p[i] 61 } 62 func (p byMountPath) Less(i, j int) bool { 63 path1 := p[i].MountPath 64 path2 := p[j].MountPath 65 return path1 < path2 66 } 67 68 func NewVolume( 69 bcVolume baggageclaim.Volume, 70 dbVolume db.CreatedVolume, 71 volumeClient VolumeClient, 72 ) Volume { 73 return &volume{ 74 bcVolume: bcVolume, 75 dbVolume: dbVolume, 76 volumeClient: volumeClient, 77 } 78 } 79 80 func (v *volume) Handle() string { return v.bcVolume.Handle() } 81 82 func (v *volume) Path() string { return v.bcVolume.Path() } 83 84 func (v *volume) SetProperty(key string, value string) error { 85 return v.bcVolume.SetProperty(key, value) 86 } 87 88 func (v *volume) SetPrivileged(privileged bool) error { 89 return v.bcVolume.SetPrivileged(privileged) 90 } 91 92 func (v *volume) StreamIn(ctx context.Context, path string, encoding baggageclaim.Encoding, tarStream io.Reader) error { 93 _, span := tracing.StartSpan(ctx, "volume.StreamIn", tracing.Attrs{ 94 "destination-volume": v.Handle(), 95 "destination-worker": v.WorkerName(), 96 }) 97 98 err := v.bcVolume.StreamIn(ctx, path, encoding, tarStream) 99 tracing.End(span, err) 100 101 return err 102 } 103 104 func (v *volume) StreamOut(ctx context.Context, path string, encoding baggageclaim.Encoding) (io.ReadCloser, error) { 105 return v.bcVolume.StreamOut(ctx, path, encoding) 106 } 107 108 func (v *volume) GetStreamInP2pUrl(ctx context.Context, path string) (string, error) { 109 return v.bcVolume.GetStreamInP2pUrl(ctx, path) 110 } 111 112 func (v *volume) StreamP2pOut(ctx context.Context, path string, destUrl string, encoding baggageclaim.Encoding) error { 113 return v.bcVolume.StreamP2pOut(ctx, path, destUrl, encoding) 114 } 115 116 func (v *volume) Properties() (baggageclaim.VolumeProperties, error) { 117 return v.bcVolume.Properties() 118 } 119 120 func (v *volume) WorkerName() string { 121 return v.dbVolume.WorkerName() 122 } 123 124 func (v *volume) Destroy() error { 125 return v.bcVolume.Destroy() 126 } 127 128 func (v *volume) COWStrategy() baggageclaim.COWStrategy { 129 return baggageclaim.COWStrategy{ 130 Parent: v.bcVolume, 131 } 132 } 133 134 func (v *volume) InitializeResourceCache(urc db.UsedResourceCache) error { 135 return v.dbVolume.InitializeResourceCache(urc) 136 } 137 138 func (v *volume) GetResourceCacheID() int { 139 return v.dbVolume.GetResourceCacheID() 140 } 141 142 func (v *volume) InitializeArtifact(name string, buildID int) (db.WorkerArtifact, error) { 143 return v.dbVolume.InitializeArtifact(name, buildID) 144 } 145 146 func (v *volume) InitializeTaskCache( 147 logger lager.Logger, 148 jobID int, 149 stepName string, 150 path string, 151 privileged bool, 152 ) error { 153 if v.dbVolume.ParentHandle() == "" { 154 return v.dbVolume.InitializeTaskCache(jobID, stepName, path) 155 } 156 157 logger.Debug("creating-an-import-volume", lager.Data{"path": v.bcVolume.Path()}) 158 159 // always create, if there are any existing task cache volumes they will be gced 160 // after initialization of the current one 161 importVolume, err := v.volumeClient.CreateVolumeForTaskCache( 162 logger, 163 VolumeSpec{ 164 Strategy: baggageclaim.ImportStrategy{Path: v.bcVolume.Path()}, 165 Privileged: privileged, 166 }, 167 v.dbVolume.TeamID(), 168 jobID, 169 stepName, 170 path, 171 ) 172 if err != nil { 173 return err 174 } 175 176 return importVolume.InitializeTaskCache(logger, jobID, stepName, path, privileged) 177 } 178 179 func (v *volume) CreateChildForContainer(creatingContainer db.CreatingContainer, mountPath string) (db.CreatingVolume, error) { 180 return v.dbVolume.CreateChildForContainer(creatingContainer, mountPath) 181 }