2017-06-08 00:54:29 +00:00
|
|
|
// +build standalone
|
|
|
|
|
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2017-06-14 06:26:27 +00:00
|
|
|
"context"
|
2017-06-08 00:54:29 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/containerd/containerd/content/local"
|
2017-07-10 20:03:38 +00:00
|
|
|
"github.com/containerd/containerd/differ"
|
2017-06-08 00:54:29 +00:00
|
|
|
"github.com/containerd/containerd/mount"
|
2017-06-14 06:26:27 +00:00
|
|
|
"github.com/containerd/containerd/namespaces"
|
|
|
|
ctdsnapshot "github.com/containerd/containerd/snapshot"
|
2017-06-08 00:54:29 +00:00
|
|
|
"github.com/containerd/containerd/snapshot/overlay"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/worker/runcworker"
|
2017-06-08 00:54:29 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewStandalone(root string) (*Controller, error) {
|
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to create %s", root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: take lock to make sure there are no duplicates
|
|
|
|
|
2017-06-19 20:39:00 +00:00
|
|
|
pd, err := newStandalonePullDeps(root)
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opt, err := defaultControllerOpts(root, *pd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := runcworker.New(filepath.Join(root, "runc"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.Worker = w
|
|
|
|
|
|
|
|
return NewController(*opt)
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:39:00 +00:00
|
|
|
func newStandalonePullDeps(root string) (*pullDeps, error) {
|
2017-06-08 00:54:29 +00:00
|
|
|
s, err := overlay.NewSnapshotter(filepath.Join(root, "snapshots"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-19 01:05:19 +00:00
|
|
|
c, err := local.NewStore(filepath.Join(root, "content"))
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:03:38 +00:00
|
|
|
differ, err := differ.NewBaseDiff(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-08 00:54:29 +00:00
|
|
|
|
|
|
|
return &pullDeps{
|
2017-06-14 06:26:27 +00:00
|
|
|
Snapshotter: &nsSnapshotter{s},
|
2017-06-08 00:54:29 +00:00
|
|
|
ContentStore: c,
|
2017-07-10 20:03:38 +00:00
|
|
|
Applier: differ,
|
|
|
|
Differ: differ,
|
2017-06-08 00:54:29 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-06-14 06:26:27 +00:00
|
|
|
// this should be supported by containerd. currently packages are unusable without wrapping
|
|
|
|
const dummyNs = "buildkit"
|
|
|
|
|
|
|
|
type nsSnapshotter struct {
|
|
|
|
ctdsnapshot.Snapshotter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *nsSnapshotter) Stat(ctx context.Context, key string) (ctdsnapshot.Info, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
|
|
return s.Snapshotter.Stat(ctx, key)
|
|
|
|
}
|
2017-08-18 06:27:12 +00:00
|
|
|
|
|
|
|
func (s *nsSnapshotter) Update(ctx context.Context, info ctdsnapshot.Info, fieldpaths ...string) (ctdsnapshot.Info, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
|
|
return s.Snapshotter.Update(ctx, info, fieldpaths...)
|
|
|
|
}
|
|
|
|
|
2017-06-14 06:26:27 +00:00
|
|
|
func (s *nsSnapshotter) Usage(ctx context.Context, key string) (ctdsnapshot.Usage, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
|
|
return s.Snapshotter.Usage(ctx, key)
|
|
|
|
}
|
|
|
|
func (s *nsSnapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
|
|
return s.Snapshotter.Mounts(ctx, key)
|
|
|
|
}
|
2017-08-18 06:27:12 +00:00
|
|
|
func (s *nsSnapshotter) Prepare(ctx context.Context, key, parent string, opts ...ctdsnapshot.Opt) ([]mount.Mount, error) {
|
2017-06-14 06:26:27 +00:00
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
2017-08-18 06:27:12 +00:00
|
|
|
return s.Snapshotter.Prepare(ctx, key, parent, opts...)
|
2017-06-14 06:26:27 +00:00
|
|
|
}
|
2017-08-18 06:27:12 +00:00
|
|
|
func (s *nsSnapshotter) View(ctx context.Context, key, parent string, opts ...ctdsnapshot.Opt) ([]mount.Mount, error) {
|
2017-06-14 06:26:27 +00:00
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
2017-08-18 06:27:12 +00:00
|
|
|
return s.Snapshotter.View(ctx, key, parent, opts...)
|
2017-06-14 06:26:27 +00:00
|
|
|
}
|
2017-08-18 06:27:12 +00:00
|
|
|
func (s *nsSnapshotter) Commit(ctx context.Context, name, key string, opts ...ctdsnapshot.Opt) error {
|
2017-06-14 06:26:27 +00:00
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
2017-08-18 06:27:12 +00:00
|
|
|
return s.Snapshotter.Commit(ctx, name, key, opts...)
|
2017-06-14 06:26:27 +00:00
|
|
|
}
|
|
|
|
func (s *nsSnapshotter) Remove(ctx context.Context, key string) error {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
|
|
return s.Snapshotter.Remove(ctx, key)
|
|
|
|
}
|
|
|
|
func (s *nsSnapshotter) Walk(ctx context.Context, fn func(context.Context, ctdsnapshot.Info) error) error {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
|
|
return s.Snapshotter.Walk(ctx, fn)
|
|
|
|
}
|