2017-03-09 06:04:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2017-03-10 03:50:23 +00:00
|
|
|
"fmt"
|
2017-03-09 06:04:44 +00:00
|
|
|
"io/ioutil"
|
2017-03-10 03:50:23 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2017-03-09 06:04:44 +00:00
|
|
|
|
2017-04-03 20:14:15 +00:00
|
|
|
contentapi "github.com/containerd/containerd/api/services/content"
|
|
|
|
rootfsapi "github.com/containerd/containerd/api/services/rootfs"
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/log"
|
|
|
|
contentservice "github.com/containerd/containerd/services/content"
|
|
|
|
rootfsservice "github.com/containerd/containerd/services/rootfs"
|
2017-03-09 06:04:44 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rootfsCommand = cli.Command{
|
|
|
|
Name: "rootfs",
|
|
|
|
Usage: "rootfs setups a rootfs",
|
|
|
|
Subcommands: []cli.Command{
|
2017-03-16 00:17:25 +00:00
|
|
|
rootfsUnpackCommand,
|
2017-03-09 06:04:44 +00:00
|
|
|
rootfsPrepareCommand,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:17:25 +00:00
|
|
|
var rootfsUnpackCommand = cli.Command{
|
2017-03-15 06:34:32 +00:00
|
|
|
Name: "unpack",
|
|
|
|
Usage: "unpack applies layers from a manifest to a snapshot",
|
2017-03-09 06:04:44 +00:00
|
|
|
ArgsUsage: "[flags] <digest>",
|
|
|
|
Flags: []cli.Flag{},
|
|
|
|
Action: func(clicontext *cli.Context) error {
|
|
|
|
var (
|
|
|
|
ctx = background
|
|
|
|
)
|
|
|
|
|
|
|
|
dgst, err := digest.Parse(clicontext.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:34:32 +00:00
|
|
|
log.G(ctx).Infof("unpacking layers from manifest %s", dgst.String())
|
2017-03-09 06:04:44 +00:00
|
|
|
|
|
|
|
conn, err := connectGRPC(clicontext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := contentservice.NewProviderFromClient(contentapi.NewContentClient(conn))
|
|
|
|
m, err := resolveManifest(ctx, provider, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:34:32 +00:00
|
|
|
unpacker := rootfsservice.NewUnpackerFromClient(rootfsapi.NewRootFSClient(conn))
|
|
|
|
chainID, err := unpacker.Unpack(ctx, m.Layers)
|
2017-03-09 06:04:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.G(ctx).Infof("chain ID: %s", chainID.String())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:17:25 +00:00
|
|
|
var rootfsPrepareCommand = cli.Command{
|
2017-03-15 06:34:32 +00:00
|
|
|
Name: "prepare",
|
|
|
|
Usage: "prepare gets mount commands for digest",
|
2017-03-10 03:50:23 +00:00
|
|
|
ArgsUsage: "[flags] <digest> <target>",
|
|
|
|
Flags: []cli.Flag{},
|
|
|
|
Action: func(clicontext *cli.Context) error {
|
|
|
|
var (
|
|
|
|
ctx = background
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-03-15 06:34:32 +00:00
|
|
|
log.G(ctx).Infof("preparing mounts %s", dgst.String())
|
2017-03-10 03:50:23 +00:00
|
|
|
|
|
|
|
conn, err := connectGRPC(clicontext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rclient := rootfsapi.NewRootFSClient(conn)
|
|
|
|
|
2017-03-15 06:34:32 +00:00
|
|
|
ir := &rootfsapi.PrepareRequest{
|
2017-03-10 03:50:23 +00:00
|
|
|
Name: target,
|
|
|
|
ChainID: dgst,
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:34:32 +00:00
|
|
|
resp, err := rclient.Prepare(ctx, ir)
|
2017-03-10 03:50:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range resp.Mounts {
|
|
|
|
fmt.Fprintf(os.Stdout, "mount -t %s %s %s -o %s\n", m.Type, m.Source, target, strings.Join(m.Options, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-09 06:04:44 +00:00
|
|
|
func resolveManifest(ctx context.Context, provider content.Provider, dgst digest.Digest) (ocispec.Manifest, error) {
|
|
|
|
p, err := readAll(ctx, provider, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return ocispec.Manifest{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): This assumption that we get a manifest is unfortunate.
|
|
|
|
// Need to provide way to resolve what the type of the target is.
|
|
|
|
var manifest ocispec.Manifest
|
|
|
|
if err := json.Unmarshal(p, &manifest); err != nil {
|
|
|
|
return ocispec.Manifest{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAll(ctx context.Context, provider content.Provider, dgst digest.Digest) ([]byte, error) {
|
|
|
|
rc, err := provider.Reader(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
|
|
|
|
return ioutil.ReadAll(rc)
|
|
|
|
}
|