Skip to content

Commit

Permalink
chore(CLI): Move destroy logic from docker to envd engine. (#1050)
Browse files Browse the repository at this point in the history
* moved destroy logic from docker client to docker engine

Signed-off-by: Yves Tumushimire <yvestumushimire@gmail.com>

* restor newclient in e2e tests

Signed-off-by: Yves Tumushimire <yvestumushimire@gmail.com>

* refactor e2e test and logic

Signed-off-by: Yves Tumushimire <yvestumushimire@gmail.com>

* use context from home

Signed-off-by: Yves Tumushimire <yvestumushimire@gmail.com>

Signed-off-by: Yves Tumushimire <yvestumushimire@gmail.com>
  • Loading branch information
yvestumushimire authored Oct 20, 2022
1 parent 41e63ab commit 181ee71
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 38 deletions.
28 changes: 19 additions & 9 deletions e2e/cli/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"github.com/tensorchord/envd/e2e"
"github.com/tensorchord/envd/pkg/app"
"github.com/tensorchord/envd/pkg/docker"
"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/types"
)

var _ = Describe("build command", Ordered, func() {
Expand All @@ -35,11 +37,14 @@ var _ = Describe("build command", Ordered, func() {
e2e.ResetEnvdApp()
err := envdApp.Run([]string{"envd.test", "--debug", "bootstrap"})
Expect(err).NotTo(HaveOccurred())
cli, err := docker.NewClient(context.TODO())
_, err = docker.NewClient(context.TODO())
Expect(err).NotTo(HaveOccurred())
_, err = cli.Destroy(context.TODO(), buildTestName)
c := types.Context{Runner: types.RunnerTypeDocker}
opt := envd.Options{Context: &c}
envdEngine, err := envd.New(context.TODO(), opt)
Expect(err).NotTo(HaveOccurred())
_, err = envdEngine.Destroy(context.TODO(), buildTestName)
Expect(err).NotTo(HaveOccurred())

envdApp = app.New()
args := []string{
"envd.test", "--debug", "build", "--path", buildTestName,
Expand All @@ -54,11 +59,14 @@ var _ = Describe("build command", Ordered, func() {
e2e.ResetEnvdApp()
err := envdApp.Run([]string{"envd.test", "--debug", "bootstrap"})
Expect(err).NotTo(HaveOccurred())
cli, err := docker.NewClient(context.TODO())
_, err = docker.NewClient(context.TODO())
Expect(err).NotTo(HaveOccurred())
_, err = cli.Destroy(context.TODO(), customImageTestName)
c := types.Context{Runner: types.RunnerTypeDocker}
opt := envd.Options{Context: &c}
envdEngine, err := envd.New(context.TODO(), opt)
Expect(err).NotTo(HaveOccurred())
_, err = envdEngine.Destroy(context.TODO(), buildTestName)
Expect(err).NotTo(HaveOccurred())

envdApp = app.New()
args := []string{
"envd.test", "--debug", "build", "--path", customImageTestName,
Expand All @@ -73,11 +81,13 @@ var _ = Describe("build command", Ordered, func() {
e2e.ResetEnvdApp()
err := envdApp.Run([]string{"envd.test", "--debug", "bootstrap"})
Expect(err).NotTo(HaveOccurred())
cli, err := docker.NewClient(context.TODO())
_, err = docker.NewClient(context.TODO())
Expect(err).NotTo(HaveOccurred())
_, err = cli.Destroy(context.TODO(), buildTestName)
c := types.Context{Runner: types.RunnerTypeDocker}
opt := envd.Options{Context: &c}
envdEngine, err := envd.New(context.TODO(), opt)
Expect(err).NotTo(HaveOccurred())
_, err = cli.Destroy(context.TODO(), customImageTestName)
_, err = envdEngine.Destroy(context.TODO(), buildTestName)
Expect(err).NotTo(HaveOccurred())
// Init DefaultGraph.
// ir.DefaultGraph = ir.NewGraph()
Expand Down
11 changes: 9 additions & 2 deletions e2e/cli/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"github.com/tensorchord/envd/e2e"
"github.com/tensorchord/envd/pkg/app"
"github.com/tensorchord/envd/pkg/docker"
"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/types"
)

var _ = Describe("up command", Ordered, func() {
Expand All @@ -37,10 +39,15 @@ var _ = Describe("up command", Ordered, func() {
envdApp := app.New()
err := envdApp.Run(append(baseArgs, "bootstrap"))
Expect(err).NotTo(HaveOccurred())
cli, err := docker.NewClient(context.TODO())
_, err = docker.NewClient(context.TODO())
Expect(err).NotTo(HaveOccurred())
_, err = cli.Destroy(context.TODO(), env)
c := types.Context{Runner: types.RunnerTypeDocker}
opt := envd.Options{Context: &c}
envdEngine, err := envd.New(context.TODO(), opt)
Expect(err).NotTo(HaveOccurred())
_, err = envdEngine.Destroy(context.TODO(), env)
Expect(err).NotTo(HaveOccurred())

})
When("given the right arguments", func() {
It("should up and destroy successfully", func() {
Expand Down
12 changes: 10 additions & 2 deletions pkg/app/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,21 @@ func destroy(clicontext *cli.Context) error {
}
ctrName = filepath.Base(buildContext)
}
context, err := home.GetManager().ContextGetCurrent()
if err != nil {
return errors.Wrap(err, "failed to get the current context")
}
opt := envd.Options{Context: context}
envdEngine, err := envd.New(clicontext.Context, opt)
if err != nil {
return errors.Wrap(err, "failed to create envd engine")
}

if ctrName, err := dockerClient.Destroy(clicontext.Context, ctrName); err != nil {
if ctrName, err := envdEngine.Destroy(clicontext.Context, ctrName); err != nil {
return errors.Wrapf(err, "failed to destroy the environment: %s", ctrName)
} else if ctrName != "" {
logrus.Infof("container(%s) is destroyed", ctrName)
}

tags, err := getContainerTag(clicontext, ctrName)
if err != nil {
return err
Expand Down
25 changes: 0 additions & 25 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type Client interface {
StartBuildkitd(ctx context.Context, tag, name, mirror string) (string, error)

Exec(ctx context.Context, cname string, cmd []string) error
Destroy(ctx context.Context, name string) (string, error)

GetImageWithCacheHashLabel(ctx context.Context, image string, hash string) (types.ImageSummary, error)
RemoveImage(ctx context.Context, image string) error
Expand Down Expand Up @@ -179,30 +178,6 @@ func (c generalClient) ResumeContainer(ctx context.Context, name string) (string
return name, nil
}

func (c generalClient) Destroy(ctx context.Context, name string) (string, error) {
logger := logrus.WithField("container", name)
// Refer to https://docs.docker.com/engine/reference/commandline/container_kill/
if err := c.ContainerKill(ctx, name, "KILL"); err != nil {
errCause := errors.UnwrapAll(err).Error()
switch {
case strings.Contains(errCause, "is not running"):
// If the container is not running, there is no need to kill it.
logger.Debug("container is not running, there is no need to kill it")
case strings.Contains(errCause, "No such container"):
// If the container is not found, it is already destroyed or the name is wrong.
logger.Infof("cannot find container %s, maybe it's already destroyed or the name is wrong", name)
return "", nil
default:
return "", errors.Wrap(err, "failed to kill the container")
}
}

if err := c.ContainerRemove(ctx, name, types.ContainerRemoveOptions{}); err != nil {
return "", errors.Wrap(err, "failed to remove the container")
}
return name, nil
}

func (c generalClient) StartBuildkitd(ctx context.Context, tag, name, mirror string) (string, error) {
logger := logrus.WithFields(logrus.Fields{
"tag": tag,
Expand Down
24 changes: 24 additions & 0 deletions pkg/envd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,30 @@ func (e dockerEngine) StartEnvd(ctx context.Context, so StartOptions) (*StartRes
return result, nil
}

func (e dockerEngine) Destroy(ctx context.Context, name string) (string, error) {
logger := logrus.WithField("container", name)
// Refer to https://docs.docker.com/engine/reference/commandline/container_kill/
if err := e.ContainerKill(ctx, name, "KILL"); err != nil {
errCause := errors.UnwrapAll(err).Error()
switch {
case strings.Contains(errCause, "is not running"):
// If the container is not running, there is no need to kill it.
logger.Debug("container is not running, there is no need to kill it")
case strings.Contains(errCause, "No such container"):
// If the container is not found, it is already destroyed or the name is wrong.
logger.Infof("cannot find container %s, maybe it's already destroyed or the name is wrong", name)
return "", nil
default:
return "", errors.Wrap(err, "failed to kill the container")
}
}

if err := e.ContainerRemove(ctx, name, dockertypes.ContainerRemoveOptions{}); err != nil {
return "", errors.Wrap(err, "failed to remove the container")
}
return name, nil
}

func (e dockerEngine) WaitUntilRunning(ctx context.Context,
name string, timeout time.Duration) error {
logger := logrus.WithField("container", name)
Expand Down
1 change: 1 addition & 0 deletions pkg/envd/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type EnvironmentClient interface {
IsRunning(ctx context.Context, name string) (bool, error)
Exists(ctx context.Context, name string) (bool, error)
WaitUntilRunning(ctx context.Context, name string, timeout time.Duration) error
Destroy(ctx context.Context, name string) (string, error)
}

type ImageClient interface {
Expand Down
4 changes: 4 additions & 0 deletions pkg/envd/envdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (e *envdServerEngine) ListImage(ctx context.Context) ([]types.EnvdImage, er
return nil, errors.New("not implemented")
}

func (e envdServerEngine) Destroy(ctx context.Context, name string) (string, error) {
return "", errors.New("not implemented")
}

func (e *envdServerEngine) ListImageDependency(ctx context.Context, image string) (*types.Dependency, error) {
return nil, errors.New("not implemented")
}
Expand Down

0 comments on commit 181ee71

Please sign in to comment.