2017-02-07 21:33:23 +00:00
|
|
|
// +build !solaris
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func killContainer(container libcontainer.Container) error {
|
2017-04-11 15:10:46 +00:00
|
|
|
_ = container.Signal(syscall.SIGKILL, false)
|
2017-02-07 21:33:23 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2017-04-11 15:10:46 +00:00
|
|
|
if err := container.Signal(syscall.Signal(0), false); err != nil {
|
2017-02-07 21:33:23 +00:00
|
|
|
destroy(container)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("container init still running")
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteCommand = cli.Command{
|
|
|
|
Name: "delete",
|
2017-04-11 15:10:46 +00:00
|
|
|
Usage: "delete any resources held by the container often used with detached container",
|
2017-02-07 21:33:23 +00:00
|
|
|
ArgsUsage: `<container-id>
|
|
|
|
|
|
|
|
Where "<container-id>" is the name for the instance of the container.
|
|
|
|
|
|
|
|
EXAMPLE:
|
|
|
|
For example, if the container id is "ubuntu01" and runc list currently shows the
|
|
|
|
status of "ubuntu01" as "stopped" the following will delete resources held for
|
|
|
|
"ubuntu01" removing "ubuntu01" from the runc list of containers:
|
|
|
|
|
|
|
|
# runc delete ubuntu01`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
2017-04-11 15:10:46 +00:00
|
|
|
Usage: "Forcibly deletes the container if it is still running (uses SIGKILL)",
|
2017-02-07 21:33:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
2017-04-11 15:10:46 +00:00
|
|
|
if err := checkArgs(context, 1, exactArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := context.Args().First()
|
2017-02-07 21:33:23 +00:00
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
|
|
|
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.
|
2017-04-11 15:10:46 +00:00
|
|
|
path := filepath.Join(context.GlobalString("root"), id)
|
|
|
|
if e := os.RemoveAll(path); e != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "remove %s: %v\n", path, e)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
s, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch s {
|
|
|
|
case libcontainer.Stopped:
|
|
|
|
destroy(container)
|
|
|
|
case libcontainer.Created:
|
|
|
|
return killContainer(container)
|
|
|
|
default:
|
|
|
|
if context.Bool("force") {
|
|
|
|
return killContainer(container)
|
2017-04-11 15:10:46 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("cannot delete container %s that is not stopped: %s\n", id, s)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|