github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/images/push.go (about)

     1  package images
     2  
     3  import (
     4  	buildahcli "github.com/containers/buildah/pkg/cli"
     5  	"github.com/containers/image/v5/types"
     6  	"github.com/containers/libpod/cmd/podmanV2/registry"
     7  	"github.com/containers/libpod/pkg/domain/entities"
     8  	"github.com/pkg/errors"
     9  	"github.com/spf13/cobra"
    10  	"github.com/spf13/pflag"
    11  )
    12  
    13  // pushOptionsWrapper wraps entities.ImagepushOptions and prevents leaking
    14  // CLI-only fields into the API types.
    15  type pushOptionsWrapper struct {
    16  	entities.ImagePushOptions
    17  	TLSVerifyCLI bool // CLI only
    18  }
    19  
    20  var (
    21  	pushOptions     = pushOptionsWrapper{}
    22  	pushDescription = `Pushes a source image to a specified destination.
    23  
    24  	The Image "DESTINATION" uses a "transport":"details" format. See podman-push(1) section "DESTINATION" for the expected format.`
    25  
    26  	// Command: podman push
    27  	pushCmd = &cobra.Command{
    28  		Use:     "push [flags] SOURCE DESTINATION",
    29  		Short:   "Push an image to a specified destination",
    30  		Long:    pushDescription,
    31  		PreRunE: preRunE,
    32  		RunE:    imagePush,
    33  		Example: `podman push imageID docker://registry.example.com/repository:tag
    34  		podman push imageID oci-archive:/path/to/layout:image:tag`,
    35  	}
    36  
    37  	// Command: podman image push
    38  	// It's basically a clone of `pushCmd` with the exception of being a
    39  	// child of the images command.
    40  	imagePushCmd = &cobra.Command{
    41  		Use:     pushCmd.Use,
    42  		Short:   pushCmd.Short,
    43  		Long:    pushCmd.Long,
    44  		PreRunE: pushCmd.PreRunE,
    45  		RunE:    pushCmd.RunE,
    46  		Example: `podman image push imageID docker://registry.example.com/repository:tag
    47  		podman image push imageID oci-archive:/path/to/layout:image:tag`,
    48  	}
    49  )
    50  
    51  func init() {
    52  	// push
    53  	registry.Commands = append(registry.Commands, registry.CliCommand{
    54  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    55  		Command: pushCmd,
    56  	})
    57  
    58  	pushCmd.SetHelpTemplate(registry.HelpTemplate())
    59  	pushCmd.SetUsageTemplate(registry.UsageTemplate())
    60  
    61  	flags := pushCmd.Flags()
    62  	pushFlags(flags)
    63  
    64  	// images push
    65  	registry.Commands = append(registry.Commands, registry.CliCommand{
    66  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    67  		Command: imagePushCmd,
    68  		Parent:  imageCmd,
    69  	})
    70  
    71  	imagePushCmd.SetHelpTemplate(registry.HelpTemplate())
    72  	imagePushCmd.SetUsageTemplate(registry.UsageTemplate())
    73  	pushFlags(imagePushCmd.Flags())
    74  }
    75  
    76  // pushFlags set the flags for the push command.
    77  func pushFlags(flags *pflag.FlagSet) {
    78  	flags.StringVar(&pushOptions.Authfile, "authfile", buildahcli.GetDefaultAuthFile(), "Path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override")
    79  	flags.StringVar(&pushOptions.CertDir, "cert-dir", "", "Path to a directory containing TLS certificates and keys")
    80  	flags.BoolVar(&pushOptions.Compress, "compress", false, "Compress tarball image layers when pushing to a directory using the 'dir' transport. (default is same compression type as source)")
    81  	flags.StringVar(&pushOptions.Credentials, "creds", "", "`Credentials` (USERNAME:PASSWORD) to use for authenticating to a registry")
    82  	flags.StringVar(&pushOptions.DigestFile, "digestfile", "", "Write the digest of the pushed image to the specified file")
    83  	flags.StringVarP(&pushOptions.Format, "format", "f", "", "Manifest type (oci, v2s1, or v2s2) to use when pushing an image using the 'dir' transport (default is manifest type of source)")
    84  	flags.BoolVarP(&pushOptions.Quiet, "quiet", "q", false, "Suppress output information when pushing images")
    85  	flags.BoolVar(&pushOptions.RemoveSignatures, "remove-signatures", false, "Discard any pre-existing signatures in the image")
    86  	flags.StringVar(&pushOptions.SignaturePolicy, "signature-policy", "", "Path to a signature-policy file")
    87  	flags.StringVar(&pushOptions.SignBy, "sign-by", "", "Add a signature at the destination using the specified key")
    88  	flags.BoolVar(&pushOptions.TLSVerifyCLI, "tls-verify", true, "Require HTTPS and verify certificates when contacting registries")
    89  
    90  	if registry.IsRemote() {
    91  		_ = flags.MarkHidden("authfile")
    92  		_ = flags.MarkHidden("cert-dir")
    93  		_ = flags.MarkHidden("compress")
    94  		_ = flags.MarkHidden("quiet")
    95  		_ = flags.MarkHidden("signature-policy")
    96  		_ = flags.MarkHidden("tls-verify")
    97  	}
    98  }
    99  
   100  // imagePush is implement the command for pushing images.
   101  func imagePush(cmd *cobra.Command, args []string) error {
   102  	var source, destination string
   103  	switch len(args) {
   104  	case 1:
   105  		source = args[0]
   106  	case 2:
   107  		source = args[0]
   108  		destination = args[1]
   109  	case 0:
   110  		fallthrough
   111  	default:
   112  		return errors.New("push requires at least one image name, or optionally a second to specify a different destination")
   113  	}
   114  
   115  	pushOptsAPI := pushOptions.ImagePushOptions
   116  	// TLS verification in c/image is controlled via a `types.OptionalBool`
   117  	// which allows for distinguishing among set-true, set-false, unspecified
   118  	// which is important to implement a sane way of dealing with defaults of
   119  	// boolean CLI flags.
   120  	if cmd.Flags().Changed("tls-verify") {
   121  		pushOptsAPI.TLSVerify = types.NewOptionalBool(pushOptions.TLSVerifyCLI)
   122  	}
   123  
   124  	// Let's do all the remaining Yoga in the API to prevent us from scattering
   125  	// logic across (too) many parts of the code.
   126  	return registry.ImageEngine().Push(registry.GetContext(), source, destination, pushOptsAPI)
   127  }