2016-12-21 18:37:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-02 22:52:06 +00:00
|
|
|
"errors"
|
2016-12-21 18:37:25 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log"
|
2017-02-02 22:52:06 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2017-01-24 18:55:06 +00:00
|
|
|
"github.com/urfave/cli"
|
2016-12-21 18:37:25 +00:00
|
|
|
|
|
|
|
ipfscluster "github.com/ipfs/ipfs-cluster"
|
2017-02-13 15:46:53 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/allocator/numpinalloc"
|
|
|
|
"github.com/ipfs/ipfs-cluster/informer/numpin"
|
2017-02-09 15:29:17 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/state/mapstate"
|
2016-12-21 18:37:25 +00:00
|
|
|
)
|
|
|
|
|
2016-12-22 16:14:15 +00:00
|
|
|
// ProgramName of this application
|
2017-01-23 12:34:22 +00:00
|
|
|
const programName = `ipfs-cluster-service`
|
2016-12-21 18:37:25 +00:00
|
|
|
|
2017-01-24 15:55:37 +00:00
|
|
|
// We store a commit id here
|
|
|
|
var commit string
|
|
|
|
|
2016-12-21 18:37:25 +00:00
|
|
|
// Description provides a short summary of the functionality of this tool
|
|
|
|
var Description = fmt.Sprintf(`
|
2017-01-24 18:55:06 +00:00
|
|
|
%s runs an IPFS Cluster node.
|
2016-12-21 18:37:25 +00:00
|
|
|
|
2017-01-24 18:55:06 +00:00
|
|
|
A node participates in the cluster consensus, follows a distributed log
|
|
|
|
of pinning and unpinning requests and manages pinning operations to a
|
|
|
|
configured IPFS daemon.
|
2016-12-21 18:37:25 +00:00
|
|
|
|
2017-01-23 12:34:22 +00:00
|
|
|
This node also provides an API for cluster management, an IPFS Proxy API which
|
2016-12-21 18:37:25 +00:00
|
|
|
forwards requests to IPFS and a number of components for internal communication
|
2017-02-02 13:34:51 +00:00
|
|
|
using LibP2P. This is a simplified view of the components:
|
|
|
|
|
|
|
|
+------------------+
|
|
|
|
| ipfs-cluster-ctl |
|
|
|
|
+---------+--------+
|
|
|
|
|
|
|
|
|
| HTTP
|
|
|
|
ipfs-cluster-service | HTTP
|
|
|
|
+----------+--------+--v--+----------------------+ +-------------+
|
|
|
|
| RPC/Raft | Peer 1 | API | IPFS Connector/Proxy +------> IPFS daemon |
|
|
|
|
+----^-----+--------+-----+----------------------+ +-------------+
|
|
|
|
| libp2p
|
|
|
|
|
|
|
|
|
+----v-----+--------+-----+----------------------+ +-------------+
|
|
|
|
| RPC/Raft | Peer 2 | API | IPFS Connector/Proxy +------> IPFS daemon |
|
|
|
|
+----^-----+--------+-----+----------------------+ +-------------+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+----v-----+--------+-----+----------------------+ +-------------+
|
|
|
|
| RPC/Raft | Peer 3 | API | IPFS Connector/Proxy +------> IPFS daemon |
|
|
|
|
+----------+--------+-----+----------------------+ +-------------+
|
|
|
|
|
2016-12-21 18:37:25 +00:00
|
|
|
|
2017-01-23 13:21:26 +00:00
|
|
|
%s needs a valid configuration to run. This configuration is
|
|
|
|
independent from IPFS and includes its own LibP2P key-pair. It can be
|
2017-01-30 12:12:25 +00:00
|
|
|
initialized with "init" and its default location is
|
2016-12-21 18:37:25 +00:00
|
|
|
~/%s/%s.
|
|
|
|
|
2016-12-22 16:14:15 +00:00
|
|
|
For feedback, bug reports or any additional information, visit
|
|
|
|
https://github.com/ipfs/ipfs-cluster.
|
2017-02-02 22:52:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
EXAMPLES
|
|
|
|
|
|
|
|
Initial configuration:
|
|
|
|
|
|
|
|
$ ipfs-cluster-service init
|
|
|
|
|
|
|
|
Launch a cluster:
|
|
|
|
|
|
|
|
$ ipfs-cluster-service
|
|
|
|
|
|
|
|
Launch a peer and join existing cluster:
|
|
|
|
|
|
|
|
$ ipfs-cluster-service --bootstrap /ip4/192.168.1.2/tcp/9096/ipfs/QmPSoSaPXpyunaBwHs1rZBKYSqRV4bLRk32VGYLuvdrypL
|
2016-12-21 18:37:25 +00:00
|
|
|
`,
|
2016-12-22 16:14:15 +00:00
|
|
|
programName,
|
|
|
|
programName,
|
2016-12-21 18:37:25 +00:00
|
|
|
DefaultPath,
|
|
|
|
DefaultConfigFile)
|
|
|
|
|
2017-01-30 12:12:25 +00:00
|
|
|
var logger = logging.Logger("service")
|
|
|
|
|
2016-12-21 18:37:25 +00:00
|
|
|
// Default location for the configurations and data
|
|
|
|
var (
|
2017-01-23 12:34:22 +00:00
|
|
|
// DefaultPath is initialized to something like ~/.ipfs-cluster/service.json
|
2016-12-21 18:37:25 +00:00
|
|
|
// and holds all the ipfs-cluster data
|
|
|
|
DefaultPath = ".ipfs-cluster"
|
|
|
|
// The name of the configuration file inside DefaultPath
|
2017-01-23 12:34:22 +00:00
|
|
|
DefaultConfigFile = "service.json"
|
2016-12-21 18:37:25 +00:00
|
|
|
// The name of the data folder inside DefaultPath
|
|
|
|
DefaultDataFolder = "data"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configPath string
|
|
|
|
dataPath string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-01-24 15:55:37 +00:00
|
|
|
// The only way I could make this work
|
|
|
|
ipfscluster.Commit = commit
|
2017-01-24 18:55:06 +00:00
|
|
|
usr, err := user.Current()
|
2017-01-23 18:06:00 +00:00
|
|
|
if err != nil {
|
2017-01-24 18:55:06 +00:00
|
|
|
panic("cannot guess the current user")
|
2016-12-22 16:14:15 +00:00
|
|
|
}
|
2017-01-24 18:55:06 +00:00
|
|
|
DefaultPath = filepath.Join(
|
|
|
|
usr.HomeDir,
|
|
|
|
".ipfs-cluster")
|
2016-12-21 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func out(m string, a ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, m, a...)
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:55:06 +00:00
|
|
|
func checkErr(doing string, err error) {
|
|
|
|
if err != nil {
|
|
|
|
out("error %s: %s\n", doing, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 18:37:25 +00:00
|
|
|
func main() {
|
2017-01-24 18:55:06 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = programName
|
|
|
|
app.Usage = "IPFS Cluster node"
|
2017-02-02 22:52:06 +00:00
|
|
|
app.Description = Description
|
|
|
|
//app.Copyright = "© Protocol Labs, Inc."
|
2017-01-24 18:55:06 +00:00
|
|
|
app.Version = ipfscluster.Version
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
2017-01-30 12:12:25 +00:00
|
|
|
Name: "init",
|
|
|
|
Usage: "create a default configuration and exit",
|
|
|
|
Hidden: true,
|
2017-01-24 18:55:06 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: DefaultPath,
|
|
|
|
Usage: "path to the configuration and data `FOLDER`",
|
|
|
|
EnvVar: "IPFS_CLUSTER_PATH",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
2017-02-02 22:52:06 +00:00
|
|
|
Usage: "forcefully proceed with some actions. i.e. overwriting configuration",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "bootstrap, j",
|
|
|
|
Usage: "join a cluster providing an existing peer's `multiaddress`. Overrides the \"bootstrap\" values from the configuration",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "leave, x",
|
|
|
|
Usage: "remove peer from cluster on exit. Overrides \"leave_on_shutdown\"",
|
|
|
|
Hidden: true,
|
2017-01-24 18:55:06 +00:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug, d",
|
2017-02-02 22:52:06 +00:00
|
|
|
Usage: "enable full debug logging (very verbose)",
|
2017-01-24 18:55:06 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "loglevel, l",
|
|
|
|
Value: "info",
|
2017-02-02 22:52:06 +00:00
|
|
|
Usage: "set the loglevel for cluster only [critical, error, warning, info, debug]",
|
2017-01-24 18:55:06 +00:00
|
|
|
},
|
|
|
|
}
|
2016-12-21 18:37:25 +00:00
|
|
|
|
2017-01-24 18:55:06 +00:00
|
|
|
app.Commands = []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "init",
|
|
|
|
Usage: "create a default configuration and exit",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
initConfig(c.GlobalBool("force"))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2017-02-02 13:34:51 +00:00
|
|
|
{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "run the IPFS Cluster peer (default)",
|
|
|
|
Action: run,
|
|
|
|
},
|
2017-01-24 18:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.Before = func(c *cli.Context) error {
|
|
|
|
absPath, err := filepath.Abs(c.String("config"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath = filepath.Join(absPath, DefaultConfigFile)
|
|
|
|
dataPath = filepath.Join(absPath, DefaultDataFolder)
|
|
|
|
|
|
|
|
setupLogging(c.String("loglevel"))
|
|
|
|
if c.Bool("debug") {
|
|
|
|
setupDebug()
|
|
|
|
}
|
|
|
|
return nil
|
2016-12-21 18:37:25 +00:00
|
|
|
}
|
2017-01-24 18:55:06 +00:00
|
|
|
|
2017-02-02 13:34:51 +00:00
|
|
|
app.Action = run
|
2017-01-24 18:55:06 +00:00
|
|
|
|
2017-02-02 13:34:51 +00:00
|
|
|
app.Run(os.Args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(c *cli.Context) error {
|
|
|
|
if c.Bool("init") {
|
|
|
|
initConfig(c.Bool("force"))
|
|
|
|
return nil
|
2017-01-24 18:55:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 13:34:51 +00:00
|
|
|
cfg, err := loadConfig()
|
|
|
|
checkErr("loading configuration", err)
|
|
|
|
|
2017-02-02 22:52:06 +00:00
|
|
|
if a := c.String("bootstrap"); a != "" {
|
|
|
|
if len(cfg.ClusterPeers) > 0 && !c.Bool("force") {
|
2017-02-08 17:04:08 +00:00
|
|
|
return errors.New("the configuration provides ClusterPeers. Use -f to ignore and proceed bootstrapping")
|
2017-02-02 22:52:06 +00:00
|
|
|
}
|
|
|
|
joinAddr, err := ma.NewMultiaddr(a)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing multiaddress: %s", err)
|
|
|
|
}
|
|
|
|
cfg.Bootstrap = []ma.Multiaddr{joinAddr}
|
|
|
|
cfg.ClusterPeers = []ma.Multiaddr{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Bool("leave") {
|
|
|
|
cfg.LeaveOnShutdown = true
|
|
|
|
}
|
|
|
|
|
2017-02-02 13:34:51 +00:00
|
|
|
api, err := ipfscluster.NewRESTAPI(cfg)
|
|
|
|
checkErr("creating REST API component", err)
|
|
|
|
|
|
|
|
proxy, err := ipfscluster.NewIPFSHTTPConnector(cfg)
|
|
|
|
checkErr("creating IPFS Connector component", err)
|
|
|
|
|
2017-02-09 15:29:17 +00:00
|
|
|
state := mapstate.NewMapState()
|
2017-02-02 13:34:51 +00:00
|
|
|
tracker := ipfscluster.NewMapPinTracker(cfg)
|
2017-02-13 15:46:53 +00:00
|
|
|
mon := ipfscluster.NewStdPeerMonitor(5)
|
|
|
|
informer := numpin.NewInformer()
|
|
|
|
alloc := numpinalloc.NewAllocator()
|
|
|
|
|
2017-02-02 13:34:51 +00:00
|
|
|
cluster, err := ipfscluster.NewCluster(
|
|
|
|
cfg,
|
|
|
|
api,
|
|
|
|
proxy,
|
|
|
|
state,
|
2017-02-13 15:46:53 +00:00
|
|
|
tracker,
|
|
|
|
mon,
|
|
|
|
alloc,
|
|
|
|
informer)
|
2017-02-02 13:34:51 +00:00
|
|
|
checkErr("starting cluster", err)
|
|
|
|
|
2017-02-02 22:52:06 +00:00
|
|
|
signalChan := make(chan os.Signal, 20)
|
2017-02-02 13:34:51 +00:00
|
|
|
signal.Notify(signalChan, os.Interrupt)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-signalChan:
|
|
|
|
err = cluster.Shutdown()
|
|
|
|
checkErr("shutting down cluster", err)
|
|
|
|
case <-cluster.Done():
|
|
|
|
return nil
|
|
|
|
case <-cluster.Ready():
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 18:55:06 +00:00
|
|
|
func setupLogging(lvl string) {
|
2017-02-02 22:52:06 +00:00
|
|
|
ipfscluster.SetFacilityLogLevel("service", lvl)
|
|
|
|
ipfscluster.SetFacilityLogLevel("cluster", lvl)
|
|
|
|
//ipfscluster.SetFacilityLogLevel("raft", lvl)
|
2016-12-21 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupDebug() {
|
2017-02-02 22:52:06 +00:00
|
|
|
l := "DEBUG"
|
|
|
|
ipfscluster.SetFacilityLogLevel("cluster", l)
|
|
|
|
ipfscluster.SetFacilityLogLevel("raft", l)
|
|
|
|
ipfscluster.SetFacilityLogLevel("p2p-gorpc", l)
|
|
|
|
//SetFacilityLogLevel("swarm2", l)
|
|
|
|
//SetFacilityLogLevel("libp2p-raft", l)
|
2016-12-21 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 18:55:06 +00:00
|
|
|
func initConfig(force bool) {
|
|
|
|
if _, err := os.Stat(configPath); err == nil && !force {
|
|
|
|
err := fmt.Errorf("%s exists. Try running with -f", configPath)
|
|
|
|
checkErr("", err)
|
2016-12-21 18:37:25 +00:00
|
|
|
}
|
|
|
|
cfg, err := ipfscluster.NewDefaultConfig()
|
2017-01-24 18:55:06 +00:00
|
|
|
checkErr("creating default configuration", err)
|
2016-12-21 18:37:25 +00:00
|
|
|
cfg.ConsensusDataFolder = dataPath
|
2017-01-30 12:12:25 +00:00
|
|
|
err = os.MkdirAll(filepath.Dir(configPath), 0700)
|
2016-12-21 18:37:25 +00:00
|
|
|
err = cfg.Save(configPath)
|
2017-01-24 18:55:06 +00:00
|
|
|
checkErr("saving new configuration", err)
|
2016-12-23 18:35:37 +00:00
|
|
|
out("%s configuration written to %s\n",
|
2016-12-22 16:14:15 +00:00
|
|
|
programName, configPath)
|
2016-12-21 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig() (*ipfscluster.Config, error) {
|
|
|
|
return ipfscluster.LoadConfig(configPath)
|
|
|
|
}
|