2016-04-13 05:41:09 +08:00
|
|
|
// +build !solaris
|
|
|
|
|
2016-01-12 08:57:18 +08:00
|
|
|
package main
|
|
|
|
|
2016-03-29 04:27:28 +08:00
|
|
|
import (
|
2016-05-26 02:24:26 +08:00
|
|
|
"fmt"
|
2016-03-29 04:27:28 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-05-28 04:13:11 +08:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2016-03-29 04:27:28 +08:00
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2017-05-11 23:06:37 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-03-29 04:27:28 +08:00
|
|
|
)
|
2016-01-12 08:57:18 +08:00
|
|
|
|
2016-06-29 06:21:42 +08:00
|
|
|
func killContainer(container libcontainer.Container) error {
|
2017-05-11 23:06:37 +08:00
|
|
|
_ = container.Signal(unix.SIGKILL, false)
|
2016-06-29 06:21:42 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2016-11-08 07:22:27 +08:00
|
|
|
if err := container.Signal(syscall.Signal(0), false); err != nil {
|
2016-06-29 06:21:42 +08:00
|
|
|
destroy(container)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("container init still running")
|
|
|
|
}
|
|
|
|
|
2016-01-12 08:57:18 +08:00
|
|
|
var deleteCommand = cli.Command{
|
|
|
|
Name: "delete",
|
2017-03-08 10:02:39 +08:00
|
|
|
Usage: "delete any resources held by the container often used with detached container",
|
|
|
|
ArgsUsage: `<container-id>
|
2016-02-11 01:30:06 +08:00
|
|
|
|
|
|
|
Where "<container-id>" is the name for the instance of the container.
|
2016-05-24 16:59:24 +08:00
|
|
|
|
|
|
|
EXAMPLE:
|
2016-02-11 01:30:06 +08:00
|
|
|
For example, if the container id is "ubuntu01" and runc list currently shows the
|
2016-05-26 02:24:26 +08:00
|
|
|
status of "ubuntu01" as "stopped" the following will delete resources held for
|
2016-06-29 06:21:42 +08:00
|
|
|
"ubuntu01" removing "ubuntu01" from the runc list of containers:
|
|
|
|
|
2016-02-11 01:30:06 +08:00
|
|
|
# runc delete ubuntu01`,
|
2016-06-29 06:21:42 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
2016-10-18 09:45:27 +08:00
|
|
|
Usage: "Forcibly deletes the container if it is still running (uses SIGKILL)",
|
2016-06-29 06:21:42 +08:00
|
|
|
},
|
|
|
|
},
|
2016-05-10 13:58:09 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2017-03-08 10:02:39 +08:00
|
|
|
if err := checkArgs(context, 1, exactArgs); err != nil {
|
2016-10-28 23:43:10 +08:00
|
|
|
return err
|
2016-01-12 08:57:18 +08:00
|
|
|
}
|
2016-09-18 11:44:17 +08:00
|
|
|
|
2017-03-08 10:02:39 +08:00
|
|
|
id := context.Args().First()
|
2017-05-17 04:17:34 +08:00
|
|
|
force := context.Bool("force")
|
2017-03-08 10:02:39 +08:00
|
|
|
container, err := getContainer(context)
|
2016-05-26 02:24:26 +08:00
|
|
|
if err != nil {
|
2017-03-08 10:02:39 +08:00
|
|
|
if lerr, ok := err.(libcontainer.Error); ok && lerr.Code() == libcontainer.ContainerNotExists {
|
|
|
|
// if there was an aborted start or something of the sort then the container's directory could exist but
|
|
|
|
// libcontainer does not see it because the state.json file inside that directory was never created.
|
|
|
|
path := filepath.Join(context.GlobalString("root"), id)
|
|
|
|
if e := os.RemoveAll(path); e != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "remove %s: %v\n", path, e)
|
2016-09-18 11:44:17 +08:00
|
|
|
}
|
2017-05-17 04:17:34 +08:00
|
|
|
if force {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-18 11:44:17 +08:00
|
|
|
}
|
2017-03-08 10:02:39 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch s {
|
|
|
|
case libcontainer.Stopped:
|
|
|
|
destroy(container)
|
|
|
|
case libcontainer.Created:
|
|
|
|
return killContainer(container)
|
|
|
|
default:
|
2017-05-17 04:17:34 +08:00
|
|
|
if force {
|
2017-03-08 10:02:39 +08:00
|
|
|
return killContainer(container)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("cannot delete container %s that is not stopped: %s\n", id, s)
|
2016-06-29 06:21:42 +08:00
|
|
|
}
|
2016-05-17 08:04:49 +08:00
|
|
|
}
|
2016-09-27 17:50:13 +08:00
|
|
|
|
2016-05-10 13:58:09 +08:00
|
|
|
return nil
|
2016-01-12 08:57:18 +08:00
|
|
|
},
|
|
|
|
}
|