Skip to content

Commit

Permalink
feat(CLI): Support multiple environments (#142)
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed May 13, 2022
1 parent b35c287 commit 0b187da
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
21 changes: 17 additions & 4 deletions cmd/envd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"path/filepath"

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

"github.com/tensorchord/envd/pkg/builder"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/util/fileutil"
)

var CommandBuild = &cli.Command{
Expand All @@ -31,15 +33,14 @@ var CommandBuild = &cli.Command{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tag",
Usage: "Name and optionally a tag in the 'name:tag' format",
Usage: "Name and optionally a tag in the 'name:tag' format (default: PROJECT:dev)",
Aliases: []string{"t"},
Value: "envd:dev",
},
&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.PathFlag{
Name: "path",
Expand Down Expand Up @@ -69,6 +70,18 @@ func build(clicontext *cli.Context) error {
config := home.GetManager().ConfigFile()

tag := clicontext.String("tag")
if tag == "" {
logrus.Debug("tag not specified, using default")
tag = fileutil.Base(buildContext)
}

logger := logrus.WithFields(logrus.Fields{
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
})
logger.Debug("starting build")

builder, err := builder.New(clicontext.Context, config, manifest, buildContext, tag)
if err != nil {
Expand Down
24 changes: 21 additions & 3 deletions cmd/envd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@
package main

import (
"path/filepath"

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

"github.com/tensorchord/envd/pkg/docker"
"github.com/tensorchord/envd/pkg/util/fileutil"
)

var CommandDestroy = &cli.Command{
Name: "destroy",
Aliases: []string{"d"},
Usage: "destroys the envd environment",
Flags: []cli.Flag{},
Flags: []cli.Flag{
&cli.PathFlag{
Name: "path",
Usage: "Path to the directory containing the build.envd",
Aliases: []string{"p"},
Value: ".",
},
},

Action: destroy,
}
Expand All @@ -36,8 +46,16 @@ func destroy(clicontext *cli.Context) error {
if err != nil {
return err
}
if err := dockerClient.Destroy(clicontext.Context, "envd"); err != nil {
return errors.Wrap(err, "failed to destroy the envd environment")

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

ctr := fileutil.Base(buildContext)

if err := dockerClient.Destroy(clicontext.Context, ctr); err != nil {
return errors.Wrapf(err, "failed to destroy the environment: %s", ctr)
}
logrus.Info("envd environment destroyed")
return nil
Expand Down
21 changes: 19 additions & 2 deletions cmd/envd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

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

Expand All @@ -27,6 +28,7 @@ import (
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/lang/ir"
"github.com/tensorchord/envd/pkg/ssh"
"github.com/tensorchord/envd/pkg/util/fileutil"
)

var CommandUp = &cli.Command{
Expand Down Expand Up @@ -95,6 +97,20 @@ func up(clicontext *cli.Context) error {
config := home.GetManager().ConfigFile()

tag := clicontext.String("tag")
if tag == "" {
logrus.Debug("tag not specified, using default")
tag = fmt.Sprintf("%s:%s", fileutil.Base(buildContext), "dev")
}
ctr := fileutil.Base(buildContext)

logger := logrus.WithFields(logrus.Fields{
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
"container-name": ctr,
})
logger.Debug("starting up")

builder, err := builder.New(clicontext.Context, config, manifest, buildContext, tag)
if err != nil {
Expand All @@ -110,8 +126,9 @@ func up(clicontext *cli.Context) error {
if err != nil {
return err
}
containerID, containerIP, err := dockerClient.Startenvd(
clicontext.Context, tag, "envd", gpu, *ir.DefaultGraph, clicontext.Duration("timeout"), clicontext.StringSlice("volume"))
containerID, containerIP, err := dockerClient.StartEnvd(clicontext.Context,
tag, ctr, buildContext, gpu, *ir.DefaultGraph, clicontext.Duration("timeout"),
clicontext.StringSlice("volume"))
if err != nil {
return err
}
Expand Down
26 changes: 10 additions & 16 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Client interface {
// Load loads the image from the reader to the docker host.
Load(ctx context.Context, r io.ReadCloser, quiet bool) error
// Start creates the container for the given tag and container name.
Startenvd(ctx context.Context, tag, name string,
StartEnvd(ctx context.Context, tag, name, buildContext string,
gpuEnabled bool, g ir.Graph, timeout time.Duration, mountOptionsStr []string) (string, string, error)
StartBuildkitd(ctx context.Context, tag, name string) (string, error)
IsRunning(ctx context.Context, name string) (bool, error)
Expand Down Expand Up @@ -163,12 +163,13 @@ func (g generalClient) StartBuildkitd(ctx context.Context,
}

// Start creates the container for the given tag and container name.
func (c generalClient) Startenvd(ctx context.Context, tag, name string,
func (c generalClient) StartEnvd(ctx context.Context, tag, name, buildContext string,
gpuEnabled bool, g ir.Graph, timeout time.Duration, mountOptionsStr []string) (string, string, error) {
logger := logrus.WithFields(logrus.Fields{
"tag": tag,
"container": name,
"gpu": gpuEnabled,
"tag": tag,
"container": name,
"gpu": gpuEnabled,
"build-context": buildContext,
})
config := &container.Config{
Image: tag,
Expand All @@ -178,22 +179,15 @@ func (c generalClient) Startenvd(ctx context.Context, tag, name string,
},
ExposedPorts: nat.PortSet{},
}
path, err := fileutil.CWD()
if err != nil {
return "", "", errors.Wrap(err, "failed to get current working directory")
}
base, err := fileutil.RootDir()
if err != nil {
return "", "", errors.Wrap(err, "failed to get root directory")
}
base := fileutil.Base(buildContext)
base = fmt.Sprintf("/root/%s", base)
config.WorkingDir = base

mountOption := make([]mount.Mount, len(mountOptionsStr)+1)
for i, option := range mountOptionsStr {
mStr := strings.Split(option, ":")
if len(mStr) != 2 {
return "", "", errors.Wrap(err, fmt.Sprintf("Invalid mount options %s", option))
return "", "", errors.Newf("Invalid mount options %s", option)
}

logger.WithFields(logrus.Fields{
Expand All @@ -208,12 +202,12 @@ func (c generalClient) Startenvd(ctx context.Context, tag, name string,
}
mountOption[len(mountOptionsStr)] = mount.Mount{
Type: mount.TypeBind,
Source: path,
Source: buildContext,
Target: base,
}

logger.WithFields(logrus.Fields{
"mount-path": path,
"mount-path": buildContext,
"working-dir": base,
}).Debug("setting up container working directory")

Expand Down
4 changes: 4 additions & 0 deletions pkg/util/fileutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ func RootDir() (string, error) {
}
return filepath.Base(cwd), nil
}

func Base(dir string) string {
return filepath.Base(dir)
}

0 comments on commit 0b187da

Please sign in to comment.