Merge pull request #1369 from AkihiroSuda/ctr-snapshot-view

ctr: add `ctr snapshot view`
This commit is contained in:
Stephen Day 2017-08-17 15:32:13 -07:00 committed by GitHub
commit 15f56727e0

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/progress" "github.com/containerd/containerd/progress"
"github.com/containerd/containerd/snapshot" "github.com/containerd/containerd/snapshot"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -22,6 +23,7 @@ var snapshotCommand = cli.Command{
usageSnapshotCommand, usageSnapshotCommand,
removeSnapshotCommand, removeSnapshotCommand,
prepareSnapshotCommand, prepareSnapshotCommand,
viewSnapshotCommand,
treeSnapshotCommand, treeSnapshotCommand,
mountSnapshotCommand, mountSnapshotCommand,
commitSnapshotCommand, commitSnapshotCommand,
@ -174,9 +176,47 @@ var prepareSnapshotCommand = cli.Command{
} }
if target != "" { if target != "" {
for _, m := range mounts { printMounts(target, mounts)
fmt.Fprintf(os.Stdout, "mount -t %s %s %s -o %s\n", m.Type, m.Source, target, strings.Join(m.Options, ","))
} }
return nil
},
}
var viewSnapshotCommand = cli.Command{
Name: "view",
Usage: "create a read-only snapshot from a committed snapshot",
ArgsUsage: "[flags] <key> [<parent>]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "target, t",
Usage: "mount target path, will print mount, if provided",
},
},
Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext)
defer cancel()
if clicontext.NArg() != 2 {
return cli.ShowSubcommandHelp(clicontext)
}
target := clicontext.String("target")
key := clicontext.Args().Get(0)
parent := clicontext.Args().Get(1)
snapshotter, err := getSnapshotter(clicontext)
if err != nil {
return err
}
mounts, err := snapshotter.View(ctx, key, parent)
if err != nil {
return err
}
if target != "" {
printMounts(target, mounts)
} }
return nil return nil
@ -186,7 +226,7 @@ var prepareSnapshotCommand = cli.Command{
var mountSnapshotCommand = cli.Command{ var mountSnapshotCommand = cli.Command{
Name: "mounts", Name: "mounts",
Aliases: []string{"m", "mount"}, Aliases: []string{"m", "mount"},
Usage: "mount gets mount commands for the active snapshots", Usage: "mount gets mount commands for the snapshots",
ArgsUsage: "[flags] <target> <key>", ArgsUsage: "[flags] <target> <key>",
Action: func(clicontext *cli.Context) error { Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext) ctx, cancel := appContext(clicontext)
@ -208,9 +248,7 @@ var mountSnapshotCommand = cli.Command{
return err return err
} }
for _, m := range mounts { printMounts(target, 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
}, },
@ -308,3 +346,10 @@ func printNode(name string, tree map[string]*snapshotTreeNode, level int) {
printNode(child, tree, level) printNode(child, tree, level)
} }
} }
func printMounts(target string, mounts []mount.Mount) {
// FIXME: This is specific to Unix
for _, m := range mounts {
fmt.Printf("mount -t %s %s %s -o %s\n", m.Type, m.Source, target, strings.Join(m.Options, ","))
}
}