2017-08-30 20:03:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/kubernetes-incubator/cri-o/libkpod"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
2017-10-15 20:05:41 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-08-30 20:03:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rmFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
|
|
|
Usage: "Force removal of a running container. The default is false",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rmDescription = "Remove one or more containers"
|
|
|
|
rmCommand = cli.Command{
|
|
|
|
Name: "rm",
|
|
|
|
Usage: fmt.Sprintf(`kpod rm will remove one or more containers from the host. The container name or ID can be used.
|
|
|
|
This does not remove images. Running containers will not be removed without the -f option.`),
|
|
|
|
Description: rmDescription,
|
|
|
|
Flags: rmFlags,
|
|
|
|
Action: rmCmd,
|
|
|
|
ArgsUsage: "",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// saveCmd saves the image to either docker-archive or oci
|
|
|
|
func rmCmd(c *cli.Context) error {
|
|
|
|
args := c.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
return errors.Errorf("specify one or more containers to remove")
|
|
|
|
}
|
2017-09-28 18:44:48 +00:00
|
|
|
if err := validateFlags(c, rmFlags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-30 20:03:26 +00:00
|
|
|
|
|
|
|
config, err := getConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get config")
|
|
|
|
}
|
|
|
|
server, err := libkpod.New(config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get container server")
|
|
|
|
}
|
|
|
|
defer server.Shutdown()
|
|
|
|
err = server.Update()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not update list of containers")
|
|
|
|
}
|
|
|
|
force := c.Bool("force")
|
|
|
|
|
|
|
|
for _, container := range c.Args() {
|
2017-10-15 20:05:41 +00:00
|
|
|
id, err2 := server.Remove(context.Background(), container, force)
|
2017-08-30 20:03:26 +00:00
|
|
|
if err2 != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = err2
|
|
|
|
} else {
|
|
|
|
err = errors.Wrapf(err, "%v. Stop the container before attempting removal or use -f\n", err2)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|