move history to a job

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
This commit is contained in:
Victor Vieux 2014-01-13 16:50:10 -08:00
parent bdcd1cfba5
commit bea6dd3888
5 changed files with 1360 additions and 32 deletions

30
api.go
View file

@ -202,7 +202,7 @@ func getImagesJSON(srv *Server, version float64, w http.ResponseWriter, r *http.
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)
if _, err := outs.ReadFrom(buffer); err != nil {
return err
@ -313,13 +313,31 @@ func getImagesHistory(srv *Server, version float64, w http.ResponseWriter, r *ht
if vars == nil {
return fmt.Errorf("Missing parameter")
}
name := vars["name"]
outs, err := srv.ImageHistory(name)
if err != nil {
var (
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 writeJSON(w, http.StatusOK, outs)
if version < 1.9 { // Send as a valid JSON array
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 {

View file

@ -862,9 +862,8 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
return err
}
var outs []APIHistory
err = json.Unmarshal(body, &outs)
if err != nil {
outs := engine.NewTable("Created", 0)
if _, err := outs.ReadFrom(bytes.NewReader(body)); err != nil {
return err
}
@ -873,27 +872,28 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE")
}
for _, out := range outs {
for _, out := range outs.Data {
outID := out.Get("ID")
if !*quiet {
if *noTrunc {
fmt.Fprintf(w, "%s\t", out.ID)
fmt.Fprintf(w, "%s\t", outID)
} 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 {
fmt.Fprintf(w, "%s\t", out.CreatedBy)
fmt.Fprintf(w, "%s\t", out.Get("CreatedBy"))
} 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 {
if *noTrunc {
fmt.Fprintln(w, out.ID)
fmt.Fprintln(w, outID)
} else {
fmt.Fprintln(w, utils.TruncateID(out.ID))
fmt.Fprintln(w, utils.TruncateID(outID))
}
}
}

View file

@ -26,15 +26,34 @@ Docker Remote API
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
/v1.8/images/<name>/insert
/v1.9/images/<name>/insert
You can still call an old version of the api using
/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
****

File diff suppressed because it is too large Load diff

View file

@ -131,6 +131,10 @@ func jobInitApi(job *engine.Job) engine.Status {
job.Error(err)
return engine.StatusErr
}
if err := job.Eng.Register("history", srv.ImageHistory); err != nil {
job.Error(err)
return engine.StatusErr
}
return engine.StatusOK
}
@ -695,10 +699,16 @@ func (srv *Server) DockerInfo(job *engine.Job) engine.Status {
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)
if err != nil {
return nil, err
job.Error(err)
return engine.StatusErr
}
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 {
var out APIHistory
out.ID = img.ID
out.Created = img.Created.Unix()
out.CreatedBy = strings.Join(img.ContainerConfig.Cmd, " ")
out.Tags = lookupMap[img.ID]
out.Size = img.Size
outs = append(outs, out)
out := &engine.Env{}
out.Set("ID", img.ID)
out.SetInt64("Created", img.Created.Unix())
out.Set("CreatedBy", strings.Join(img.ContainerConfig.Cmd, " "))
out.SetList("Tags", lookupMap[img.ID])
out.SetInt64("Size", img.Size)
outs.Add(out)
return nil
})
return outs, nil
outs.ReverseSort()
if _, err := outs.WriteTo(job.Stdout); err != nil {
job.Errorf("%s", err)
return engine.StatusErr
}
return engine.StatusOK
}
func (srv *Server) ContainerTop(name, psArgs string) (*APITop, error) {