Browse code

move commit to job

Victor Vieux authored on 2013/12/12 10:03:48
Showing 5 changed files
... ...
@@ -377,13 +377,18 @@ func postCommit(srv *Server, version float64, w http.ResponseWriter, r *http.Req
377 377
 	if err := json.NewDecoder(r.Body).Decode(config); err != nil && err != io.EOF {
378 378
 		utils.Errorf("%s", err)
379 379
 	}
380
-	repo := r.Form.Get("repo")
381
-	tag := r.Form.Get("tag")
382
-	container := r.Form.Get("container")
383
-	author := r.Form.Get("author")
384
-	comment := r.Form.Get("comment")
385
-	id, err := srv.ContainerCommit(container, repo, tag, author, comment, config)
386
-	if err != nil {
380
+
381
+	job := srv.Eng.Job("commit")
382
+	job.Setenv("repo", r.Form.Get("repo"))
383
+	job.Setenv("tag", r.Form.Get("tag"))
384
+	job.Setenv("container", r.Form.Get("container"))
385
+	job.Setenv("author", r.Form.Get("author"))
386
+	job.Setenv("comment", r.Form.Get("comment"))
387
+	job.SetenvJson("config", config)
388
+
389
+	var id string
390
+	job.Stdout.AddString(&id)
391
+	if err := job.Run(); err != nil {
387 392
 		return err
388 393
 	}
389 394
 
... ...
@@ -1,17 +1,16 @@
1 1
 package engine
2 2
 
3 3
 import (
4
-	"strings"
5
-	"io"
6
-	"encoding/json"
7
-	"strconv"
8 4
 	"bytes"
5
+	"encoding/json"
9 6
 	"fmt"
7
+	"io"
8
+	"strconv"
9
+	"strings"
10 10
 )
11 11
 
12 12
 type Env []string
13 13
 
14
-
15 14
 func (env *Env) Get(key string) (value string) {
16 15
 	// FIXME: use Map()
17 16
 	for _, kv := range *env {
... ...
@@ -44,7 +43,6 @@ func (env *Env) GetBool(key string) (value bool) {
44 44
 	return true
45 45
 }
46 46
 
47
-
48 47
 func (env *Env) SetBool(key string, value bool) {
49 48
 	if value {
50 49
 		env.Set(key, "1")
... ...
@@ -79,6 +77,17 @@ func (env *Env) GetList(key string) []string {
79 79
 	return l
80 80
 }
81 81
 
82
+func (env *Env) GetJson(key string) interface{} {
83
+	sval := env.Get(key)
84
+	if sval == "" {
85
+		return nil
86
+	}
87
+	var m interface{}
88
+	//Discard error on purpose
89
+	json.Unmarshal([]byte(sval), &m)
90
+	return m
91
+}
92
+
82 93
 func (env *Env) SetJson(key string, value interface{}) error {
83 94
 	sval, err := json.Marshal(value)
84 95
 	if err != nil {
... ...
@@ -218,4 +227,3 @@ func (env *Env) Map() map[string]string {
218 218
 	}
219 219
 	return m
220 220
 }
221
-
... ...
@@ -126,6 +126,10 @@ func (job *Job) GetenvList(key string) []string {
126 126
 	return job.env.GetList(key)
127 127
 }
128 128
 
129
+func (job *Job) GetenvJson(key string) interface{} {
130
+	return job.env.GetJson(key)
131
+}
132
+
129 133
 func (job *Job) SetenvJson(key string, value interface{}) error {
130 134
 	return job.env.SetJson(key, value)
131 135
 }
... ...
@@ -146,7 +146,6 @@ func TestCreateRmVolumes(t *testing.T) {
146 146
 
147 147
 func TestCommit(t *testing.T) {
148 148
 	eng := NewTestEngine(t)
149
-	srv := mkServerFromEngine(eng, t)
150 149
 	defer mkRuntimeFromEngine(eng, t).Nuke()
151 150
 
152 151
 	config, _, _, err := docker.ParseRun([]string{unitTestImageID, "/bin/cat"}, nil)
... ...
@@ -156,7 +155,12 @@ func TestCommit(t *testing.T) {
156 156
 
157 157
 	id := createTestContainer(eng, config, t)
158 158
 
159
-	if _, err := srv.ContainerCommit(id, "testrepo", "testtag", "", "", config); err != nil {
159
+	job := eng.Job("commit")
160
+	job.Setenv("container", id)
161
+	job.Setenv("repo", "testrepo")
162
+	job.Setenv("tag", "testtag")
163
+	job.SetenvJson("config", config)
164
+	if err := job.Run(); err != nil {
160 165
 		t.Fatal(err)
161 166
 	}
162 167
 }
... ...
@@ -264,8 +268,12 @@ func TestRmi(t *testing.T) {
264 264
 		t.Fatal(err)
265 265
 	}
266 266
 
267
-	imageID, err := srv.ContainerCommit(containerID, "test", "", "", "", nil)
268
-	if err != nil {
267
+	job = eng.Job("commit")
268
+	job.Setenv("container", containerID)
269
+	job.Setenv("repo", "test")
270
+	var imageID string
271
+	job.Stdout.AddString(&imageID)
272
+	if err := job.Run(); err != nil {
269 273
 		t.Fatal(err)
270 274
 	}
271 275
 
... ...
@@ -288,8 +296,10 @@ func TestRmi(t *testing.T) {
288 288
 		t.Fatal(err)
289 289
 	}
290 290
 
291
-	_, err = srv.ContainerCommit(containerID, "test", "", "", "", nil)
292
-	if err != nil {
291
+	job = eng.Job("commit")
292
+	job.Setenv("container", containerID)
293
+	job.Setenv("repo", "test")
294
+	if err := job.Run(); err != nil {
293 295
 		t.Fatal(err)
294 296
 	}
295 297
 
... ...
@@ -107,6 +107,10 @@ func jobInitApi(job *engine.Job) engine.Status {
107 107
 		job.Error(err)
108 108
 		return engine.StatusErr
109 109
 	}
110
+	if err := job.Eng.Register("commit", srv.ContainerCommit); err != nil {
111
+		job.Error(err)
112
+		return engine.StatusErr
113
+	}
110 114
 	return engine.StatusOK
111 115
 }
112 116
 
... ...
@@ -769,16 +773,31 @@ func createAPIContainer(names []string, container *Container, size bool, runtime
769 769
 	}
770 770
 	return c
771 771
 }
772
-func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) {
772
+func (srv *Server) ContainerCommit(job *engine.Job) engine.Status {
773
+	if len(job.Args) != 0 {
774
+		job.Errorf("Usage: %s\n", job.Name)
775
+		return engine.StatusErr
776
+	}
777
+	name := job.Getenv("container")
778
+
773 779
 	container := srv.runtime.Get(name)
774 780
 	if container == nil {
775
-		return "", fmt.Errorf("No such container: %s", name)
781
+		job.Errorf("No such container: %s", name)
782
+		return engine.StatusErr
783
+	}
784
+	var config *Config
785
+	iConfig, ok := job.GetenvJson("config").(Config)
786
+	if ok {
787
+		config = &iConfig
776 788
 	}
777
-	img, err := srv.runtime.Commit(container, repo, tag, comment, author, config)
789
+
790
+	img, err := srv.runtime.Commit(container, job.Getenv("repo"), job.Getenv("tag"), job.Getenv("comment"), job.Getenv("author"), config)
778 791
 	if err != nil {
779
-		return "", err
792
+		job.Error(err)
793
+		return engine.StatusErr
780 794
 	}
781
-	return img.ID, err
795
+	job.Printf("%s\n", img.ID)
796
+	return engine.StatusOK
782 797
 }
783 798
 
784 799
 func (srv *Server) ImageTag(job *engine.Job) engine.Status {