Browse code

Add a Debugf() helper and a -D (debug) flag to docker

Guillaume J. Charmes authored on 2013/03/23 03:44:12
Showing 5 changed files
... ...
@@ -16,8 +16,11 @@ func main() {
16 16
 		docker.SysInit()
17 17
 		return
18 18
 	}
19
+	// FIXME: Switch d and D ? (to be more sshd like)
19 20
 	fl_daemon := flag.Bool("d", false, "Daemon mode")
21
+	fl_debug := flag.Bool("D", false, "Debug mode")
20 22
 	flag.Parse()
23
+	rcli.DEBUG_FLAG = *fl_debug
21 24
 	if *fl_daemon {
22 25
 		if flag.NArg() != 0 {
23 26
 			flag.Usage()
... ...
@@ -10,6 +10,11 @@ import (
10 10
 	"net"
11 11
 )
12 12
 
13
+// Note: the globals are here to avoid import cycle
14
+// FIXME: Handle debug levels mode?
15
+var DEBUG_FLAG bool = false
16
+var CLIENT_SOCKET io.Writer = nil
17
+
13 18
 // Connect to a remote endpoint using protocol `proto` and address `addr`,
14 19
 // issue a single call, and return the result.
15 20
 // `proto` may be "tcp", "unix", etc. See the `net` package for available protocols.
... ...
@@ -42,6 +47,9 @@ func ListenAndServe(proto, addr string, service Service) error {
42 42
 			return err
43 43
 		} else {
44 44
 			go func() {
45
+				if DEBUG_FLAG {
46
+					CLIENT_SOCKET = conn
47
+				}
45 48
 				if err := Serve(conn, service); err != nil {
46 49
 					log.Printf("Error: " + err.Error() + "\n")
47 50
 					fmt.Fprintf(conn, "Error: "+err.Error()+"\n")
... ...
@@ -213,6 +213,9 @@ func (graph *Graph) PushImage(imgOrig *Image, authConfig *auth.AuthConfig) error
213 213
 		if err != nil {
214 214
 			return fmt.Errorf("Error while retreiving the path for {%s}: %s", img.Id, err)
215 215
 		}
216
+
217
+		Debugf("Pushing image [%s] on {%s}\n", img.Id, REGISTRY_ENDPOINT+"/images/"+img.Id+"/json")
218
+
216 219
 		// FIXME: try json with UTF8
217 220
 		jsonData := strings.NewReader(string(jsonRaw))
218 221
 		req, err := http.NewRequest("PUT", REGISTRY_ENDPOINT+"/images/"+img.Id+"/json", jsonData)
... ...
@@ -257,6 +260,7 @@ func (graph *Graph) PushImage(imgOrig *Image, authConfig *auth.AuthConfig) error
257 257
 				"Fail to retrieve layer storage URL for image {%s}: %s\n",
258 258
 				img.Id, err)
259 259
 		}
260
+
260 261
 		// FIXME: Don't do this :D. Check the S3 requierement and implement chunks of 5MB
261 262
 		// FIXME2: I won't stress it enough, DON'T DO THIS! very high priority
262 263
 		layerData2, err := Tar(path.Join(graph.Root, img.Id, "layer"), Gzip)
... ...
@@ -307,6 +311,8 @@ func (graph *Graph) pushTag(remote, revision, tag string, authConfig *auth.AuthC
307 307
 	// "jsonify" the string
308 308
 	revision = "\"" + revision + "\""
309 309
 
310
+	Debugf("Pushing tags for rev [%s] on {%s}\n", revision, REGISTRY_ENDPOINT+"/users/"+remote+"/"+tag)
311
+
310 312
 	client := &http.Client{}
311 313
 	req, err := http.NewRequest("PUT", REGISTRY_ENDPOINT+"/users/"+remote+"/"+tag, strings.NewReader(revision))
312 314
 	req.Header.Add("Content-type", "application/json")
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"github.com/dotcloud/docker/auth"
7 7
 	"io"
8 8
 	"io/ioutil"
9
-	"log"
10 9
 	"os"
11 10
 	"path"
12 11
 	"sort"
... ...
@@ -216,10 +215,10 @@ func (runtime *Runtime) restore() error {
216 216
 		id := v.Name()
217 217
 		container, err := runtime.Load(id)
218 218
 		if err != nil {
219
-			log.Printf("Failed to load container %v: %v", id, err)
219
+			Debugf("Failed to load container %v: %v", id, err)
220 220
 			continue
221 221
 		}
222
-		log.Printf("Loaded container %v", container.Id)
222
+		Debugf("Loaded container %v", container.Id)
223 223
 	}
224 224
 	return nil
225 225
 }
... ...
@@ -5,7 +5,9 @@ import (
5 5
 	"container/list"
6 6
 	"errors"
7 7
 	"fmt"
8
+	"github.com/dotcloud/docker/rcli"
8 9
 	"io"
10
+	"log"
9 11
 	"net/http"
10 12
 	"os"
11 13
 	"os/exec"
... ...
@@ -37,6 +39,17 @@ func Download(url string, stderr io.Writer) (*http.Response, error) {
37 37
 	return resp, nil
38 38
 }
39 39
 
40
+// Debug function, if the debug flag is set, then display. Do nothing otherwise
41
+// If Docker is in damon mode, also send the debug info on the socket
42
+func Debugf(format string, a ...interface{}) {
43
+	if rcli.DEBUG_FLAG {
44
+		log.Printf(format, a...)
45
+		if rcli.CLIENT_SOCKET != nil {
46
+			fmt.Fprintf(rcli.CLIENT_SOCKET, log.Prefix()+format, a...)
47
+		}
48
+	}
49
+}
50
+
40 51
 // Reader with progress bar
41 52
 type progressReader struct {
42 53
 	reader        io.ReadCloser // Stream to read from