2020-11-10 15:35:43 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/fpath"
|
|
|
|
"storj.io/private/cfgstruct"
|
|
|
|
"storj.io/private/process"
|
|
|
|
"storj.io/storj/multinode"
|
|
|
|
"storj.io/storj/multinode/multinodedb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config defines multinode configuration.
|
|
|
|
type Config struct {
|
2021-02-04 14:38:45 +00:00
|
|
|
Database string `help:"multinode database connection string" default:"sqlite3://file:$CONFDIR/master.db"`
|
2020-11-10 15:35:43 +00:00
|
|
|
|
|
|
|
multinode.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "multinode",
|
|
|
|
Short: "Multinode Dashboard",
|
|
|
|
}
|
|
|
|
runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "Run the multinode dashboard",
|
|
|
|
RunE: cmdRun,
|
|
|
|
}
|
|
|
|
setupCmd = &cobra.Command{
|
|
|
|
Use: "setup",
|
|
|
|
Short: "Create config files",
|
|
|
|
RunE: cmdSetup,
|
|
|
|
Annotations: map[string]string{"type": "setup"},
|
|
|
|
}
|
|
|
|
|
|
|
|
runCfg Config
|
|
|
|
setupCfg Config
|
|
|
|
confDir string
|
|
|
|
identityDir string
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
process.ExecCustomDebug(rootCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
defaultConfDir := fpath.ApplicationDir("storj", "multinode")
|
|
|
|
defaultIdentityDir := fpath.ApplicationDir("storj", "identity", "multinode")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for multinode configuration")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &identityDir, "identity-dir", defaultIdentityDir, "main directory for multinode identity credentials")
|
|
|
|
defaults := cfgstruct.DefaultsFlag(rootCmd)
|
|
|
|
|
|
|
|
rootCmd.AddCommand(setupCmd)
|
2020-11-12 17:21:56 +00:00
|
|
|
rootCmd.AddCommand(runCmd)
|
2020-11-10 15:35:43 +00:00
|
|
|
|
|
|
|
process.Bind(runCmd, &runCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
|
|
|
process.Bind(setupCmd, &setupCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir), cfgstruct.SetupMode())
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:21:56 +00:00
|
|
|
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
setupDir, err := filepath.Abs(confDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, _ := fpath.IsValidSetupDir(setupDir)
|
|
|
|
if !valid {
|
|
|
|
return fmt.Errorf("multinode configuration already exists (%v)", setupDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(setupDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return process.SaveConfig(cmd, filepath.Join(setupDir, "config.yaml"))
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:35:43 +00:00
|
|
|
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
ctx, _ := process.Ctx(cmd)
|
|
|
|
log := zap.L()
|
|
|
|
|
|
|
|
runCfg.Debug.Address = *process.DebugAddrFlag
|
|
|
|
|
|
|
|
identity, err := runCfg.Identity.Load()
|
|
|
|
if err != nil {
|
2020-11-12 17:21:56 +00:00
|
|
|
log.Error("failed to load identity", zap.Error(err))
|
|
|
|
return errs.New("failed to load identity: %+v", err)
|
2020-11-10 15:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db, err := multinodedb.Open(ctx, log.Named("db"), runCfg.Database)
|
|
|
|
if err != nil {
|
2020-11-12 17:21:56 +00:00
|
|
|
return errs.New("error connecting to master database on multinode: %+v", err)
|
2020-11-10 15:35:43 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
2021-04-28 02:30:01 +01:00
|
|
|
if err := db.MigrateToLatest(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 15:35:43 +00:00
|
|
|
|
|
|
|
peer, err := multinode.New(log, identity, runCfg.Config, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
runError := peer.Run(ctx)
|
|
|
|
closeError := peer.Close()
|
|
|
|
return errs.Combine(runError, closeError)
|
|
|
|
}
|