From 9fc9d10e574daeb128fb6678b69f98d93051dcfe Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Tue, 28 Jun 2016 15:21:42 -0700 Subject: [PATCH] Add `--force` flag to `runc delete` Signed-off-by: Kenfe-Mickael Laventure --- delete.go | 35 ++++++++++++++++++++++++----------- tests/integration/delete.bats | 17 +++++++++++++++++ 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/delete.go b/delete.go index 920dade2..803bdcda 100644 --- a/delete.go +++ b/delete.go @@ -13,6 +13,18 @@ import ( "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{ Name: "delete", Usage: "delete any resources held by the container often used with detached containers", @@ -23,9 +35,15 @@ Where "" 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: - +"ubuntu01" removing "ubuntu01" from the runc list of containers: + # 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 { container, err := getContainer(context) if err != nil { @@ -47,16 +65,11 @@ status of "ubuntu01" as "stopped" the following will delete resources held for case libcontainer.Stopped: destroy(container) case libcontainer.Created: - 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") + return killContainer(container) default: + if context.Bool("force") { + return killContainer(container) + } return fmt.Errorf("cannot delete container that is not stopped: %s", s) } return nil diff --git a/tests/integration/delete.bats b/tests/integration/delete.bats index 89a60ebc..22747aa6 100644 --- a/tests/integration/delete.bats +++ b/tests/integration/delete.bats @@ -31,3 +31,20 @@ function teardown() { runc state test_busybox [ "$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 ] +}