Skip to content

Commit

Permalink
feat(CLI): Support destroy subcommand (#110)
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 7, 2022
1 parent b8fbce1 commit 3c28ffb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cmd/midi/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 The MIDI Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

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

"github.com/tensorchord/MIDI/pkg/docker"
)

var CommandDestroy = &cli.Command{
Name: "destroy",
Aliases: []string{"d"},
Usage: "destroys the MIDI environment",
Flags: []cli.Flag{},

Action: destroy,
}

func destroy(clicontext *cli.Context) error {
dockerClient, err := docker.NewClient()
if err != nil {
return err
}
if err := dockerClient.Destroy(clicontext.Context, "midi"); err != nil {
return errors.Wrap(err, "failed to destroy the midi environment")
}
logrus.Info("MIDI environment destroyed")
return nil
}
1 change: 1 addition & 0 deletions cmd/midi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
app.Commands = []*cli.Command{
CommandBootstrap,
CommandBuild,
CommandDestroy,
CommandUp,
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Client interface {
IsCreated(ctx context.Context, name string) (bool, error)
WaitUntilRunning(ctx context.Context, name string, timeout time.Duration) error
Exec(ctx context.Context, cname string, cmd []string) error
Destroy(ctx context.Context, name string) error
}

type generalClient struct {
Expand Down Expand Up @@ -91,6 +92,17 @@ func (g generalClient) WaitUntilRunning(ctx context.Context,
}
}

func (c generalClient) Destroy(ctx context.Context, name string) error {
// Refer to https://docs.docker.com/engine/reference/commandline/container_kill/
if err := c.ContainerKill(ctx, name, "KILL"); err != nil {
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 nil
}

func (g generalClient) StartBuildkitd(ctx context.Context,
tag, name string) (string, error) {
logger := logrus.WithFields(logrus.Fields{
Expand Down

0 comments on commit 3c28ffb

Please sign in to comment.