2018-07-26 15:21:35 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-08 13:20:23 +00:00
|
|
|
"context"
|
2018-08-08 23:22:59 +01:00
|
|
|
"fmt"
|
2018-07-26 15:21:35 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-08 13:20:23 +00:00
|
|
|
"sort"
|
|
|
|
"text/tabwriter"
|
2018-07-26 15:21:35 +01:00
|
|
|
|
2018-11-20 18:29:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-07-26 15:21:35 +01:00
|
|
|
"github.com/spf13/cobra"
|
2018-11-15 19:06:09 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-10-04 22:40:34 +01:00
|
|
|
|
2018-12-03 15:51:56 +00:00
|
|
|
"storj.io/storj/internal/fpath"
|
2018-12-07 18:52:58 +00:00
|
|
|
"storj.io/storj/pkg/audit"
|
2018-10-11 21:25:54 +01:00
|
|
|
"storj.io/storj/pkg/auth/grpcauth"
|
2018-11-08 13:20:23 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2018-10-16 12:43:44 +01:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
2018-11-20 15:54:22 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/checker"
|
|
|
|
"storj.io/storj/pkg/datarepair/repairer"
|
2018-12-14 19:23:54 +00:00
|
|
|
"storj.io/storj/pkg/discovery"
|
2018-07-26 15:21:35 +01:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-11-08 13:20:23 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-07-26 15:21:35 +01:00
|
|
|
"storj.io/storj/pkg/pointerdb"
|
|
|
|
"storj.io/storj/pkg/process"
|
|
|
|
"storj.io/storj/pkg/provider"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-12-05 09:35:50 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
2018-07-26 15:21:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rootCmd = &cobra.Command{
|
2018-08-29 19:32:41 +01:00
|
|
|
Use: "satellite",
|
|
|
|
Short: "Satellite",
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
2018-07-30 08:38:31 +01:00
|
|
|
runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
2018-08-29 19:32:41 +01:00
|
|
|
Short: "Run the satellite",
|
2018-07-30 08:38:31 +01:00
|
|
|
RunE: cmdRun,
|
|
|
|
}
|
|
|
|
setupCmd = &cobra.Command{
|
2018-12-14 21:14:59 +00:00
|
|
|
Use: "setup",
|
|
|
|
Short: "Create config files",
|
|
|
|
RunE: cmdSetup,
|
|
|
|
Annotations: map[string]string{"type": "setup"},
|
2018-07-30 08:38:31 +01:00
|
|
|
}
|
2018-11-08 13:20:23 +00:00
|
|
|
diagCmd = &cobra.Command{
|
|
|
|
Use: "diag",
|
|
|
|
Short: "Diagnostic Tool support",
|
|
|
|
RunE: cmdDiag,
|
|
|
|
}
|
2018-11-16 13:31:33 +00:00
|
|
|
qdiagCmd = &cobra.Command{
|
|
|
|
Use: "qdiag",
|
|
|
|
Short: "Repair Queue Diagnostic Tool support",
|
|
|
|
RunE: cmdQDiag,
|
|
|
|
}
|
2018-07-26 15:21:35 +01:00
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
runCfg struct {
|
2018-12-07 18:52:58 +00:00
|
|
|
Identity provider.IdentityConfig
|
|
|
|
Kademlia kademlia.Config
|
|
|
|
PointerDB pointerdb.Config
|
|
|
|
Overlay overlay.Config
|
|
|
|
Checker checker.Config
|
|
|
|
Repairer repairer.Config
|
|
|
|
Audit audit.Config
|
2018-11-12 21:59:30 +00:00
|
|
|
BwAgreement bwagreement.Config
|
2018-12-07 09:59:31 +00:00
|
|
|
Database string `help:"satellite database connection string" default:"sqlite3://$CONFDIR/master.db"`
|
2018-12-14 19:23:54 +00:00
|
|
|
Discovery discovery.Config
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
2018-07-30 08:38:31 +01:00
|
|
|
setupCfg struct {
|
2018-08-13 09:39:45 +01:00
|
|
|
CA provider.CASetupConfig
|
|
|
|
Identity provider.IdentitySetupConfig
|
|
|
|
Overwrite bool `default:"false" help:"whether to overwrite pre-existing configuration files"`
|
2018-07-30 08:38:31 +01:00
|
|
|
}
|
2018-11-08 13:20:23 +00:00
|
|
|
diagCfg struct {
|
2018-12-07 09:59:31 +00:00
|
|
|
Database string `help:"satellite database connection string" default:"sqlite3://$CONFDIR/master.db"`
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|
2018-11-16 13:31:33 +00:00
|
|
|
qdiagCfg struct {
|
2018-12-21 15:11:19 +00:00
|
|
|
Database string `help:"satellite database connection string" default:"sqlite3://$CONFDIR/master.db"`
|
|
|
|
QListLimit int `help:"maximum segments that can be requested" default:"1000"`
|
2018-11-16 13:31:33 +00:00
|
|
|
}
|
2018-07-30 08:38:31 +01:00
|
|
|
|
2018-12-03 15:51:56 +00:00
|
|
|
defaultConfDir string
|
2018-12-14 21:14:59 +00:00
|
|
|
confDir *string
|
2018-07-26 15:21:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-12-03 15:51:56 +00:00
|
|
|
defaultConfDir = fpath.ApplicationDir("storj", "satellite")
|
2018-12-14 21:14:59 +00:00
|
|
|
|
|
|
|
dirParam := cfgstruct.FindConfigDirParam()
|
|
|
|
if dirParam != "" {
|
|
|
|
defaultConfDir = dirParam
|
|
|
|
}
|
|
|
|
|
|
|
|
confDir = rootCmd.PersistentFlags().String("config-dir", defaultConfDir, "main directory for satellite configuration")
|
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
rootCmd.AddCommand(setupCmd)
|
2018-11-08 13:20:23 +00:00
|
|
|
rootCmd.AddCommand(diagCmd)
|
2018-11-16 13:31:33 +00:00
|
|
|
rootCmd.AddCommand(qdiagCmd)
|
2018-07-30 08:38:31 +01:00
|
|
|
cfgstruct.Bind(runCmd.Flags(), &runCfg, cfgstruct.ConfDir(defaultConfDir))
|
|
|
|
cfgstruct.Bind(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir))
|
2018-11-15 19:06:09 +00:00
|
|
|
cfgstruct.Bind(diagCmd.Flags(), &diagCfg, cfgstruct.ConfDir(defaultConfDir))
|
2018-11-16 13:31:33 +00:00
|
|
|
cfgstruct.Bind(qdiagCmd.Flags(), &qdiagCfg, cfgstruct.ConfDir(defaultConfDir))
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
2018-12-05 09:35:50 +00:00
|
|
|
ctx := process.Ctx(cmd)
|
|
|
|
|
2018-12-11 09:30:09 +00:00
|
|
|
database, err := satellitedb.New(runCfg.Database)
|
2018-12-05 09:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error starting master database on satellite: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = database.CreateTables()
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error creating tables for master database on satellite: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint ignoring context rules to not create cyclic dependency, will be removed later
|
|
|
|
ctx = context.WithValue(ctx, "masterdb", database)
|
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
return runCfg.Identity.Run(
|
2018-12-05 09:35:50 +00:00
|
|
|
ctx,
|
2018-10-09 15:39:14 +01:00
|
|
|
grpcauth.NewAPIKeyInterceptor(),
|
|
|
|
runCfg.Kademlia,
|
2018-12-04 21:10:23 +00:00
|
|
|
runCfg.Overlay,
|
2018-12-17 19:27:19 +00:00
|
|
|
runCfg.PointerDB,
|
2018-11-20 15:54:22 +00:00
|
|
|
runCfg.Checker,
|
|
|
|
runCfg.Repairer,
|
2018-12-07 18:52:58 +00:00
|
|
|
runCfg.Audit,
|
2018-11-12 21:59:30 +00:00
|
|
|
runCfg.BwAgreement,
|
2018-12-14 19:23:54 +00:00
|
|
|
runCfg.Discovery,
|
2018-10-09 15:39:14 +01:00
|
|
|
)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
2018-12-14 21:14:59 +00:00
|
|
|
setupDir, err := filepath.Abs(*confDir)
|
2018-08-13 19:29:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-14 21:14:59 +00:00
|
|
|
valid, err := fpath.IsValidSetupDir(setupDir)
|
2018-12-04 11:42:16 +00:00
|
|
|
if !setupCfg.Overwrite && !valid {
|
2018-12-20 13:27:51 +00:00
|
|
|
return fmt.Errorf("satellite configuration already exists (%v). Rerun with --overwrite", setupDir)
|
2018-10-19 16:06:02 +01:00
|
|
|
} else if setupCfg.Overwrite && err == nil {
|
|
|
|
fmt.Println("overwriting existing satellite config")
|
2018-12-14 21:14:59 +00:00
|
|
|
err = os.RemoveAll(setupDir)
|
2018-10-19 16:06:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-08 23:22:59 +01:00
|
|
|
}
|
|
|
|
|
2018-12-14 21:14:59 +00:00
|
|
|
err = os.MkdirAll(setupDir, 0700)
|
2018-07-26 15:21:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
// TODO: handle setting base path *and* identity file paths via args
|
|
|
|
// NB: if base path is set this overrides identity and CA path options
|
2018-12-14 21:14:59 +00:00
|
|
|
if setupDir != defaultConfDir {
|
|
|
|
setupCfg.CA.CertPath = filepath.Join(setupDir, "ca.cert")
|
|
|
|
setupCfg.CA.KeyPath = filepath.Join(setupDir, "ca.key")
|
|
|
|
setupCfg.Identity.CertPath = filepath.Join(setupDir, "identity.cert")
|
|
|
|
setupCfg.Identity.KeyPath = filepath.Join(setupDir, "identity.key")
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
err = provider.SetupIdentity(process.Ctx(cmd), setupCfg.CA, setupCfg.Identity)
|
2018-07-26 15:21:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
o := map[string]interface{}{
|
2018-08-13 19:29:13 +01:00
|
|
|
"identity.cert-path": setupCfg.Identity.CertPath,
|
|
|
|
"identity.key-path": setupCfg.Identity.KeyPath,
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
return process.SaveConfig(runCmd.Flags(),
|
2018-12-14 21:14:59 +00:00
|
|
|
filepath.Join(setupDir, "config.yaml"), o)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:20:23 +00:00
|
|
|
func cmdDiag(cmd *cobra.Command, args []string) (err error) {
|
2018-12-11 09:30:09 +00:00
|
|
|
database, err := satellitedb.New(diagCfg.Database)
|
2018-11-15 19:06:09 +00:00
|
|
|
if err != nil {
|
2018-12-07 09:59:31 +00:00
|
|
|
return errs.New("error connecting to master database on satellite: %+v", err)
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|
2018-12-07 09:59:31 +00:00
|
|
|
defer func() {
|
|
|
|
err := database.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error closing connection to master database on satellite: %+v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-12 21:59:30 +00:00
|
|
|
|
2018-12-07 09:59:31 +00:00
|
|
|
//get all bandwidth agreements rows already ordered
|
|
|
|
baRows, err := database.BandwidthAgreement().GetAgreements(context.Background())
|
2018-11-08 13:20:23 +00:00
|
|
|
if err != nil {
|
2018-12-07 09:59:31 +00:00
|
|
|
fmt.Printf("error reading satellite database %v: %v\n", diagCfg.Database, err)
|
2018-11-08 13:20:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Agreement is a struct that contains a bandwidth agreement and the associated signature
|
|
|
|
type UplinkSummary struct {
|
|
|
|
TotalBytes int64
|
|
|
|
PutActionCount int64
|
|
|
|
GetActionCount int64
|
|
|
|
TotalTransactions int64
|
|
|
|
// additional attributes add here ...
|
|
|
|
}
|
|
|
|
|
|
|
|
// attributes per uplinkid
|
2018-11-29 18:39:27 +00:00
|
|
|
summaries := make(map[storj.NodeID]*UplinkSummary)
|
|
|
|
uplinkIDs := storj.NodeIDList{}
|
2018-11-08 13:20:23 +00:00
|
|
|
|
|
|
|
for _, baRow := range baRows {
|
|
|
|
// deserializing rbad you get payerbwallocation, total & storage node id
|
|
|
|
rbad := &pb.RenterBandwidthAllocation_Data{}
|
2018-12-07 09:59:31 +00:00
|
|
|
if err := proto.Unmarshal(baRow.Agreement, rbad); err != nil {
|
2018-11-08 13:20:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// deserializing pbad you get satelliteID, uplinkID, max size, exp, serial# & action
|
|
|
|
pbad := &pb.PayerBandwidthAllocation_Data{}
|
|
|
|
if err := proto.Unmarshal(rbad.GetPayerAllocation().GetData(), pbad); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
uplinkID := pbad.UplinkId
|
2018-11-08 13:20:23 +00:00
|
|
|
summary, ok := summaries[uplinkID]
|
|
|
|
if !ok {
|
|
|
|
summaries[uplinkID] = &UplinkSummary{}
|
|
|
|
uplinkIDs = append(uplinkIDs, uplinkID)
|
|
|
|
summary = summaries[uplinkID]
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill the summary info
|
|
|
|
summary.TotalBytes += rbad.GetTotal()
|
|
|
|
summary.TotalTransactions++
|
|
|
|
if pbad.GetAction() == pb.PayerBandwidthAllocation_PUT {
|
|
|
|
summary.PutActionCount++
|
|
|
|
} else {
|
|
|
|
summary.GetActionCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the table header (fields)
|
|
|
|
const padding = 3
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', tabwriter.AlignRight|tabwriter.Debug)
|
|
|
|
fmt.Fprintln(w, "UplinkID\tTotal\t# Of Transactions\tPUT Action\tGET Action\t")
|
|
|
|
|
|
|
|
// populate the row fields
|
2018-11-29 18:39:27 +00:00
|
|
|
sort.Sort(uplinkIDs)
|
2018-11-08 13:20:23 +00:00
|
|
|
for _, uplinkID := range uplinkIDs {
|
|
|
|
summary := summaries[uplinkID]
|
|
|
|
fmt.Fprint(w, uplinkID, "\t", summary.TotalBytes, "\t", summary.TotalTransactions, "\t", summary.PutActionCount, "\t", summary.GetActionCount, "\t\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// display the data
|
2018-11-16 13:31:33 +00:00
|
|
|
return w.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdQDiag(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
// open the master db
|
|
|
|
database, err := satellitedb.New(qdiagCfg.Database)
|
2018-11-16 13:31:33 +00:00
|
|
|
if err != nil {
|
2018-12-21 15:11:19 +00:00
|
|
|
return errs.New("error connecting to master database on satellite: %+v", err)
|
2018-11-16 13:31:33 +00:00
|
|
|
}
|
2018-12-21 15:11:19 +00:00
|
|
|
defer func() {
|
|
|
|
err := database.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error closing connection to master database on satellite: %+v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-16 13:31:33 +00:00
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
list, err := database.RepairQueue().Peekqueue(qdiagCfg.QListLimit)
|
2018-11-16 13:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the table header (fields)
|
|
|
|
const padding = 3
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', tabwriter.AlignRight|tabwriter.Debug)
|
|
|
|
fmt.Fprintln(w, "Path\tLost Pieces\t")
|
|
|
|
|
|
|
|
// populate the row fields
|
|
|
|
for _, v := range list {
|
|
|
|
fmt.Fprint(w, v.GetPath(), "\t", v.GetLostPieces(), "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
// display the data
|
|
|
|
return w.Flush()
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 15:21:35 +01:00
|
|
|
func main() {
|
2018-07-30 08:38:31 +01:00
|
|
|
process.Exec(rootCmd)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|