1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
// Copyright (c) 2020-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package oras
import (
"context"
"fmt"
"os"
ocitypes "github.com/containers/image/v5/types"
"github.com/sylabs/singularity/v4/internal/pkg/cache"
"github.com/sylabs/singularity/v4/internal/pkg/client/progress"
"github.com/sylabs/singularity/v4/internal/pkg/util/fs"
"github.com/sylabs/singularity/v4/pkg/sylog"
"golang.org/x/term"
)
// pull will pull an oras image into the cache if directTo="", or a specific file if directTo is set.
func pull(ctx context.Context, imgCache *cache.Handle, directTo, pullFrom string, ociAuth *ocitypes.DockerAuthConfig) (imagePath string, err error) {
hash, err := RefHash(ctx, pullFrom, ociAuth)
if err != nil {
return "", fmt.Errorf("failed to get checksum for %s: %s", pullFrom, err)
}
var pb *progress.DownloadBar
if term.IsTerminal(2) {
pb = &progress.DownloadBar{}
}
if directTo != "" {
sylog.Infof("Downloading oras image")
if err := DownloadImage(ctx, directTo, pullFrom, ociAuth, pb); err != nil {
return "", fmt.Errorf("unable to Download Image: %v", err)
}
imagePath = directTo
} else {
cacheEntry, err := imgCache.GetEntry(cache.OrasCacheType, hash.String())
if err != nil {
return "", fmt.Errorf("unable to check if %v exists in cache: %v", hash, err)
}
defer cacheEntry.CleanTmp()
if !cacheEntry.Exists {
sylog.Infof("Downloading oras image")
if err := DownloadImage(ctx, cacheEntry.TmpPath, pullFrom, ociAuth, pb); err != nil {
return "", fmt.Errorf("unable to Download Image: %v", err)
}
if cacheFileHash, err := ImageHash(cacheEntry.TmpPath); err != nil {
return "", fmt.Errorf("error getting ImageHash: %v", err)
} else if cacheFileHash != hash {
return "", fmt.Errorf("cached file hash(%s) and expected hash(%s) does not match", cacheFileHash, hash)
}
err = cacheEntry.Finalize()
if err != nil {
return "", err
}
} else {
sylog.Infof("Using cached SIF image")
}
imagePath = cacheEntry.Path
}
return imagePath, nil
}
// Pull will pull an oras image to the cache or direct to a temporary file if cache is disabled
func Pull(ctx context.Context, imgCache *cache.Handle, pullFrom, tmpDir string, ociAuth *ocitypes.DockerAuthConfig) (imagePath string, err error) {
directTo := ""
if imgCache.IsDisabled() {
file, err := os.CreateTemp(tmpDir, "sbuild-tmp-cache-")
if err != nil {
return "", fmt.Errorf("unable to create tmp file: %v", err)
}
directTo = file.Name()
sylog.Infof("Downloading oras image to tmp cache: %s", directTo)
}
return pull(ctx, imgCache, directTo, pullFrom, ociAuth)
}
// PullToFile will pull an oras image to the specified location, through the cache, or directly if cache is disabled
func PullToFile(ctx context.Context, imgCache *cache.Handle, pullTo, pullFrom string, ociAuth *ocitypes.DockerAuthConfig) (imagePath string, err error) {
directTo := ""
if imgCache.IsDisabled() {
directTo = pullTo
sylog.Debugf("Cache disabled, pulling directly to: %s", directTo)
}
src, err := pull(ctx, imgCache, directTo, pullFrom, ociAuth)
if err != nil {
return "", fmt.Errorf("error fetching image to cache: %v", err)
}
if directTo == "" {
// mode is before umask if pullTo doesn't exist
err = fs.CopyFileAtomic(src, pullTo, 0o777)
if err != nil {
return "", fmt.Errorf("error copying image out of cache: %v", err)
}
}
return pullTo, nil
}
|