Merge pull request #1321 from stevvooe/refactor-snapshot-commands

cmd/snapshot: follow conventions from Snapshotter
This commit is contained in:
Michael Crosby 2017-08-10 10:08:38 -04:00 committed by GitHub
commit 17d7a5c65b

View File

@ -8,11 +8,8 @@ import (
"text/tabwriter" "text/tabwriter"
"github.com/containerd/containerd/progress" "github.com/containerd/containerd/progress"
"github.com/containerd/containerd/rootfs"
"github.com/containerd/containerd/snapshot" "github.com/containerd/containerd/snapshot"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -21,7 +18,6 @@ var snapshotCommand = cli.Command{
Usage: "snapshot management", Usage: "snapshot management",
Flags: snapshotterFlags, Flags: snapshotterFlags,
Subcommands: cli.Commands{ Subcommands: cli.Commands{
archiveSnapshotCommand,
listSnapshotCommand, listSnapshotCommand,
usageSnapshotCommand, usageSnapshotCommand,
removeSnapshotCommand, removeSnapshotCommand,
@ -32,49 +28,6 @@ var snapshotCommand = cli.Command{
}, },
} }
var archiveSnapshotCommand = cli.Command{
Name: "archive",
Usage: "Create an archive of a snapshot",
ArgsUsage: "[flags] id",
Flags: []cli.Flag{
cli.StringFlag{
Name: "id",
Usage: "id of the container or snapshot",
},
},
Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext)
defer cancel()
id := clicontext.String("id")
if id == "" {
return errors.New("container id must be provided")
}
snapshotter, err := getSnapshotter(clicontext)
if err != nil {
return err
}
differ, err := getDiffService(clicontext)
if err != nil {
return err
}
contentRef := fmt.Sprintf("diff-%s", id)
d, err := rootfs.Diff(ctx, id, contentRef, snapshotter, differ)
if err != nil {
return err
}
// TODO: Track progress
fmt.Printf("%s %s\n", d.MediaType, d.Digest)
return nil
},
}
var listSnapshotCommand = cli.Command{ var listSnapshotCommand = cli.Command{
Name: "list", Name: "list",
Aliases: []string{"ls"}, Aliases: []string{"ls"},
@ -89,7 +42,7 @@ var listSnapshotCommand = cli.Command{
} }
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, ' ', 0) tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, ' ', 0)
fmt.Fprintln(tw, "ID\tPARENT\tKIND\t") fmt.Fprintln(tw, "KEY\tPARENT\tKIND\t")
if err := snapshotter.Walk(ctx, func(ctx context.Context, info snapshot.Info) error { if err := snapshotter.Walk(ctx, func(ctx context.Context, info snapshot.Info) error {
fmt.Fprintf(tw, "%v\t%v\t%v\t\n", fmt.Fprintf(tw, "%v\t%v\t%v\t\n",
@ -108,7 +61,7 @@ var listSnapshotCommand = cli.Command{
var usageSnapshotCommand = cli.Command{ var usageSnapshotCommand = cli.Command{
Name: "usage", Name: "usage",
Usage: "Usage snapshots", Usage: "Usage snapshots",
ArgsUsage: "[flags] [id] ...", ArgsUsage: "[flags] [<key>, ...]",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "b", Name: "b",
@ -136,7 +89,7 @@ var usageSnapshotCommand = cli.Command{
} }
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, ' ', 0) tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, ' ', 0)
fmt.Fprintln(tw, "ID\tSize\tInodes\t") fmt.Fprintln(tw, "KEY\tSIZE\tINODES\t")
if clicontext.NArg() == 0 { if clicontext.NArg() == 0 {
if err := snapshotter.Walk(ctx, func(ctx context.Context, info snapshot.Info) error { if err := snapshotter.Walk(ctx, func(ctx context.Context, info snapshot.Info) error {
@ -166,7 +119,7 @@ var usageSnapshotCommand = cli.Command{
var removeSnapshotCommand = cli.Command{ var removeSnapshotCommand = cli.Command{
Name: "remove", Name: "remove",
Aliases: []string{"rm"}, Aliases: []string{"rm"},
ArgsUsage: "id [id] ...", ArgsUsage: "<key> [<key>, ...]",
Usage: "remove snapshots", Usage: "remove snapshots",
Action: func(clicontext *cli.Context) error { Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext) ctx, cancel := appContext(clicontext)
@ -177,10 +130,10 @@ var removeSnapshotCommand = cli.Command{
return err return err
} }
for _, id := range clicontext.Args() { for _, key := range clicontext.Args() {
err = snapshotter.Remove(ctx, id) err = snapshotter.Remove(ctx, key)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to remove %q", id) return errors.Wrapf(err, "failed to remove %q", key)
} }
} }
@ -191,11 +144,11 @@ var removeSnapshotCommand = cli.Command{
var prepareSnapshotCommand = cli.Command{ var prepareSnapshotCommand = cli.Command{
Name: "prepare", Name: "prepare",
Usage: "prepare a snapshot from a committed snapshot", Usage: "prepare a snapshot from a committed snapshot",
ArgsUsage: "[flags] digest target", ArgsUsage: "[flags] <key> [<parent>]",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{ cli.StringFlag{
Name: "snapshot-name", Name: "target, t",
Usage: "name of the target snapshot", Usage: "mount target path, will print mount, if provided",
}, },
}, },
Action: func(clicontext *cli.Context) error { Action: func(clicontext *cli.Context) error {
@ -206,33 +159,24 @@ var prepareSnapshotCommand = cli.Command{
return cli.ShowSubcommandHelp(clicontext) return cli.ShowSubcommandHelp(clicontext)
} }
dgst, err := digest.Parse(clicontext.Args().Get(0)) target := clicontext.String("target")
if err != nil { key := clicontext.Args().Get(0)
return err parent := clicontext.Args().Get(1)
}
target := clicontext.Args().Get(1)
snapshotName := clicontext.String("snapshot-name")
// Use the target as the snapshotName if no snapshot-name is provided
if snapshotName == "" {
snapshotName = target
}
logrus.Infof("preparing mounts %s", dgst.String())
snapshotter, err := getSnapshotter(clicontext) snapshotter, err := getSnapshotter(clicontext)
if err != nil { if err != nil {
return err return err
} }
mounts, err := snapshotter.Prepare(ctx, snapshotName, dgst.String()) mounts, err := snapshotter.Prepare(ctx, key, parent)
if err != nil { if err != nil {
return err return err
} }
for _, m := range mounts { if target != "" {
fmt.Fprintf(os.Stdout, "mount -t %s %s %s -o %s\n", m.Type, m.Source, target, strings.Join(m.Options, ",")) for _, m := range mounts {
fmt.Fprintf(os.Stdout, "mount -t %s %s %s -o %s\n", m.Type, m.Source, target, strings.Join(m.Options, ","))
}
} }
return nil return nil
@ -240,37 +184,26 @@ var prepareSnapshotCommand = cli.Command{
} }
var mountSnapshotCommand = cli.Command{ var mountSnapshotCommand = cli.Command{
Name: "mount", Name: "mounts",
Aliases: []string{"m", "mount"},
Usage: "mount gets mount commands for the active snapshots", Usage: "mount gets mount commands for the active snapshots",
ArgsUsage: "[flags] target", ArgsUsage: "[flags] <target> <key>",
Flags: []cli.Flag{
cli.StringFlag{
Name: "snapshot-name",
Usage: "name of the snapshot",
},
},
Action: func(clicontext *cli.Context) error { Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext) ctx, cancel := appContext(clicontext)
defer cancel() defer cancel()
if clicontext.NArg() != 1 { if clicontext.NArg() != 2 {
return cli.ShowSubcommandHelp(clicontext) return cli.ShowSubcommandHelp(clicontext)
} }
target := clicontext.Args().Get(0) target := clicontext.Args().Get(0)
key := clicontext.Args().Get(1)
snapshotName := clicontext.String("snapshot-name")
// Use the target as the snapshotName if no snapshot-name is provided
if snapshotName == "" {
snapshotName = target
}
snapshotter, err := getSnapshotter(clicontext) snapshotter, err := getSnapshotter(clicontext)
if err != nil { if err != nil {
return err return err
} }
mounts, err := snapshotter.Mounts(ctx, snapshotName) mounts, err := snapshotter.Mounts(ctx, key)
if err != nil { if err != nil {
return err return err
} }
@ -285,8 +218,8 @@ var mountSnapshotCommand = cli.Command{
var commitSnapshotCommand = cli.Command{ var commitSnapshotCommand = cli.Command{
Name: "commit", Name: "commit",
Usage: "commit creates a new snapshot with diff from parent snapshot", Usage: "commit an active snapshot into the provided name",
ArgsUsage: "[flags] <id> <target>", ArgsUsage: "[flags] <key> <active>",
Action: func(clicontext *cli.Context) error { Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext) ctx, cancel := appContext(clicontext)
defer cancel() defer cancel()
@ -295,15 +228,15 @@ var commitSnapshotCommand = cli.Command{
return cli.ShowSubcommandHelp(clicontext) return cli.ShowSubcommandHelp(clicontext)
} }
id := clicontext.Args().Get(0) key := clicontext.Args().Get(1)
target := clicontext.Args().Get(1) active := clicontext.Args().Get(0)
snapshotter, err := getSnapshotter(clicontext) snapshotter, err := getSnapshotter(clicontext)
if err != nil { if err != nil {
return err return err
} }
return snapshotter.Commit(ctx, target, id) return snapshotter.Commit(ctx, key, active)
}, },
} }