1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Cleanup container rm funcs

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2015-05-26 15:38:52 -04:00
parent fc679bebb9
commit f8628ba891
3 changed files with 22 additions and 46 deletions

View file

@ -764,16 +764,14 @@ func fixPermissions(source, destination string, uid, gid int, destExisted bool)
func (b *Builder) clearTmp() {
for c := range b.TmpContainers {
tmp, err := b.Daemon.Get(c)
if err != nil {
fmt.Fprint(b.OutStream, err.Error())
rmConfig := &daemon.ContainerRmConfig{
ForceRemove: true,
RemoveVolume: true,
}
if err := b.Daemon.Rm(tmp); err != nil {
if err := b.Daemon.ContainerRm(c, rmConfig); err != nil {
fmt.Fprintf(b.OutStream, "Error removing intermediate container %s: %v\n", stringid.TruncateID(c), err)
return
}
b.Daemon.DeleteVolumes(tmp)
delete(b.TmpContainers, c)
fmt.Fprintf(b.OutStream, "Removing intermediate container %s\n", stringid.TruncateID(c))
}

View file

@ -22,8 +22,6 @@ func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error
name, err := GetFullContainerName(name)
if err != nil {
return err
// TODO: why was just job.Error(err) without return if the function cannot continue w/o container name?
//job.Error(err)
}
parent, n := path.Split(name)
if parent == "/" {
@ -46,50 +44,31 @@ func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error
return nil
}
if container != nil {
// stop collection of stats for the container regardless
// if stats are currently getting collected.
daemon.statsCollector.stopCollection(container)
if container.IsRunning() {
if config.ForceRemove {
if err := container.Kill(); err != nil {
return fmt.Errorf("Could not kill running container, cannot remove - %v", err)
}
} else {
return fmt.Errorf("Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f")
}
}
if config.ForceRemove {
if err := daemon.ForceRm(container); err != nil {
logrus.Errorf("Cannot destroy container %s: %v", name, err)
}
} else {
if err := daemon.Rm(container); err != nil {
if err := daemon.rm(container, config.ForceRemove); err != nil {
return fmt.Errorf("Cannot destroy container %s: %v", name, err)
}
}
container.LogEvent("destroy")
if config.RemoveVolume {
container.removeMountPoints()
}
}
return nil
}
func (daemon *Daemon) Rm(container *Container) (err error) {
return daemon.commonRm(container, false)
}
func (daemon *Daemon) ForceRm(container *Container) (err error) {
return daemon.commonRm(container, true)
}
// Destroy unregisters a container from the daemon and cleanly removes its contents from the filesystem.
func (daemon *Daemon) commonRm(container *Container, forceRemove bool) (err error) {
if container == nil {
return fmt.Errorf("The given container is <nil>")
func (daemon *Daemon) rm(container *Container, forceRemove bool) (err error) {
// stop collection of stats for the container regardless
// if stats are currently getting collected.
daemon.statsCollector.stopCollection(container)
if container.IsRunning() {
if !forceRemove {
return fmt.Errorf("Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f")
}
if err := container.Kill(); err != nil {
return fmt.Errorf("Could not kill running container, cannot remove - %v", err)
}
}
element := daemon.containers.Get(container.ID)

View file

@ -435,7 +435,6 @@ func (s *DockerSuite) TestCpVolumePath(c *check.C) {
}
cleanedContainerID := strings.TrimSpace(out)
defer dockerCmd(c, "rm", "-fv", cleanedContainerID)
out, _ = dockerCmd(c, "wait", cleanedContainerID)
if strings.TrimSpace(out) != "0" {