Skip to content

Commit

Permalink
Add control for removing stopped docker containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed Apr 13, 2016
1 parent 4ce6c13 commit e35c982
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
RestartContainer, StopContainer, PauseContainer, AttachContainer, ExecContainer,
)
} else {
result = result.WithControls(StartContainer)
result = result.WithControls(StartContainer, RemoveContainer)
}

result = AddLabels(result, c.container.Config.Labels)
Expand Down
10 changes: 10 additions & 0 deletions probe/docker/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
RestartContainer = "docker_restart_container"
PauseContainer = "docker_pause_container"
UnpauseContainer = "docker_unpause_container"
RemoveContainer = "docker_remove_container"
AttachContainer = "docker_attach_container"
ExecContainer = "docker_exec_container"

Expand Down Expand Up @@ -48,6 +49,13 @@ func (r *registry) unpauseContainer(containerID string, _ xfer.Request) xfer.Res
return xfer.ResponseError(r.client.UnpauseContainer(containerID))
}

func (r *registry) removeContainer(containerID string, _ xfer.Request) xfer.Response {
log.Infof("Removing container %s", containerID)
return xfer.ResponseError(r.client.RemoveContainer(docker_client.RemoveContainerOptions{
ID: containerID,
}))
}

func (r *registry) attachContainer(containerID string, req xfer.Request) xfer.Response {
c, ok := r.GetContainer(containerID)
if !ok {
Expand Down Expand Up @@ -156,6 +164,7 @@ func (r *registry) registerControls() {
controls.Register(RestartContainer, captureContainerID(r.restartContainer))
controls.Register(PauseContainer, captureContainerID(r.pauseContainer))
controls.Register(UnpauseContainer, captureContainerID(r.unpauseContainer))
controls.Register(RemoveContainer, captureContainerID(r.removeContainer))
controls.Register(AttachContainer, captureContainerID(r.attachContainer))
controls.Register(ExecContainer, captureContainerID(r.execContainer))
}
Expand All @@ -166,6 +175,7 @@ func (r *registry) deregisterControls() {
controls.Rm(RestartContainer)
controls.Rm(PauseContainer)
controls.Rm(UnpauseContainer)
controls.Rm(RemoveContainer)
controls.Rm(AttachContainer)
controls.Rm(ExecContainer)
}
1 change: 1 addition & 0 deletions probe/docker/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Client interface {
RestartContainer(string, uint) error
PauseContainer(string) error
UnpauseContainer(string) error
RemoveContainer(docker_client.RemoveContainerOptions) error
AttachToContainerNonBlocking(docker_client.AttachToContainerOptions) (docker_client.CloseWaiter, error)
CreateExec(docker_client.CreateExecOptions) (*docker_client.Exec, error)
StartExecNonBlocking(string, docker_client.StartExecOptions) (docker_client.CloseWaiter, error)
Expand Down
4 changes: 4 additions & 0 deletions probe/docker/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func (m *mockDockerClient) UnpauseContainer(_ string) error {
return fmt.Errorf("unpaused")
}

func (m *mockDockerClient) RemoveContainer(_ client.RemoveContainerOptions) error {
return fmt.Errorf("remove")
}

type mockCloseWaiter struct{}

func (mockCloseWaiter) Close() error { return nil }
Expand Down
5 changes: 5 additions & 0 deletions probe/docker/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology {
Human: "Unpause",
Icon: "fa-play",
})
result.Controls.AddControl(report.Control{
ID: RemoveContainer,
Human: "Remove",
Icon: "fa-trash-o",
})
result.Controls.AddControl(report.Control{
ID: AttachContainer,
Human: "Attach",
Expand Down

0 comments on commit e35c982

Please sign in to comment.