2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-09 15:59:51 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2019-01-23 15:48:46 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-01-15 15:02:54 +00:00
|
|
|
"go.uber.org/zap"
|
2019-01-09 15:59:51 +00:00
|
|
|
|
2019-01-23 15:48:46 +00:00
|
|
|
"storj.io/storj/bootstrap"
|
|
|
|
"storj.io/storj/bootstrap/bootstrapdb"
|
2019-01-09 15:59:51 +00:00
|
|
|
"storj.io/storj/internal/fpath"
|
2019-04-03 20:13:39 +01:00
|
|
|
"storj.io/storj/internal/version"
|
2019-01-09 15:59:51 +00:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
|
|
|
"storj.io/storj/pkg/process"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "bootstrap",
|
|
|
|
Short: "bootstrap",
|
|
|
|
}
|
|
|
|
runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "Run the bootstrap server",
|
|
|
|
RunE: cmdRun,
|
|
|
|
}
|
|
|
|
setupCmd = &cobra.Command{
|
|
|
|
Use: "setup",
|
|
|
|
Short: "Create config files",
|
|
|
|
RunE: cmdSetup,
|
|
|
|
Annotations: map[string]string{"type": "setup"},
|
|
|
|
}
|
|
|
|
|
2019-01-23 15:48:46 +00:00
|
|
|
runCfg bootstrap.Config
|
|
|
|
setupCfg bootstrap.Config
|
2019-01-09 15:59:51 +00:00
|
|
|
|
2019-03-12 12:51:06 +00:00
|
|
|
confDir string
|
|
|
|
identityDir string
|
2019-01-09 15:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultServerAddr = ":28967"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-03-12 12:51:06 +00:00
|
|
|
defaultConfDir := fpath.ApplicationDir("storj", "bootstrap")
|
|
|
|
defaultIdentityDir := fpath.ApplicationDir("storj", "identity", "bootstrap")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for bootstrap configuration")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &identityDir, "identity-dir", defaultIdentityDir, "main directory for bootstrap identity credentials")
|
2019-04-19 19:17:30 +01:00
|
|
|
defaults := cfgstruct.DefaultsFlag(rootCmd)
|
2019-01-09 15:59:51 +00:00
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
rootCmd.AddCommand(setupCmd)
|
2019-04-19 19:17:30 +01:00
|
|
|
cfgstruct.Bind(runCmd.Flags(), &runCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
|
|
|
cfgstruct.BindSetup(setupCmd.Flags(), &setupCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2019-01-09 15:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
2019-04-04 16:40:07 +01:00
|
|
|
// inert constructors only ====
|
|
|
|
|
|
|
|
ctx := process.Ctx(cmd)
|
2019-01-23 15:48:46 +00:00
|
|
|
log := zap.L()
|
|
|
|
|
2019-01-25 14:54:54 +00:00
|
|
|
identity, err := runCfg.Identity.Load()
|
2019-01-23 15:48:46 +00:00
|
|
|
if err != nil {
|
2019-01-22 12:35:48 +00:00
|
|
|
zap.S().Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-01-23 15:48:46 +00:00
|
|
|
if err := runCfg.Verify(log); err != nil {
|
|
|
|
log.Sugar().Error("Invalid configuration: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := bootstrapdb.New(bootstrapdb.Config{
|
|
|
|
Kademlia: runCfg.Kademlia.DBPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-01-24 20:28:06 +00:00
|
|
|
return errs.New("Error starting master database on bootstrap: %+v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-30 05:22:58 +00:00
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
|
|
|
|
2019-04-04 16:40:07 +01:00
|
|
|
peer, err := bootstrap.New(log, identity, db, runCfg, version.Build)
|
2019-01-24 20:28:06 +00:00
|
|
|
if err != nil {
|
2019-04-04 16:40:07 +01:00
|
|
|
return err
|
2019-01-23 15:48:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 16:40:07 +01:00
|
|
|
// okay, start doing stuff ====
|
|
|
|
|
|
|
|
err = peer.Version.CheckVersion(ctx)
|
2019-01-23 15:48:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-04 16:40:07 +01:00
|
|
|
if err := process.InitMetricsWithCertPath(ctx, nil, runCfg.Identity.CertPath); err != nil {
|
|
|
|
zap.S().Error("Failed to initialize telemetry batcher: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.CreateTables()
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error creating tables for master database on bootstrap: %+v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-23 15:48:46 +00:00
|
|
|
runError := peer.Run(ctx)
|
|
|
|
closeError := peer.Close()
|
|
|
|
|
2019-01-30 05:22:58 +00:00
|
|
|
return errs.Combine(runError, closeError)
|
2019-01-09 15:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
2019-01-22 12:35:48 +00:00
|
|
|
setupDir, err := filepath.Abs(confDir)
|
2019-01-09 15:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:57:58 +00:00
|
|
|
valid, _ := fpath.IsValidSetupDir(setupDir)
|
2019-01-09 15:59:51 +00:00
|
|
|
if !valid {
|
2019-01-14 15:57:58 +00:00
|
|
|
return fmt.Errorf("bootstrap configuration already exists (%v)", setupDir)
|
2019-01-09 15:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(setupDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:48:49 +00:00
|
|
|
overrides := map[string]interface{}{}
|
|
|
|
|
|
|
|
serverAddress := cmd.Flag("server.address")
|
|
|
|
if !serverAddress.Changed {
|
|
|
|
overrides[serverAddress.Name] = defaultServerAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
kademliaBootstrapAddr := cmd.Flag("kademlia.bootstrap-addr")
|
|
|
|
if !kademliaBootstrapAddr.Changed {
|
2019-04-05 13:09:16 +01:00
|
|
|
overrides[kademliaBootstrapAddr.Name] = "127.0.0.1" + defaultServerAddr
|
2019-01-09 15:59:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 13:55:33 +00:00
|
|
|
return process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides)
|
2019-01-09 15:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
process.Exec(rootCmd)
|
|
|
|
}
|