diff --git a/api/types.go b/api/types.go index e1c82bbc..80e2bbd7 100644 --- a/api/types.go +++ b/api/types.go @@ -239,6 +239,7 @@ type ID struct { RPCProtocolVersion protocol.ID Error string IPFS IPFSID + Hostname string //PublicKey crypto.PubKey } @@ -253,6 +254,7 @@ type IDSerial struct { RPCProtocolVersion string `json:"rpc_protocol_version"` Error string `json:"error"` IPFS IPFSIDSerial `json:"ipfs"` + Hostname string `json:"hostname"` //PublicKey []byte } @@ -279,6 +281,7 @@ func (id ID) ToSerial() IDSerial { RPCProtocolVersion: string(id.RPCProtocolVersion), Error: id.Error, IPFS: id.IPFS.ToSerial(), + Hostname: id.Hostname, } } @@ -306,6 +309,7 @@ func (ids IDSerial) ToID() ID { id.RPCProtocolVersion = protocol.ID(ids.RPCProtocolVersion) id.Error = ids.Error id.IPFS = ids.IPFS.ToIPFSID() + id.Hostname = ids.Hostname return id } diff --git a/cluster.go b/cluster.go index 26a8cd32..165375f0 100644 --- a/cluster.go +++ b/cluster.go @@ -599,6 +599,7 @@ func (c *Cluster) ID() api.ID { Commit: Commit, RPCProtocolVersion: RPCProtocol, IPFS: ipfsID, + Hostname: c.config.Hostname, } } diff --git a/cluster_config.go b/cluster_config.go index ee12c2a6..25822f21 100644 --- a/cluster_config.go +++ b/cluster_config.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "os" "sync" "time" @@ -44,6 +45,9 @@ type Config struct { ID peer.ID PrivateKey crypto.PrivKey + // User-defined hostname for use as human-readable identifier. + Hostname string + // Cluster secret for private network. Peers will be in the same cluster if and // only if they have the same ClusterSecret. The cluster secret must be exactly // 64 characters and contain only hexadecimal characters (`[0-9a-f]`). @@ -100,6 +104,7 @@ type Config struct { // like strings, and key names aim to be self-explanatory for the user. type configJSON struct { ID string `json:"id"` + Hostname string `json:"hostname"` PrivateKey string `json:"private_key"` Secret string `json:"secret"` Peers []string `json:"peers"` @@ -194,6 +199,9 @@ func (cfg *Config) Validate() error { // this just sets non-generated defaults func (cfg *Config) setDefaults() { + hostname, _ := os.Hostname() + cfg.Hostname = hostname + addr, _ := ma.NewMultiaddr(DefaultListenAddr) cfg.ListenAddr = addr cfg.Peers = []ma.Multiaddr{} @@ -226,6 +234,10 @@ func (cfg *Config) LoadJSON(raw []byte) error { } cfg.ID = id + if jcfg.Hostname != "" { + cfg.Hostname = jcfg.Hostname + } + pkb, err := base64.StdEncoding.DecodeString(jcfg.PrivateKey) if err != nil { err = fmt.Errorf("error decoding private_key: %s", err) @@ -330,6 +342,7 @@ func (cfg *Config) ToJSON() (raw []byte, err error) { // Set all configuration fields jcfg.ID = cfg.ID.Pretty() + jcfg.Hostname = cfg.Hostname jcfg.PrivateKey = pKey jcfg.Secret = EncodeClusterSecret(cfg.Secret) jcfg.Peers = clusterPeers diff --git a/ipfs-cluster-ctl/formatters.go b/ipfs-cluster-ctl/formatters.go index ffbbeb4d..9674d2fc 100644 --- a/ipfs-cluster-ctl/formatters.go +++ b/ipfs-cluster-ctl/formatters.go @@ -81,7 +81,7 @@ func textFormatPrintIDSerial(obj *api.IDSerial) { return } - fmt.Printf("%s | Sees %d other peers\n", obj.ID, len(obj.ClusterPeers)-1) + fmt.Printf("%s | '%s' | Sees %d other peers\n", obj.ID, obj.Hostname, len(obj.ClusterPeers)-1) addrs := make(sort.StringSlice, 0, len(obj.Addresses)) for _, a := range obj.Addresses { addrs = append(addrs, string(a))