mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
merge 2 jobs, no more buffer
Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
This commit is contained in:
parent
a7e9baaf89
commit
5fd8aa02ba
2 changed files with 50 additions and 70 deletions
43
api.go
43
api.go
|
@ -751,8 +751,9 @@ func postContainersAttach(srv *Server, version float64, w http.ResponseWriter, r
|
|||
return fmt.Errorf("Missing parameter")
|
||||
}
|
||||
|
||||
// TODO: replace the buffer by job.AddEnv()
|
||||
var (
|
||||
job = srv.Eng.Job("inspect_container", vars["name"])
|
||||
job = srv.Eng.Job("inspect", vars["name"], "container")
|
||||
buffer = bytes.NewBuffer(nil)
|
||||
c Container
|
||||
)
|
||||
|
@ -819,7 +820,7 @@ func wsContainersAttach(srv *Server, version float64, w http.ResponseWriter, r *
|
|||
return fmt.Errorf("Missing parameter")
|
||||
}
|
||||
|
||||
if err := srv.Eng.Job("inspect_container", vars["name"]).Run(); err != nil {
|
||||
if err := srv.Eng.Job("inspect", vars["name"], "container").Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -847,42 +848,20 @@ func getContainersByName(srv *Server, version float64, w http.ResponseWriter, r
|
|||
if vars == nil {
|
||||
return fmt.Errorf("Missing parameter")
|
||||
}
|
||||
var (
|
||||
buffer = bytes.NewBuffer(nil)
|
||||
job = srv.Eng.Job("inspect_container", vars["name"])
|
||||
)
|
||||
|
||||
job.Stdout.Add(buffer)
|
||||
if err := job.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := srv.Eng.Job("inspect_image", vars["name"]).Run(); err == nil {
|
||||
return fmt.Errorf("Conflict between containers and images")
|
||||
}
|
||||
_, err := io.Copy(w, buffer)
|
||||
return err
|
||||
var job = srv.Eng.Job("inspect", vars["name"], "container")
|
||||
job.Stdout.Add(w)
|
||||
job.SetenvBool("conflict", true) //conflict=true to detect conflict between containers and images in the job
|
||||
return job.Run()
|
||||
}
|
||||
|
||||
func getImagesByName(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
||||
if vars == nil {
|
||||
return fmt.Errorf("Missing parameter")
|
||||
}
|
||||
var (
|
||||
buffer = bytes.NewBuffer(nil)
|
||||
job = srv.Eng.Job("inspect_image", vars["name"])
|
||||
)
|
||||
|
||||
job.Stdout.Add(buffer)
|
||||
if err := job.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := srv.Eng.Job("inspect_container", vars["name"]).Run(); err == nil {
|
||||
return fmt.Errorf("Conflict between containers and images")
|
||||
}
|
||||
_, err := io.Copy(w, buffer)
|
||||
return err
|
||||
var job = srv.Eng.Job("inspect", vars["name"], "image")
|
||||
job.Stdout.Add(w)
|
||||
job.SetenvBool("conflict", true) //conflict=true to detect conflict between containers and images in the job
|
||||
return job.Run()
|
||||
}
|
||||
|
||||
func postBuild(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
||||
|
|
77
server.go
77
server.go
|
@ -100,6 +100,7 @@ func jobInitApi(job *engine.Job) engine.Status {
|
|||
"pull": srv.ImagePull,
|
||||
"import": srv.ImageImport,
|
||||
"image_delete": srv.ImageDelete,
|
||||
"inspect": srv.JobInspect,
|
||||
} {
|
||||
if err := job.Eng.Register(name, handler); err != nil {
|
||||
job.Error(err)
|
||||
|
@ -337,14 +338,6 @@ func (srv *Server) ImageExport(job *engine.Job) engine.Status {
|
|||
job.Error(err)
|
||||
return engine.StatusErr
|
||||
}
|
||||
if err := job.Eng.Register("inspect_image", srv.JobImageInspect); err != nil {
|
||||
job.Error(err)
|
||||
return engine.StatusErr
|
||||
}
|
||||
if err := job.Eng.Register("inspect_container", srv.JobContainerInspect); err != nil {
|
||||
job.Error(err)
|
||||
return engine.StatusErr
|
||||
}
|
||||
return engine.StatusOK
|
||||
}
|
||||
|
||||
|
@ -2311,29 +2304,6 @@ func (srv *Server) ContainerInspect(name string) (*Container, error) {
|
|||
return nil, fmt.Errorf("No such container: %s", name)
|
||||
}
|
||||
|
||||
func (srv *Server) JobContainerInspect(job *engine.Job) engine.Status {
|
||||
if n := len(job.Args); n != 1 {
|
||||
job.Errorf("Usage: %s CONTAINER", job.Name)
|
||||
return engine.StatusErr
|
||||
}
|
||||
container, err := srv.ContainerInspect(job.Args[0])
|
||||
if err != nil {
|
||||
job.Error(err)
|
||||
return engine.StatusErr
|
||||
}
|
||||
|
||||
b, err := json.Marshal(&struct {
|
||||
*Container
|
||||
HostConfig *HostConfig
|
||||
}{container, container.hostConfig})
|
||||
if err != nil {
|
||||
job.Error(err)
|
||||
return engine.StatusErr
|
||||
}
|
||||
job.Stdout.Write(b)
|
||||
return engine.StatusOK
|
||||
}
|
||||
|
||||
func (srv *Server) ImageInspect(name string) (*Image, error) {
|
||||
if image, err := srv.runtime.repositories.LookupImage(name); err == nil && image != nil {
|
||||
return image, nil
|
||||
|
@ -2341,17 +2311,48 @@ func (srv *Server) ImageInspect(name string) (*Image, error) {
|
|||
return nil, fmt.Errorf("No such image: %s", name)
|
||||
}
|
||||
|
||||
func (srv *Server) JobImageInspect(job *engine.Job) engine.Status {
|
||||
if n := len(job.Args); n != 1 {
|
||||
job.Errorf("Usage: %s IMAGE", job.Name)
|
||||
func (srv *Server) JobInspect(job *engine.Job) engine.Status {
|
||||
// TODO: deprecate KIND/conflict
|
||||
if n := len(job.Args); n != 2 {
|
||||
job.Errorf("Usage: %s CONTAINER|IMAGE KIND", job.Name)
|
||||
return engine.StatusErr
|
||||
}
|
||||
image, err := srv.ImageInspect(job.Args[0])
|
||||
if err != nil {
|
||||
job.Error(err)
|
||||
var (
|
||||
name = job.Args[0]
|
||||
kind = job.Args[1]
|
||||
object interface{}
|
||||
conflict = job.GetenvBool("conflict") //should the job detect conflict between containers and images
|
||||
image, errImage = srv.ImageInspect(name)
|
||||
container, errContainer = srv.ContainerInspect(name)
|
||||
)
|
||||
|
||||
if conflict && image != nil && container != nil {
|
||||
job.Errorf("Conflict between containers and images")
|
||||
return engine.StatusErr
|
||||
}
|
||||
b, err := json.Marshal(image)
|
||||
|
||||
switch kind {
|
||||
case "image":
|
||||
if errImage != nil {
|
||||
job.Error(errImage)
|
||||
return engine.StatusErr
|
||||
}
|
||||
object = image
|
||||
case "container":
|
||||
if errContainer != nil {
|
||||
job.Error(errContainer)
|
||||
return engine.StatusErr
|
||||
}
|
||||
object = &struct {
|
||||
*Container
|
||||
HostConfig *HostConfig
|
||||
}{container, container.hostConfig}
|
||||
default:
|
||||
job.Errorf("Unknown kind: %s", kind)
|
||||
return engine.StatusErr
|
||||
}
|
||||
|
||||
b, err := json.Marshal(object)
|
||||
if err != nil {
|
||||
job.Error(err)
|
||||
return engine.StatusErr
|
||||
|
|
Loading…
Reference in a new issue