Skip to content

Commit ae23900

Browse files
committed
Simplify depot Track function and make setup call it.
1 parent 99c5bbb commit ae23900

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

pkg/runtime/depot.go

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -186,84 +186,78 @@ func (d *depot) Put(id strfmt.UUID) error {
186186
}
187187

188188
// DeployViaLink will take an artifact from the depot and link it to the target path.
189-
func (d *depot) DeployViaLink(id strfmt.UUID, relativeSrc, absoluteDest string) error {
189+
func (d *depot) DeployViaLink(id strfmt.UUID, relativeSrc, absoluteDest string) (*deployment, error) {
190190
d.fsMutex.Lock()
191191
defer d.fsMutex.Unlock()
192192

193193
if exists, _ := d.Exists(id); !exists {
194-
return errs.New("artifact not found in depot")
194+
return nil, errs.New("artifact not found in depot")
195195
}
196196

197197
if err := d.validateVolume(absoluteDest); err != nil {
198-
return errs.Wrap(err, "volume validation failed")
198+
return nil, errs.Wrap(err, "volume validation failed")
199199
}
200200

201201
// Collect artifact meta info
202202
var err error
203203
absoluteDest, err = fileutils.ResolvePath(absoluteDest)
204204
if err != nil {
205-
return errs.Wrap(err, "failed to resolve path")
205+
return nil, errs.Wrap(err, "failed to resolve path")
206206
}
207207

208208
if err := fileutils.MkdirUnlessExists(absoluteDest); err != nil {
209-
return errs.Wrap(err, "failed to create path")
209+
return nil, errs.Wrap(err, "failed to create path")
210210
}
211211

212212
absoluteSrc := filepath.Join(d.Path(id), relativeSrc)
213213
if !fileutils.DirExists(absoluteSrc) {
214-
return errs.New("artifact src does not exist: %s", absoluteSrc)
214+
return nil, errs.New("artifact src does not exist: %s", absoluteSrc)
215215
}
216216

217217
// Copy or link the artifact files, depending on whether the artifact in question relies on file transformations
218218
if err := smartlink.LinkContents(absoluteSrc, absoluteDest); err != nil {
219-
return errs.Wrap(err, "failed to link artifact")
219+
return nil, errs.Wrap(err, "failed to link artifact")
220220
}
221221

222222
files, err := fileutils.ListDir(absoluteSrc, false)
223223
if err != nil {
224-
return errs.Wrap(err, "failed to list files")
224+
return nil, errs.Wrap(err, "failed to list files")
225225
}
226226

227-
// Record deployment to config
228-
err = d.Track(id, &deployment{
227+
return &deployment{
229228
Type: deploymentTypeLink,
230229
Path: absoluteDest,
231230
Files: files.RelativePaths(),
232231
RelativeSrc: relativeSrc,
233-
}, nil)
234-
if err != nil {
235-
return errs.Wrap(err, "Could not record artifact use")
236-
}
237-
238-
return nil
232+
}, nil
239233
}
240234

241235
// DeployViaCopy will take an artifact from the depot and copy it to the target path.
242-
func (d *depot) DeployViaCopy(id strfmt.UUID, relativeSrc, absoluteDest string) error {
236+
func (d *depot) DeployViaCopy(id strfmt.UUID, relativeSrc, absoluteDest string) (*deployment, error) {
243237
d.fsMutex.Lock()
244238
defer d.fsMutex.Unlock()
245239

246240
if exists, _ := d.Exists(id); !exists {
247-
return errs.New("artifact not found in depot")
241+
return nil, errs.New("artifact not found in depot")
248242
}
249243

250244
var err error
251245
absoluteDest, err = fileutils.ResolvePath(absoluteDest)
252246
if err != nil {
253-
return errs.Wrap(err, "failed to resolve path")
247+
return nil, errs.Wrap(err, "failed to resolve path")
254248
}
255249

256250
if err := d.validateVolume(absoluteDest); err != nil {
257-
return errs.Wrap(err, "volume validation failed")
251+
return nil, errs.Wrap(err, "volume validation failed")
258252
}
259253

260254
if err := fileutils.MkdirUnlessExists(absoluteDest); err != nil {
261-
return errs.Wrap(err, "failed to create path")
255+
return nil, errs.Wrap(err, "failed to create path")
262256
}
263257

264258
absoluteSrc := filepath.Join(d.Path(id), relativeSrc)
265259
if !fileutils.DirExists(absoluteSrc) {
266-
return errs.New("artifact src does not exist: %s", absoluteSrc)
260+
return nil, errs.New("artifact src does not exist: %s", absoluteSrc)
267261
}
268262

269263
// Copy or link the artifact files, depending on whether the artifact in question relies on file transformations
@@ -272,38 +266,30 @@ func (d *depot) DeployViaCopy(id strfmt.UUID, relativeSrc, absoluteDest string)
272266
if errors.As(err, &errExist) {
273267
logging.Warning("Skipping files that already exist: " + errs.JoinMessage(errExist))
274268
} else {
275-
return errs.Wrap(err, "failed to copy artifact")
269+
return nil, errs.Wrap(err, "failed to copy artifact")
276270
}
277271
}
278272

279273
files, err := fileutils.ListDir(absoluteSrc, false)
280274
if err != nil {
281-
return errs.Wrap(err, "failed to list files")
275+
return nil, errs.Wrap(err, "failed to list files")
282276
}
283277

284-
// Record deployment to config
285-
err = d.Track(id, &deployment{
278+
return &deployment{
286279
Type: deploymentTypeCopy,
287280
Path: absoluteDest,
288281
Files: files.RelativePaths(),
289282
RelativeSrc: relativeSrc,
290-
}, nil)
291-
if err != nil {
292-
return errs.Wrap(err, "Could not record artifact use")
293-
}
294-
295-
return nil
283+
}, nil
296284
}
297285

298286
// Track will record an artifact deployment.
299-
// This is automatically called by `DeployVia*()` functions.
300-
// This should be called for ecosystems that handle installation of artifacts.
301-
// The artifact parameter is only necessary for tracking dynamically imported artifacts after being
302-
// added by an ecosystem.
303-
func (d *depot) Track(id strfmt.UUID, deploy *deployment, artifact *buildplan.Artifact) error {
287+
func (d *depot) Track(artifact *buildplan.Artifact, deploy *deployment) error {
304288
d.mapMutex.Lock()
305289
defer d.mapMutex.Unlock()
306290

291+
id := artifact.ArtifactID
292+
307293
// Record deployment of this artifact.
308294
if _, ok := d.config.Deployments[id]; !ok {
309295
d.config.Deployments[id] = []deployment{}
@@ -474,10 +460,12 @@ func (d *depot) Save() error {
474460
for id := range d.artifacts {
475461
if deployments, ok := d.config.Deployments[id]; !ok || len(deployments) == 0 {
476462
if _, exists := d.config.Cache[id]; !exists {
477-
err := d.Track(id, nil, nil) // create cache entry for previously used artifact
463+
// Create cache entry for previously used artifact.
464+
size, err := fileutils.GetDirSize(d.Path(id))
478465
if err != nil {
479-
return errs.Wrap(err, "Could not update depot cache with previously used artifact")
466+
return errs.Wrap(err, "Could not get artifact size on disk")
480467
}
468+
d.config.Cache[id] = &artifactInfo{Size: size, id: id}
481469
}
482470
d.config.Cache[id].InUse = false
483471
logging.Debug("Artifact '%s' is no longer in use", id.String())

pkg/runtime/setup.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,14 @@ func (s *setup) install(artifact *buildplan.Artifact) (rerr error) {
486486
return errs.Wrap(err, "Ecosystem unable to add artifact")
487487
}
488488

489-
s.depot.Track(id, &deployment{
489+
err = s.depot.Track(artifact, &deployment{
490490
Type: deploymentTypeEcosystem,
491491
Path: s.path,
492492
Files: files,
493-
}, artifact)
493+
})
494+
if err != nil {
495+
return errs.Wrap(err, "Could not track deployment")
496+
}
494497
return nil
495498
}
496499

@@ -499,8 +502,10 @@ func (s *setup) install(artifact *buildplan.Artifact) (rerr error) {
499502
return errs.Wrap(err, "Could not get env")
500503
}
501504

505+
var deploy *deployment
502506
if envDef.NeedsTransforms() || !s.supportsHardLinks || s.opts.Portable {
503-
if err := s.depot.DeployViaCopy(id, envDef.InstallDir, s.path); err != nil {
507+
deploy, err = s.depot.DeployViaCopy(id, envDef.InstallDir, s.path)
508+
if err != nil {
504509
return errs.Wrap(err, "Could not deploy artifact via copy")
505510
}
506511
if envDef.NeedsTransforms() {
@@ -509,10 +514,15 @@ func (s *setup) install(artifact *buildplan.Artifact) (rerr error) {
509514
}
510515
}
511516
} else {
512-
if err := s.depot.DeployViaLink(id, envDef.InstallDir, s.path); err != nil {
517+
deploy, err = s.depot.DeployViaLink(id, envDef.InstallDir, s.path)
518+
if err != nil {
513519
return errs.Wrap(err, "Could not deploy artifact via link")
514520
}
515521
}
522+
err = s.depot.Track(artifact, deploy)
523+
if err != nil {
524+
return errs.Wrap(err, "Could not track deployment")
525+
}
516526

517527
return nil
518528
}

0 commit comments

Comments
 (0)