Skip to content

Instantly share code, notes, and snippets.

@kunalkushwaha
Created November 16, 2018 07:25
Show Gist options
  • Select an option

  • Save kunalkushwaha/bae5fdd062107a9379d476a034ffaa62 to your computer and use it in GitHub Desktop.

Select an option

Save kunalkushwaha/bae5fdd062107a9379d476a034ffaa62 to your computer and use it in GitHub Desktop.
buildkit example which prints output of intermediate steps
package main
import (
"bufio"
"flag"
"log"
"os"
"strings"
"github.com/moby/buildkit/client/llb"
)
const (
defaultImage = "docker.io/library/alpine:latest"
)
func main() {
scriptFile := flag.String("f", "", "input file")
flag.Parse()
if *scriptFile == "" {
log.Fatal("requires input file", *scriptFile)
os.Exit(1)
}
file, err := os.Open(*scriptFile)
if err != nil {
log.Fatal("cannot open file : ", *scriptFile)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan()
baseImage := scanner.Text()
if baseImage == "" {
baseImage = defaultImage
}
bk := buildkit(baseImage)
for scanner.Scan() {
cmd := scanner.Text()
if strings.HasPrefix(cmd, "#") {
continue
}
log.Println(cmd)
out := bk.Run(llb.Shlex(cmd)).Root() // debug output
dt, err := out.Marshal()
if err != nil {
panic(err)
}
llb.WriteTo(dt, os.Stdout)
bk = out
}
}
func goBuildBase(baseImage string) llb.State {
goBase := llb.Image(baseImage)
return goBase.
AddEnv("http_proxy", os.Getenv("http_proxy")).
AddEnv("https_proxy", os.Getenv("https_proxy")).
Run(llb.Shlex("echo \"Buildkit Shell\"")).Root()
}
func buildkit(baseImage string) llb.State {
return goBuildBase(baseImage)
}
@kunalkushwaha
Copy link
Author

input example

docker.io/library/golang:latest
#echo hello
ip route
#/usr/bin/apt-get update  
#/usr/bin/apt-get install -y tcpdump
#/usr/bin/apt-get install vim
ping -v -c10 172.20.20.50
#ping -v -c4 172.19.0.3
#ip addr
whereis tcpdump
#mv /usr/sbin/tcpdump /usr/bin/tcpdump
#/usr/bin/curl http://www.google.com
#/usr/bin/tcpdump -i enp1s0 -c 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment