2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-07-26 15:21:35 +01:00
|
|
|
// 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
|
|
|
"text/tabwriter"
|
2019-01-30 21:44:50 +00:00
|
|
|
"time"
|
2018-07-26 15:21:35 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2018-11-15 19:06:09 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-01-15 15:02:54 +00:00
|
|
|
"go.uber.org/zap"
|
2018-10-04 22:40:34 +01:00
|
|
|
|
2018-12-03 15:51:56 +00:00
|
|
|
"storj.io/storj/internal/fpath"
|
2019-04-03 20:13:39 +01:00
|
|
|
"storj.io/storj/internal/version"
|
2018-10-16 12:43:44 +01:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
2018-07-26 15:21:35 +01:00
|
|
|
"storj.io/storj/pkg/process"
|
2019-01-23 19:58:44 +00:00
|
|
|
"storj.io/storj/satellite"
|
2018-12-05 09:35:50 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
2018-07-26 15:21:35 +01:00
|
|
|
)
|
|
|
|
|
2019-01-07 11:06:10 +00:00
|
|
|
// Satellite defines satellite configuration
|
2019-01-07 09:48:16 +00:00
|
|
|
type Satellite struct {
|
2019-01-23 19:58:44 +00:00
|
|
|
Database string `help:"satellite database connection string" default:"sqlite3://$CONFDIR/master.db"`
|
|
|
|
|
|
|
|
satellite.Config
|
2019-01-07 09:48:16 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
2019-01-30 21:44:50 +00:00
|
|
|
reportsCmd = &cobra.Command{
|
|
|
|
Use: "reports",
|
|
|
|
Short: "Generate a report",
|
|
|
|
}
|
2019-02-27 21:55:19 +00:00
|
|
|
nodeUsageCmd = &cobra.Command{
|
|
|
|
Use: "storagenode-usage [start] [end]",
|
|
|
|
Short: "Generate a node usage report for a given period to use for payments",
|
|
|
|
Long: "Generate a node usage report for a given period to use for payments. Format dates using YYYY-MM-DD",
|
2019-01-30 21:44:50 +00:00
|
|
|
Args: cobra.MinimumNArgs(2),
|
2019-02-27 21:55:19 +00:00
|
|
|
RunE: cmdNodeUsage,
|
2019-01-30 21:44:50 +00:00
|
|
|
}
|
2018-07-26 15:21:35 +01:00
|
|
|
|
2019-01-07 09:48:16 +00:00
|
|
|
runCfg Satellite
|
2019-01-07 11:06:10 +00:00
|
|
|
setupCfg Satellite
|
2019-01-07 09:48:16 +00: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
|
|
|
}
|
2019-02-27 21:55:19 +00:00
|
|
|
nodeUsageCfg struct {
|
2019-01-30 21:44:50 +00:00
|
|
|
Database string `help:"satellite database connection string" default:"sqlite3://$CONFDIR/master.db"`
|
|
|
|
Output string `help:"destination of report output" default:""`
|
|
|
|
}
|
2019-03-12 12:51:06 +00:00
|
|
|
confDir string
|
|
|
|
identityDir string
|
2018-07-26 15:21:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-03-12 12:51:06 +00:00
|
|
|
defaultConfDir := fpath.ApplicationDir("storj", "satellite")
|
|
|
|
defaultIdentityDir := fpath.ApplicationDir("storj", "identity", "satellite")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for satellite configuration")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &identityDir, "identity-dir", defaultIdentityDir, "main directory for satellite identity credentials")
|
2019-04-19 19:17:30 +01:00
|
|
|
defaults := cfgstruct.DefaultsFlag(rootCmd)
|
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)
|
2019-01-30 21:44:50 +00:00
|
|
|
rootCmd.AddCommand(reportsCmd)
|
2019-02-27 21:55:19 +00:00
|
|
|
reportsCmd.AddCommand(nodeUsageCmd)
|
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))
|
|
|
|
cfgstruct.Bind(diagCmd.Flags(), &diagCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
|
|
|
cfgstruct.Bind(qdiagCmd.Flags(), &qdiagCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
|
|
|
cfgstruct.Bind(nodeUsageCmd.Flags(), &nodeUsageCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2018-07-26 15:21:35 +01: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 19:58:44 +00:00
|
|
|
log := zap.L()
|
|
|
|
|
2019-01-25 14:54:54 +00:00
|
|
|
identity, err := runCfg.Identity.Load()
|
2019-01-23 19:58:44 +00:00
|
|
|
if err != nil {
|
2019-01-22 12:35:48 +00:00
|
|
|
zap.S().Fatal(err)
|
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2019-02-14 21:55:21 +00:00
|
|
|
db, err := satellitedb.New(log.Named("db"), runCfg.Database)
|
2018-12-05 09:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error starting master database on satellite: %+v", err)
|
|
|
|
}
|
2019-01-24 20:28:06 +00:00
|
|
|
|
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 := satellite.New(log, identity, db, &runCfg.Config, version.Build)
|
2018-12-05 09:35:50 +00:00
|
|
|
if err != nil {
|
2019-04-04 16:40:07 +01:00
|
|
|
return err
|
2018-12-05 09:35:50 +00:00
|
|
|
}
|
2019-01-23 19:58:44 +00:00
|
|
|
|
2019-04-04 16:40:07 +01:00
|
|
|
// okay, start doing stuff ====
|
|
|
|
|
|
|
|
err = peer.Version.CheckVersion(ctx)
|
2019-01-23 19:58:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-15 15:02:54 +00:00
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
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 satellite: %+v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
runError := peer.Run(ctx)
|
|
|
|
closeError := peer.Close()
|
2019-01-30 05:22:58 +00:00
|
|
|
return errs.Combine(runError, closeError)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
2019-01-22 12:35:48 +00:00
|
|
|
setupDir, err := filepath.Abs(confDir)
|
2018-08-13 19:29:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:57:58 +00:00
|
|
|
valid, _ := fpath.IsValidSetupDir(setupDir)
|
|
|
|
if !valid {
|
|
|
|
return fmt.Errorf("satellite configuration already exists (%v)", setupDir)
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-22 12:35:48 +00:00
|
|
|
return process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), nil)
|
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) {
|
2019-02-14 21:55:21 +00:00
|
|
|
database, err := satellitedb.New(zap.L().Named("db"), 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
|
2019-02-01 18:50:12 +00:00
|
|
|
stats, err := database.BandwidthAgreement().GetUplinkStats(context.Background(), time.Time{}, time.Now())
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-02-01 18:50:12 +00:00
|
|
|
for _, s := range stats {
|
|
|
|
fmt.Fprint(w, s.NodeID, "\t", s.TotalBytes, "\t", s.TotalTransactions, "\t", s.PutActionCount, "\t", s.GetActionCount, "\t\n")
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-02-14 21:55:21 +00:00
|
|
|
database, err := satellitedb.New(zap.L().Named("db"), 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
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
list, err := database.RepairQueue().SelectN(context.Background(), 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
|
|
|
}
|
|
|
|
|
2019-02-27 21:55:19 +00:00
|
|
|
func cmdNodeUsage(cmd *cobra.Command, args []string) (err error) {
|
2019-01-30 21:44:50 +00:00
|
|
|
ctx := process.Ctx(cmd)
|
|
|
|
|
|
|
|
layout := "2006-01-02"
|
|
|
|
start, err := time.Parse(layout, args[0])
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("Invalid date format. Please use YYYY-MM-DD")
|
|
|
|
}
|
|
|
|
end, err := time.Parse(layout, args[1])
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("Invalid date format. Please use YYYY-MM-DD")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that start date is not after end date
|
|
|
|
if start.After(end) {
|
|
|
|
return errs.New("Invalid time period (%v) - (%v)", start, end)
|
|
|
|
}
|
|
|
|
|
|
|
|
// send output to stdout
|
2019-02-27 21:55:19 +00:00
|
|
|
if nodeUsageCfg.Output == "" {
|
2019-01-30 21:44:50 +00:00
|
|
|
return generateCSV(ctx, start, end, os.Stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// send output to file
|
2019-02-27 21:55:19 +00:00
|
|
|
file, err := os.Create(nodeUsageCfg.Output)
|
2019-01-30 21:44:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, file.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
return generateCSV(ctx, start, end, file)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|