-
Notifications
You must be signed in to change notification settings - Fork 922
Expand file tree
/
Copy pathaddcopy.go
More file actions
361 lines (333 loc) · 13.5 KB
/
Copy pathaddcopy.go
File metadata and controls
361 lines (333 loc) · 13.5 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.podman.io/buildah"
"go.podman.io/buildah/pkg/cli"
"go.podman.io/buildah/pkg/parse"
"go.podman.io/common/pkg/auth"
"go.podman.io/image/v5/types"
"go.podman.io/storage"
)
type addCopyResults struct {
addHistory bool
chmod string
chown string
checksum string
quiet bool
ignoreFile string
contextdir string
from string
blobCache string
decryptionKeys []string
removeSignatures bool
signaturePolicy string
authfile string
creds string
tlsVerify bool
certDir string
retry int
retryDelay string
excludes []string
parents bool
timestamp string
link bool
allowWildcard bool
allowEmptyWildcard bool
noFollowSymlinks bool
includes []string
}
func createCommand(addCopy string, desc string, short string, opts *addCopyResults) *cobra.Command {
return &cobra.Command{
Use: addCopy,
Short: short,
Long: desc,
RunE: func(cmd *cobra.Command, args []string) error {
return addAndCopyCmd(cmd, args, strings.ToUpper(addCopy), *opts)
},
Example: `buildah ` + addCopy + ` containerID '/myapp/app.conf'
buildah ` + addCopy + ` containerID 'app.conf' '/myapp/app.conf'
buildah ` + addCopy + ` containerID 'app.conf' 'drop-in.conf' '/myapp/app.conf.d/'`,
Args: cobra.MinimumNArgs(1),
GroupID: groupContainers,
}
}
func applyFlagVars(flags *pflag.FlagSet, opts *addCopyResults) {
flags.SetInterspersed(false)
flags.BoolVar(&opts.addHistory, "add-history", false, "add an entry for this operation to the image's history. Use BUILDAH_HISTORY environment variable to override. (default false)")
flags.StringVar(&opts.authfile, "authfile", auth.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override")
if err := flags.MarkHidden("authfile"); err != nil {
panic(fmt.Sprintf("error marking authfile as hidden: %v", err))
}
flags.StringVar(&opts.blobCache, "blob-cache", "", "store copies of pulled image blobs in the specified directory")
if err := flags.MarkHidden("blob-cache"); err != nil {
panic(fmt.Sprintf("error marking blob-cache as hidden: %v", err))
}
flags.StringVar(&opts.certDir, "cert-dir", "", "use certificates at the specified path to access registries and sources in HTTPS locations")
flags.StringVar(&opts.checksum, "checksum", "", "checksum the HTTP source content")
flags.StringVar(&opts.chown, "chown", "", "set the user and group ownership of the destination content")
flags.StringVar(&opts.chmod, "chmod", "", "set the access permissions of the destination content")
flags.StringVar(&opts.creds, "creds", "", "use `[username[:password]]` for accessing registries when pulling images")
flags.BoolVar(&opts.link, "link", false, "enable layer caching for this operation (creates an independent layer)")
flags.BoolVar(&opts.noFollowSymlinks, "no-follow-symlinks", false, "do not follow symlinks when copying content (copy the symlink itself)")
if err := flags.MarkHidden("creds"); err != nil {
panic(fmt.Sprintf("error marking creds as hidden: %v", err))
}
flags.StringVar(&opts.from, "from", "", "use the specified container's or image's root directory as the source root directory")
flags.StringSliceVar(&opts.decryptionKeys, "decryption-key", nil, "key needed to decrypt a pulled image")
if err := flags.MarkHidden("decryption-key"); err != nil {
panic(fmt.Sprintf("error marking decryption-key as hidden: %v", err))
}
flags.StringSliceVar(&opts.excludes, "exclude", nil, "exclude pattern when copying files")
flags.StringVar(&opts.ignoreFile, "ignorefile", "", "path to .containerignore file")
flags.StringVar(&opts.contextdir, "contextdir", "", "context directory path")
flags.IntVar(&opts.retry, "retry", cli.MaxPullPushRetries, "number of times to retry in case of failure when performing pull")
flags.StringVar(&opts.retryDelay, "retry-delay", cli.PullPushRetryDelay.String(), "delay between retries in case of pull failures")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "don't output a digest of the newly-added/copied content")
flags.BoolVar(&opts.tlsVerify, "tls-verify", true, "require HTTPS and verify certificates when accessing registries when pulling images, and when retrieving sources from HTTPS URLs. TLS verification cannot be used when talking to an insecure registry.")
flags.BoolVarP(&opts.removeSignatures, "remove-signatures", "", false, "don't copy signatures when pulling image")
if err := flags.MarkHidden("remove-signatures"); err != nil {
panic(fmt.Sprintf("error marking remove-signatures as hidden: %v", err))
}
flags.StringVar(&opts.signaturePolicy, "signature-policy", "", "`pathname` of signature policy file (not usually used)")
if err := flags.MarkHidden("signature-policy"); err != nil {
panic(fmt.Sprintf("error marking signature-policy as hidden: %v", err))
}
flags.StringVar(&opts.timestamp, "timestamp", "", "set timestamps on new content to `seconds` after the epoch")
flags.BoolVar(&opts.allowWildcard, "allow-wildcard", true, "allow glob patterns in source paths")
flags.BoolVar(&opts.allowEmptyWildcard, "allow-empty-wildcard", false, "don't error when glob patterns match nothing")
flags.StringSliceVar(&opts.includes, "include", nil, "include pattern when copying files")
}
func addcopyInit() {
var (
addDescription = "\n Adds the contents of a file, URL, or directory to a container's working\n directory. If a local file appears to be an archive, its contents are\n extracted and added instead of the archive file itself."
copyDescription = "\n Copies the contents of a file, URL, or directory into a container's working\n directory."
shortAdd = "Add content to a working container"
shortCopy = "Copy content into a working container"
addOpts addCopyResults
copyOpts addCopyResults
)
addCommand := createCommand("add", addDescription, shortAdd, &addOpts)
addCommand.SetUsageTemplate(UsageTemplate())
copyCommand := createCommand("copy", copyDescription, shortCopy, ©Opts)
copyCommand.SetUsageTemplate(UsageTemplate())
addFlags := addCommand.Flags()
applyFlagVars(addFlags, &addOpts)
copyFlags := copyCommand.Flags()
applyFlagVars(copyFlags, ©Opts)
copyFlags.BoolVar(©Opts.parents, "parents", false, "preserve leading directories in the paths of items being copied")
rootCmd.AddCommand(addCommand)
rootCmd.AddCommand(copyCommand)
}
func addAndCopyCmd(c *cobra.Command, args []string, verb string, iopts addCopyResults) error {
if len(args) == 0 {
return errors.New("container ID must be specified")
}
name := args[0]
args = Tail(args)
if len(args) == 0 {
return errors.New("src must be specified")
}
if err := cli.VerifyFlagsArgsOrder(args); err != nil {
return err
}
// If list is greater than one, the last item is the destination
dest := ""
size := len(args)
if size > 1 {
dest = args[size-1]
args = args[:size-1]
}
store, err := getStore(c)
if err != nil {
return err
}
var from *buildah.Builder
unmountFrom := false
removeFrom := false
var idMappingOptions *buildah.IDMappingOptions
contextdir := iopts.contextdir
if iopts.ignoreFile != "" && contextdir == "" {
return errors.New("--ignorefile option requires that you specify a context dir using --contextdir")
}
systemContext, err := parse.SystemContextFromOptions(c)
if err != nil {
return fmt.Errorf("building system context: %w", err)
}
var preserveOwnership bool
if iopts.from != "" {
if from, err = openBuilder(getContext(), store, iopts.from); err != nil && errors.Is(err, storage.ErrContainerUnknown) {
decryptConfig, err2 := cli.DecryptConfig(iopts.decryptionKeys)
if err2 != nil {
return fmt.Errorf("unable to obtain decrypt config: %w", err2)
}
options := buildah.BuilderOptions{
FromImage: iopts.from,
BlobDirectory: iopts.blobCache,
SignaturePolicyPath: iopts.signaturePolicy,
SystemContext: systemContext,
MaxPullRetries: iopts.retry,
OciDecryptConfig: decryptConfig,
}
if iopts.retryDelay != "" {
options.PullRetryDelay, err = time.ParseDuration(iopts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.retryDelay, err)
}
}
if !iopts.quiet {
options.ReportWriter = os.Stderr
}
if from, err = buildah.NewBuilder(getContext(), store, options); err != nil {
return fmt.Errorf("no container named %q, error copying content from image %q: %w", iopts.from, iopts.from, err)
}
removeFrom = true
defer func() {
if !removeFrom {
return
}
if err := from.Delete(); err != nil {
logrus.Errorf("error deleting %q temporary working container %q", iopts.from, from.Container)
}
}()
}
if err != nil {
return fmt.Errorf("reading --from container %q: %w", iopts.from, err)
}
fromcdir, err := store.ContainerDirectory(from.ContainerID)
if err != nil {
return fmt.Errorf("finding container state directory for %q: %w", iopts.from, err)
}
if err := flagServingSFTP(fromcdir); err != nil {
return fmt.Errorf(`unable to use "buildah add" or "buildah copy" while container appears to be mounted: %v`, err)
}
fromMountPoint, err := from.Mount(from.MountLabel)
if err != nil {
return fmt.Errorf("mounting %q container %q: %w", iopts.from, from.Container, err)
}
unmountFrom = true
defer func() {
if !unmountFrom {
return
}
if err := from.Unmount(); err != nil {
logrus.Errorf("error unmounting %q container %q", iopts.from, from.Container)
}
if err := from.Save(); err != nil {
logrus.Errorf("error saving information about %q container %q", iopts.from, from.Container)
}
}()
idMappingOptions = &from.IDMappingOptions
preserveOwnership = true
contextdir = filepath.Join(fromMountPoint, iopts.contextdir)
for i := range args {
args[i] = filepath.Join(fromMountPoint, args[i])
}
}
builder, err := openBuilder(getContext(), store, name)
if err != nil {
return fmt.Errorf("reading build container %q: %w", name, err)
}
cdir, err := store.ContainerDirectory(builder.ContainerID)
if err != nil {
return fmt.Errorf("finding container state directory for %q: %w", name, err)
}
if err := flagServingSFTP(cdir); err != nil {
return fmt.Errorf(`unable to use "buildah add" or "buildah copy" while container appears to be busy: %v`, err)
}
builder.ContentDigester.Restart()
var timestamp *time.Time
if iopts.timestamp != "" {
u, err := strconv.ParseInt(iopts.timestamp, 10, 64)
if err != nil {
return fmt.Errorf("parsing timestamp value %q: %w", iopts.timestamp, err)
}
t := time.Unix(u, 0).UTC()
timestamp = &t
}
followSymlink := types.OptionalBoolUndefined
if iopts.noFollowSymlinks {
followSymlink = types.OptionalBoolFalse
}
options := buildah.AddAndCopyOptions{
Chmod: iopts.chmod,
Chown: iopts.chown,
PreserveOwnership: preserveOwnership,
Checksum: iopts.checksum,
ContextDir: contextdir,
Excludes: iopts.excludes,
IDMappingOptions: idMappingOptions,
// These next two fields are set based on command line flags
// with more generic-sounding names.
CertPath: systemContext.DockerCertPath,
InsecureSkipTLSVerify: systemContext.DockerInsecureSkipTLSVerify,
MaxRetries: iopts.retry,
Parents: iopts.parents,
Timestamp: timestamp,
Link: iopts.link,
FollowSymlink: followSymlink,
Includes: iopts.includes,
}
if iopts.contextdir != "" {
var excludes []string
excludes, options.IgnoreFile, err = parse.ContainerIgnoreFile(options.ContextDir, iopts.ignoreFile, []string{})
if err != nil {
return err
}
options.Excludes = append(excludes, options.Excludes...)
}
if c.Flags().Changed("allow-wildcard") {
options.AllowWildcard = types.NewOptionalBool(iopts.allowWildcard)
}
if c.Flags().Changed("allow-empty-wildcard") {
options.AllowEmptyWildcard = types.NewOptionalBool(iopts.allowEmptyWildcard)
}
if iopts.retryDelay != "" {
retryDelay, err := time.ParseDuration(iopts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.retryDelay, err)
}
options.RetryDelay = retryDelay
}
extractLocalArchives := verb == "ADD"
err = builder.Add(dest, extractLocalArchives, options, args...)
if err != nil {
return fmt.Errorf("adding content to container %q: %w", builder.Container, err)
}
if unmountFrom {
if err := from.Unmount(); err != nil {
return fmt.Errorf("unmounting %q container %q: %w", iopts.from, from.Container, err)
}
if err := from.Save(); err != nil {
return fmt.Errorf("saving information about %q container %q: %w", iopts.from, from.Container, err)
}
unmountFrom = false
}
if removeFrom {
if err := from.Delete(); err != nil {
return fmt.Errorf("deleting %q temporary working container %q: %w", iopts.from, from.Container, err)
}
removeFrom = false
}
contentType, digest := builder.ContentDigester.Digest()
if digest == "" {
logrus.Debug("no content copied, skipping digest and history")
return nil
}
if !iopts.quiet {
fmt.Printf("%s\n", digest.Hex())
}
if contentType != "" {
contentType = contentType + ":"
}
conditionallyAddHistory(builder, c, "/bin/sh -c #(nop) %s %s%s", verb, contentType, digest.Hex())
return builder.Save()
}