Merge pull request #928 from mlaventure/add-force-to-delete

Add force to delete
This commit is contained in:
Daniel, Dao Quang Minh 2016-07-06 16:59:58 +01:00 committed by GitHub
commit 0b5d51cbb0
2 changed files with 41 additions and 11 deletions

View File

@ -13,6 +13,18 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
) )
func killContainer(container libcontainer.Container) error {
container.Signal(syscall.SIGKILL)
for i := 0; i < 100; i++ {
time.Sleep(100 * time.Millisecond)
if err := container.Signal(syscall.Signal(0)); err != nil {
destroy(container)
return nil
}
}
return fmt.Errorf("container init still running")
}
var deleteCommand = cli.Command{ var deleteCommand = cli.Command{
Name: "delete", Name: "delete",
Usage: "delete any resources held by the container often used with detached containers", Usage: "delete any resources held by the container often used with detached containers",
@ -26,6 +38,12 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
"ubuntu01" removing "ubuntu01" from the runc list of containers: "ubuntu01" removing "ubuntu01" from the runc list of containers:
# runc delete ubuntu01`, # runc delete ubuntu01`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "Forcibly kills the container if it is still running",
},
},
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
container, err := getContainer(context) container, err := getContainer(context)
if err != nil { if err != nil {
@ -47,16 +65,11 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
case libcontainer.Stopped: case libcontainer.Stopped:
destroy(container) destroy(container)
case libcontainer.Created: case libcontainer.Created:
container.Signal(syscall.SIGKILL) return killContainer(container)
for i := 0; i < 100; i++ {
time.Sleep(100 * time.Millisecond)
if err := container.Signal(syscall.Signal(0)); err != nil {
destroy(container)
return nil
}
}
return fmt.Errorf("container init still running")
default: default:
if context.Bool("force") {
return killContainer(container)
}
return fmt.Errorf("cannot delete container that is not stopped: %s", s) return fmt.Errorf("cannot delete container that is not stopped: %s", s)
} }
return nil return nil

View File

@ -31,3 +31,20 @@ function teardown() {
runc state test_busybox runc state test_busybox
[ "$status" -ne 0 ] [ "$status" -ne 0 ]
} }
@test "runc delete --force" {
# run busybox detached
runc run -d --console /dev/pts/ptmx test_busybox
[ "$status" -eq 0 ]
# check state
wait_for_container 15 1 test_busybox
testcontainer test_busybox running
# force delete test_busybox
runc delete --force test_busybox
runc state test_busybox
[ "$status" -ne 0 ]
}