Merge pull request #3582 from vieux/history_job

move history to a job
This commit is contained in:
Victor Vieux 2014-01-15 11:06:54 -08:00
Родитель 5a91781ef9 7898dca8b3
Коммит ad5d675553
7 изменённых файлов: 1366 добавлений и 46 удалений

30
api.go
Просмотреть файл

@ -202,7 +202,7 @@ func getImagesJSON(srv *Server, version float64, w http.ResponseWriter, r *http.
return err return err
} }
if version < 1.9 { // Send as a valide JSON array if version < 1.9 { // Send as a valid JSON array
outs := engine.NewTable("Created", 0) outs := engine.NewTable("Created", 0)
if _, err := outs.ReadFrom(buffer); err != nil { if _, err := outs.ReadFrom(buffer); err != nil {
return err return err
@ -313,13 +313,31 @@ func getImagesHistory(srv *Server, version float64, w http.ResponseWriter, r *ht
if vars == nil { if vars == nil {
return fmt.Errorf("Missing parameter") return fmt.Errorf("Missing parameter")
} }
name := vars["name"]
outs, err := srv.ImageHistory(name) var (
if err != nil { buffer *bytes.Buffer
job = srv.Eng.Job("history", vars["name"])
)
if version >= 1.9 {
job.Stdout.Add(w)
} else {
buffer = bytes.NewBuffer(nil)
job.Stdout.Add(buffer)
}
if err := job.Run(); err != nil {
return err return err
} }
if version < 1.9 { // Send as a valid JSON array
return writeJSON(w, http.StatusOK, outs) outs := engine.NewTable("Created", 0)
if _, err := outs.ReadFrom(buffer); err != nil {
return err
}
if _, err := outs.WriteListTo(w); err != nil {
return err
}
}
return nil
} }
func getContainersChanges(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func getContainersChanges(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

Просмотреть файл

@ -1,14 +1,6 @@
package docker package docker
type ( type (
APIHistory struct {
ID string `json:"Id"`
Tags []string `json:",omitempty"`
Created int64
CreatedBy string `json:",omitempty"`
Size int64
}
APITop struct { APITop struct {
Titles []string Titles []string
Processes [][]string Processes [][]string

Просмотреть файл

@ -862,9 +862,8 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
return err return err
} }
var outs []APIHistory outs := engine.NewTable("Created", 0)
err = json.Unmarshal(body, &outs) if _, err := outs.ReadFrom(bytes.NewReader(body)); err != nil {
if err != nil {
return err return err
} }
@ -873,27 +872,28 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE") fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE")
} }
for _, out := range outs { for _, out := range outs.Data {
outID := out.Get("ID")
if !*quiet { if !*quiet {
if *noTrunc { if *noTrunc {
fmt.Fprintf(w, "%s\t", out.ID) fmt.Fprintf(w, "%s\t", outID)
} else { } else {
fmt.Fprintf(w, "%s\t", utils.TruncateID(out.ID)) fmt.Fprintf(w, "%s\t", utils.TruncateID(outID))
} }
fmt.Fprintf(w, "%s ago\t", utils.HumanDuration(time.Now().UTC().Sub(time.Unix(out.Created, 0)))) fmt.Fprintf(w, "%s ago\t", utils.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))))
if *noTrunc { if *noTrunc {
fmt.Fprintf(w, "%s\t", out.CreatedBy) fmt.Fprintf(w, "%s\t", out.Get("CreatedBy"))
} else { } else {
fmt.Fprintf(w, "%s\t", utils.Trunc(out.CreatedBy, 45)) fmt.Fprintf(w, "%s\t", utils.Trunc(out.Get("CreatedBy"), 45))
} }
fmt.Fprintf(w, "%s\n", utils.HumanSize(out.Size)) fmt.Fprintf(w, "%s\n", utils.HumanSize(out.GetInt64("Size")))
} else { } else {
if *noTrunc { if *noTrunc {
fmt.Fprintln(w, out.ID) fmt.Fprintln(w, outID)
} else { } else {
fmt.Fprintln(w, utils.TruncateID(out.ID)) fmt.Fprintln(w, utils.TruncateID(outID))
} }
} }
} }

Просмотреть файл

@ -26,15 +26,34 @@ Docker Remote API
2. Versions 2. Versions
=========== ===========
The current version of the API is 1.8 The current version of the API is 1.9
Calling /images/<name>/insert is the same as calling Calling /images/<name>/insert is the same as calling
/v1.8/images/<name>/insert /v1.9/images/<name>/insert
You can still call an old version of the api using You can still call an old version of the api using
/v1.0/images/<name>/insert /v1.0/images/<name>/insert
v1.9
****
Full Documentation
------------------
:doc:`docker_remote_api_v1.9`
What's new
----------
.. http:get:: /images/json
**New!** This endpoint now returns a list of json message, like the events endpoint
.. http:get:: /images/(name)/history
**New!** This endpoint now returns a list of json message, like the events endpoint
v1.8 v1.8
**** ****

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -263,12 +263,12 @@ func TestGetImagesHistory(t *testing.T) {
} }
assertHttpNotError(r, t) assertHttpNotError(r, t)
history := []docker.APIHistory{} outs := engine.NewTable("Created", 0)
if err := json.Unmarshal(r.Body.Bytes(), &history); err != nil { if _, err := outs.ReadFrom(r.Body); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(history) != 1 { if len(outs.Data) != 1 {
t.Errorf("Expected 1 line, %d found", len(history)) t.Errorf("Expected 1 line, %d found", len(outs.Data))
} }
} }

Просмотреть файл

@ -131,6 +131,10 @@ func jobInitApi(job *engine.Job) engine.Status {
job.Error(err) job.Error(err)
return engine.StatusErr return engine.StatusErr
} }
if err := job.Eng.Register("history", srv.ImageHistory); err != nil {
job.Error(err)
return engine.StatusErr
}
return engine.StatusOK return engine.StatusOK
} }
@ -583,7 +587,7 @@ func (srv *Server) Images(job *engine.Job) engine.Status {
allImages, err = srv.runtime.graph.Heads() allImages, err = srv.runtime.graph.Heads()
} }
if err != nil { if err != nil {
job.Errorf("%s", err) job.Error(err)
return engine.StatusErr return engine.StatusErr
} }
lookup := make(map[string]*engine.Env) lookup := make(map[string]*engine.Env)
@ -638,7 +642,7 @@ func (srv *Server) Images(job *engine.Job) engine.Status {
outs.ReverseSort() outs.ReverseSort()
if _, err := outs.WriteTo(job.Stdout); err != nil { if _, err := outs.WriteTo(job.Stdout); err != nil {
job.Errorf("%s", err) job.Error(err)
return engine.StatusErr return engine.StatusErr
} }
return engine.StatusOK return engine.StatusOK
@ -695,10 +699,16 @@ func (srv *Server) DockerInfo(job *engine.Job) engine.Status {
return engine.StatusOK return engine.StatusOK
} }
func (srv *Server) ImageHistory(name string) ([]APIHistory, error) { func (srv *Server) ImageHistory(job *engine.Job) engine.Status {
if n := len(job.Args); n != 1 {
job.Errorf("Usage: %s IMAGE", job.Name)
return engine.StatusErr
}
name := job.Args[0]
image, err := srv.runtime.repositories.LookupImage(name) image, err := srv.runtime.repositories.LookupImage(name)
if err != nil { if err != nil {
return nil, err job.Error(err)
return engine.StatusErr
} }
lookupMap := make(map[string][]string) lookupMap := make(map[string][]string)
@ -712,19 +722,23 @@ func (srv *Server) ImageHistory(name string) ([]APIHistory, error) {
} }
} }
outs := []APIHistory{} //produce [] when empty instead of 'null' outs := engine.NewTable("Created", 0)
err = image.WalkHistory(func(img *Image) error { err = image.WalkHistory(func(img *Image) error {
var out APIHistory out := &engine.Env{}
out.ID = img.ID out.Set("ID", img.ID)
out.Created = img.Created.Unix() out.SetInt64("Created", img.Created.Unix())
out.CreatedBy = strings.Join(img.ContainerConfig.Cmd, " ") out.Set("CreatedBy", strings.Join(img.ContainerConfig.Cmd, " "))
out.Tags = lookupMap[img.ID] out.SetList("Tags", lookupMap[img.ID])
out.Size = img.Size out.SetInt64("Size", img.Size)
outs = append(outs, out) outs.Add(out)
return nil return nil
}) })
return outs, nil outs.ReverseSort()
if _, err := outs.WriteTo(job.Stdout); err != nil {
job.Error(err)
return engine.StatusErr
}
return engine.StatusOK
} }
func (srv *Server) ContainerTop(name, psArgs string) (*APITop, error) { func (srv *Server) ContainerTop(name, psArgs string) (*APITop, error) {