2017-06-21 21:48:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"github.com/moby/buildkit/util/system"
|
2017-06-21 21:48:44 +00:00
|
|
|
)
|
|
|
|
|
2017-06-29 23:22:37 +00:00
|
|
|
type buildOpt struct {
|
2017-12-15 07:00:13 +00:00
|
|
|
withContainerd bool
|
|
|
|
containerd string
|
|
|
|
runc string
|
2017-06-29 23:22:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 21:48:44 +00:00
|
|
|
func main() {
|
2017-06-29 23:22:37 +00:00
|
|
|
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-06-21 21:48:44 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-06-29 23:22:37 +00:00
|
|
|
bk := buildkit(opt)
|
2017-06-21 21:48:44 +00:00
|
|
|
out := bk.Run(llb.Shlex("ls -l /bin")) // debug output
|
|
|
|
|
|
|
|
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-06-21 21:48:44 +00:00
|
|
|
return goAlpine.
|
|
|
|
AddEnv("PATH", "/usr/local/go/bin:"+system.DefaultPathEnv).
|
|
|
|
AddEnv("GOPATH", "/go").
|
2017-07-09 23:58:29 +00:00
|
|
|
Run(llb.Shlex("apk add --no-cache g++ linux-headers make")).Root()
|
2017-06-21 21:48:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func goRepo(s llb.State, repo, ref string, g ...llb.GitOption) func(ro ...llb.RunOption) llb.State {
|
2017-07-09 23:58:29 +00:00
|
|
|
dir := "/go/src/" + repo
|
2017-07-23 17:33:19 +00:00
|
|
|
return func(ro ...llb.RunOption) llb.State {
|
2017-07-09 23:58:29 +00:00
|
|
|
es := s.Dir(dir).Run(ro...)
|
|
|
|
es.AddMount(dir, llb.Git(repo, ref, g...))
|
|
|
|
return es.AddMount(dir+"/bin", llb.Scratch())
|
|
|
|
}
|
|
|
|
}
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func runc(version string) llb.State {
|
2017-07-09 23:58:29 +00:00
|
|
|
return goRepo(goBuildBase(), "github.com/opencontainers/runc", version)(
|
|
|
|
llb.Shlex("go build -o ./bin/runc ./"),
|
|
|
|
)
|
2017-06-21 21:48:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func containerd(version string) llb.State {
|
2017-07-09 23:58:29 +00:00
|
|
|
return goRepo(
|
|
|
|
goBuildBase().
|
|
|
|
Run(llb.Shlex("apk add --no-cache btrfs-progs-dev")).Root(),
|
|
|
|
"github.com/containerd/containerd", version, llb.KeepGitDir())(
|
|
|
|
llb.Shlex("make bin/containerd"),
|
|
|
|
)
|
2017-06-21 21:48:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func buildkit(opt buildOpt) llb.State {
|
2017-07-09 23:58:29 +00:00
|
|
|
run := goRepo(goBuildBase(), "github.com/moby/buildkit", "master")
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-12-15 07:00:13 +00:00
|
|
|
builddOCIWorkerOnly := run(llb.Shlex("go build -o ./bin/buildd.oci_only -tags no_containerd_worker ./cmd/buildd"))
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-12-15 07:00:13 +00:00
|
|
|
buildd := run(llb.Shlex("go build -o ./bin/buildd ./cmd/buildd"))
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-07-09 23:58:29 +00:00
|
|
|
buildctl := run(llb.Shlex("go build -o ./bin/buildctl ./cmd/buildctl"))
|
2017-06-26 17:56:09 +00:00
|
|
|
|
|
|
|
r := llb.Image("docker.io/library/alpine:latest").With(
|
2017-07-09 23:58:29 +00:00
|
|
|
copyAll(buildctl, "/bin"),
|
|
|
|
copyAll(runc(opt.runc), "/bin"),
|
2017-06-26 17:56:09 +00:00
|
|
|
)
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-12-15 07:00:13 +00:00
|
|
|
if opt.withContainerd {
|
2017-06-26 17:56:09 +00:00
|
|
|
return r.With(
|
2017-07-09 23:58:29 +00:00
|
|
|
copyAll(containerd(opt.containerd), "/bin"),
|
2017-12-15 07:00:13 +00:00
|
|
|
copyAll(buildd, "/bin"))
|
2017-06-26 17:56:09 +00:00
|
|
|
}
|
2017-12-15 07:00:13 +00:00
|
|
|
return r.With(copyAll(builddOCIWorkerOnly, "/bin"))
|
2017-06-26 17:56:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func copyAll(src llb.State, destPath string) llb.StateOption {
|
2017-07-09 23:58:29 +00:00
|
|
|
return copyFrom(src, "/.", destPath)
|
|
|
|
}
|
|
|
|
|
2017-06-26 17:56:09 +00:00
|
|
|
// 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-06-26 17:56:09 +00:00
|
|
|
return copy(src, srcPath, s, destPath)
|
2017-06-21 21:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 17:56:09 +00:00
|
|
|
// 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-06-21 21:48:44 +00:00
|
|
|
cpImage := llb.Image("docker.io/library/alpine:latest")
|
2017-06-26 18:51:27 +00:00
|
|
|
cp := cpImage.Run(llb.Shlexf("cp -a /src%s /dest%s", srcPath, destPath))
|
2017-06-21 21:48:44 +00:00
|
|
|
cp.AddMount("/src", src)
|
|
|
|
return cp.AddMount("/dest", dest)
|
|
|
|
}
|