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-04-03 09:37:16 +00:00
|
|
|
flag.StringVar(&opt.containerd, "containerd", "v1.0.3", "containerd version")
|
2018-02-01 09:00:09 +00:00
|
|
|
flag.StringVar(&opt.runc, "runc", "9f9c96235cc97674e935002fc3d78361b696a69e", "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 {
|
2018-04-03 09:37:16 +00:00
|
|
|
goAlpine := llb.Image("docker.io/library/golang:1.10-alpine")
|
2017-06-21 21:48:44 +00:00
|
|
|
return goAlpine.
|
|
|
|
AddEnv("PATH", "/usr/local/go/bin:"+system.DefaultPathEnv).
|
|
|
|
AddEnv("GOPATH", "/go").
|
2017-07-10 17:47:52 +00:00
|
|
|
Run(llb.Shlex("apk add --no-cache g++ linux-headers")).
|
|
|
|
Run(llb.Shlex("apk add --no-cache git make")).Root()
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
2017-07-07 21:35:10 +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().
|
|
|
|
With(goFromGit("github.com/opencontainers/runc", version)).
|
|
|
|
Run(llb.Shlex("go build -o /usr/bin/runc ./")).
|
|
|
|
Root()
|
2017-06-21 21:48:44 +00:00
|
|
|
}
|
|
|
|
|
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")).
|
|
|
|
With(goFromGit("github.com/containerd/containerd", version)).
|
|
|
|
Run(llb.Shlex("make bin/containerd")).Root()
|
2017-06-21 21:48:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func buildkit(opt buildOpt) llb.State {
|
2017-07-10 17:47:52 +00:00
|
|
|
src := goBuildBase().With(goFromGit("github.com/moby/buildkit", "master"))
|
2017-06-21 21:48:44 +00:00
|
|
|
|
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")).Root()
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-12-18 07:33:02 +00:00
|
|
|
buildkitd := src.
|
|
|
|
Run(llb.Shlex("go build -o /bin/buildkitd ./cmd/buildkitd")).Root()
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-07-10 17:47:52 +00:00
|
|
|
buildctl := src.
|
|
|
|
Run(llb.Shlex("go build -o /bin/buildctl ./cmd/buildctl")).Root()
|
2017-06-26 17:56:09 +00:00
|
|
|
|
|
|
|
r := llb.Image("docker.io/library/alpine:latest").With(
|
2017-07-10 17:47:52 +00:00
|
|
|
copyFrom(buildctl, "/bin/buildctl", "/bin/"),
|
|
|
|
copyFrom(runc(opt.runc), "/usr/bin/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-10 17:47:52 +00:00
|
|
|
copyFrom(containerd(opt.containerd), "/go/src/github.com/containerd/containerd/bin/containerd", "/bin/"),
|
2017-12-18 07:33:02 +00:00
|
|
|
copyFrom(buildkitd, "/bin/buildkitd", "/bin/"))
|
2017-06-26 17:56:09 +00:00
|
|
|
}
|
2017-12-18 07:33:02 +00:00
|
|
|
return r.With(copyFrom(buildkitdOCIWorkerOnly, "/bin/buildkitd.oci_only", "/bin/"))
|
2017-06-26 17:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// goFromGit is a helper for cloning a git repo, checking out a tag and copying
|
|
|
|
// source directory into
|
|
|
|
func goFromGit(repo, tag string) llb.StateOption {
|
2017-07-10 17:47:52 +00:00
|
|
|
src := llb.Image("docker.io/library/alpine:latest").
|
|
|
|
Run(llb.Shlex("apk add --no-cache git")).
|
|
|
|
Run(llb.Shlexf("git clone https://%[1]s.git /go/src/%[1]s", repo)).
|
|
|
|
Dirf("/go/src/%s", repo).
|
|
|
|
Run(llb.Shlexf("git checkout -q %s", tag)).Root()
|
2017-07-23 17:33:19 +00:00
|
|
|
return func(s llb.State) llb.State {
|
2017-07-10 17:47:52 +00:00
|
|
|
return s.With(copyFrom(src, "/go", "/")).Reset(s).Dir(src.GetDir())
|
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)
|
|
|
|
}
|