cmd/ctr: add prepare subcommand to snapshot

This changeset adds `prepare` subcommand to `ctr snapshot` and removes
`prepare` from `dist rootfs` to keep the basic snapshot operation commands
together.

Signed-off-by: Sunny Gogoi <me@darkowlzz.space>
This commit is contained in:
Sunny Gogoi 2017-07-11 21:30:40 +05:30
parent 13f90e95a7
commit 2c1b54f573
2 changed files with 44 additions and 43 deletions

View File

@ -4,11 +4,14 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"strings"
"text/tabwriter" "text/tabwriter"
"github.com/Sirupsen/logrus"
"github.com/containerd/containerd/progress" "github.com/containerd/containerd/progress"
"github.com/containerd/containerd/rootfs" "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/urfave/cli" "github.com/urfave/cli"
) )
@ -21,6 +24,7 @@ var snapshotCommand = cli.Command{
listSnapshotCommand, listSnapshotCommand,
usageSnapshotCommand, usageSnapshotCommand,
removeSnapshotCommand, removeSnapshotCommand,
prepareSnapshotCommand,
}, },
} }
@ -193,3 +197,43 @@ var removeSnapshotCommand = cli.Command{
return nil return nil
}, },
} }
var prepareSnapshotCommand = cli.Command{
Name: "prepare",
Usage: "prepare gets mount commands for digest",
ArgsUsage: "[flags] <digest> <target>",
Flags: []cli.Flag{},
Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext)
defer cancel()
if clicontext.NArg() != 2 {
return cli.ShowSubcommandHelp(clicontext)
}
dgst, err := digest.Parse(clicontext.Args().Get(0))
if err != nil {
return err
}
target := clicontext.Args().Get(1)
logrus.Infof("preparing mounts %s", dgst.String())
client, err := newClient(clicontext)
if err != nil {
return err
}
snapshotter := client.SnapshotService()
mounts, err := snapshotter.Prepare(ctx, target, dgst.String())
if err != nil {
return err
}
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
},
}

43
cmd/dist/rootfs.go vendored
View File

@ -3,8 +3,6 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"strings"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
@ -16,7 +14,6 @@ var rootfsCommand = cli.Command{
Usage: "rootfs setups a rootfs", Usage: "rootfs setups a rootfs",
Subcommands: []cli.Command{ Subcommands: []cli.Command{
rootfsUnpackCommand, rootfsUnpackCommand,
rootfsPrepareCommand,
}, },
} }
@ -71,43 +68,3 @@ var rootfsUnpackCommand = cli.Command{
return nil return nil
}, },
} }
var rootfsPrepareCommand = cli.Command{
Name: "prepare",
Usage: "prepare gets mount commands for digest",
ArgsUsage: "[flags] <digest> <target>",
Flags: []cli.Flag{},
Action: func(clicontext *cli.Context) error {
ctx, cancel := appContext(clicontext)
defer cancel()
if clicontext.NArg() != 2 {
return cli.ShowSubcommandHelp(clicontext)
}
dgst, err := digest.Parse(clicontext.Args().Get(0))
if err != nil {
return err
}
target := clicontext.Args().Get(1)
log.G(ctx).Infof("preparing mounts %s", dgst.String())
client, err := getClient(clicontext)
if err != nil {
return err
}
snapshotter := client.SnapshotService()
mounts, err := snapshotter.Prepare(ctx, target, dgst.String())
if err != nil {
return err
}
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
},
}