Merge pull request #12710 from runcom/bye-engine-from-graph

Remove engine/job from graph
This commit is contained in:
Jessie Frazelle 2015-04-23 15:42:50 -07:00
commit 2ebfc7c874
9 changed files with 81 additions and 236 deletions

View file

@ -899,10 +899,7 @@ func (s *Server) getImagesGet(eng *engine.Engine, version version.Version, w htt
}
output := utils.NewWriteFlusher(w)
imageExportConfig := &graph.ImageExportConfig{
Engine: eng,
Outstream: output,
}
imageExportConfig := &graph.ImageExportConfig{Outstream: output}
if name, ok := vars["name"]; ok {
imageExportConfig.Names = []string{name}
} else {
@ -921,14 +918,7 @@ func (s *Server) getImagesGet(eng *engine.Engine, version version.Version, w htt
}
func (s *Server) postImagesLoad(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
imageLoadConfig := &graph.ImageLoadConfig{
InTar: r.Body,
OutStream: w,
Engine: eng,
}
return s.daemon.Repositories().Load(imageLoadConfig)
return s.daemon.Repositories().Load(r.Body, w)
}
func (s *Server) postContainersCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
@ -1269,12 +1259,23 @@ func (s *Server) getImagesByName(eng *engine.Engine, version version.Version, w
if vars == nil {
return fmt.Errorf("Missing parameter")
}
var job = eng.Job("image_inspect", vars["name"])
name := vars["name"]
if version.LessThan("1.12") {
job.SetenvBool("raw", true)
imageInspectRaw, err := s.daemon.Repositories().LookupRaw(name)
if err != nil {
return err
}
return writeJSON(w, http.StatusOK, imageInspectRaw)
}
streamJSON(job.Stdout, w, false)
return job.Run()
imageInspect, err := s.daemon.Repositories().Lookup(name)
if err != nil {
return err
}
return writeJSON(w, http.StatusOK, imageInspect)
}
func (s *Server) postBuild(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View file

@ -1,104 +0,0 @@
package server
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/docker/docker/api"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/version"
)
func TestHttpError(t *testing.T) {
r := httptest.NewRecorder()
httpError(r, fmt.Errorf("No such method"))
if r.Code != http.StatusNotFound {
t.Fatalf("Expected %d, got %d", http.StatusNotFound, r.Code)
}
r = httptest.NewRecorder()
httpError(r, fmt.Errorf("This accound hasn't been activated"))
if r.Code != http.StatusForbidden {
t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
}
r = httptest.NewRecorder()
httpError(r, fmt.Errorf("Some error"))
if r.Code != http.StatusInternalServerError {
t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
}
}
func TestGetImagesByName(t *testing.T) {
eng := engine.New()
name := "image_name"
var called bool
eng.Register("image_inspect", func(job *engine.Job) error {
called = true
if job.Args[0] != name {
t.Fatalf("name != '%s': %#v", name, job.Args[0])
}
if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
t.Fatal("dirty env variable not set")
} else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
t.Fatal("dirty env variable set when it shouldn't")
}
v := &engine.Env{}
v.SetBool("dirty", true)
if _, err := v.WriteTo(job.Stdout); err != nil {
return err
}
return nil
})
r := serveRequest("GET", "/images/"+name+"/json", nil, eng, t)
if !called {
t.Fatal("handler was not called")
}
if r.HeaderMap.Get("Content-Type") != "application/json" {
t.Fatalf("%#v\n", r)
}
var stdoutJson interface{}
if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
t.Fatalf("%#v", err)
}
if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
t.Fatalf("%#v", stdoutJson)
}
}
func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
}
func serveRequestUsingVersion(method, target string, version version.Version, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
r := httptest.NewRecorder()
req, err := http.NewRequest(method, target, body)
if err != nil {
t.Fatal(err)
}
ServeRequest(eng, version, r, req)
return r
}
func readEnv(src io.Reader, t *testing.T) *engine.Env {
out := engine.NewOutput()
v, err := out.AddEnv()
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(out, src); err != nil {
t.Fatal(err)
}
out.Close()
return v
}
func assertContentType(recorder *httptest.ResponseRecorder, contentType string, t *testing.T) {
if recorder.HeaderMap.Get("Content-Type") != contentType {
t.Fatalf("%#v\n", recorder)
}
}

View file

@ -116,9 +116,6 @@ type Daemon struct {
// Install installs daemon capabilities to eng.
func (daemon *Daemon) Install(eng *engine.Engine) error {
if err := daemon.Repositories().Install(eng); err != nil {
return err
}
// FIXME: this hack is necessary for legacy integration tests to access
// the daemon object.
eng.HackSetGlobalVar("httpapi.daemon", daemon)

View file

@ -8,7 +8,6 @@ import (
"path"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/registry"
@ -22,7 +21,6 @@ import (
type ImageExportConfig struct {
Names []string
Outstream io.Writer
Engine *engine.Engine
}
func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
@ -51,7 +49,7 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
// this is a base repo name, like 'busybox'
for tag, id := range rootRepo {
addKey(name, tag, id)
if err := s.exportImage(imageExportConfig.Engine, id, tempdir); err != nil {
if err := s.exportImage(id, tempdir); err != nil {
return err
}
}
@ -70,13 +68,13 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
if len(repoTag) > 0 {
addKey(repoName, repoTag, img.ID)
}
if err := s.exportImage(imageExportConfig.Engine, img.ID, tempdir); err != nil {
if err := s.exportImage(img.ID, tempdir); err != nil {
return err
}
} else {
// this must be an ID that didn't get looked up just right?
if err := s.exportImage(imageExportConfig.Engine, name, tempdir); err != nil {
if err := s.exportImage(name, tempdir); err != nil {
return err
}
}
@ -107,7 +105,7 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
}
// FIXME: this should be a top-level function, not a class method
func (s *TagStore) exportImage(eng *engine.Engine, name, tempdir string) error {
func (s *TagStore) exportImage(name, tempdir string) error {
for n := name; n != ""; {
// temporary directory
tmpImageDir := path.Join(tempdir, n)
@ -130,12 +128,17 @@ func (s *TagStore) exportImage(eng *engine.Engine, name, tempdir string) error {
if err != nil {
return err
}
job := eng.Job("image_inspect", n)
job.SetenvBool("raw", true)
job.Stdout.Add(json)
if err := job.Run(); err != nil {
imageInspectRaw, err := s.LookupRaw(n)
if err != nil {
return err
}
written, err := json.Write(imageInspectRaw)
if err != nil {
return err
}
if written != len(imageInspectRaw) {
logrus.Warnf("%d byes should have been written instead %d have been written", written, len(imageInspectRaw))
}
// serialize filesystem
fsTar, err := os.Create(path.Join(tmpImageDir, "layer.tar"))

View file

@ -10,21 +10,14 @@ import (
"path"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/engine"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/chrootarchive"
)
type ImageLoadConfig struct {
InTar io.ReadCloser
OutStream io.Writer
Engine *engine.Engine
}
// Loads a set of images into the repository. This is the complementary of ImageExport.
// The input stream is an uncompressed tar ball containing images and metadata.
func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
func (s *TagStore) Load(inTar io.ReadCloser, outStream io.Writer) error {
tmpImageDir, err := ioutil.TempDir("", "docker-import-")
if err != nil {
return err
@ -48,7 +41,7 @@ func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
excludes[i] = k
i++
}
if err := chrootarchive.Untar(imageLoadConfig.InTar, repoDir, &archive.TarOptions{ExcludePatterns: excludes}); err != nil {
if err := chrootarchive.Untar(inTar, repoDir, &archive.TarOptions{ExcludePatterns: excludes}); err != nil {
return err
}
@ -59,7 +52,7 @@ func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
for _, d := range dirs {
if d.IsDir() {
if err := s.recursiveLoad(imageLoadConfig.Engine, d.Name(), tmpImageDir); err != nil {
if err := s.recursiveLoad(d.Name(), tmpImageDir); err != nil {
return err
}
}
@ -74,7 +67,7 @@ func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
for imageName, tagMap := range repositories {
for tag, address := range tagMap {
if err := s.SetLoad(imageName, tag, address, true, imageLoadConfig.OutStream); err != nil {
if err := s.SetLoad(imageName, tag, address, true, outStream); err != nil {
return err
}
}
@ -86,7 +79,7 @@ func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
return nil
}
func (s *TagStore) recursiveLoad(eng *engine.Engine, address, tmpImageDir string) error {
func (s *TagStore) recursiveLoad(address, tmpImageDir string) error {
if _, err := s.LookupImage(address); err != nil {
logrus.Debugf("Loading %s", address)
@ -126,7 +119,7 @@ func (s *TagStore) recursiveLoad(eng *engine.Engine, address, tmpImageDir string
if img.Parent != "" {
if !s.graph.Exists(img.Parent) {
if err := s.recursiveLoad(eng, img.Parent, tmpImageDir); err != nil {
if err := s.recursiveLoad(img.Parent, tmpImageDir); err != nil {
return err
}
}

View file

@ -4,10 +4,9 @@ package graph
import (
"fmt"
"github.com/docker/docker/engine"
"io"
)
func (s *TagStore) CmdLoad(job *engine.Job) error {
return fmt.Errorf("CmdLoad is not supported on this platform")
func (s *TagStore) Load(inTar io.ReadCloser, outStream io.Writer) error {
return fmt.Errorf("Load is not supported on this platform")
}

View file

@ -5,57 +5,47 @@ import (
"io"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/engine"
"github.com/docker/docker/api/types"
)
func (s *TagStore) Install(eng *engine.Engine) error {
for name, handler := range map[string]engine.Handler{
"image_inspect": s.CmdLookup,
"viz": s.CmdViz,
} {
if err := eng.Register(name, handler); err != nil {
return fmt.Errorf("Could not register %q: %v", name, err)
}
func (s *TagStore) LookupRaw(name string) ([]byte, error) {
image, err := s.LookupImage(name)
if err != nil || image == nil {
return nil, fmt.Errorf("No such image %s", name)
}
return nil
imageInspectRaw, err := image.RawJson()
if err != nil {
return nil, err
}
return imageInspectRaw, nil
}
// CmdLookup return an image encoded in JSON
func (s *TagStore) CmdLookup(job *engine.Job) error {
if len(job.Args) != 1 {
return fmt.Errorf("usage: %s NAME", job.Name)
// Lookup return an image encoded in JSON
func (s *TagStore) Lookup(name string) (*types.ImageInspect, error) {
image, err := s.LookupImage(name)
if err != nil || image == nil {
return nil, fmt.Errorf("No such image: %s", name)
}
name := job.Args[0]
if image, err := s.LookupImage(name); err == nil && image != nil {
if job.GetenvBool("raw") {
b, err := image.RawJson()
if err != nil {
return err
}
job.Stdout.Write(b)
return nil
}
out := &engine.Env{}
out.SetJson("Id", image.ID)
out.SetJson("Parent", image.Parent)
out.SetJson("Comment", image.Comment)
out.SetAuto("Created", image.Created)
out.SetJson("Container", image.Container)
out.SetJson("ContainerConfig", image.ContainerConfig)
out.Set("DockerVersion", image.DockerVersion)
out.SetJson("Author", image.Author)
out.SetJson("Config", image.Config)
out.Set("Architecture", image.Architecture)
out.Set("Os", image.OS)
out.SetInt64("Size", image.Size)
out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size)
if _, err = out.WriteTo(job.Stdout); err != nil {
return err
}
return nil
imageInspect := &types.ImageInspect{
Id: image.ID,
Parent: image.Parent,
Comment: image.Comment,
Created: image.Created,
Container: image.Container,
ContainerConfig: &image.ContainerConfig,
DockerVersion: image.DockerVersion,
Author: image.Author,
Config: image.Config,
Architecture: image.Architecture,
Os: image.OS,
Size: image.Size,
VirtualSize: image.GetParentsSize(0) + image.Size,
}
return fmt.Errorf("No such image: %s", name)
return imageInspect, nil
}
// ImageTarLayer return the tarLayer of the image

View file

@ -1,39 +0,0 @@
package graph
import (
"fmt"
"strings"
"github.com/docker/docker/engine"
"github.com/docker/docker/image"
)
func (s *TagStore) CmdViz(job *engine.Job) error {
images, _ := s.graph.Map()
if images == nil {
return nil
}
job.Stdout.Write([]byte("digraph docker {\n"))
var (
parentImage *image.Image
err error
)
for _, image := range images {
parentImage, err = image.GetParent()
if err != nil {
return fmt.Errorf("Error while getting parent image: %v", err)
}
if parentImage != nil {
job.Stdout.Write([]byte(" \"" + parentImage.ID + "\" -> \"" + image.ID + "\"\n"))
} else {
job.Stdout.Write([]byte(" base -> \"" + image.ID + "\" [style=invis]\n"))
}
}
for id, repos := range s.GetRepoRefs() {
job.Stdout.Write([]byte(" \"" + id + "\" [label=\"" + id + "\\n" + strings.Join(repos, "\\n") + "\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n"))
}
job.Stdout.Write([]byte(" base [style=invisible]\n}\n"))
return nil
}

View file

@ -125,17 +125,22 @@ func init() {
func setupBaseImage() {
eng := newTestEngine(std_log.New(os.Stderr, "", 0), false, unitTestStoreBase)
job := eng.Job("image_inspect", unitTestImageName)
img, _ := job.Stdout.AddEnv()
d := getDaemon(eng)
_, err := d.Repositories().Lookup(unitTestImageName)
// If the unit test is not found, try to download it.
if err := job.Run(); err != nil || img.Get("Id") != unitTestImageID {
if err != nil {
// seems like we can just ignore the error here...
// there was a check of imgId from job stdout against unittestid but
// if there was an error how could the imgid from the job
// be compared?! it's obvious it's different, am I totally wrong?
// Retrieve the Image
imagePullConfig := &graph.ImagePullConfig{
Parallel: true,
OutStream: ioutils.NopWriteCloser(os.Stdout),
AuthConfig: &cliconfig.AuthConfig{},
}
d := getDaemon(eng)
if err := d.Repositories().Pull(unitTestImageName, "", imagePullConfig); err != nil {
logrus.Fatalf("Unable to pull the test image: %s", err)
}