Skip to content

Commit

Permalink
chore(buildkit): Add test cases for builder (#83)
Browse files Browse the repository at this point in the history
* chore(buildkit): Add test cases for builder

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* chore: Update workflow

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Fix download

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Fix

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Add verify

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* chore: Send cov report

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Comment coverage

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* chore: Add go mod cache

Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege authored Apr 28, 2022
1 parent 9534253 commit 62ce39e
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 50 deletions.
46 changes: 42 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,42 @@ on:
- pull_request

jobs:
test:
name: test
runs-on: ubuntu-latest
env:
GOPATH: ${{ github.workspace }}/go
defaults:
run:
working-directory: ${{ env.GOPATH }}/src/github.com/tensorchord/MIDI
steps:
- name: Check out code
uses: actions/checkout@v2
with:
path: ${{ env.GOPATH }}/src/github.com/tensorchord/MIDI
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.17.2
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Verify mockgen
run: |
make generate && git add pkg
git diff --cached --exit-code || (echo 'Please run "make fmt" to verify gofmt' && exit 1);
- name: Test
run: make test
# - name: send coverage
# uses: shogo82148/actions-goveralls@v1
# with:
# path-to-profile: coverage.out
# working-directory: src/github.com/tensorchord/MIDI
# flag-name: unit-test
# parallel: true
build:
name: build
runs-on: ubuntu-latest
Expand All @@ -19,12 +55,16 @@ jobs:
uses: actions/checkout@v2
with:
path: ${{ env.GOPATH }}/src/github.com/tensorchord/MIDI

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.17.2

- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Check Go modules
run: |
-compat=1.17 && git add go.*
Expand All @@ -41,5 +81,3 @@ jobs:
run: |
make vet && git add pkg cmd
git diff --cached --exit-code || (echo 'Please run "make vet" to verify govet' && exit 1);
- name: Build
run: make build
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export GOFLAGS ?= -count=1
#

# All targets.
.PHONY: lint test build container push addlicense debug debug-local build-local
.PHONY: lint test build container push addlicense debug debug-local build-local generate test

build: build-local

Expand All @@ -104,13 +104,20 @@ lint: $(GOLANGCI_LINT)
$(GOLANGCI_LINT):
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(BIN_DIR) v1.23.6

mockgen-install:
go get -u github.com/golang/mock/mockgen

build-local:
@for target in $(TARGETS); do \
CGO_ENABLED=$(CGO_ENABLED) go build -trimpath -v -o $(OUTPUT_DIR)/$${target} \
-ldflags "-s -w -X $(ROOT)/pkg/version.Version=$(VERSION)" \
$(CMD_DIR)/$${target}; \
done

generate: mockgen-install
@mockgen -source pkg/buildkitd/buildkitd.go -destination pkg/buildkitd/mock/mock.go -package mock
@mockgen -source pkg/lang/frontend/starlark/interpreter.go -destination pkg/lang/frontend/starlark/mock/mock.go -package mock

# It is used by vscode to attach into the process.
debug-local:
@for target in $(TARGETS); do \
Expand All @@ -123,6 +130,10 @@ debug-local:
addlicense:
addlicense -c "The MIDI Authors" **/*.go **/**/*.go

test: generate
@go test -race -coverprofile=coverage.out ./...
@go tool cover -func coverage.out | tail -n 1 | awk '{ print "Total coverage: " $$3 }'

.PHONY: clean
clean:
@-rm -vrf ${OUTPUT_DIR}
Expand Down
7 changes: 5 additions & 2 deletions cmd/midi/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func bootstrap(clicontext *cli.Context) error {

if buildkit {
logrus.Debug("bootstrap the buildkitd container")
bkClient := buildkitd.NewClient()
bkClient, err := buildkitd.NewClient(clicontext.Context)
if err != nil {
return errors.Wrap(err, "failed to create buildkit client")
}
defer bkClient.Close()
addr, err := bkClient.Bootstrap(clicontext.Context)
if err != nil {
Expand All @@ -95,7 +98,7 @@ func insertZSHCompleteEntry() error {
return errors.Wrapf(err, "failed to check if %s exists", dirPath)
}
if !dirPathExists {
log.L.Warn("Warning: unable to enable zsh-completion: %s does not exist\n", dirPath)
log.L.Warnf("Warning: unable to enable zsh-completion: %s does not exist", dirPath)
return nil // zsh-completion isn't available, silently fail.
}

Expand Down
9 changes: 4 additions & 5 deletions cmd/midi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
package main

import (
"fmt"
"path/filepath"

"github.com/cockroachdb/errors"
"github.com/spf13/viper"
cli "github.com/urfave/cli/v2"

"github.com/tensorchord/MIDI/pkg/builder"
"github.com/tensorchord/MIDI/pkg/flag"
"github.com/tensorchord/MIDI/pkg/home"
)

Expand Down Expand Up @@ -62,7 +59,9 @@ func build(clicontext *cli.Context) error {

tag := clicontext.String("tag")

builder := builder.New(fmt.Sprintf("docker-container://%s",
viper.GetString(flag.FlagBuildkitdContainer)), config, path, tag)
builder, err := builder.New(clicontext.Context, config, path, tag)
if err != nil {
return errors.Wrap(err, "failed to create the builder")
}
return builder.Build(clicontext.Context)
}
10 changes: 5 additions & 5 deletions cmd/midi/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
package main

import (
"fmt"
"path/filepath"
"time"

"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
cli "github.com/urfave/cli/v2"

"github.com/tensorchord/MIDI/pkg/builder"
"github.com/tensorchord/MIDI/pkg/docker"
"github.com/tensorchord/MIDI/pkg/flag"
"github.com/tensorchord/MIDI/pkg/home"
"github.com/tensorchord/MIDI/pkg/ssh"
)
Expand Down Expand Up @@ -82,8 +79,11 @@ func up(clicontext *cli.Context) error {

tag := clicontext.String("tag")

builder := builder.New(fmt.Sprintf("docker-container://%s",
viper.GetString(flag.FlagBuildkitdContainer)), config, path, tag)
builder, err := builder.New(clicontext.Context, config, path, tag)
if err != nil {
return errors.Wrap(err, "failed to create the builder")
}

if err := builder.Build(clicontext.Context); err != nil {
return err
}
Expand Down
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ require (
github.com/docker/docker v20.10.7+incompatible
github.com/fatih/color v1.13.0
github.com/gliderlabs/ssh v0.3.3
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/moby/buildkit v0.10.1
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.19.0
github.com/opencontainers/go-digest v1.0.0
github.com/pkg/sftp v1.13.4
github.com/sirupsen/logrus v1.8.1
Expand All @@ -20,7 +23,7 @@ require (
go.starlark.net v0.0.0-20220328144851-d1966c6b9fcd
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)

require (
Expand Down Expand Up @@ -79,10 +82,13 @@ require (
go.opentelemetry.io/otel/sdk v1.4.1 // indirect
go.opentelemetry.io/otel/trace v1.4.1 // indirect
go.opentelemetry.io/proto/otlp v0.12.0 // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
Expand Down
26 changes: 21 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -398,8 +400,12 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.13.0/go.mod h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1lskyM0=
github.com/onsi/ginkgo/v2 v2.1.4 h1:GNapqRSid3zijZ9H77KrgVG4/8KqiyRsxcSxe+7ApXY=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 h1:rc3tiVYb5z54aKaDfakKn0dDjIyPpTtszkjuMzyt7ec=
Expand Down Expand Up @@ -594,6 +600,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -636,8 +644,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20211008194852-3b03d305991f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f h1:hEYJvxw1lSnWIl8X9ofsYMklzaDs90JI2az5YMd4fPM=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -726,11 +734,14 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -801,12 +812,17 @@ golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U=
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down
37 changes: 20 additions & 17 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import (
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/progress/progresswriter"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

"github.com/tensorchord/MIDI/pkg/buildkitd"
"github.com/tensorchord/MIDI/pkg/docker"
"github.com/tensorchord/MIDI/pkg/flag"
"github.com/tensorchord/MIDI/pkg/home"
"github.com/tensorchord/MIDI/pkg/lang/frontend/starlark"
"github.com/tensorchord/MIDI/pkg/lang/ir"
"golang.org/x/sync/errgroup"
)

type Builder interface {
Expand All @@ -37,28 +39,36 @@ type Builder interface {
}

type generalBuilder struct {
buildkitdSocket string
manifestFilePath string
configFilePath string
progressMode string
tag string

logger *logrus.Entry
starlark.Interpreter
buildkitd.Client
}

func New(buildkitdSocket, configFilePath, manifestFilePath, tag string) Builder {
return &generalBuilder{
buildkitdSocket: buildkitdSocket,
func New(ctx context.Context, configFilePath, manifestFilePath, tag string) (Builder, error) {
b := &generalBuilder{
manifestFilePath: manifestFilePath,
configFilePath: configFilePath,
// TODO(gaocegege): Support other mode?
progressMode: "auto",
tag: tag,
logger: logrus.WithFields(logrus.Fields{
"tag": tag,
"buildkitd": buildkitdSocket,
"tag": tag,
}),
}

cli, err := buildkitd.NewClient(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to create buildkit client")
}
b.Client = cli

b.Interpreter = starlark.NewInterpreter()
return b, nil
}

// GPUEnabled returns true if cuda is enabled.
Expand All @@ -68,22 +78,15 @@ func (b generalBuilder) GPUEnabled() bool {
}

func (b generalBuilder) Build(ctx context.Context) error {
interpreter := starlark.NewInterpreter()
// Evaluate config first.
if _, err := interpreter.ExecFile(b.configFilePath); err != nil {
if _, err := b.ExecFile(b.configFilePath); err != nil {
return err
}

if _, err := interpreter.ExecFile(b.manifestFilePath); err != nil {
if _, err := b.ExecFile(b.manifestFilePath); err != nil {
return err
}

bkClient, err := client.New(ctx, b.buildkitdSocket, client.WithFailFast())
if err != nil {
return errors.Wrap(err, "failed to new buildkitd client")
}
defer bkClient.Close()

def, err := ir.Compile(ctx)
if err != nil {
return errors.Wrap(err, "failed to compile build.MIDI")
Expand All @@ -106,7 +109,7 @@ func (b generalBuilder) Build(ctx context.Context) error {
return err
}
b.logger.Debug("building image in ", wd)
_, err = bkClient.Solve(ctx, def, client.SolveOpt{
_, err = b.Solve(ctx, def, client.SolveOpt{
Exports: []client.ExportEntry{
{
Type: client.ExporterDocker,
Expand Down
Loading

0 comments on commit 62ce39e

Please sign in to comment.