2017-06-30 19:10:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-10-13 21:04:57 +00:00
|
|
|
"fmt"
|
2017-09-06 14:28:59 +00:00
|
|
|
"os"
|
|
|
|
|
2017-10-13 21:04:57 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2017-09-09 11:14:40 +00:00
|
|
|
|
2017-08-31 13:04:02 +00:00
|
|
|
"github.com/containers/image/types"
|
2017-10-13 21:04:57 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libpod"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/libpod/common"
|
2017-06-30 19:10:57 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-30 19:10:57 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pullFlags = []cli.Flag{
|
2017-09-09 11:14:40 +00:00
|
|
|
cli.StringFlag{
|
2017-10-13 00:35:08 +00:00
|
|
|
Name: "signature-policy",
|
|
|
|
Usage: "`pathname` of signature policy file (not usually used)",
|
2017-09-09 11:14:40 +00:00
|
|
|
},
|
2017-10-13 21:04:57 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "authfile",
|
|
|
|
Usage: "Path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "creds",
|
|
|
|
Usage: "`credentials` (USERNAME:PASSWORD) to use for authenticating to a registry",
|
|
|
|
},
|
2017-06-30 19:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pullDescription = "Pulls an image from a registry and stores it locally.\n" +
|
|
|
|
"An image can be pulled using its tag or digest. If a tag is not\n" +
|
|
|
|
"specified, the image with the 'latest' tag (if it exists) is pulled."
|
|
|
|
pullCommand = cli.Command{
|
|
|
|
Name: "pull",
|
|
|
|
Usage: "pull an image from a registry",
|
|
|
|
Description: pullDescription,
|
|
|
|
Flags: pullFlags,
|
|
|
|
Action: pullCmd,
|
|
|
|
ArgsUsage: "",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// pullCmd gets the data from the command line and calls pullImage
|
|
|
|
// to copy an image from a registry to a local machine
|
|
|
|
func pullCmd(c *cli.Context) error {
|
2017-10-13 21:04:57 +00:00
|
|
|
runtime, err := getRuntime(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get runtime")
|
|
|
|
}
|
|
|
|
defer runtime.Shutdown(false)
|
2017-08-31 13:04:02 +00:00
|
|
|
|
2017-06-30 19:10:57 +00:00
|
|
|
args := c.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
logrus.Errorf("an image name must be specified")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(args) > 1 {
|
|
|
|
logrus.Errorf("too many arguments. Requires exactly 1")
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-28 18:44:48 +00:00
|
|
|
if err := validateFlags(c, pullFlags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 19:10:57 +00:00
|
|
|
image := args[0]
|
2017-10-13 21:04:57 +00:00
|
|
|
|
|
|
|
var registryCreds *types.DockerAuthConfig
|
|
|
|
if c.String("creds") != "" {
|
|
|
|
creds, err := common.ParseRegistryCreds(c.String("creds"))
|
2017-08-31 13:04:02 +00:00
|
|
|
if err != nil {
|
2017-10-13 21:04:57 +00:00
|
|
|
if err == common.ErrNoPassword {
|
|
|
|
fmt.Print("Password: ")
|
|
|
|
password, err := terminal.ReadPassword(0)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not read password from terminal")
|
|
|
|
}
|
|
|
|
creds.Password = string(password)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-31 13:04:02 +00:00
|
|
|
}
|
2017-10-13 21:04:57 +00:00
|
|
|
registryCreds = creds
|
2017-08-31 13:04:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:04:57 +00:00
|
|
|
options := libpod.CopyOptions{
|
|
|
|
SignaturePolicyPath: c.String("signature-policy"),
|
|
|
|
AuthFile: c.String("authfile"),
|
|
|
|
DockerRegistryOptions: common.DockerRegistryOptions{
|
|
|
|
DockerRegistryCreds: registryCreds,
|
|
|
|
},
|
|
|
|
Writer: os.Stdout,
|
2017-06-30 19:10:57 +00:00
|
|
|
}
|
2017-10-13 21:04:57 +00:00
|
|
|
|
|
|
|
return runtime.PullImage(image, options)
|
|
|
|
|
2017-06-30 19:10:57 +00:00
|
|
|
}
|