2018-03-19 18:56:51 -04:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2018-09-14 13:53:56 -04:00
|
|
|
"context"
|
2018-03-19 18:56:51 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2018-09-14 13:53:56 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/cli/internal/versions"
|
2018-09-11 08:46:30 -04:00
|
|
|
clitypes "github.com/docker/cli/types"
|
2018-03-19 18:56:51 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type checkOptions struct {
|
|
|
|
registryPrefix string
|
|
|
|
preReleases bool
|
2018-09-28 17:06:28 -04:00
|
|
|
engineImage string
|
2018-03-19 18:56:51 -04:00
|
|
|
downgrades bool
|
|
|
|
upgrades bool
|
|
|
|
format string
|
|
|
|
quiet bool
|
|
|
|
sockPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCheckForUpdatesCommand(dockerCli command.Cli) *cobra.Command {
|
|
|
|
var options checkOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "check [OPTIONS]",
|
|
|
|
Short: "Check for available engine updates",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runCheck(dockerCli, options)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2018-09-20 14:02:11 -04:00
|
|
|
flags.StringVar(&options.registryPrefix, "registry-prefix", clitypes.RegistryPrefix, "Override the existing location where engine images are pulled")
|
2018-03-19 18:56:51 -04:00
|
|
|
flags.BoolVar(&options.downgrades, "downgrades", false, "Report downgrades (default omits older versions)")
|
|
|
|
flags.BoolVar(&options.preReleases, "pre-releases", false, "Include pre-release versions")
|
2018-09-28 17:06:28 -04:00
|
|
|
flags.StringVar(&options.engineImage, "engine-image", "", "Specify engine image (default uses the same image as currently running)")
|
2018-03-19 18:56:51 -04:00
|
|
|
flags.BoolVar(&options.upgrades, "upgrades", true, "Report available upgrades")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print updates using a Go template")
|
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display available versions")
|
|
|
|
flags.StringVar(&options.sockPath, "containerd", "", "override default location of containerd endpoint")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCheck(dockerCli command.Cli, options checkOptions) error {
|
2018-09-14 13:53:56 -04:00
|
|
|
if !isRoot() {
|
2018-09-20 14:02:11 -04:00
|
|
|
return errors.New("this command must be run as a privileged user")
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
2018-09-14 13:53:56 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
client := dockerCli.Client()
|
|
|
|
serverVersion, err := client.ServerVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-19 18:56:51 -04:00
|
|
|
|
2018-09-28 17:06:28 -04:00
|
|
|
availVersions, err := versions.GetEngineVersions(ctx, dockerCli.RegistryClient(false), options.registryPrefix, options.engineImage, serverVersion.Version)
|
2018-09-14 13:53:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-19 18:56:51 -04:00
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
availUpdates := []clitypes.Update{
|
|
|
|
{Type: "current", Version: serverVersion.Version},
|
|
|
|
}
|
|
|
|
if len(availVersions.Patches) > 0 {
|
|
|
|
availUpdates = append(availUpdates,
|
|
|
|
processVersions(
|
|
|
|
serverVersion.Version,
|
|
|
|
"patch",
|
|
|
|
options.preReleases,
|
|
|
|
availVersions.Patches)...)
|
|
|
|
}
|
|
|
|
if options.upgrades {
|
|
|
|
availUpdates = append(availUpdates,
|
|
|
|
processVersions(
|
|
|
|
serverVersion.Version,
|
|
|
|
"upgrade",
|
|
|
|
options.preReleases,
|
|
|
|
availVersions.Upgrades)...)
|
|
|
|
}
|
|
|
|
if options.downgrades {
|
|
|
|
availUpdates = append(availUpdates,
|
|
|
|
processVersions(
|
|
|
|
serverVersion.Version,
|
|
|
|
"downgrade",
|
|
|
|
options.preReleases,
|
|
|
|
availVersions.Downgrades)...)
|
|
|
|
}
|
2018-03-19 18:56:51 -04:00
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
format := options.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
2018-03-19 18:56:51 -04:00
|
|
|
|
2018-09-14 13:53:56 -04:00
|
|
|
updatesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewUpdatesFormat(format, options.quiet),
|
|
|
|
Trunc: false,
|
|
|
|
}
|
|
|
|
return formatter.UpdatesWrite(updatesCtx, availUpdates)
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func processVersions(currentVersion, verType string,
|
|
|
|
includePrerelease bool,
|
2018-09-14 13:53:56 -04:00
|
|
|
availVersions []clitypes.DockerVersion) []clitypes.Update {
|
2018-09-11 08:46:30 -04:00
|
|
|
availUpdates := []clitypes.Update{}
|
2018-09-14 13:53:56 -04:00
|
|
|
for _, ver := range availVersions {
|
2018-03-19 18:56:51 -04:00
|
|
|
if !includePrerelease && ver.Prerelease() != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ver.Tag != currentVersion {
|
2018-09-11 08:46:30 -04:00
|
|
|
availUpdates = append(availUpdates, clitypes.Update{
|
2018-03-19 18:56:51 -04:00
|
|
|
Type: verType,
|
|
|
|
Version: ver.Tag,
|
2018-09-20 14:02:11 -04:00
|
|
|
Notes: fmt.Sprintf("%s/%s", clitypes.ReleaseNotePrefix, ver.Tag),
|
2018-03-19 18:56:51 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return availUpdates
|
|
|
|
}
|