From f7c5e92a2e1ec30f50b0affe952a0496c62195f5 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 24 Apr 2013 15:24:14 -0700 Subject: [PATCH] Move runtime.Commit to builder.Commit --- builder.go | 23 ++++++++++++++++++++++- commands.go | 7 ++++++- runtime.go | 27 --------------------------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/builder.go b/builder.go index 66118a9879..1c354ea8a9 100644 --- a/builder.go +++ b/builder.go @@ -13,11 +13,13 @@ import ( type Builder struct { runtime *Runtime repositories *TagStore + graph *Graph } func NewBuilder(runtime *Runtime) *Builder { return &Builder{ runtime: runtime, + graph: runtime.graph, repositories: runtime.repositories, } } @@ -83,8 +85,27 @@ func (builder *Builder) Create(config *Config) (*Container, error) { return container, nil } +// Commit creates a new filesystem image from the current state of a container. +// The image can optionally be tagged into a repository func (builder *Builder) Commit(container *Container, repository, tag, comment, author string) (*Image, error) { - return builder.runtime.Commit(container.Id, repository, tag, comment, author) + // FIXME: freeze the container before copying it to avoid data corruption? + // FIXME: this shouldn't be in commands. + rwTar, err := container.ExportRw() + if err != nil { + return nil, err + } + // Create a new image from the container's base layers + a new layer from container changes + img, err := builder.graph.Create(rwTar, container, comment, author) + if err != nil { + return nil, err + } + // Register the image if needed + if repository != "" { + if err := builder.repositories.Set(repository, tag, img.Id, true); err != nil { + return img, err + } + } + return img, nil } func (builder *Builder) clearTmp(containers, images map[string]struct{}) { diff --git a/commands.go b/commands.go index aaa4dab3e5..39730afe56 100644 --- a/commands.go +++ b/commands.go @@ -852,7 +852,12 @@ func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...stri } } - img, err := srv.runtime.Commit(containerName, repository, tag, *flComment, *flAuthor, config) + container := srv.runtime.Get(containerName) + if container == nil { + return fmt.Errorf("No such container: %s", containerName) + } + + img, err := NewBuilder(srv.runtime).Commit(container, repository, tag, *flComment, *flAuthor, config) if err != nil { return err } diff --git a/runtime.go b/runtime.go index a298d9b714..ca27b7a7bf 100644 --- a/runtime.go +++ b/runtime.go @@ -309,33 +309,6 @@ func (runtime *Runtime) Destroy(container *Container) error { return nil } -// Commit creates a new filesystem image from the current state of a container. -// The image can optionally be tagged into a repository -func (runtime *Runtime) Commit(id, repository, tag, comment, author string, config *Config) (*Image, error) { - container := runtime.Get(id) - if container == nil { - return nil, fmt.Errorf("No such container: %s", id) - } - // FIXME: freeze the container before copying it to avoid data corruption? - // FIXME: this shouldn't be in commands. - rwTar, err := container.ExportRw() - if err != nil { - return nil, err - } - // Create a new image from the container's base layers + a new layer from container changes - img, err := runtime.graph.Create(rwTar, container, comment, author, config) - if err != nil { - return nil, err - } - // Register the image if needed - if repository != "" { - if err := runtime.repositories.Set(repository, tag, img.Id, true); err != nil { - return img, err - } - } - return img, nil -} - func (runtime *Runtime) restore() error { dir, err := ioutil.ReadDir(runtime.repository) if err != nil {