2017-07-10 17:47:52 +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
|
2017-07-10 17:47:52 +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")
|
2018-12-26 10:48:01 +00:00
|
|
|
flag.StringVar(&opt.containerd, "containerd", "v1.2.1", "containerd version")
|
2019-05-15 21:22:42 +00:00
|
|
|
flag.StringVar(&opt.runc, "runc", "v1.0.0-rc8", "runc version")
|
2017-07-10 17:47:52 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
bk := buildkit(opt)
|
|
|
|
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-07-10 17:47:52 +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-07-10 17:47:52 +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")).
|
2018-05-17 08:27:43 +00:00
|
|
|
Run(llb.Shlex("apk add --no-cache git libseccomp-dev make")).Root()
|
2017-07-10 17:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func runc(version string) llb.State {
|
2017-07-10 17:47:52 +00:00
|
|
|
return goBuildBase().
|
|
|
|
Run(llb.Shlex("git clone https://github.com/opencontainers/runc.git /go/src/github.com/opencontainers/runc")).
|
|
|
|
Dir("/go/src/github.com/opencontainers/runc").
|
|
|
|
Run(llb.Shlexf("git checkout -q %s", version)).
|
|
|
|
Run(llb.Shlex("go build -o /usr/bin/runc ./")).Root()
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func containerd(version string) llb.State {
|
2017-07-10 17:47:52 +00:00
|
|
|
return goBuildBase().
|
|
|
|
Run(llb.Shlex("apk add --no-cache btrfs-progs-dev")).
|
|
|
|
Run(llb.Shlex("git clone https://github.com/containerd/containerd.git /go/src/github.com/containerd/containerd")).
|
|
|
|
Dir("/go/src/github.com/containerd/containerd").
|
|
|
|
Run(llb.Shlexf("git checkout -q %s", version)).
|
|
|
|
Run(llb.Shlex("make bin/containerd")).Root()
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func buildkit(opt buildOpt) llb.State {
|
2017-07-10 17:47:52 +00:00
|
|
|
src := goBuildBase().
|
|
|
|
Run(llb.Shlex("git clone https://github.com/moby/buildkit.git /go/src/github.com/moby/buildkit")).
|
|
|
|
Dir("/go/src/github.com/moby/buildkit")
|
|
|
|
|
2017-12-18 07:33:02 +00:00
|
|
|
buildkitdOCIWorkerOnly := src.
|
|
|
|
Run(llb.Shlex("go build -o /bin/buildkitd.oci_only -tags no_containerd_worker ./cmd/buildkitd"))
|
2017-07-10 17:47:52 +00:00
|
|
|
|
2017-12-18 07:33:02 +00:00
|
|
|
buildkitd := src.
|
|
|
|
Run(llb.Shlex("go build -o /bin/buildkitd ./cmd/buildkitd"))
|
2017-07-10 17:47:52 +00:00
|
|
|
|
|
|
|
buildctl := src.
|
|
|
|
Run(llb.Shlex("go build -o /bin/buildctl ./cmd/buildctl"))
|
|
|
|
|
|
|
|
r := llb.Image("docker.io/library/alpine:latest")
|
|
|
|
r = copy(buildctl.Root(), "/bin/buildctl", r, "/bin/")
|
|
|
|
r = copy(runc(opt.runc), "/usr/bin/runc", r, "/bin/")
|
2017-12-15 07:00:13 +00:00
|
|
|
if opt.withContainerd {
|
2017-07-10 17:47:52 +00:00
|
|
|
r = copy(containerd(opt.containerd), "/go/src/github.com/containerd/containerd/bin/containerd", r, "/bin/")
|
2017-12-18 07:33:02 +00:00
|
|
|
r = copy(buildkitd.Root(), "/bin/buildkitd", r, "/bin/")
|
2017-07-10 17:47:52 +00:00
|
|
|
} else {
|
2017-12-18 07:33:02 +00:00
|
|
|
r = copy(buildkitdOCIWorkerOnly.Root(), "/bin/buildkitd.oci_only", r, "/bin/")
|
2017-07-10 17:47:52 +00:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func copy(src llb.State, srcPath string, dest llb.State, destPath string) llb.State {
|
2017-07-10 17:47:52 +00:00
|
|
|
cpImage := llb.Image("docker.io/library/alpine:latest")
|
|
|
|
cp := cpImage.Run(llb.Shlexf("cp -a /src%s /dest%s", srcPath, destPath))
|
|
|
|
cp.AddMount("/src", src)
|
|
|
|
return cp.AddMount("/dest", dest)
|
|
|
|
}
|