Skip to content

Instantly share code, notes, and snippets.

@aaronlehmann
Created April 14, 2021 04:36
Show Gist options
  • Save aaronlehmann/cfeaefc028df8127fb85b9b5f9125f2d to your computer and use it in GitHub Desktop.
Save aaronlehmann/cfeaefc028df8127fb85b9b5f9125f2d to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"os"
"path/filepath"
_ "github.com/moby/buildkit/client/connhelper/dockercontainer"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/progress/progresswriter"
"golang.org/x/sync/errgroup"
)
func main() {
err := run(context.Background(), false)
if err != nil {
fmt.Fprintf(os.Stderr, "err: %s\n", err)
os.Exit(1)
}
err = run(context.Background(), true)
if err != nil {
fmt.Fprintf(os.Stderr, "err: %s\n", err)
os.Exit(1)
}
}
const (
busyboxLatest = "busybox:latest@sha256:ae39a6f5c07297d7ab64dbd4f82c77c874cc6a94cea29fdec309d0992574b4f7"
busyboxGlibc = "busybox:latest@sha256:efa4903894afd1b59358ec0b8dda6959fe9f43f9ba97bc270743fe31aea78d93"
)
func run(ctx context.Context, pass2 bool) error {
st := llb.Image(busyboxLatest, llb.LinuxAmd64)
fromImage := llb.Image(busyboxLatest, llb.LinuxAmd64)
imageRef := busyboxGlibc
if pass2 {
imageRef = busyboxLatest
}
toImage := llb.Image(imageRef, llb.LinuxAmd64)
st = st.Run(
llb.Shlex(`sh -c "diff -rN /from /to > /out/diff.out || true"`),
llb.AddMount("/from", fromImage, llb.Readonly),
llb.AddMount("/to", toImage, llb.Readonly),
).AddMount("/out", llb.Scratch())
def, err := st.Marshal(ctx)
if err != nil {
return err
}
addr := os.Getenv("BUILDKIT_HOST")
if addr == "" {
addr = appdefaults.Address
}
c, err := client.New(ctx, addr, client.WithFailFast())
if err != nil {
return err
}
pw, err := progresswriter.NewPrinter(context.TODO(), os.Stderr, "auto")
if err != nil {
return err
}
mw := progresswriter.NewMultiWriter(pw)
eg, ctx := errgroup.WithContext(ctx)
cwd, err := os.Getwd()
if err != nil {
return err
}
outputDir := filepath.Join(cwd, "pass1")
if pass2 {
outputDir = filepath.Join(cwd, "pass2")
}
eg.Go(func() error {
solveOpt := client.SolveOpt{
Exports: []client.ExportEntry{{
Type: client.ExporterLocal,
OutputDir: outputDir,
}},
}
_, err := c.Solve(ctx, def, solveOpt, progresswriter.ResetTime(mw.WithPrefix("", false)).Status())
return err
})
eg.Go(func() error {
<-pw.Done()
return pw.Err()
})
return eg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment