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-11-15 19:06:09 +00:00
|
|
|
"net/url"
|
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-08 13:20:23 +00:00
|
|
|
"github.com/golang/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-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-11-16 13:31:33 +00:00
|
|
|
dbmanager "storj.io/storj/pkg/bwagreement/database-manager"
|
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"
|
2018-11-16 13:31:33 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
2018-11-20 15:54:22 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/repairer"
|
2018-07-26 15:21:35 +01:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-10-08 23:15:54 +01:00
|
|
|
mockOverlay "storj.io/storj/pkg/overlay/mocks"
|
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-10-08 23:15:54 +01:00
|
|
|
"storj.io/storj/pkg/statdb"
|
2018-11-16 13:31:33 +00:00
|
|
|
"storj.io/storj/storage/redis"
|
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{
|
|
|
|
Use: "setup",
|
|
|
|
Short: "Create config files",
|
|
|
|
RunE: cmdSetup,
|
|
|
|
}
|
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-11-15 19:06:09 +00:00
|
|
|
Identity provider.IdentityConfig
|
|
|
|
Kademlia kademlia.Config
|
|
|
|
PointerDB pointerdb.Config
|
2018-08-20 19:24:11 +01:00
|
|
|
Overlay overlay.Config
|
2018-10-08 23:15:54 +01:00
|
|
|
MockOverlay mockOverlay.Config
|
|
|
|
StatDB statdb.Config
|
2018-11-20 15:54:22 +00:00
|
|
|
Checker checker.Config
|
|
|
|
Repairer repairer.Config
|
2018-11-07 01:16:43 +00:00
|
|
|
// Audit audit.Config
|
2018-11-12 21:59:30 +00:00
|
|
|
BwAgreement bwagreement.Config
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
2018-07-30 08:38:31 +01:00
|
|
|
setupCfg struct {
|
2018-08-08 23:22:59 +01:00
|
|
|
BasePath string `default:"$CONFDIR" help:"base path for setup"`
|
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-11-15 19:06:09 +00:00
|
|
|
DatabaseURL string `help:"the database connection string to use" default:"sqlite3://$CONFDIR/bw.db"`
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|
2018-11-16 13:31:33 +00:00
|
|
|
qdiagCfg struct {
|
|
|
|
DatabaseURL string `help:"the database connection string to use" default:"redis://127.0.0.1:6378?db=1&password=abc123"`
|
|
|
|
QListLimit int `help:"maximum segments that can be requested" default:"1000"`
|
|
|
|
}
|
2018-07-30 08:38:31 +01:00
|
|
|
|
2018-08-29 19:32:41 +01:00
|
|
|
defaultConfDir = "$HOME/.storj/satellite"
|
2018-07-26 15:21:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
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-08-20 19:24:11 +01:00
|
|
|
var o provider.Responsibility = runCfg.Overlay
|
|
|
|
if runCfg.MockOverlay.Nodes != "" {
|
|
|
|
o = runCfg.MockOverlay
|
|
|
|
}
|
2018-10-09 15:39:14 +01:00
|
|
|
return runCfg.Identity.Run(
|
|
|
|
process.Ctx(cmd),
|
|
|
|
grpcauth.NewAPIKeyInterceptor(),
|
|
|
|
runCfg.Kademlia,
|
|
|
|
runCfg.PointerDB,
|
|
|
|
o,
|
|
|
|
runCfg.StatDB,
|
2018-11-20 15:54:22 +00:00
|
|
|
runCfg.Checker,
|
|
|
|
runCfg.Repairer,
|
2018-11-07 01:16:43 +00:00
|
|
|
// runCfg.Audit,
|
2018-11-12 21:59:30 +00:00
|
|
|
runCfg.BwAgreement,
|
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-08-13 19:29:13 +01:00
|
|
|
setupCfg.BasePath, err = filepath.Abs(setupCfg.BasePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:22:59 +01:00
|
|
|
_, err = os.Stat(setupCfg.BasePath)
|
|
|
|
if !setupCfg.Overwrite && err == nil {
|
2018-08-29 19:32:41 +01:00
|
|
|
fmt.Println("An satellite configuration already exists. Rerun with --overwrite")
|
2018-08-08 23:22:59 +01:00
|
|
|
return nil
|
2018-10-19 16:06:02 +01:00
|
|
|
} else if setupCfg.Overwrite && err == nil {
|
|
|
|
fmt.Println("overwriting existing satellite config")
|
|
|
|
err = os.RemoveAll(setupCfg.BasePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-08 23:22:59 +01:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
err = os.MkdirAll(setupCfg.BasePath, 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
|
|
|
|
if setupCfg.BasePath != defaultConfDir {
|
|
|
|
setupCfg.CA.CertPath = filepath.Join(setupCfg.BasePath, "ca.cert")
|
|
|
|
setupCfg.CA.KeyPath = filepath.Join(setupCfg.BasePath, "ca.key")
|
|
|
|
setupCfg.Identity.CertPath = filepath.Join(setupCfg.BasePath, "identity.cert")
|
|
|
|
setupCfg.Identity.KeyPath = filepath.Join(setupCfg.BasePath, "identity.key")
|
|
|
|
}
|
|
|
|
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-08-13 09:39:45 +01:00
|
|
|
filepath.Join(setupCfg.BasePath, "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) {
|
|
|
|
// open the psql db
|
2018-11-15 19:06:09 +00:00
|
|
|
u, err := url.Parse(diagCfg.DatabaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("Invalid Database URL: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbm, err := dbmanager.NewDBManager(u.Scheme, u.Path)
|
2018-11-08 13:20:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-12 21:59:30 +00:00
|
|
|
|
2018-11-08 13:20:23 +00:00
|
|
|
//get all bandwidth aggrements rows already ordered
|
2018-11-12 21:59:30 +00:00
|
|
|
baRows, err := dbm.GetBandwidthAllocations(context.Background())
|
2018-11-08 13:20:23 +00:00
|
|
|
if err != nil {
|
2018-11-15 19:06:09 +00:00
|
|
|
fmt.Printf("error reading satellite database %v: %v\n", u.Path, 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
|
|
|
|
summaries := make(map[string]*UplinkSummary)
|
|
|
|
uplinkIDs := []string{}
|
|
|
|
|
|
|
|
for _, baRow := range baRows {
|
|
|
|
// deserializing rbad you get payerbwallocation, total & storage node id
|
|
|
|
rbad := &pb.RenterBandwidthAllocation_Data{}
|
|
|
|
if err := proto.Unmarshal(baRow.Data, rbad); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
uplinkID := string(pbad.GetUplinkId())
|
|
|
|
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
|
|
|
|
sort.Strings(uplinkIDs)
|
|
|
|
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) {
|
|
|
|
// open the redis db
|
|
|
|
dbpath := qdiagCfg.DatabaseURL
|
|
|
|
|
|
|
|
redisQ, err := redis.NewQueueFrom(dbpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
queue := queue.NewQueue(redisQ)
|
|
|
|
list, err := queue.Peekqueue(qdiagCfg.QListLimit)
|
|
|
|
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
|
|
|
runCmd.Flags().String("config",
|
|
|
|
filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
|
|
|
|
process.Exec(rootCmd)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|