Skip to content

Commit

Permalink
Allow delete of created container
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
  • Loading branch information
crosbymichael committed Jun 2, 2016
1 parent 06fab0f commit 1d61abe
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 54 deletions.
18 changes: 16 additions & 2 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"os"
"path/filepath"
"syscall"
"time"

"github.com/codegangsta/cli"
"github.com/opencontainers/runc/libcontainer"
Expand Down Expand Up @@ -41,10 +43,22 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
if err != nil {
return err
}
if s != libcontainer.Stopped {
switch s {
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")
default:
return fmt.Errorf("cannot delete container that is not stopped: %s", s)
}
destroy(container)
return nil
},
}
4 changes: 2 additions & 2 deletions libcontainer/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type BaseContainer interface {
// SystemError - System error.
Start(process *Process) (err error)

// StartI immediatly starts the process inside the conatiner. Returns error if process
// Run immediatly starts the process inside the conatiner. Returns error if process
// fails to start. It does not block waiting for a SIGCONT after start returns but
// sends the signal when the process has completed.
//
Expand All @@ -132,7 +132,7 @@ type BaseContainer interface {
// ConfigInvalid - config is invalid,
// ContainerPaused - Container is paused,
// SystemError - System error.
StartI(process *Process) (err error)
Run(process *Process) (err error)

// Destroys the container after killing all running processes.
//
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c *linuxContainer) Start(process *Process) error {
return c.start(process, status == Stopped)
}

func (c *linuxContainer) StartI(process *Process) error {
func (c *linuxContainer) Run(process *Process) error {
c.m.Lock()
defer c.m.Unlock()
status, err := c.currentStatus()
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/integration/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestCheckpoint(t *testing.T) {
Stdout: &stdout,
}

err = container.StartI(&pconfig)
err = container.Run(&pconfig)
stdinR.Close()
defer stdinW.Close()
if err != nil {
Expand Down
46 changes: 23 additions & 23 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestEnter(t *testing.T) {
Stdin: stdinR,
Stdout: &stdout,
}
err = container.StartI(&pconfig)
err = container.Run(&pconfig)
stdinR.Close()
defer stdinW.Close()
ok(t, err)
Expand All @@ -259,7 +259,7 @@ func TestEnter(t *testing.T) {
pconfig2.Stdin = stdinR2
pconfig2.Stdout = &stdout2

err = container.StartI(&pconfig2)
err = container.Run(&pconfig2)
stdinR2.Close()
defer stdinW2.Close()
ok(t, err)
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestProcessEnv(t *testing.T) {
Stdin: nil,
Stdout: &stdout,
}
err = container.StartI(&pconfig)
err = container.Run(&pconfig)
ok(t, err)

// Wait for process
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestProcessCaps(t *testing.T) {
Stdin: nil,
Stdout: &stdout,
}
err = container.StartI(&pconfig)
err = container.Run(&pconfig)
ok(t, err)

// Wait for process
Expand Down Expand Up @@ -448,7 +448,7 @@ func TestAdditionalGroups(t *testing.T) {
Stdin: nil,
Stdout: &stdout,
}
err = container.StartI(&pconfig)
err = container.Run(&pconfig)
ok(t, err)

// Wait for process
Expand Down Expand Up @@ -508,7 +508,7 @@ func testFreeze(t *testing.T, systemd bool) {
Env: standardEnvironment,
Stdin: stdinR,
}
err = container.StartI(pconfig)
err = container.Run(pconfig)
stdinR.Close()
defer stdinW.Close()
ok(t, err)
Expand Down Expand Up @@ -719,7 +719,7 @@ func TestContainerState(t *testing.T) {
Env: standardEnvironment,
Stdin: stdinR,
}
err = container.StartI(p)
err = container.Run(p)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -772,7 +772,7 @@ func TestPassExtraFiles(t *testing.T) {
Stdin: nil,
Stdout: &stdout,
}
err = container.StartI(&process)
err = container.Run(&process)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -853,7 +853,7 @@ func TestMountCmds(t *testing.T) {
Args: []string{"sh", "-c", "env"},
Env: standardEnvironment,
}
err = container.StartI(&pconfig)
err = container.Run(&pconfig)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -902,7 +902,7 @@ func TestSysctl(t *testing.T) {
Stdin: nil,
Stdout: &stdout,
}
err = container.StartI(&pconfig)
err = container.Run(&pconfig)
ok(t, err)

// Wait for process
Expand Down Expand Up @@ -1042,7 +1042,7 @@ func TestOomScoreAdj(t *testing.T) {
Stdin: nil,
Stdout: &stdout,
}
err = container.StartI(&pconfig)
err = container.Run(&pconfig)
ok(t, err)

// Wait for process
Expand Down Expand Up @@ -1114,7 +1114,7 @@ func TestHook(t *testing.T) {
Stdin: nil,
Stdout: &stdout,
}
err = container.StartI(&pconfig)
err = container.Run(&pconfig)
ok(t, err)

// Wait for process
Expand Down Expand Up @@ -1231,7 +1231,7 @@ func TestRootfsPropagationSlaveMount(t *testing.T) {
Stdin: stdinR,
}

err = container.StartI(pconfig)
err = container.Run(pconfig)
stdinR.Close()
defer stdinW.Close()
ok(t, err)
Expand Down Expand Up @@ -1260,7 +1260,7 @@ func TestRootfsPropagationSlaveMount(t *testing.T) {
Stdout: &stdout2,
}

err = container.StartI(pconfig2)
err = container.Run(pconfig2)
stdinR2.Close()
defer stdinW2.Close()
ok(t, err)
Expand Down Expand Up @@ -1348,7 +1348,7 @@ func TestRootfsPropagationSharedMount(t *testing.T) {
Stdin: stdinR,
}

err = container.StartI(pconfig)
err = container.Run(pconfig)
stdinR.Close()
defer stdinW.Close()
ok(t, err)
Expand Down Expand Up @@ -1380,7 +1380,7 @@ func TestRootfsPropagationSharedMount(t *testing.T) {
Capabilities: processCaps,
}

err = container.StartI(pconfig2)
err = container.Run(pconfig2)
stdinR2.Close()
defer stdinW2.Close()
ok(t, err)
Expand Down Expand Up @@ -1452,7 +1452,7 @@ func TestInitJoinPID(t *testing.T) {
Env: standardEnvironment,
Stdin: stdinR1,
}
err = container1.StartI(init1)
err = container1.Run(init1)
stdinR1.Close()
defer stdinW1.Close()
ok(t, err)
Expand All @@ -1462,7 +1462,7 @@ func TestInitJoinPID(t *testing.T) {
ok(t, err)
pidns1 := state1.NamespacePaths[configs.NEWPID]

// StartI a container inside the existing pidns but with different cgroups
// Run a container inside the existing pidns but with different cgroups
config2 := newTemplateConfig(rootfs)
config2.Namespaces.Add(configs.NEWPID, pidns1)
config2.Cgroups.Path = "integration/test2"
Expand All @@ -1478,7 +1478,7 @@ func TestInitJoinPID(t *testing.T) {
Env: standardEnvironment,
Stdin: stdinR2,
}
err = container2.StartI(init2)
err = container2.Run(init2)
stdinR2.Close()
defer stdinW2.Close()
ok(t, err)
Expand Down Expand Up @@ -1508,7 +1508,7 @@ func TestInitJoinPID(t *testing.T) {
Env: standardEnvironment,
Stdout: buffers.Stdout,
}
err = container1.StartI(ps)
err = container1.Run(ps)
ok(t, err)
waitProcess(ps, t)

Expand Down Expand Up @@ -1557,7 +1557,7 @@ func TestInitJoinNetworkAndUser(t *testing.T) {
Env: standardEnvironment,
Stdin: stdinR1,
}
err = container1.StartI(init1)
err = container1.Run(init1)
stdinR1.Close()
defer stdinW1.Close()
ok(t, err)
Expand All @@ -1568,7 +1568,7 @@ func TestInitJoinNetworkAndUser(t *testing.T) {
netns1 := state1.NamespacePaths[configs.NEWNET]
userns1 := state1.NamespacePaths[configs.NEWUSER]

// StartI a container inside the existing pidns but with different cgroups
// Run a container inside the existing pidns but with different cgroups
rootfs2, err := newRootfs()
ok(t, err)
defer remove(rootfs2)
Expand All @@ -1591,7 +1591,7 @@ func TestInitJoinNetworkAndUser(t *testing.T) {
Env: standardEnvironment,
Stdin: stdinR2,
}
err = container2.StartI(init2)
err = container2.Run(init2)
stdinR2.Close()
defer stdinW2.Close()
ok(t, err)
Expand Down
Loading

0 comments on commit 1d61abe

Please sign in to comment.