Skip to content

Commit

Permalink
feat(cli): rm image when destroying the env (#925)
Browse files Browse the repository at this point in the history
* feat(cli): rm image when destroying the env

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* rm RemoveImage in e2e

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* fix e2e doc test

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* fix cli test

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* fix cli init test

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* fix doc test cuda version

Signed-off-by: Keming <kemingyang@tensorchord.ai>

Signed-off-by: Keming <kemingyang@tensorchord.ai>
  • Loading branch information
kemingy committed Sep 20, 2022
1 parent d551813 commit ce36545
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 19 deletions.
6 changes: 1 addition & 5 deletions e2e/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ var _ = Describe("init project", Ordered, func() {
It("exec installed command inside container", func() {
_, err := e.Exec("via --help")
Expect(err).To(Succeed())
e.DestroyContainer()
})
AfterAll(func() {
e.DestroyContainer()()
e.RemoveImage()()
})

})

AfterAll(func() {
Expand Down
1 change: 0 additions & 1 deletion e2e/cli/quick_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ var _ = Describe("e2e quickstart", Ordered, func() {
Expect(res).To(Equal("[2 3 4]"))
})
AfterEach(e.DestroyContainer())
AfterAll(e.RemoveImage())
})
8 changes: 3 additions & 5 deletions e2e/docs/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
)

var _ = Describe("check examples in documentation", func() {
buildContext := "doctest"
// env := "up-test"
baseArgs := []string{
"envd.test", "--debug",
}
Expand Down Expand Up @@ -67,20 +65,20 @@ var _ = Describe("check examples in documentation", func() {
Expect(err).NotTo(HaveOccurred())
})

up_tests := []string{"testdata/minimal", "testdata/getting_started", "testdata/jupyter", "testsdata/complex"}
up_tests := []string{"testdata/minimal", "testdata/getting_started", "testdata/jupyter", "testdata/complex"}

for _, v := range up_tests {
It(fmt.Sprintf("can up %s environment", v), func() {
args := append(baseArgs, []string{
"up", "--path", "testdata/getting_started", "-f", "build.envd", "--detach", "--force",
"up", "--path", v, "-f", "build.envd", "--detach", "--force",
}...)
e2e.ResetEnvdApp()
envdApp := app.New()
err := envdApp.Run(args)
Expect(err).NotTo(HaveOccurred())

destroyArgs := append(baseArgs, []string{
"destroy", "--path", buildContext,
"destroy", "--path", v,
}...)
err = envdApp.Run(destroyArgs)
Expect(err).NotTo(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion e2e/docs/testdata/complex/build.envd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ deb https://mirror.sjtu.edu.cn/ubuntu focal-security main restricted universe mu
install.python_packages(name = [
"numpy",
])
install.cuda(version="11.6", cudnn="8")
install.cuda(version="11.2", cudnn="8")
shell("zsh")
install.apt_packages(name = [
"htop"
Expand Down
4 changes: 0 additions & 4 deletions e2e/language/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var _ = Describe("python", Ordered, func() {
e.BuildImage(true)()
e.RunContainer()()
e.DestroyContainer()()
e.RemoveImage()()
})
It("Should build requirements successfully", func() {
exampleName := "python/requirements"
Expand All @@ -37,7 +36,6 @@ var _ = Describe("python", Ordered, func() {
e.BuildImage(true)()
e.RunContainer()()
e.DestroyContainer()()
e.RemoveImage()()
})
It("Should build hybrid successfully", func() {
exampleName := "python/hybrid"
Expand All @@ -46,7 +44,6 @@ var _ = Describe("python", Ordered, func() {
e.BuildImage(true)()
e.RunContainer()()
e.DestroyContainer()()
e.RemoveImage()()
})

It("Should build conda with channel successfully", func() {
Expand All @@ -56,6 +53,5 @@ var _ = Describe("python", Ordered, func() {
e.BuildImage(true)()
e.RunContainer()()
e.DestroyContainer()()
e.RemoveImage()()
})
})
1 change: 0 additions & 1 deletion e2e/language/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ var _ = Describe("runtime", Ordered, func() {
Expect(res).To(Equal("[2 3 4]"))
})
AfterEach(e.DestroyContainer())
AfterAll(e.RemoveImage())
})
2 changes: 1 addition & 1 deletion pkg/app/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func ParseBuildOpt(clicontext *cli.Context) (builder.Options, error) {
logrus.Debug("tag not specified, using default")
tag = fmt.Sprintf("%s:%s", filepath.Base(buildContext), "dev")
}
// The current container engine is only Docker. It should be expaned to support other container engines.
// The current container engine is only Docker. It should be expanded to support other container engines.
tag, err = docker.NormalizeNamed(tag)
if err != nil {
return builder.Options{}, err
Expand Down
28 changes: 27 additions & 1 deletion pkg/app/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/docker"
"github.com/tensorchord/envd/pkg/envd"
sshconfig "github.com/tensorchord/envd/pkg/ssh/config"
)

Expand Down Expand Up @@ -70,15 +71,40 @@ func destroy(clicontext *cli.Context) error {
}
ctrName = filepath.Base(buildContext)
}

tag, err := getContainerTag(clicontext, ctrName)
if err != nil {
return err
}

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

if err := dockerClient.RemoveImage(clicontext.Context, tag); err != nil {
return errors.Wrapf(err, "failed to remove the image: %s", tag)
}
if err = sshconfig.RemoveEntry(ctrName); err != nil {
logrus.Infof("failed to remove entry %s from your SSH config file: %s", ctrName, err)
return errors.Wrap(err, "failed to remove entry from your SSH config file")
}
return nil
}

func getContainerTag(clicontext *cli.Context, name string) (string, error) {
envdEngine, err := envd.New(clicontext.Context)
if err != nil {
return "", err
}
envs, err := envdEngine.ListEnvironment(clicontext.Context)
if err != nil {
return "", err
}
for _, env := range envs {
if env.Name == name {
return env.Container.Image, nil
}
}
return "", errors.Newf("cannot find the image of %s", name)
}

0 comments on commit ce36545

Please sign in to comment.