Browse code

Remove engine/job from graph

Signed-off-by: Antonio Murdaca <me@runcom.ninja>

Antonio Murdaca authored on 2015/04/24 04:05:21
Showing 9 changed files
... ...
@@ -899,10 +899,7 @@ func (s *Server) getImagesGet(eng *engine.Engine, version version.Version, w htt
899 899
 	}
900 900
 
901 901
 	output := utils.NewWriteFlusher(w)
902
-	imageExportConfig := &graph.ImageExportConfig{
903
-		Engine:    eng,
904
-		Outstream: output,
905
-	}
902
+	imageExportConfig := &graph.ImageExportConfig{Outstream: output}
906 903
 	if name, ok := vars["name"]; ok {
907 904
 		imageExportConfig.Names = []string{name}
908 905
 	} else {
... ...
@@ -921,14 +918,7 @@ func (s *Server) getImagesGet(eng *engine.Engine, version version.Version, w htt
921 921
 }
922 922
 
923 923
 func (s *Server) postImagesLoad(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
924
-
925
-	imageLoadConfig := &graph.ImageLoadConfig{
926
-		InTar:     r.Body,
927
-		OutStream: w,
928
-		Engine:    eng,
929
-	}
930
-
931
-	return s.daemon.Repositories().Load(imageLoadConfig)
924
+	return s.daemon.Repositories().Load(r.Body, w)
932 925
 }
933 926
 
934 927
 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
1269 1269
 	if vars == nil {
1270 1270
 		return fmt.Errorf("Missing parameter")
1271 1271
 	}
1272
-	var job = eng.Job("image_inspect", vars["name"])
1272
+
1273
+	name := vars["name"]
1273 1274
 	if version.LessThan("1.12") {
1274
-		job.SetenvBool("raw", true)
1275
+		imageInspectRaw, err := s.daemon.Repositories().LookupRaw(name)
1276
+		if err != nil {
1277
+			return err
1278
+		}
1279
+
1280
+		return writeJSON(w, http.StatusOK, imageInspectRaw)
1275 1281
 	}
1276
-	streamJSON(job.Stdout, w, false)
1277
-	return job.Run()
1282
+
1283
+	imageInspect, err := s.daemon.Repositories().Lookup(name)
1284
+	if err != nil {
1285
+		return err
1286
+	}
1287
+
1288
+	return writeJSON(w, http.StatusOK, imageInspect)
1278 1289
 }
1279 1290
 
1280 1291
 func (s *Server) postBuild(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
1281 1292
deleted file mode 100644
... ...
@@ -1,104 +0,0 @@
1
-package server
2
-
3
-import (
4
-	"encoding/json"
5
-	"fmt"
6
-	"io"
7
-	"net/http"
8
-	"net/http/httptest"
9
-	"testing"
10
-
11
-	"github.com/docker/docker/api"
12
-	"github.com/docker/docker/engine"
13
-	"github.com/docker/docker/pkg/version"
14
-)
15
-
16
-func TestHttpError(t *testing.T) {
17
-	r := httptest.NewRecorder()
18
-	httpError(r, fmt.Errorf("No such method"))
19
-	if r.Code != http.StatusNotFound {
20
-		t.Fatalf("Expected %d, got %d", http.StatusNotFound, r.Code)
21
-	}
22
-
23
-	r = httptest.NewRecorder()
24
-	httpError(r, fmt.Errorf("This accound hasn't been activated"))
25
-	if r.Code != http.StatusForbidden {
26
-		t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
27
-	}
28
-
29
-	r = httptest.NewRecorder()
30
-	httpError(r, fmt.Errorf("Some error"))
31
-	if r.Code != http.StatusInternalServerError {
32
-		t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
33
-	}
34
-}
35
-
36
-func TestGetImagesByName(t *testing.T) {
37
-	eng := engine.New()
38
-	name := "image_name"
39
-	var called bool
40
-	eng.Register("image_inspect", func(job *engine.Job) error {
41
-		called = true
42
-		if job.Args[0] != name {
43
-			t.Fatalf("name != '%s': %#v", name, job.Args[0])
44
-		}
45
-		if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
46
-			t.Fatal("dirty env variable not set")
47
-		} else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
48
-			t.Fatal("dirty env variable set when it shouldn't")
49
-		}
50
-		v := &engine.Env{}
51
-		v.SetBool("dirty", true)
52
-		if _, err := v.WriteTo(job.Stdout); err != nil {
53
-			return err
54
-		}
55
-		return nil
56
-	})
57
-	r := serveRequest("GET", "/images/"+name+"/json", nil, eng, t)
58
-	if !called {
59
-		t.Fatal("handler was not called")
60
-	}
61
-	if r.HeaderMap.Get("Content-Type") != "application/json" {
62
-		t.Fatalf("%#v\n", r)
63
-	}
64
-	var stdoutJson interface{}
65
-	if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
66
-		t.Fatalf("%#v", err)
67
-	}
68
-	if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
69
-		t.Fatalf("%#v", stdoutJson)
70
-	}
71
-}
72
-
73
-func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
74
-	return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
75
-}
76
-
77
-func serveRequestUsingVersion(method, target string, version version.Version, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
78
-	r := httptest.NewRecorder()
79
-	req, err := http.NewRequest(method, target, body)
80
-	if err != nil {
81
-		t.Fatal(err)
82
-	}
83
-	ServeRequest(eng, version, r, req)
84
-	return r
85
-}
86
-
87
-func readEnv(src io.Reader, t *testing.T) *engine.Env {
88
-	out := engine.NewOutput()
89
-	v, err := out.AddEnv()
90
-	if err != nil {
91
-		t.Fatal(err)
92
-	}
93
-	if _, err := io.Copy(out, src); err != nil {
94
-		t.Fatal(err)
95
-	}
96
-	out.Close()
97
-	return v
98
-}
99
-
100
-func assertContentType(recorder *httptest.ResponseRecorder, contentType string, t *testing.T) {
101
-	if recorder.HeaderMap.Get("Content-Type") != contentType {
102
-		t.Fatalf("%#v\n", recorder)
103
-	}
104
-}
... ...
@@ -116,9 +116,6 @@ type Daemon struct {
116 116
 
117 117
 // Install installs daemon capabilities to eng.
118 118
 func (daemon *Daemon) Install(eng *engine.Engine) error {
119
-	if err := daemon.Repositories().Install(eng); err != nil {
120
-		return err
121
-	}
122 119
 	// FIXME: this hack is necessary for legacy integration tests to access
123 120
 	// the daemon object.
124 121
 	eng.HackSetGlobalVar("httpapi.daemon", daemon)
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"path"
9 9
 
10 10
 	"github.com/Sirupsen/logrus"
11
-	"github.com/docker/docker/engine"
12 11
 	"github.com/docker/docker/pkg/archive"
13 12
 	"github.com/docker/docker/pkg/parsers"
14 13
 	"github.com/docker/docker/registry"
... ...
@@ -22,7 +21,6 @@ import (
22 22
 type ImageExportConfig struct {
23 23
 	Names     []string
24 24
 	Outstream io.Writer
25
-	Engine    *engine.Engine
26 25
 }
27 26
 
28 27
 func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
... ...
@@ -51,7 +49,7 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
51 51
 			// this is a base repo name, like 'busybox'
52 52
 			for tag, id := range rootRepo {
53 53
 				addKey(name, tag, id)
54
-				if err := s.exportImage(imageExportConfig.Engine, id, tempdir); err != nil {
54
+				if err := s.exportImage(id, tempdir); err != nil {
55 55
 					return err
56 56
 				}
57 57
 			}
... ...
@@ -70,13 +68,13 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
70 70
 				if len(repoTag) > 0 {
71 71
 					addKey(repoName, repoTag, img.ID)
72 72
 				}
73
-				if err := s.exportImage(imageExportConfig.Engine, img.ID, tempdir); err != nil {
73
+				if err := s.exportImage(img.ID, tempdir); err != nil {
74 74
 					return err
75 75
 				}
76 76
 
77 77
 			} else {
78 78
 				// this must be an ID that didn't get looked up just right?
79
-				if err := s.exportImage(imageExportConfig.Engine, name, tempdir); err != nil {
79
+				if err := s.exportImage(name, tempdir); err != nil {
80 80
 					return err
81 81
 				}
82 82
 			}
... ...
@@ -107,7 +105,7 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
107 107
 }
108 108
 
109 109
 // FIXME: this should be a top-level function, not a class method
110
-func (s *TagStore) exportImage(eng *engine.Engine, name, tempdir string) error {
110
+func (s *TagStore) exportImage(name, tempdir string) error {
111 111
 	for n := name; n != ""; {
112 112
 		// temporary directory
113 113
 		tmpImageDir := path.Join(tempdir, n)
... ...
@@ -130,12 +128,17 @@ func (s *TagStore) exportImage(eng *engine.Engine, name, tempdir string) error {
130 130
 		if err != nil {
131 131
 			return err
132 132
 		}
133
-		job := eng.Job("image_inspect", n)
134
-		job.SetenvBool("raw", true)
135
-		job.Stdout.Add(json)
136
-		if err := job.Run(); err != nil {
133
+		imageInspectRaw, err := s.LookupRaw(n)
134
+		if err != nil {
137 135
 			return err
138 136
 		}
137
+		written, err := json.Write(imageInspectRaw)
138
+		if err != nil {
139
+			return err
140
+		}
141
+		if written != len(imageInspectRaw) {
142
+			logrus.Warnf("%d byes should have been written instead %d have been written", written, len(imageInspectRaw))
143
+		}
139 144
 
140 145
 		// serialize filesystem
141 146
 		fsTar, err := os.Create(path.Join(tmpImageDir, "layer.tar"))
... ...
@@ -10,21 +10,14 @@ import (
10 10
 	"path"
11 11
 
12 12
 	"github.com/Sirupsen/logrus"
13
-	"github.com/docker/docker/engine"
14 13
 	"github.com/docker/docker/image"
15 14
 	"github.com/docker/docker/pkg/archive"
16 15
 	"github.com/docker/docker/pkg/chrootarchive"
17 16
 )
18 17
 
19
-type ImageLoadConfig struct {
20
-	InTar     io.ReadCloser
21
-	OutStream io.Writer
22
-	Engine    *engine.Engine
23
-}
24
-
25 18
 // Loads a set of images into the repository. This is the complementary of ImageExport.
26 19
 // The input stream is an uncompressed tar ball containing images and metadata.
27
-func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
20
+func (s *TagStore) Load(inTar io.ReadCloser, outStream io.Writer) error {
28 21
 	tmpImageDir, err := ioutil.TempDir("", "docker-import-")
29 22
 	if err != nil {
30 23
 		return err
... ...
@@ -48,7 +41,7 @@ func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
48 48
 		excludes[i] = k
49 49
 		i++
50 50
 	}
51
-	if err := chrootarchive.Untar(imageLoadConfig.InTar, repoDir, &archive.TarOptions{ExcludePatterns: excludes}); err != nil {
51
+	if err := chrootarchive.Untar(inTar, repoDir, &archive.TarOptions{ExcludePatterns: excludes}); err != nil {
52 52
 		return err
53 53
 	}
54 54
 
... ...
@@ -59,7 +52,7 @@ func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
59 59
 
60 60
 	for _, d := range dirs {
61 61
 		if d.IsDir() {
62
-			if err := s.recursiveLoad(imageLoadConfig.Engine, d.Name(), tmpImageDir); err != nil {
62
+			if err := s.recursiveLoad(d.Name(), tmpImageDir); err != nil {
63 63
 				return err
64 64
 			}
65 65
 		}
... ...
@@ -74,7 +67,7 @@ func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
74 74
 
75 75
 		for imageName, tagMap := range repositories {
76 76
 			for tag, address := range tagMap {
77
-				if err := s.SetLoad(imageName, tag, address, true, imageLoadConfig.OutStream); err != nil {
77
+				if err := s.SetLoad(imageName, tag, address, true, outStream); err != nil {
78 78
 					return err
79 79
 				}
80 80
 			}
... ...
@@ -86,7 +79,7 @@ func (s *TagStore) Load(imageLoadConfig *ImageLoadConfig) error {
86 86
 	return nil
87 87
 }
88 88
 
89
-func (s *TagStore) recursiveLoad(eng *engine.Engine, address, tmpImageDir string) error {
89
+func (s *TagStore) recursiveLoad(address, tmpImageDir string) error {
90 90
 	if _, err := s.LookupImage(address); err != nil {
91 91
 		logrus.Debugf("Loading %s", address)
92 92
 
... ...
@@ -126,7 +119,7 @@ func (s *TagStore) recursiveLoad(eng *engine.Engine, address, tmpImageDir string
126 126
 
127 127
 		if img.Parent != "" {
128 128
 			if !s.graph.Exists(img.Parent) {
129
-				if err := s.recursiveLoad(eng, img.Parent, tmpImageDir); err != nil {
129
+				if err := s.recursiveLoad(img.Parent, tmpImageDir); err != nil {
130 130
 					return err
131 131
 				}
132 132
 			}
... ...
@@ -4,10 +4,9 @@ package graph
4 4
 
5 5
 import (
6 6
 	"fmt"
7
-
8
-	"github.com/docker/docker/engine"
7
+	"io"
9 8
 )
10 9
 
11
-func (s *TagStore) CmdLoad(job *engine.Job) error {
12
-	return fmt.Errorf("CmdLoad is not supported on this platform")
10
+func (s *TagStore) Load(inTar io.ReadCloser, outStream io.Writer) error {
11
+	return fmt.Errorf("Load is not supported on this platform")
13 12
 }
... ...
@@ -5,57 +5,47 @@ import (
5 5
 	"io"
6 6
 
7 7
 	"github.com/Sirupsen/logrus"
8
-	"github.com/docker/docker/engine"
8
+	"github.com/docker/docker/api/types"
9 9
 )
10 10
 
11
-func (s *TagStore) Install(eng *engine.Engine) error {
12
-	for name, handler := range map[string]engine.Handler{
13
-		"image_inspect": s.CmdLookup,
14
-		"viz":           s.CmdViz,
15
-	} {
16
-		if err := eng.Register(name, handler); err != nil {
17
-			return fmt.Errorf("Could not register %q: %v", name, err)
18
-		}
11
+func (s *TagStore) LookupRaw(name string) ([]byte, error) {
12
+	image, err := s.LookupImage(name)
13
+	if err != nil || image == nil {
14
+		return nil, fmt.Errorf("No such image %s", name)
15
+	}
16
+
17
+	imageInspectRaw, err := image.RawJson()
18
+	if err != nil {
19
+		return nil, err
19 20
 	}
20
-	return nil
21
+
22
+	return imageInspectRaw, nil
21 23
 }
22 24
 
23
-// CmdLookup return an image encoded in JSON
24
-func (s *TagStore) CmdLookup(job *engine.Job) error {
25
-	if len(job.Args) != 1 {
26
-		return fmt.Errorf("usage: %s NAME", job.Name)
25
+// Lookup return an image encoded in JSON
26
+func (s *TagStore) Lookup(name string) (*types.ImageInspect, error) {
27
+	image, err := s.LookupImage(name)
28
+	if err != nil || image == nil {
29
+		return nil, fmt.Errorf("No such image: %s", name)
27 30
 	}
28
-	name := job.Args[0]
29
-	if image, err := s.LookupImage(name); err == nil && image != nil {
30
-		if job.GetenvBool("raw") {
31
-			b, err := image.RawJson()
32
-			if err != nil {
33
-				return err
34
-			}
35
-			job.Stdout.Write(b)
36
-			return nil
37
-		}
38 31
 
39
-		out := &engine.Env{}
40
-		out.SetJson("Id", image.ID)
41
-		out.SetJson("Parent", image.Parent)
42
-		out.SetJson("Comment", image.Comment)
43
-		out.SetAuto("Created", image.Created)
44
-		out.SetJson("Container", image.Container)
45
-		out.SetJson("ContainerConfig", image.ContainerConfig)
46
-		out.Set("DockerVersion", image.DockerVersion)
47
-		out.SetJson("Author", image.Author)
48
-		out.SetJson("Config", image.Config)
49
-		out.Set("Architecture", image.Architecture)
50
-		out.Set("Os", image.OS)
51
-		out.SetInt64("Size", image.Size)
52
-		out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size)
53
-		if _, err = out.WriteTo(job.Stdout); err != nil {
54
-			return err
55
-		}
56
-		return nil
32
+	imageInspect := &types.ImageInspect{
33
+		Id:              image.ID,
34
+		Parent:          image.Parent,
35
+		Comment:         image.Comment,
36
+		Created:         image.Created,
37
+		Container:       image.Container,
38
+		ContainerConfig: &image.ContainerConfig,
39
+		DockerVersion:   image.DockerVersion,
40
+		Author:          image.Author,
41
+		Config:          image.Config,
42
+		Architecture:    image.Architecture,
43
+		Os:              image.OS,
44
+		Size:            image.Size,
45
+		VirtualSize:     image.GetParentsSize(0) + image.Size,
57 46
 	}
58
-	return fmt.Errorf("No such image: %s", name)
47
+
48
+	return imageInspect, nil
59 49
 }
60 50
 
61 51
 // ImageTarLayer return the tarLayer of the image
62 52
deleted file mode 100644
... ...
@@ -1,39 +0,0 @@
1
-package graph
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-
7
-	"github.com/docker/docker/engine"
8
-	"github.com/docker/docker/image"
9
-)
10
-
11
-func (s *TagStore) CmdViz(job *engine.Job) error {
12
-	images, _ := s.graph.Map()
13
-	if images == nil {
14
-		return nil
15
-	}
16
-	job.Stdout.Write([]byte("digraph docker {\n"))
17
-
18
-	var (
19
-		parentImage *image.Image
20
-		err         error
21
-	)
22
-	for _, image := range images {
23
-		parentImage, err = image.GetParent()
24
-		if err != nil {
25
-			return fmt.Errorf("Error while getting parent image: %v", err)
26
-		}
27
-		if parentImage != nil {
28
-			job.Stdout.Write([]byte(" \"" + parentImage.ID + "\" -> \"" + image.ID + "\"\n"))
29
-		} else {
30
-			job.Stdout.Write([]byte(" base -> \"" + image.ID + "\" [style=invis]\n"))
31
-		}
32
-	}
33
-
34
-	for id, repos := range s.GetRepoRefs() {
35
-		job.Stdout.Write([]byte(" \"" + id + "\" [label=\"" + id + "\\n" + strings.Join(repos, "\\n") + "\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n"))
36
-	}
37
-	job.Stdout.Write([]byte(" base [style=invisible]\n}\n"))
38
-	return nil
39
-}
... ...
@@ -125,17 +125,22 @@ func init() {
125 125
 
126 126
 func setupBaseImage() {
127 127
 	eng := newTestEngine(std_log.New(os.Stderr, "", 0), false, unitTestStoreBase)
128
-	job := eng.Job("image_inspect", unitTestImageName)
129
-	img, _ := job.Stdout.AddEnv()
128
+	d := getDaemon(eng)
129
+
130
+	_, err := d.Repositories().Lookup(unitTestImageName)
130 131
 	// If the unit test is not found, try to download it.
131
-	if err := job.Run(); err != nil || img.Get("Id") != unitTestImageID {
132
+	if err != nil {
133
+		// seems like we can just ignore the error here...
134
+		// there was a check of imgId from job stdout against unittestid but
135
+		// if there was an error how could the imgid from the job
136
+		// be compared?! it's obvious it's different, am I totally wrong?
137
+
132 138
 		// Retrieve the Image
133 139
 		imagePullConfig := &graph.ImagePullConfig{
134 140
 			Parallel:   true,
135 141
 			OutStream:  ioutils.NopWriteCloser(os.Stdout),
136 142
 			AuthConfig: &cliconfig.AuthConfig{},
137 143
 		}
138
-		d := getDaemon(eng)
139 144
 		if err := d.Repositories().Pull(unitTestImageName, "", imagePullConfig); err != nil {
140 145
 			logrus.Fatalf("Unable to pull the test image: %s", err)
141 146
 		}