Skip to content

Commit

Permalink
feat: support envd server destroy env (#1096)
Browse files Browse the repository at this point in the history
* feat: support envd server destroy env

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

* fix test code

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

Signed-off-by: Keming <kemingyang@tensorchord.ai>
  • Loading branch information
kemingy authored Oct 27, 2022
1 parent 2be2db0 commit 0a975de
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 60 deletions.
51 changes: 1 addition & 50 deletions pkg/app/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
package app

import (
"fmt"
"path/filepath"
"strings"

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

"github.com/tensorchord/envd/pkg/docker"
"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
sshconfig "github.com/tensorchord/envd/pkg/ssh/config"
Expand Down Expand Up @@ -60,10 +57,6 @@ func destroy(clicontext *cli.Context) error {
if path == "" && name == "" {
path = "."
}
dockerClient, err := docker.NewClient(clicontext.Context)
if err != nil {
return err
}
var ctrName string
if name != "" {
ctrName = name
Expand All @@ -87,18 +80,7 @@ func destroy(clicontext *cli.Context) error {
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
} else {
for _, tag := range tags {
if err := dockerClient.RemoveImage(clicontext.Context, tag); err != nil {
return errors.Errorf("remove image %s failed: %w", tag, err)
}
logrus.Infof("image(%s) is destroyed", tag)
}
logrus.Infof("environment(%s) is destroyed", ctrName)
}

if err = sshconfig.RemoveEntry(ctrName); err != nil {
Expand All @@ -107,34 +89,3 @@ func destroy(clicontext *cli.Context) error {
}
return nil
}

func getContainerTag(clicontext *cli.Context, name string) ([]string, error) {
tags := []string{}
context, err := home.GetManager().ContextGetCurrent()
if err != nil {
return tags, err
}
opt := envd.Options{
Context: context,
}
envdEngine, err := envd.New(clicontext.Context, opt)
if err != nil {
return tags, err
}
// check the images instead of running containers because `envd build` also produce images
images, err := envdEngine.ListImage(clicontext.Context)
if err != nil {
return tags, err
}
for _, img := range images {
for _, tag := range img.ImageSummary.RepoTags {
if strings.HasPrefix(tag, fmt.Sprintf("%s:", name)) {
tags = append(tags, tag)
}
}
}
if len(tags) == 0 {
logrus.Infof("cannot find the image of %s", name)
}
return tags, nil
}
34 changes: 34 additions & 0 deletions pkg/envd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,43 @@ func (e dockerEngine) Destroy(ctx context.Context, name string) (string, error)
if err := e.ContainerRemove(ctx, name, dockertypes.ContainerRemoveOptions{}); err != nil {
return "", errors.Wrap(err, "failed to remove the container")
}

// remove image
tags, err := e.getContainerTag(ctx, name)
if err != nil {
return "", errors.Wrap(err, "failed to get the container tags")
} else {
for _, tag := range tags {
if _, err := e.ImageRemove(ctx, tag, dockertypes.ImageRemoveOptions{}); err != nil {
return "", errors.Errorf("remove image %s failed: %w", tag, err)
}
logrus.Infof("image(%s) is destroyed", tag)
}
}

return name, nil
}

func (e dockerEngine) getContainerTag(ctx context.Context, name string) ([]string, error) {
tags := []string{}
// check the images instead of running containers because `envd build` also produce images
images, err := e.ListImage(ctx)
if err != nil {
return tags, err
}
for _, img := range images {
for _, tag := range img.ImageSummary.RepoTags {
if strings.HasPrefix(tag, fmt.Sprintf("%s:", name)) {
tags = append(tags, tag)
}
}
}
if len(tags) == 0 {
logrus.Infof("cannot find the image of %s", name)
}
return tags, nil
}

func (e dockerEngine) WaitUntilRunning(ctx context.Context,
name string, timeout time.Duration) error {
logger := logrus.WithField("container", name)
Expand Down
3 changes: 2 additions & 1 deletion pkg/envd/envdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (e *envdServerEngine) ListImage(ctx context.Context) ([]types.EnvdImage, er
}

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

func (e *envdServerEngine) ListImageDependency(ctx context.Context, image string) (*types.Dependency, error) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/lang/ir/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ wait = "5s"
`
)

func (g Graph) installHorust(root llb.State) llb.State {
horust := root.
File(llb.Copy(llb.Image(types.HorustImage), "/", "/usr/local/bin", llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomName("[internal] install horust")).
File(llb.Mkdir(types.HorustServiceDir, 0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid))).
File(llb.Mkdir(types.HorustLogDir, 0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid)))
return horust
}

func (g Graph) addNewProcess(root llb.State, name, command string, depends []string) llb.State {
var sb strings.Builder
if len(depends) != 0 {
Expand Down
9 changes: 0 additions & 9 deletions pkg/lang/ir/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,6 @@ func (g *Graph) compileBase() (llb.State, error) {
return final, nil
}

func (g Graph) installHorust(root llb.State) llb.State {
horust := root.
File(llb.Copy(llb.Image(types.HorustImage), "/", "/usr/local/bin", llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomName("[internal] install horust")).
File(llb.Mkdir(types.HorustServiceDir, 0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid))).
File(llb.Mkdir(types.HorustLogDir, 0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid)))
return horust
}

func (g Graph) copySSHKey(root llb.State) (llb.State, error) {
public := DefaultGraph.PublicKeyPath
bdat, err := os.ReadFile(public)
Expand Down

0 comments on commit 0a975de

Please sign in to comment.