Browse code

Add import/pull events to the stream

Closes #8160

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis authored on 2014/10/22 22:48:02
Showing 3 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"net/http"
5 5
 	"net/url"
6 6
 
7
+	log "github.com/Sirupsen/logrus"
7 8
 	"github.com/docker/docker/engine"
8 9
 	"github.com/docker/docker/pkg/archive"
9 10
 	"github.com/docker/docker/utils"
... ...
@@ -57,5 +58,12 @@ func (s *TagStore) CmdImport(job *engine.Job) engine.Status {
57 57
 		}
58 58
 	}
59 59
 	job.Stdout.Write(sf.FormatStatus("", img.ID))
60
+	logID := img.ID
61
+	if tag != "" {
62
+		logID += ":" + tag
63
+	}
64
+	if err = job.Eng.Job("log", "import", logID, "").Run(); err != nil {
65
+		log.Errorf("Error logging event 'import' for %s: %s", logID, err)
66
+	}
60 67
 	return engine.StatusOK
61 68
 }
... ...
@@ -139,6 +139,11 @@ func (s *TagStore) CmdPull(job *engine.Job) engine.Status {
139 139
 		mirrors = s.mirrors
140 140
 	}
141 141
 
142
+	logName := localName
143
+	if tag != "" {
144
+		logName += ":" + tag
145
+	}
146
+
142 147
 	if len(mirrors) == 0 && (isOfficial || endpoint.Version == registry.APIVersion2) {
143 148
 		j := job.Eng.Job("trust_update_base")
144 149
 		if err = j.Run(); err != nil {
... ...
@@ -146,6 +151,9 @@ func (s *TagStore) CmdPull(job *engine.Job) engine.Status {
146 146
 		}
147 147
 
148 148
 		if err := s.pullV2Repository(job.Eng, r, job.Stdout, localName, remoteName, tag, sf, job.GetenvBool("parallel")); err == nil {
149
+			if err = job.Eng.Job("log", "pull", logName, "").Run(); err != nil {
150
+				log.Errorf("Error logging event 'pull' for %s: %s", logName, err)
151
+			}
149 152
 			return engine.StatusOK
150 153
 		} else if err != registry.ErrDoesNotExist {
151 154
 			log.Errorf("Error from V2 registry: %s", err)
... ...
@@ -156,6 +164,10 @@ func (s *TagStore) CmdPull(job *engine.Job) engine.Status {
156 156
 		return job.Error(err)
157 157
 	}
158 158
 
159
+	if err = job.Eng.Job("log", "pull", logName, "").Run(); err != nil {
160
+		log.Errorf("Error logging event 'pull' for %s: %s", logName, err)
161
+	}
162
+
159 163
 	return engine.StatusOK
160 164
 }
161 165
 
... ...
@@ -215,3 +215,57 @@ func TestEventsRedirectStdout(t *testing.T) {
215 215
 
216 216
 	logDone("events - redirect stdout")
217 217
 }
218
+
219
+func TestEventsImagePull(t *testing.T) {
220
+	since := time.Now().Unix()
221
+	pullCmd := exec.Command(dockerBinary, "pull", "scratch")
222
+	if out, _, err := runCommandWithOutput(pullCmd); err != nil {
223
+		t.Fatal("pulling the scratch image from has failed: %s, %v", out, err)
224
+	}
225
+
226
+	eventsCmd := exec.Command(dockerBinary, "events",
227
+		fmt.Sprintf("--since=%d", since),
228
+		fmt.Sprintf("--until=%d", time.Now().Unix()))
229
+	out, _, _ := runCommandWithOutput(eventsCmd)
230
+
231
+	events := strings.Split(strings.TrimSpace(out), "\n")
232
+	event := strings.TrimSpace(events[len(events)-1])
233
+
234
+	if !strings.HasSuffix(event, "scratch:latest: pull") {
235
+		t.Fatalf("Missing pull event - got:%q", event)
236
+	}
237
+
238
+	logDone("events - image pull is logged")
239
+}
240
+
241
+func TestEventsImageImport(t *testing.T) {
242
+	since := time.Now().Unix()
243
+
244
+	server, err := fileServer(map[string]string{
245
+		"/cirros.tar.gz": "/cirros.tar.gz",
246
+	})
247
+	if err != nil {
248
+		t.Fatal(err)
249
+	}
250
+	defer server.Close()
251
+	fileURL := fmt.Sprintf("%s/cirros.tar.gz", server.URL)
252
+	importCmd := exec.Command(dockerBinary, "import", fileURL)
253
+	out, _, err := runCommandWithOutput(importCmd)
254
+	if err != nil {
255
+		t.Errorf("import failed with errors: %v, output: %q", err, out)
256
+	}
257
+
258
+	eventsCmd := exec.Command(dockerBinary, "events",
259
+		fmt.Sprintf("--since=%d", since),
260
+		fmt.Sprintf("--until=%d", time.Now().Unix()))
261
+	out, _, _ = runCommandWithOutput(eventsCmd)
262
+
263
+	events := strings.Split(strings.TrimSpace(out), "\n")
264
+	event := strings.TrimSpace(events[len(events)-1])
265
+
266
+	if !strings.HasSuffix(event, ": import") {
267
+		t.Fatalf("Missing pull event - got:%q", event)
268
+	}
269
+
270
+	logDone("events - image import is logged")
271
+}