2017-11-21 08:08:36 +00:00
|
|
|
package runc
|
2017-06-08 00:54:29 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-14 06:26:27 +00:00
|
|
|
"context"
|
2017-06-08 00:54:29 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-11-07 23:04:16 +00:00
|
|
|
"github.com/boltdb/bolt"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/containerd/containerd/content/local"
|
2017-11-06 09:44:23 +00:00
|
|
|
"github.com/containerd/containerd/diff/walking"
|
2017-11-21 08:08:36 +00:00
|
|
|
ctdmetadata "github.com/containerd/containerd/metadata"
|
2017-12-05 07:34:25 +00:00
|
|
|
ctdsnapshot "github.com/containerd/containerd/snapshots"
|
|
|
|
"github.com/containerd/containerd/snapshots/overlay"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
|
|
|
"github.com/moby/buildkit/executor/runcexecutor"
|
2017-12-28 06:29:50 +00:00
|
|
|
"github.com/moby/buildkit/snapshot/nogc"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/worker/base"
|
2017-06-08 00:54:29 +00:00
|
|
|
)
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
// NewWorkerOpt creates a WorkerOpt.
|
|
|
|
// But it does not set the following fields:
|
|
|
|
// - SessionManager
|
2017-12-19 09:34:34 +00:00
|
|
|
func NewWorkerOpt(root string, labels map[string]string) (base.WorkerOpt, error) {
|
2017-12-15 08:06:54 +00:00
|
|
|
var opt base.WorkerOpt
|
2017-12-19 09:34:34 +00:00
|
|
|
name := "runc-overlayfs"
|
2017-11-21 08:08:36 +00:00
|
|
|
root = filepath.Join(root, name)
|
2017-06-08 00:54:29 +00:00
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
2017-12-15 07:44:09 +00:00
|
|
|
md, err := metadata.NewStore(filepath.Join(root, "metadata.db"))
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
2017-11-21 08:08:36 +00:00
|
|
|
exe, err := runcexecutor.New(filepath.Join(root, "executor"))
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
|
|
|
s, err := overlay.NewSnapshotter(filepath.Join(root, "snapshots"))
|
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:04:16 +00:00
|
|
|
db, err := bolt.Open(filepath.Join(root, "containerdmeta.db"), 0644, nil)
|
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-11-07 23:04:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
mdb := ctdmetadata.NewDB(db, c, map[string]ctdsnapshot.Snapshotter{
|
2017-12-19 09:34:34 +00:00
|
|
|
"overlayfs": s,
|
2017-11-07 23:04:16 +00:00
|
|
|
})
|
|
|
|
if err := mdb.Init(context.TODO()); err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-11-07 23:04:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 06:29:50 +00:00
|
|
|
gc := func(ctx context.Context) error {
|
|
|
|
_, err := mdb.GarbageCollect(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c = nogc.NewContentStore(mdb.ContentStore(), "buildkit", gc)
|
2017-11-06 09:44:23 +00:00
|
|
|
df, err := walking.NewWalkingDiff(c)
|
2017-07-10 20:03:38 +00:00
|
|
|
if err != nil {
|
2017-11-21 08:08:36 +00:00
|
|
|
return opt, err
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
2017-06-08 00:54:29 +00:00
|
|
|
|
2017-12-19 09:34:34 +00:00
|
|
|
id, err := base.ID(root)
|
|
|
|
if err != nil {
|
|
|
|
return opt, err
|
|
|
|
}
|
|
|
|
xlabels := base.Labels("oci", "overlayfs")
|
|
|
|
for k, v := range labels {
|
|
|
|
xlabels[k] = v
|
|
|
|
}
|
2017-12-15 08:06:54 +00:00
|
|
|
opt = base.WorkerOpt{
|
2017-12-19 09:34:34 +00:00
|
|
|
ID: id,
|
|
|
|
Labels: xlabels,
|
2017-11-21 08:08:36 +00:00
|
|
|
MetadataStore: md,
|
|
|
|
Executor: exe,
|
2017-12-28 06:29:50 +00:00
|
|
|
BaseSnapshotter: nogc.NewSnapshotter(mdb.Snapshotter("overlayfs"), "buildkit", gc),
|
2017-11-21 08:08:36 +00:00
|
|
|
ContentStore: c,
|
|
|
|
Applier: df,
|
|
|
|
Differ: df,
|
|
|
|
ImageStore: nil, // explicitly
|
|
|
|
}
|
|
|
|
return opt, nil
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|