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{}) {
... ...
@@ -810,7 +810,13 @@ func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...stri
810 810
 		cmd.Usage()
811 811
 		return nil
812 812
 	}
813
-	img, err := srv.runtime.Commit(containerName, repository, tag, *flComment, *flAuthor)
813
+
814
+	container := srv.runtime.Get(containerName)
815
+	if container == nil {
816
+		return fmt.Errorf("No such container: %s", containerName)
817
+	}
818
+
819
+	img, err := NewBuilder(srv.runtime).Commit(container, repository, tag, *flComment, *flAuthor)
814 820
 	if err != nil {
815 821
 		return err
816 822
 	}
... ...
@@ -185,33 +185,6 @@ func (runtime *Runtime) Destroy(container *Container) error {
185 185
 	return nil
186 186
 }
187 187
 
188
-// Commit creates a new filesystem image from the current state of a container.
189
-// The image can optionally be tagged into a repository
190
-func (runtime *Runtime) Commit(id, repository, tag, comment, author string) (*Image, error) {
191
-	container := runtime.Get(id)
192
-	if container == nil {
193
-		return nil, fmt.Errorf("No such container: %s", id)
194
-	}
195
-	// FIXME: freeze the container before copying it to avoid data corruption?
196
-	// FIXME: this shouldn't be in commands.
197
-	rwTar, err := container.ExportRw()
198
-	if err != nil {
199
-		return nil, err
200
-	}
201
-	// Create a new image from the container's base layers + a new layer from container changes
202
-	img, err := runtime.graph.Create(rwTar, container, comment, author)
203
-	if err != nil {
204
-		return nil, err
205
-	}
206
-	// Register the image if needed
207
-	if repository != "" {
208
-		if err := runtime.repositories.Set(repository, tag, img.Id, true); err != nil {
209
-			return img, err
210
-		}
211
-	}
212
-	return img, nil
213
-}
214
-
215 188
 func (runtime *Runtime) restore() error {
216 189
 	dir, err := ioutil.ReadDir(runtime.repository)
217 190
 	if err != nil {