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
|
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/containers/buildah"
"github.com/containers/buildah/pkg/cli"
"github.com/containers/buildah/pkg/parse"
"github.com/containers/common/pkg/auth"
"github.com/containers/storage"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
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
}
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),
}
}
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)")
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")
}
func init() {
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 the container"
shortCopy = "Copy content into the 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 build container %q: %w", iopts.from, 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)
}
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
}
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,
}
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 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 !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()
}
|