-
Notifications
You must be signed in to change notification settings - Fork 124
/
pull.go
279 lines (249 loc) · 8.64 KB
/
pull.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2018-2021, 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 cli
import (
"os"
"path/filepath"
"runtime"
"github.com/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/pkg/cache"
"github.com/apptainer/apptainer/internal/pkg/client/library"
"github.com/apptainer/apptainer/internal/pkg/client/net"
"github.com/apptainer/apptainer/internal/pkg/client/oci"
"github.com/apptainer/apptainer/internal/pkg/client/oras"
"github.com/apptainer/apptainer/internal/pkg/client/shub"
"github.com/apptainer/apptainer/internal/pkg/remote/endpoint"
"github.com/apptainer/apptainer/internal/pkg/util/uri"
"github.com/apptainer/apptainer/pkg/cmdline"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/spf13/cobra"
)
const (
// LibraryProtocol holds the library base URI.
LibraryProtocol = "library"
// ShubProtocol holds the singularity hub base URI.
ShubProtocol = "shub"
// HTTPProtocol holds the remote http base URI.
HTTPProtocol = "http"
// HTTPSProtocol holds the remote https base URI.
HTTPSProtocol = "https"
// OrasProtocol holds the oras URI.
OrasProtocol = "oras"
)
var (
// pullLibraryURI holds the base URI to a Sylabs library API instance.
pullLibraryURI string
// pullImageName holds the name to be given to the pulled image.
pullImageName string
// unauthenticatedPull when true; won't ask to keep a unsigned container after pulling it.
unauthenticatedPull bool
// pullDir is the path that the containers will be pulled to, if set.
pullDir string
// pullArch is the architecture for which containers will be pulled from the
// SCS library.
pullArch string
)
// --arch
var pullArchFlag = cmdline.Flag{
ID: "pullArchFlag",
Value: &pullArch,
DefaultValue: runtime.GOARCH,
Name: "arch",
Usage: "architecture to pull from library",
EnvKeys: []string{"PULL_ARCH"},
}
// --library
var pullLibraryURIFlag = cmdline.Flag{
ID: "pullLibraryURIFlag",
Value: &pullLibraryURI,
DefaultValue: "",
Name: "library",
Usage: "download images from the provided library",
EnvKeys: []string{"LIBRARY"},
}
// --name
var pullNameFlag = cmdline.Flag{
ID: "pullNameFlag",
Value: &pullImageName,
DefaultValue: "",
Name: "name",
Hidden: true,
Usage: "specify a custom image name",
EnvKeys: []string{"PULL_NAME"},
}
// --dir
var pullDirFlag = cmdline.Flag{
ID: "pullDirFlag",
Value: &pullDir,
DefaultValue: "",
Name: "dir",
Usage: "download images to the specific directory",
EnvKeys: []string{"PULLDIR", "PULLFOLDER"},
}
// --disable-cache
var pullDisableCacheFlag = cmdline.Flag{
ID: "pullDisableCacheFlag",
Value: &disableCache,
DefaultValue: false,
Name: "disable-cache",
Usage: "do not use or create cached images/blobs",
EnvKeys: []string{"DISABLE_CACHE"},
}
// -U|--allow-unsigned
var pullAllowUnsignedFlag = cmdline.Flag{
ID: "pullAllowUnauthenticatedFlag",
Value: &unauthenticatedPull,
DefaultValue: false,
Name: "allow-unsigned",
ShortHand: "U",
Usage: "do not require a signed container",
EnvKeys: []string{"ALLOW_UNSIGNED"},
Deprecated: `pull no longer exits with an error code in case of unsigned image. Now the flag only suppress warning message.`,
}
// --allow-unauthenticated
var pullAllowUnauthenticatedFlag = cmdline.Flag{
ID: "pullAllowUnauthenticatedFlag",
Value: &unauthenticatedPull,
DefaultValue: false,
Name: "allow-unauthenticated",
ShortHand: "",
Usage: "do not require a signed container",
EnvKeys: []string{"ALLOW_UNAUTHENTICATED"},
Hidden: true,
}
func init() {
addCmdInit(func(cmdManager *cmdline.CommandManager) {
cmdManager.RegisterCmd(PullCmd)
cmdManager.RegisterFlagForCmd(&commonForceFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&pullLibraryURIFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&pullNameFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&commonNoHTTPSFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&commonTmpDirFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&pullDisableCacheFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&pullDirFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&dockerUsernameFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&dockerPasswordFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&dockerLoginFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&buildNoCleanupFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&pullAllowUnsignedFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&pullAllowUnauthenticatedFlag, PullCmd)
cmdManager.RegisterFlagForCmd(&pullArchFlag, PullCmd)
})
}
// PullCmd apptainer pull
var PullCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Args: cobra.RangeArgs(1, 2),
Run: pullRun,
Use: docs.PullUse,
Short: docs.PullShort,
Long: docs.PullLong,
Example: docs.PullExample,
}
func pullRun(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
imgCache := getCacheHandle(cache.Config{Disable: disableCache})
if imgCache == nil {
sylog.Fatalf("Failed to create an image cache handle")
}
pullFrom := args[len(args)-1]
transport, ref := uri.Split(pullFrom)
if ref == "" {
sylog.Fatalf("Bad URI %s", pullFrom)
}
pullTo := pullImageName
if pullTo == "" {
pullTo = args[0]
if len(args) == 1 {
if transport == "" {
sylog.Fatalf("No transport type URI supplied")
}
pullTo = uri.GetName(pullFrom) // TODO: If not library/shub & no name specified, simply put to cache
}
}
if pullDir != "" {
pullTo = filepath.Join(pullDir, pullTo)
}
_, err := os.Stat(pullTo)
if !os.IsNotExist(err) {
// image already exists
if !forceOverwrite {
sylog.Fatalf("Image file already exists: %q - will not overwrite", pullTo)
}
}
switch transport {
case LibraryProtocol:
ref, err := library.NormalizeLibraryRef(pullFrom)
if err != nil {
sylog.Fatalf("Malformed library reference: %v", err)
}
if pullLibraryURI != "" && ref.Host != "" {
sylog.Fatalf("Conflicting arguments; do not use --library with a library URI containing host name")
}
var libraryURI string
if pullLibraryURI != "" {
libraryURI = pullLibraryURI
} else if ref.Host != "" {
// override libraryURI if ref contains host name
if noHTTPS {
libraryURI = "http://" + ref.Host
} else {
libraryURI = "https://" + ref.Host
}
}
lc, err := getLibraryClientConfig(libraryURI)
if err != nil {
sylog.Fatalf("Unable to get library client configuration: %v", err)
}
co, err := getKeyserverClientOpts("", endpoint.KeyserverVerifyOp)
if err != nil {
sylog.Fatalf("Unable to get keyserver client configuration: %v", err)
}
_, err = library.PullToFile(ctx, imgCache, pullTo, ref, pullArch, tmpDir, lc, co)
if err != nil && err != library.ErrLibraryPullUnsigned {
sylog.Fatalf("While pulling library image: %v", err)
}
if err == library.ErrLibraryPullUnsigned {
sylog.Warningf("Skipping container verification")
}
case ShubProtocol:
_, err := shub.PullToFile(ctx, imgCache, pullTo, pullFrom, tmpDir, noHTTPS)
if err != nil {
sylog.Fatalf("While pulling shub image: %v\n", err)
}
case OrasProtocol:
ociAuth, err := makeDockerCredentials(cmd)
if err != nil {
sylog.Fatalf("Unable to make docker oci credentials: %s", err)
}
_, err = oras.PullToFile(ctx, imgCache, pullTo, pullFrom, tmpDir, ociAuth, noHTTPS)
if err != nil {
sylog.Fatalf("While pulling image from oci registry: %v", err)
}
case HTTPProtocol, HTTPSProtocol:
_, err := net.PullToFile(ctx, imgCache, pullTo, pullFrom, tmpDir)
if err != nil {
sylog.Fatalf("While pulling from image from http(s): %v\n", err)
}
case oci.IsSupported(transport):
ociAuth, err := makeDockerCredentials(cmd)
if err != nil {
sylog.Fatalf("While creating Docker credentials: %v", err)
}
_, err = oci.PullToFile(ctx, imgCache, pullTo, pullFrom, tmpDir, ociAuth, noHTTPS, buildArgs.noCleanUp)
if err != nil {
sylog.Fatalf("While making image from oci registry: %v", err)
}
case "":
sylog.Fatalf("No transport type URI supplied")
default:
sylog.Fatalf("Unsupported transport type: %s", transport)
}
}