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 {
|
|
|
|
target string
|
|
|
|
containerd string
|
2017-07-07 21:35:10 +00:00
|
|
|
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
|
|
|
|
flag.StringVar(&opt.target, "target", "containerd", "target (standalone, containerd)")
|
2017-09-19 11:21:55 +00:00
|
|
|
flag.StringVar(&opt.containerd, "containerd", "v1.0.0-beta.1", "containerd version")
|
|
|
|
flag.StringVar(&opt.runc, "runc", "v1.0.0-rc4", "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-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-07-10 17:47:52 +00:00
|
|
|
builddStandalone := src.
|
|
|
|
Run(llb.Shlex("go build -o /bin/buildd-standalone -tags standalone ./cmd/buildd")).Root()
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-07-10 17:47:52 +00:00
|
|
|
builddContainerd := src.
|
|
|
|
Run(llb.Shlex("go build -o /bin/buildd-containerd -tags containerd ./cmd/buildd")).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-06-29 23:22:37 +00:00
|
|
|
if opt.target == "containerd" {
|
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/"),
|
|
|
|
copyFrom(builddContainerd, "/bin/buildd-containerd", "/bin/"))
|
2017-06-26 17:56:09 +00:00
|
|
|
}
|
2017-07-10 17:47:52 +00:00
|
|
|
return r.With(copyFrom(builddStandalone, "/bin/buildd-standalone", "/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)
|
|
|
|
}
|