2016-12-02 18:33:39 +00:00
|
|
|
package ipfscluster
|
|
|
|
|
|
|
|
import (
|
2016-12-21 18:37:25 +00:00
|
|
|
"encoding/base64"
|
2016-12-02 18:33:39 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2016-12-21 18:37:25 +00:00
|
|
|
|
|
|
|
crypto "github.com/libp2p/go-libp2p-crypto"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Default parameters for the configuration
|
|
|
|
const (
|
|
|
|
DefaultConfigCrypto = crypto.RSA
|
|
|
|
DefaultConfigKeyLength = 2048
|
|
|
|
DefaultAPIAddr = "127.0.0.1"
|
|
|
|
DefaultAPIPort = 9094
|
|
|
|
DefaultIPFSAPIAddr = "127.0.0.1"
|
|
|
|
DefaultIPFSAPIPort = 9095
|
|
|
|
DefaultIPFSAddr = "127.0.0.1"
|
|
|
|
DefaultIPFSPort = 5001
|
|
|
|
DefaultClusterAddr = "0.0.0.0"
|
|
|
|
DefaultClusterPort = 9096
|
2016-12-02 18:33:39 +00:00
|
|
|
)
|
|
|
|
|
2016-12-28 15:25:24 +00:00
|
|
|
// Config represents an ipfs-cluster configuration which can be
|
|
|
|
// saved and loaded from disk. Currently it holds configuration
|
|
|
|
// values used by all components.
|
2016-12-15 18:08:46 +00:00
|
|
|
type Config struct {
|
2016-12-16 18:14:45 +00:00
|
|
|
// Libp2p ID and private key for Cluster communication (including)
|
|
|
|
// the Consensus component.
|
|
|
|
ID string `json:"id"`
|
|
|
|
PrivateKey string `json:"private_key"`
|
|
|
|
|
|
|
|
// List of multiaddresses of the peers of this cluster.
|
|
|
|
ClusterPeers []string `json:"cluster_peers"`
|
|
|
|
|
|
|
|
// Listen parameters for the Cluster libp2p Host. Used by
|
|
|
|
// the Remote RPC and Consensus components.
|
|
|
|
ClusterAddr string `json:"cluster_addr"`
|
|
|
|
ClusterPort int `json:"cluster_port"`
|
|
|
|
|
|
|
|
// Storage folder for snapshots, log store etc. Used by
|
|
|
|
// the Consensus component.
|
|
|
|
ConsensusDataFolder string `json:"consensus_data_folder"`
|
|
|
|
|
|
|
|
// Listen parameters for the the Cluster HTTP API component.
|
|
|
|
APIAddr string `json:"api_addr"`
|
|
|
|
APIPort int `json:"api_port"`
|
|
|
|
|
|
|
|
// Listen parameters for the IPFS Proxy. Used by the IPFS
|
|
|
|
// connector component.
|
|
|
|
IPFSAPIAddr string `json:"ipfs_api_addr"`
|
|
|
|
IPFSAPIPort int `json:"ipfs_api_port"`
|
|
|
|
|
|
|
|
// Host/Port for the IPFS daemon.
|
|
|
|
IPFSAddr string `json:"ipfs_addr"`
|
|
|
|
IPFSPort int `json:"ipfs_port"`
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 15:25:24 +00:00
|
|
|
// LoadConfig reads a JSON configuration file from the given path,
|
|
|
|
// parses it and returns a new Config object.
|
2016-12-15 18:08:46 +00:00
|
|
|
func LoadConfig(path string) (*Config, error) {
|
|
|
|
config := &Config{}
|
2016-12-02 18:33:39 +00:00
|
|
|
file, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
json.Unmarshal(file, config)
|
|
|
|
return config, nil
|
|
|
|
}
|
2016-12-21 18:37:25 +00:00
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration object with a randomly
|
|
|
|
// generated ID and private key.
|
|
|
|
func NewDefaultConfig() (*Config, error) {
|
|
|
|
priv, pub, err := crypto.GenerateKeyPair(
|
|
|
|
DefaultConfigCrypto,
|
|
|
|
DefaultConfigKeyLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pid, err := peer.IDFromPublicKey(pub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
privBytes, err := priv.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b64priv := base64.StdEncoding.EncodeToString(privBytes)
|
|
|
|
|
|
|
|
return &Config{
|
|
|
|
ID: peer.IDB58Encode(pid),
|
|
|
|
PrivateKey: b64priv,
|
|
|
|
ClusterPeers: []string{},
|
|
|
|
ClusterAddr: DefaultClusterAddr,
|
|
|
|
ClusterPort: DefaultClusterPort,
|
|
|
|
ConsensusDataFolder: "ipfscluster-data",
|
|
|
|
APIAddr: DefaultAPIAddr,
|
|
|
|
APIPort: DefaultAPIPort,
|
|
|
|
IPFSAPIAddr: DefaultIPFSAPIAddr,
|
|
|
|
IPFSAPIPort: DefaultIPFSAPIPort,
|
|
|
|
IPFSAddr: DefaultIPFSAddr,
|
|
|
|
IPFSPort: DefaultIPFSPort,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:25:24 +00:00
|
|
|
// Save stores a configuration as a JSON file in the given path.
|
2016-12-21 18:37:25 +00:00
|
|
|
func (c *Config) Save(path string) error {
|
|
|
|
json, err := json.MarshalIndent(c, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(path, json, 0600)
|
|
|
|
return err
|
|
|
|
}
|