From 3edd14b8c2eb13f45834f5d9306a579b256f9348 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Fri, 12 Apr 2013 09:23:57 -0700 Subject: [PATCH] Implement the data volume removal --- commands.go | 28 +++++++++++++++++++++++++++- container.go | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/commands.go b/commands.go index cb7b57ff00..732fdcaaaa 100644 --- a/commands.go +++ b/commands.go @@ -401,7 +401,8 @@ func (srv *Server) CmdHistory(stdin io.ReadCloser, stdout io.Writer, args ...str } func (srv *Server) CmdRm(stdin io.ReadCloser, stdout io.Writer, args ...string) error { - cmd := rcli.Subcmd(stdout, "rm", "CONTAINER [CONTAINER...]", "Remove a container") + cmd := rcli.Subcmd(stdout, "rm", "[OPTIONS] CONTAINER [CONTAINER...]", "Remove a container") + v := cmd.Bool("v", false, "Remove the volumes associated to the container") if err := cmd.Parse(args); err != nil { return nil } @@ -409,15 +410,40 @@ func (srv *Server) CmdRm(stdin io.ReadCloser, stdout io.Writer, args ...string) cmd.Usage() return nil } + volumes := make(map[string]struct{}) for _, name := range cmd.Args() { container := srv.runtime.Get(name) if container == nil { return fmt.Errorf("No such container: %s", name) } + // Store all the deleted containers volumes + for _, volumeId := range container.Volumes { + volumes[volumeId] = struct{}{} + } if err := srv.runtime.Destroy(container); err != nil { fmt.Fprintln(stdout, "Error destroying container "+name+": "+err.Error()) } } + if *v { + // Retrieve all volumes from all remaining containers + usedVolumes := make(map[string]*Container) + for _, container := range srv.runtime.List() { + for _, containerVolumeId := range container.Volumes { + usedVolumes[containerVolumeId] = container + } + } + + for volumeId := range volumes { + // If the requested volu + if c, exists := usedVolumes[volumeId]; exists { + fmt.Fprintf(stdout, "The volume %s is used by the container %s. Impossible to remove it. Skipping.\n", volumeId, c.Id) + continue + } + if err := srv.runtime.volumes.Delete(volumeId); err != nil { + return err + } + } + } return nil } diff --git a/container.go b/container.go index 7bd5a791ac..92bcc2506d 100644 --- a/container.go +++ b/container.go @@ -421,7 +421,7 @@ func (container *Container) Start() error { if container.Config.VolumesFrom != "" { c := container.runtime.Get(container.Config.VolumesFrom) if c == nil { - return fmt.Errorf("Container %s not found. Impossible to mount its volumes") + return fmt.Errorf("Container %s not found. Impossible to mount its volumes", container.Id) } for volPath, id := range c.Volumes { if _, exists := container.Volumes[volPath]; exists {