Merge pull request #3183 from vieux/job_commit

Move commit to job
This commit is contained in:
Guillaume J. Charmes 2013-12-13 16:11:58 -08:00
commit 20899cdb34
5 changed files with 64 additions and 25 deletions

18
api.go
View File

@ -377,13 +377,17 @@ 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", r.Form.Get("container"))
job.Setenv("repo", r.Form.Get("repo"))
job.Setenv("tag", r.Form.Get("tag"))
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
}

View File

@ -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,14 @@ func (env *Env) GetList(key string) []string {
return l
}
func (env *Env) GetJson(key string, iface interface{}) error {
sval := env.Get(key)
if sval == "" {
return nil
}
return json.Unmarshal([]byte(sval), iface)
}
func (env *Env) SetJson(key string, value interface{}) error {
sval, err := json.Marshal(value)
if err != nil {
@ -218,4 +224,3 @@ func (env *Env) Map() map[string]string {
}
return m
}

View File

@ -126,6 +126,10 @@ func (job *Job) GetenvList(key string) []string {
return job.env.GetList(key)
}
func (job *Job) GetenvJson(key string, iface interface{}) error {
return job.env.GetJson(key, iface)
}
func (job *Job) SetenvJson(key string, value interface{}) error {
return job.env.SetJson(key, value)
}

View File

@ -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,11 @@ func TestCommit(t *testing.T) {
id := createTestContainer(eng, config, t)
if _, err := srv.ContainerCommit(id, "testrepo", "testtag", "", "", config); err != nil {
job := eng.Job("commit", id)
job.Setenv("repo", "testrepo")
job.Setenv("tag", "testtag")
job.SetenvJson("config", config)
if err := job.Run(); err != nil {
t.Fatal(err)
}
}
@ -264,8 +267,11 @@ func TestRmi(t *testing.T) {
t.Fatal(err)
}
imageID, err := srv.ContainerCommit(containerID, "test", "", "", "", nil)
if err != nil {
job = eng.Job("commit", containerID)
job.Setenv("repo", "test")
var imageID string
job.Stdout.AddString(&imageID)
if err := job.Run(); err != nil {
t.Fatal(err)
}
@ -288,8 +294,9 @@ func TestRmi(t *testing.T) {
t.Fatal(err)
}
_, err = srv.ContainerCommit(containerID, "test", "", "", "", nil)
if err != nil {
job = eng.Job("commit", containerID)
job.Setenv("repo", "test")
if err := job.Run(); err != nil {
t.Fatal(err)
}

View File

@ -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) != 1 {
job.Errorf("Not enough arguments. Usage: %s CONTAINER\n", job.Name)
return engine.StatusErr
}
name := job.Args[0]
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
if err := job.GetenvJson("config", &config); err != nil {
job.Error(err)
return engine.StatusErr
}
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 {