From 3c28ffbab95e3106a124d87b71293435daf8faaf Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Sat, 7 May 2022 16:12:38 +0800 Subject: [PATCH] feat(CLI): Support destroy subcommand (#110) Signed-off-by: Ce Gao --- cmd/midi/destroy.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ cmd/midi/main.go | 1 + pkg/docker/docker.go | 12 ++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 cmd/midi/destroy.go diff --git a/cmd/midi/destroy.go b/cmd/midi/destroy.go new file mode 100644 index 000000000..ccbb8fd7f --- /dev/null +++ b/cmd/midi/destroy.go @@ -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 +} diff --git a/cmd/midi/main.go b/cmd/midi/main.go index 591b3a072..7128fe900 100644 --- a/cmd/midi/main.go +++ b/cmd/midi/main.go @@ -79,6 +79,7 @@ func main() { app.Commands = []*cli.Command{ CommandBootstrap, CommandBuild, + CommandDestroy, CommandUp, } diff --git a/pkg/docker/docker.go b/pkg/docker/docker.go index 3fab7683c..d8d566b9c 100644 --- a/pkg/docker/docker.go +++ b/pkg/docker/docker.go @@ -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 { @@ -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{