Browse code

add ID and Hostname in docker info

Signed-off-by: Victor Vieux <vieux@docker.com>

Victor Vieux authored on 2014/11/18 04:23:41
Showing 6 changed files
... ...
@@ -505,6 +505,12 @@ func (cli *DockerCli) CmdInfo(args ...string) error {
505 505
 	if remoteInfo.Exists("MemTotal") {
506 506
 		fmt.Fprintf(cli.out, "Total Memory: %s\n", units.BytesSize(float64(remoteInfo.GetInt64("MemTotal"))))
507 507
 	}
508
+	if remoteInfo.Exists("Hostname") {
509
+		fmt.Fprintf(cli.out, "Hostname: %s\n", remoteInfo.Get("Hostname"))
510
+	}
511
+	if remoteInfo.Exists("ID") {
512
+		fmt.Fprintf(cli.out, "ID: %s\n", remoteInfo.Get("ID"))
513
+	}
508 514
 
509 515
 	if remoteInfo.GetBool("Debug") || os.Getenv("DEBUG") != "" {
510 516
 		if remoteInfo.Exists("Debug") {
... ...
@@ -3,12 +3,15 @@ package api
3 3
 import (
4 4
 	"fmt"
5 5
 	"mime"
6
+	"os"
7
+	"path"
6 8
 	"strings"
7 9
 
8 10
 	log "github.com/Sirupsen/logrus"
9 11
 	"github.com/docker/docker/engine"
10 12
 	"github.com/docker/docker/pkg/parsers"
11 13
 	"github.com/docker/docker/pkg/version"
14
+	"github.com/docker/docker/vendor/src/github.com/docker/libtrust"
12 15
 )
13 16
 
14 17
 const (
... ...
@@ -47,3 +50,25 @@ func MatchesContentType(contentType, expectedType string) bool {
47 47
 	}
48 48
 	return err == nil && mimetype == expectedType
49 49
 }
50
+
51
+// LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
52
+// otherwise generates a new one
53
+func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
54
+	err := os.MkdirAll(path.Dir(trustKeyPath), 0700)
55
+	if err != nil {
56
+		return nil, err
57
+	}
58
+	trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
59
+	if err == libtrust.ErrKeyFileDoesNotExist {
60
+		trustKey, err = libtrust.GenerateECP256PrivateKey()
61
+		if err != nil {
62
+			return nil, fmt.Errorf("Error generating key: %s", err)
63
+		}
64
+		if err := libtrust.SaveKey(trustKeyPath, trustKey); err != nil {
65
+			return nil, fmt.Errorf("Error saving key file: %s", err)
66
+		}
67
+	} else if err != nil {
68
+		log.Fatalf("Error loading key file: %s", err)
69
+	}
70
+	return trustKey, nil
71
+}
... ...
@@ -40,6 +40,7 @@ type Config struct {
40 40
 	DisableNetwork              bool
41 41
 	EnableSelinuxSupport        bool
42 42
 	Context                     map[string][]string
43
+	TrustKeyPath                string
43 44
 }
44 45
 
45 46
 // InstallFlags adds command-line options to the top-level flag parser for
... ...
@@ -15,6 +15,7 @@ import (
15 15
 	"github.com/docker/libcontainer/label"
16 16
 
17 17
 	log "github.com/Sirupsen/logrus"
18
+	"github.com/docker/docker/api"
18 19
 	"github.com/docker/docker/daemon/execdriver"
19 20
 	"github.com/docker/docker/daemon/execdriver/execdrivers"
20 21
 	"github.com/docker/docker/daemon/execdriver/lxc"
... ...
@@ -83,6 +84,7 @@ func (c *contStore) List() []*Container {
83 83
 }
84 84
 
85 85
 type Daemon struct {
86
+	ID             string
86 87
 	repository     string
87 88
 	sysInitPath    string
88 89
 	containers     *contStore
... ...
@@ -893,7 +895,13 @@ func NewDaemonFromDirectory(config *Config, eng *engine.Engine) (*Daemon, error)
893 893
 		return nil, err
894 894
 	}
895 895
 
896
+	trustKey, err := api.LoadOrCreateTrustKey(config.TrustKeyPath)
897
+	if err != nil {
898
+		return nil, err
899
+	}
900
+
896 901
 	daemon := &Daemon{
902
+		ID:             trustKey.PublicKey().KeyID(),
897 903
 		repository:     daemonRepo,
898 904
 		containers:     &contStore{s: make(map[string]*Container)},
899 905
 		execCommands:   newExecStore(),
... ...
@@ -56,6 +56,7 @@ func (daemon *Daemon) CmdInfo(job *engine.Job) engine.Status {
56 56
 		return job.Error(err)
57 57
 	}
58 58
 	v := &engine.Env{}
59
+	v.Set("ID", daemon.ID)
59 60
 	v.SetInt("Containers", len(daemon.List()))
60 61
 	v.SetInt("Images", imgcount)
61 62
 	v.Set("Driver", daemon.GraphDriver().String())
... ...
@@ -75,6 +76,9 @@ func (daemon *Daemon) CmdInfo(job *engine.Job) engine.Status {
75 75
 	v.Set("InitPath", initPath)
76 76
 	v.SetInt("NCPU", runtime.NumCPU())
77 77
 	v.SetInt64("MemTotal", meminfo.MemTotal)
78
+	if hostname, err := os.Hostname(); err == nil {
79
+		v.Set("Hostname", hostname)
80
+	}
78 81
 	if _, err := v.WriteTo(job.Stdout); err != nil {
79 82
 		return job.Error(err)
80 83
 	}
... ...
@@ -34,6 +34,8 @@ func mainDaemon() {
34 34
 	eng := engine.New()
35 35
 	signal.Trap(eng.Shutdown)
36 36
 
37
+	daemonCfg.TrustKeyPath = *flTrustKey
38
+
37 39
 	// Load builtins
38 40
 	if err := builtins.Register(eng); err != nil {
39 41
 		log.Fatal(err)