github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/build/sources/conveyorPacker_library.go (about) 1 // Copyright (c) 2018, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package sources 7 8 import ( 9 "fmt" 10 "os" 11 12 "github.com/sylabs/singularity/internal/pkg/client/cache" 13 "github.com/sylabs/singularity/internal/pkg/sylog" 14 "github.com/sylabs/singularity/internal/pkg/util/uri" 15 "github.com/sylabs/singularity/pkg/build/types" 16 client "github.com/sylabs/singularity/pkg/client/library" 17 ) 18 19 // LibraryConveyorPacker only needs to hold a packer to pack the image it pulls 20 // as well as extra information about the library it's pulling from 21 type LibraryConveyorPacker struct { 22 b *types.Bundle 23 LocalPacker 24 LibraryURL string 25 AuthToken string 26 } 27 28 // Get downloads container from Singularityhub 29 func (cp *LibraryConveyorPacker) Get(b *types.Bundle) (err error) { 30 sylog.Debugf("Getting container from Library") 31 32 cp.b = b 33 34 if err = makeBaseEnv(cp.b.Rootfs()); err != nil { 35 return fmt.Errorf("While inserting base environment: %v", err) 36 } 37 38 // check for custom library from definition 39 customLib, ok := b.Recipe.Header["library"] 40 if ok { 41 sylog.Debugf("Using custom library: %v", customLib) 42 cp.LibraryURL = customLib 43 } 44 45 sylog.Debugf("LibraryURL: %v", cp.LibraryURL) 46 sylog.Debugf("LibraryRef: %v", b.Recipe.Header["from"]) 47 48 libURI := "library://" + b.Recipe.Header["from"] 49 libraryImage, err := client.GetImage(cp.LibraryURL, cp.AuthToken, libURI) 50 if err != nil { 51 return err 52 } 53 54 imageName := uri.GetName(libURI) 55 imagePath := cache.LibraryImage(libraryImage.Hash, imageName) 56 57 if exists, err := cache.LibraryImageExists(libraryImage.Hash, imageName); err != nil { 58 return fmt.Errorf("unable to check if %v exists: %v", imagePath, err) 59 } else if !exists { 60 sylog.Infof("Downloading library image") 61 if err = client.DownloadImage(imagePath, libURI, cp.LibraryURL, true, cp.AuthToken); err != nil { 62 return fmt.Errorf("unable to Download Image: %v", err) 63 } 64 65 if cacheFileHash, err := client.ImageHash(imagePath); err != nil { 66 return fmt.Errorf("Error getting ImageHash: %v", err) 67 } else if cacheFileHash != libraryImage.Hash { 68 return fmt.Errorf("Cached File Hash(%s) and Expected Hash(%s) does not match", cacheFileHash, libraryImage.Hash) 69 } 70 } 71 72 // insert base metadata before unpacking fs 73 if err = makeBaseEnv(cp.b.Rootfs()); err != nil { 74 return fmt.Errorf("While inserting base environment: %v", err) 75 } 76 77 cp.LocalPacker, err = GetLocalPacker(imagePath, cp.b) 78 79 return err 80 } 81 82 // CleanUp removes any tmpfs owned by the conveyorPacker on the filesystem 83 func (cp *LibraryConveyorPacker) CleanUp() { 84 os.RemoveAll(cp.b.Path) 85 }