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-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-07-09 23:58:29 +00:00
|
|
|
builddStandalone := run(llb.Shlex("go build -o ./bin/buildd-standalone -tags standalone ./cmd/buildd"))
|
2017-06-21 21:48:44 +00:00
|
|
|
|
2017-07-09 23:58:29 +00:00
|
|
|
builddContainerd := run(llb.Shlex("go build -o ./bin/buildd-containerd -tags containerd ./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-06-29 23:22:37 +00:00
|
|
|
if opt.target == "containerd" {
|
2017-06-26 17:56:09 +00:00
|
|
|
return r.With(
|
2017-07-09 23:58:29 +00:00
|
|
|
copyAll(containerd(opt.containerd), "/bin"),
|
|
|
|
copyAll(builddContainerd, "/bin"))
|
2017-06-26 17:56:09 +00:00
|
|
|
}
|
2017-07-09 23:58:29 +00:00
|
|
|
return r.With(copyAll(builddStandalone, "/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)
|
|
|
|
}
|