Skip to content

Commit

Permalink
feat: Support specify build target (#497)
Browse files Browse the repository at this point in the history
* support specify function

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* change example

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix test

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix naming

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix lint

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>
  • Loading branch information
VoVAllen committed Jul 5, 2022
1 parent e443784 commit 6e9e44d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/mnist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Then you can open jupyter notebook at [`http://localhost:8888`](http://localhost
## Quick start (GPU)

```bash
$ envd up -f build_gpu.envd
$ envd up -f :build_gpu # Call build_gpu function in build.envd
```

Also you can use `-f` option to specify the file to build
Expand Down
13 changes: 0 additions & 13 deletions examples/mnist/build_gpu.envd

This file was deleted.

14 changes: 9 additions & 5 deletions pkg/app/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ To build and push the image to a registry:
DefaultText: "PROJECT:dev",
},
&cli.PathFlag{
Name: "file",
Usage: "Name of the build.envd",
Name: "from",
Usage: "Function to execute, format `file:func`",
Aliases: []string{"f"},
Value: "build.envd",
Value: "build.envd:build",
},
&cli.PathFlag{
Name: "path",
Expand Down Expand Up @@ -82,7 +82,11 @@ func build(clicontext *cli.Context) error {
return errors.Wrap(err, "failed to get absolute path of the build context")
}

manifest, err := filepath.Abs(filepath.Join(buildContext, clicontext.Path("file")))
filename, funcname, err := builder.ParseFromStr(clicontext.String("from"))
if err != nil {
return err
}
manifest, err := filepath.Abs(filepath.Join(buildContext, filename))
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build file")
}
Expand Down Expand Up @@ -111,7 +115,7 @@ func build(clicontext *cli.Context) error {
"output": output,
}).Debug("starting build command")
builder, err := builder.New(clicontext.Context, cfg,
manifest, buildContext, tag, output, debug)
manifest, funcname, buildContext, tag, output, debug)
if err != nil {
return errors.Wrap(err, "failed to create the builder")
}
Expand Down
14 changes: 9 additions & 5 deletions pkg/app/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ var CommandUp = &cli.Command{
Aliases: []string{"v"},
},
&cli.PathFlag{
Name: "file",
Usage: "Name of the build.envd",
Name: "from",
Usage: "Function to execute, format `file:func`",
Aliases: []string{"f"},
Value: "build.envd",
Value: "build.envd:build",
},
&cli.PathFlag{
Name: "private-key",
Expand Down Expand Up @@ -105,8 +105,12 @@ func up(clicontext *cli.Context) error {
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build context")
}
filename, funcname, err := builder.ParseFromStr(clicontext.String("from"))
if err != nil {
return err
}

manifest, err := filepath.Abs(filepath.Join(buildContext, clicontext.Path("file")))
manifest, err := filepath.Abs(filepath.Join(buildContext, filename))
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build file")
}
Expand Down Expand Up @@ -137,7 +141,7 @@ func up(clicontext *cli.Context) error {
logger.Debug("starting up command")
debug := clicontext.Bool("debug")
output := ""
builder, err := builder.New(clicontext.Context, config, manifest,
builder, err := builder.New(clicontext.Context, config, manifest, funcname,
buildContext, tag, output, debug)
if err != nil {
return errors.Wrap(err, "failed to create the builder")
Expand Down
6 changes: 4 additions & 2 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type generalBuilder struct {
progressMode string
tag string
buildContextDir string
buildfuncname string

entries []client.ExportEntry

Expand All @@ -58,7 +59,7 @@ type generalBuilder struct {
buildkitd.Client
}

func New(ctx context.Context, configFilePath, manifestFilePath,
func New(ctx context.Context, configFilePath, manifestFilePath, funcname,
buildContextDir, tag string, output string, debug bool) (Builder, error) {
entries, err := parseOutput(output)
if err != nil {
Expand All @@ -84,6 +85,7 @@ func New(ctx context.Context, configFilePath, manifestFilePath,

b := &generalBuilder{
manifestFilePath: manifestFilePath,
buildfuncname: funcname,
configFilePath: configFilePath,
buildContextDir: buildContextDir,
entries: entries,
Expand Down Expand Up @@ -141,7 +143,7 @@ func (b generalBuilder) interpret() error {
return errors.Wrap(err, "failed to exec starlark file")
}

if _, err := b.ExecFile(b.manifestFilePath, "build"); err != nil {
if _, err := b.ExecFile(b.manifestFilePath, b.buildfuncname); err != nil {
return errors.Wrap(err, "failed to exec starlark file")
}
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var _ = Describe("Builder", func() {
configFilePath: configFilePath,
progressMode: "auto",
tag: tag,
buildfuncname: "build",
logger: logrus.WithFields(logrus.Fields{
"tag": tag,
}),
Expand Down
24 changes: 24 additions & 0 deletions pkg/builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import (
"github.com/sirupsen/logrus"
)

const (
defaultFile = "build.envd"
defaultFunc = "build"
)

func ImageConfigStr(labels map[string]string,
ports map[string]struct{}, entrypoint []string) (string, error) {
pl := platforms.Normalize(platforms.DefaultSpec())
Expand Down Expand Up @@ -172,3 +177,22 @@ func resolveExporterDest(exporter, dest string) (func(map[string]string) (io.Wri
return nil, "", nil
}
}

func ParseFromStr(fromStr string) (string, string, error) {
filename := defaultFile
funcname := defaultFunc
if strings.Contains(fromStr, ":") {
fromArr := strings.Split(fromStr, ":")

if len(fromArr) != 2 {
return "", "", errors.New("invalid from format, expected `file:func`")
}
if fromArr[0] != "" {
filename = fromArr[0]
}
if fromArr[1] != "" {
funcname = fromArr[1]
}
}
return filename, funcname, nil
}

0 comments on commit 6e9e44d

Please sign in to comment.