2017-07-13 00:32:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"github.com/moby/buildkit/util/system"
|
|
|
|
)
|
|
|
|
|
|
|
|
type buildOpt struct {
|
2017-12-15 07:00:13 +00:00
|
|
|
withContainerd bool
|
|
|
|
containerd string
|
|
|
|
runc string
|
|
|
|
buildkit string
|
2017-07-13 00:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var opt buildOpt
|
2017-12-15 07:00:13 +00:00
|
|
|
flag.BoolVar(&opt.withContainerd, "with-containerd", true, "enable containerd worker")
|
2017-12-05 07:34:25 +00:00
|
|
|
flag.StringVar(&opt.containerd, "containerd", "v1.0.0", "containerd version")
|
2017-11-06 09:44:23 +00:00
|
|
|
flag.StringVar(&opt.runc, "runc", "74a17296470088de3805e138d3d87c62e613dfc4", "runc version")
|
2017-07-20 22:13:00 +00:00
|
|
|
flag.StringVar(&opt.buildkit, "buildkit", "master", "buildkit version")
|
2017-07-13 00:32:09 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
bk := buildkit(opt)
|
2017-08-03 22:24:02 +00:00
|
|
|
out := bk
|
2017-07-13 00:32:09 +00:00
|
|
|
dt, err := out.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
llb.WriteTo(dt, os.Stdout)
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func goBuildBase() llb.State {
|
2017-11-02 01:11:38 +00:00
|
|
|
goAlpine := llb.Image("docker.io/library/golang:1.9-alpine")
|
2017-07-13 00:32:09 +00:00
|
|
|
return goAlpine.
|
|
|
|
AddEnv("PATH", "/usr/local/go/bin:"+system.DefaultPathEnv).
|
|
|
|
AddEnv("GOPATH", "/go").
|
|
|
|
Run(llb.Shlex("apk add --no-cache g++ linux-headers make")).Root()
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func goRepo(s llb.State, repo string, src llb.State) func(ro ...llb.RunOption) llb.State {
|
2017-07-13 00:32:09 +00:00
|
|
|
dir := "/go/src/" + repo
|
2017-07-23 17:33:19 +00:00
|
|
|
return func(ro ...llb.RunOption) llb.State {
|
2017-07-13 00:32:09 +00:00
|
|
|
es := s.Dir(dir).Run(ro...)
|
2017-07-18 20:29:52 +00:00
|
|
|
es.AddMount(dir, src, llb.Readonly)
|
|
|
|
return es.AddMount("/out", llb.Scratch())
|
2017-07-13 00:32:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func runc(version string) llb.State {
|
2017-07-13 00:32:09 +00:00
|
|
|
repo := "github.com/opencontainers/runc"
|
2017-07-20 22:13:00 +00:00
|
|
|
src := llb.Git(repo, version)
|
|
|
|
if version == "local" {
|
|
|
|
src = llb.Local("runc-src")
|
|
|
|
}
|
|
|
|
return goRepo(goBuildBase(), repo, src)(
|
2017-07-18 20:29:52 +00:00
|
|
|
llb.Shlex("go build -o /out/runc ./"),
|
2017-07-13 00:32:09 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func containerd(version string) llb.State {
|
2017-07-13 00:32:09 +00:00
|
|
|
repo := "github.com/containerd/containerd"
|
2017-07-20 22:13:00 +00:00
|
|
|
src := llb.Git(repo, version, llb.KeepGitDir())
|
|
|
|
if version == "local" {
|
|
|
|
src = llb.Local("containerd-src")
|
|
|
|
}
|
2017-07-13 00:32:09 +00:00
|
|
|
return goRepo(
|
|
|
|
goBuildBase().
|
|
|
|
Run(llb.Shlex("apk add --no-cache btrfs-progs-dev")).Root(),
|
2017-07-20 22:13:00 +00:00
|
|
|
repo, src)(
|
2017-07-18 20:29:52 +00:00
|
|
|
llb.Shlex("go build -o /out/containerd ./cmd/containerd"),
|
2017-07-13 00:32:09 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func buildkit(opt buildOpt) llb.State {
|
2017-07-13 00:32:09 +00:00
|
|
|
repo := "github.com/moby/buildkit"
|
2017-08-10 01:20:33 +00:00
|
|
|
src := llb.Git(repo, opt.buildkit)
|
2017-07-20 22:13:00 +00:00
|
|
|
if opt.buildkit == "local" {
|
2017-07-13 00:32:09 +00:00
|
|
|
src = llb.Local("buildkit-src")
|
|
|
|
}
|
|
|
|
run := goRepo(goBuildBase(), repo, src)
|
|
|
|
|
2017-12-18 07:33:02 +00:00
|
|
|
buildkitdOCIWorkerOnly := run(llb.Shlex("go build -o /out/buildkitd.oci_only -tags no_containerd_worker ./cmd/buildkitd"))
|
2017-07-13 00:32:09 +00:00
|
|
|
|
2017-12-18 07:33:02 +00:00
|
|
|
buildkitd := run(llb.Shlex("go build -o /out/buildkitd ./cmd/buildkitd"))
|
2017-07-13 00:32:09 +00:00
|
|
|
|
2017-07-18 20:29:52 +00:00
|
|
|
buildctl := run(llb.Shlex("go build -o /out/buildctl ./cmd/buildctl"))
|
2017-07-13 00:32:09 +00:00
|
|
|
|
2017-08-03 22:24:02 +00:00
|
|
|
r := llb.Scratch().With(
|
|
|
|
copyAll(buildctl, "/"),
|
|
|
|
copyAll(runc(opt.runc), "/"),
|
2017-07-13 00:32:09 +00:00
|
|
|
)
|
|
|
|
|
2017-12-15 07:00:13 +00:00
|
|
|
if opt.withContainerd {
|
2017-07-13 00:32:09 +00:00
|
|
|
return r.With(
|
2017-08-03 22:24:02 +00:00
|
|
|
copyAll(containerd(opt.containerd), "/"),
|
2017-12-18 07:33:02 +00:00
|
|
|
copyAll(buildkitd, "/"))
|
2017-07-13 00:32:09 +00:00
|
|
|
}
|
2017-12-18 07:33:02 +00:00
|
|
|
return r.With(copyAll(buildkitdOCIWorkerOnly, "/"))
|
2017-07-13 00:32:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func copyAll(src llb.State, destPath string) llb.StateOption {
|
2017-07-13 00:32:09 +00:00
|
|
|
return copyFrom(src, "/.", destPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyFrom has similar semantics as `COPY --from`
|
2017-07-23 17:33:19 +00:00
|
|
|
func copyFrom(src llb.State, srcPath, destPath string) llb.StateOption {
|
|
|
|
return func(s llb.State) llb.State {
|
2017-07-13 00:32:09 +00:00
|
|
|
return copy(src, srcPath, s, destPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy copies files between 2 states using cp until there is no copyOp
|
2017-07-23 17:33:19 +00:00
|
|
|
func copy(src llb.State, srcPath string, dest llb.State, destPath string) llb.State {
|
2017-08-10 01:20:33 +00:00
|
|
|
cpImage := llb.Image("docker.io/library/alpine:latest@sha256:1072e499f3f655a032e88542330cf75b02e7bdf673278f701d7ba61629ee3ebe")
|
2017-09-15 20:21:02 +00:00
|
|
|
cp := cpImage.Run(llb.Shlexf("cp -a /src%s /dest%s", srcPath, destPath))
|
2017-07-20 22:13:00 +00:00
|
|
|
cp.AddMount("/src", src, llb.Readonly)
|
2017-07-13 00:32:09 +00:00
|
|
|
return cp.AddMount("/dest", dest)
|
|
|
|
}
|