Skip to content

Commit

Permalink
feat(CLI): Support build context (#138)
Browse files Browse the repository at this point in the history
* feat(CLI): Support build context

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

* fix: Remove wd

Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed May 12, 2022
1 parent 62d5049 commit e027a8b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
17 changes: 14 additions & 3 deletions cmd/envd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,36 @@ var CommandBuild = &cli.Command{
Aliases: []string{"f"},
Value: "./build.envd",
},
&cli.PathFlag{
Name: "path",
Usage: "Path to the directory containing the build.envd",
Aliases: []string{"p"},
Value: ".",
},
},

Action: build,
}

func build(clicontext *cli.Context) error {
path, err := filepath.Abs(clicontext.Path("file"))
buildContext, err := filepath.Abs(clicontext.Path("path"))
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build context")
}

manifest, err := filepath.Abs(filepath.Join(buildContext, clicontext.Path("file")))
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build file")
}
if path == "" {
if manifest == "" {
return errors.New("file does not exist")
}

config := home.GetManager().ConfigFile()

tag := clicontext.String("tag")

builder, err := builder.New(clicontext.Context, config, path, tag)
builder, err := builder.New(clicontext.Context, config, manifest, buildContext, tag)
if err != nil {
return errors.Wrap(err, "failed to create the builder")
}
Expand Down
31 changes: 21 additions & 10 deletions cmd/envd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,28 @@ var CommandUp = &cli.Command{
Aliases: []string{"u"},
Usage: "build and run the envd environment",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "volume",
Usage: "Mount host directory into container",
Aliases: []string{"v"},
},
&cli.StringFlag{
Name: "tag",
Usage: "Name and optionally a tag in the 'name:tag' format",
Aliases: []string{"t"},
Value: "envd:dev",
},
&cli.PathFlag{
Name: "path",
Usage: "Path to the directory containing the build.envd",
Aliases: []string{"p"},
Value: ".",
},
&cli.StringSliceFlag{
Name: "volume",
Usage: "Mount host directory into container",
Aliases: []string{"v"},
},
&cli.PathFlag{
Name: "file",
Usage: "Name of the build.envd (Default is 'PATH/build.envd')",
Usage: "Name of the build.envd",
Aliases: []string{"f"},
Value: "./build.envd",
Value: "build.envd",
},
&cli.BoolFlag{
Name: "auth",
Expand All @@ -73,19 +79,24 @@ var CommandUp = &cli.Command{
}

func up(clicontext *cli.Context) error {
path, err := filepath.Abs(clicontext.Path("file"))
buildContext, err := filepath.Abs(clicontext.Path("path"))
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build context")
}

manifest, err := filepath.Abs(filepath.Join(buildContext, clicontext.Path("file")))
if err != nil {
return errors.Wrap(err, "failed to get absolute path of the build file")
}
if path == "" {
if manifest == "" {
return errors.New("file does not exist")
}

config := home.GetManager().ConfigFile()

tag := clicontext.String("tag")

builder, err := builder.New(clicontext.Context, config, path, tag)
builder, err := builder.New(clicontext.Context, config, manifest, buildContext, tag)
if err != nil {
return errors.Wrap(err, "failed to create the builder")
}
Expand Down
14 changes: 6 additions & 8 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,19 @@ type generalBuilder struct {
configFilePath string
progressMode string
tag string
buildContextDir string

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

func New(ctx context.Context, configFilePath, manifestFilePath, tag string) (Builder, error) {
func New(ctx context.Context,
configFilePath, manifestFilePath, buildContextDir, tag string) (Builder, error) {
b := &generalBuilder{
manifestFilePath: manifestFilePath,
configFilePath: configFilePath,
buildContextDir: buildContextDir,
// TODO(gaocegege): Support other mode?
progressMode: "auto",
tag: tag,
Expand Down Expand Up @@ -127,12 +130,7 @@ func (b generalBuilder) build(ctx context.Context, def *llb.Definition, pw progr
pipeR, pipeW := io.Pipe()
eg.Go(func() error {
defer pipeW.Close()
wd, err := os.Getwd()
if err != nil {
return err
}
b.logger.Debug("building image in ", wd)
_, err = b.Solve(ctx, def, client.SolveOpt{
_, err := b.Solve(ctx, def, client.SolveOpt{
Exports: []client.ExportEntry{
{
Type: client.ExporterDocker,
Expand All @@ -145,7 +143,7 @@ func (b generalBuilder) build(ctx context.Context, def *llb.Definition, pw progr
},
},
LocalDirs: map[string]string{
flag.FlagContextDir: wd,
flag.FlagContextDir: b.buildContextDir,
flag.FlagCacheDir: home.GetManager().CacheDir(),
},
FrontendAttrs: map[string]string{
Expand Down
9 changes: 5 additions & 4 deletions pkg/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ import (

var _ = Describe("Builder", func() {
Describe("building image", Label("buildkitd"), func() {
var buildkitdSocket, configFilePath, manifestFilePath, tag string
var buildkitdSocket, configFilePath, manifestFilePath, buildContext, tag string
BeforeEach(func() {
buildkitdSocket = "docker-container://envd_buildkitd"
configFilePath = "testdata/config.envd"
manifestFilePath = "testdata/build.envd"
configFilePath = "config.envd"
manifestFilePath = "build.envd"
buildContext = "testdata"
tag = "envd-dev:test"
viper.Set(flag.FlagBuildkitdContainer, "envd_buildkitd")
viper.Set(flag.FlagSSHImage, "envd-ssh:latest")
Expand All @@ -56,7 +57,7 @@ var _ = Describe("Builder", func() {
buildkitdSocket = "wrong"
viper.Set(flag.FlagBuildkitdContainer, buildkitdSocket)
It("should return an error", func() {
_, err := New(context.TODO(), configFilePath, manifestFilePath, tag)
_, err := New(context.TODO(), configFilePath, manifestFilePath, buildContext, tag)
Expect(err).To(HaveOccurred())
})
})
Expand Down

0 comments on commit e027a8b

Please sign in to comment.