podman

Форк
0
100 строк · 3.8 Кб
1
package buildah
2

3
import (
4
	"context"
5
	"fmt"
6
	"io"
7
	"time"
8

9
	"github.com/containers/buildah/define"
10
	"github.com/containers/common/libimage"
11
	"github.com/containers/common/pkg/config"
12
	"github.com/containers/image/v5/types"
13
	encconfig "github.com/containers/ocicrypt/config"
14
	"github.com/containers/storage"
15
)
16

17
// PullOptions can be used to alter how an image is copied in from somewhere.
18
type PullOptions struct {
19
	// SignaturePolicyPath specifies an override location for the signature
20
	// policy which should be used for verifying the new image as it is
21
	// being written.  Except in specific circumstances, no value should be
22
	// specified, indicating that the shared, system-wide default policy
23
	// should be used.
24
	SignaturePolicyPath string
25
	// ReportWriter is an io.Writer which will be used to log the writing
26
	// of the new image.
27
	ReportWriter io.Writer
28
	// Store is the local storage store which holds the source image.
29
	Store storage.Store
30
	// github.com/containers/image/types SystemContext to hold credentials
31
	// and other authentication/authorization information.
32
	SystemContext *types.SystemContext
33
	// BlobDirectory is the name of a directory in which we'll attempt to
34
	// store copies of layer blobs that we pull down, if any.  It should
35
	// already exist.
36
	BlobDirectory string
37
	// AllTags is a boolean value that determines if all tagged images
38
	// will be downloaded from the repository. The default is false.
39
	AllTags bool
40
	// RemoveSignatures causes any existing signatures for the image to be
41
	// discarded when pulling it.
42
	RemoveSignatures bool
43
	// MaxRetries is the maximum number of attempts we'll make to pull any
44
	// one image from the external registry if the first attempt fails.
45
	MaxRetries int
46
	// RetryDelay is how long to wait before retrying a pull attempt.
47
	RetryDelay time.Duration
48
	// OciDecryptConfig contains the config that can be used to decrypt an image if it is
49
	// encrypted if non-nil. If nil, it does not attempt to decrypt an image.
50
	OciDecryptConfig *encconfig.DecryptConfig
51
	// PullPolicy takes the value PullIfMissing, PullAlways, PullIfNewer, or PullNever.
52
	PullPolicy define.PullPolicy
53
}
54

55
// Pull copies the contents of the image from somewhere else to local storage.  Returns the
56
// ID of the local image or an error.
57
func Pull(ctx context.Context, imageName string, options PullOptions) (imageID string, err error) {
58
	libimageOptions := &libimage.PullOptions{}
59
	libimageOptions.SignaturePolicyPath = options.SignaturePolicyPath
60
	libimageOptions.Writer = options.ReportWriter
61
	libimageOptions.RemoveSignatures = options.RemoveSignatures
62
	libimageOptions.OciDecryptConfig = options.OciDecryptConfig
63
	libimageOptions.AllTags = options.AllTags
64
	libimageOptions.RetryDelay = &options.RetryDelay
65
	libimageOptions.DestinationLookupReferenceFunc = cacheLookupReferenceFunc(options.BlobDirectory, types.PreserveOriginal)
66

67
	if options.MaxRetries > 0 {
68
		retries := uint(options.MaxRetries)
69
		libimageOptions.MaxRetries = &retries
70
	}
71

72
	pullPolicy, err := config.ParsePullPolicy(options.PullPolicy.String())
73
	if err != nil {
74
		return "", err
75
	}
76

77
	// Note: It is important to do this before we pull any images/create containers.
78
	// The default backend detection logic needs an empty store to correctly detect
79
	// that we can use netavark, if the store was not empty it will use CNI to not break existing installs.
80
	_, err = getNetworkInterface(options.Store, "", "")
81
	if err != nil {
82
		return "", err
83
	}
84

85
	runtime, err := libimage.RuntimeFromStore(options.Store, &libimage.RuntimeOptions{SystemContext: options.SystemContext})
86
	if err != nil {
87
		return "", err
88
	}
89

90
	pulledImages, err := runtime.Pull(context.Background(), imageName, pullPolicy, libimageOptions)
91
	if err != nil {
92
		return "", err
93
	}
94

95
	if len(pulledImages) == 0 {
96
		return "", fmt.Errorf("internal error pulling %s: no image pulled and no error", imageName)
97
	}
98

99
	return pulledImages[0].ID(), nil
100
}
101

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.