Browse code

Move runtime.Commit to builder.Commit

Guillaume J. Charmes authored on 2013/04/25 07:24:14
Showing 3 changed files
... ...
@@ -13,11 +13,13 @@ import (
13 13
 type Builder struct {
14 14
 	runtime      *Runtime
15 15
 	repositories *TagStore
16
+	graph        *Graph
16 17
 }
17 18
 
18 19
 func NewBuilder(runtime *Runtime) *Builder {
19 20
 	return &Builder{
20 21
 		runtime:      runtime,
22
+		graph:        runtime.graph,
21 23
 		repositories: runtime.repositories,
22 24
 	}
23 25
 }
... ...
@@ -83,8 +85,27 @@ func (builder *Builder) Create(config *Config) (*Container, error) {
83 83
 	return container, nil
84 84
 }
85 85
 
86
+// Commit creates a new filesystem image from the current state of a container.
87
+// The image can optionally be tagged into a repository
86 88
 func (builder *Builder) Commit(container *Container, repository, tag, comment, author string) (*Image, error) {
87
-	return builder.runtime.Commit(container.Id, repository, tag, comment, author)
89
+	// FIXME: freeze the container before copying it to avoid data corruption?
90
+	// FIXME: this shouldn't be in commands.
91
+	rwTar, err := container.ExportRw()
92
+	if err != nil {
93
+		return nil, err
94
+	}
95
+	// Create a new image from the container's base layers + a new layer from container changes
96
+	img, err := builder.graph.Create(rwTar, container, comment, author)
97
+	if err != nil {
98
+		return nil, err
99
+	}
100
+	// Register the image if needed
101
+	if repository != "" {
102
+		if err := builder.repositories.Set(repository, tag, img.Id, true); err != nil {
103
+			return img, err
104
+		}
105
+	}
106
+	return img, nil
88 107
 }
89 108
 
90 109
 func (builder *Builder) clearTmp(containers, images map[string]struct{}) {
... ...
@@ -852,7 +852,12 @@ func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...stri
852 852
 		}
853 853
 	}
854 854
 
855
-	img, err := srv.runtime.Commit(containerName, repository, tag, *flComment, *flAuthor, config)
855
+	container := srv.runtime.Get(containerName)
856
+	if container == nil {
857
+		return fmt.Errorf("No such container: %s", containerName)
858
+	}
859
+
860
+	img, err := NewBuilder(srv.runtime).Commit(container, repository, tag, *flComment, *flAuthor, config)
856 861
 	if err != nil {
857 862
 		return err
858 863
 	}
... ...
@@ -309,33 +309,6 @@ func (runtime *Runtime) Destroy(container *Container) error {
309 309
 	return nil
310 310
 }
311 311
 
312
-// Commit creates a new filesystem image from the current state of a container.
313
-// The image can optionally be tagged into a repository
314
-func (runtime *Runtime) Commit(id, repository, tag, comment, author string, config *Config) (*Image, error) {
315
-	container := runtime.Get(id)
316
-	if container == nil {
317
-		return nil, fmt.Errorf("No such container: %s", id)
318
-	}
319
-	// FIXME: freeze the container before copying it to avoid data corruption?
320
-	// FIXME: this shouldn't be in commands.
321
-	rwTar, err := container.ExportRw()
322
-	if err != nil {
323
-		return nil, err
324
-	}
325
-	// Create a new image from the container's base layers + a new layer from container changes
326
-	img, err := runtime.graph.Create(rwTar, container, comment, author, config)
327
-	if err != nil {
328
-		return nil, err
329
-	}
330
-	// Register the image if needed
331
-	if repository != "" {
332
-		if err := runtime.repositories.Set(repository, tag, img.Id, true); err != nil {
333
-			return img, err
334
-		}
335
-	}
336
-	return img, nil
337
-}
338
-
339 312
 func (runtime *Runtime) restore() error {
340 313
 	dir, err := ioutil.ReadDir(runtime.repository)
341 314
 	if err != nil {