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")
|
2018-12-26 10:48:01 +00:00
|
|
|
flag.StringVar(&opt.containerd, "containerd", "v1.2.1", "containerd version")
|
2019-02-11 14:41:25 +00:00
|
|
|
flag.StringVar(&opt.runc, "runc", "6635b4f0c6af3810594d2770f662f34ddc15b40d", "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
|
|
|
|
|
2018-06-23 00:31:55 +00:00
|
|
|
dt, err := out.Marshal(llb.LinuxAmd64)
|
2017-06-21 21:48:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
llb.WriteTo(dt, os.Stdout)
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func goBuildBase() llb.State {
|
2019-02-26 06:46:12 +00:00
|
|
|
goAlpine := llb.Image("docker.io/library/golang:1.12-alpine")
|
2017-06-21 21:48:44 +00:00
|
|
|
return goAlpine.
|
|
|
|
AddEnv("PATH", "/usr/local/go/bin:"+system.DefaultPathEnv).
|
|
|
|
AddEnv("GOPATH", "/go").
|
2018-05-17 08:27:43 +00:00
|
|
|
Run(llb.Shlex("apk add --no-cache g++ linux-headers libseccomp-dev 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-18 07:33:02 +00:00
|
|
|
buildkitdOCIWorkerOnly := run(llb.Shlex("go build -o ./bin/buildkitd.oci_only -tags no_containerd_worker ./cmd/buildkitd"))
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-12-18 07:33:02 +00:00
|
|
|
buildkitd := run(llb.Shlex("go build -o ./bin/buildkitd ./cmd/buildkitd"))
|
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-18 07:33:02 +00:00
|
|
|
copyAll(buildkitd, "/bin"))
|
2017-06-26 17:56:09 +00:00
|
|
|
}
|
2017-12-18 07:33:02 +00:00
|
|
|
return r.With(copyAll(buildkitdOCIWorkerOnly, "/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)
|
|
|
|
}
|