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