From 930ec9f52ceceabcb72589ac7f20e1e8bbccbed5 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Wed, 11 Dec 2013 17:03:48 -0800 Subject: [PATCH] move commit to job --- api.go | 19 ++++++++++++------- engine/env.go | 22 +++++++++++++++------- engine/job.go | 4 ++++ integration/server_test.go | 22 ++++++++++++++++------ server.go | 29 ++++++++++++++++++++++++----- 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/api.go b/api.go index f7e37ef07c..4e8e3330c7 100644 --- a/api.go +++ b/api.go @@ -377,13 +377,18 @@ func postCommit(srv *Server, version float64, w http.ResponseWriter, r *http.Req if err := json.NewDecoder(r.Body).Decode(config); err != nil && err != io.EOF { utils.Errorf("%s", err) } - repo := r.Form.Get("repo") - tag := r.Form.Get("tag") - container := r.Form.Get("container") - author := r.Form.Get("author") - comment := r.Form.Get("comment") - id, err := srv.ContainerCommit(container, repo, tag, author, comment, config) - if err != nil { + + job := srv.Eng.Job("commit") + job.Setenv("repo", r.Form.Get("repo")) + job.Setenv("tag", r.Form.Get("tag")) + job.Setenv("container", r.Form.Get("container")) + job.Setenv("author", r.Form.Get("author")) + job.Setenv("comment", r.Form.Get("comment")) + job.SetenvJson("config", config) + + var id string + job.Stdout.AddString(&id) + if err := job.Run(); err != nil { return err } diff --git a/engine/env.go b/engine/env.go index 5e4f59395a..588cdeab2a 100644 --- a/engine/env.go +++ b/engine/env.go @@ -1,17 +1,16 @@ package engine import ( - "strings" - "io" - "encoding/json" - "strconv" "bytes" + "encoding/json" "fmt" + "io" + "strconv" + "strings" ) type Env []string - func (env *Env) Get(key string) (value string) { // FIXME: use Map() for _, kv := range *env { @@ -44,7 +43,6 @@ func (env *Env) GetBool(key string) (value bool) { return true } - func (env *Env) SetBool(key string, value bool) { if value { env.Set(key, "1") @@ -79,6 +77,17 @@ func (env *Env) GetList(key string) []string { return l } +func (env *Env) GetJson(key string) interface{} { + sval := env.Get(key) + if sval == "" { + return nil + } + var m interface{} + //Discard error on purpose + json.Unmarshal([]byte(sval), &m) + return m +} + func (env *Env) SetJson(key string, value interface{}) error { sval, err := json.Marshal(value) if err != nil { @@ -218,4 +227,3 @@ func (env *Env) Map() map[string]string { } return m } - diff --git a/engine/job.go b/engine/job.go index 465086af91..c7b510d47b 100644 --- a/engine/job.go +++ b/engine/job.go @@ -126,6 +126,10 @@ func (job *Job) GetenvList(key string) []string { return job.env.GetList(key) } +func (job *Job) GetenvJson(key string) interface{} { + return job.env.GetJson(key) +} + func (job *Job) SetenvJson(key string, value interface{}) error { return job.env.SetJson(key, value) } diff --git a/integration/server_test.go b/integration/server_test.go index 7fb7611192..ab9e39c2e0 100644 --- a/integration/server_test.go +++ b/integration/server_test.go @@ -146,7 +146,6 @@ func TestCreateRmVolumes(t *testing.T) { func TestCommit(t *testing.T) { eng := NewTestEngine(t) - srv := mkServerFromEngine(eng, t) defer mkRuntimeFromEngine(eng, t).Nuke() config, _, _, err := docker.ParseRun([]string{unitTestImageID, "/bin/cat"}, nil) @@ -156,7 +155,12 @@ func TestCommit(t *testing.T) { id := createTestContainer(eng, config, t) - if _, err := srv.ContainerCommit(id, "testrepo", "testtag", "", "", config); err != nil { + job := eng.Job("commit") + job.Setenv("container", id) + job.Setenv("repo", "testrepo") + job.Setenv("tag", "testtag") + job.SetenvJson("config", config) + if err := job.Run(); err != nil { t.Fatal(err) } } @@ -264,8 +268,12 @@ func TestRmi(t *testing.T) { t.Fatal(err) } - imageID, err := srv.ContainerCommit(containerID, "test", "", "", "", nil) - if err != nil { + job = eng.Job("commit") + job.Setenv("container", containerID) + job.Setenv("repo", "test") + var imageID string + job.Stdout.AddString(&imageID) + if err := job.Run(); err != nil { t.Fatal(err) } @@ -288,8 +296,10 @@ func TestRmi(t *testing.T) { t.Fatal(err) } - _, err = srv.ContainerCommit(containerID, "test", "", "", "", nil) - if err != nil { + job = eng.Job("commit") + job.Setenv("container", containerID) + job.Setenv("repo", "test") + if err := job.Run(); err != nil { t.Fatal(err) } diff --git a/server.go b/server.go index 63643cb20e..4ae34c4290 100644 --- a/server.go +++ b/server.go @@ -107,6 +107,10 @@ func jobInitApi(job *engine.Job) engine.Status { job.Error(err) return engine.StatusErr } + if err := job.Eng.Register("commit", srv.ContainerCommit); err != nil { + job.Error(err) + return engine.StatusErr + } return engine.StatusOK } @@ -769,16 +773,31 @@ func createAPIContainer(names []string, container *Container, size bool, runtime } return c } -func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) { +func (srv *Server) ContainerCommit(job *engine.Job) engine.Status { + if len(job.Args) != 0 { + job.Errorf("Usage: %s\n", job.Name) + return engine.StatusErr + } + name := job.Getenv("container") + container := srv.runtime.Get(name) if container == nil { - return "", fmt.Errorf("No such container: %s", name) + job.Errorf("No such container: %s", name) + return engine.StatusErr } - img, err := srv.runtime.Commit(container, repo, tag, comment, author, config) + var config *Config + iConfig, ok := job.GetenvJson("config").(Config) + if ok { + config = &iConfig + } + + img, err := srv.runtime.Commit(container, job.Getenv("repo"), job.Getenv("tag"), job.Getenv("comment"), job.Getenv("author"), config) if err != nil { - return "", err + job.Error(err) + return engine.StatusErr } - return img.ID, err + job.Printf("%s\n", img.ID) + return engine.StatusOK } func (srv *Server) ImageTag(job *engine.Job) engine.Status {