cmd/satellite: proper log usage

log.Fatal immediately terminates the program without running any defers.
We should properly close all the services and databases.

Change-Id: I5e959cef3eafedeacb3a2062e3da47e8d04e8e75
This commit is contained in:
Egon Elbre 2020-10-13 16:49:33 +03:00
parent 2268cc1df3
commit cf2dd76db7
9 changed files with 30 additions and 23 deletions

View File

@ -22,7 +22,8 @@ func cmdAdminRun(cmd *cobra.Command, args []string) (err error) {
identity, err := runCfg.Identity.Load() identity, err := runCfg.Identity.Load()
if err != nil { if err != nil {
log.Fatal("Failed to load identity.", zap.Error(err)) log.Error("Failed to load identity.", zap.Error(err))
return errs.New("Failed to load identity: %+v", err)
} }
db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{ db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{
@ -51,7 +52,7 @@ func cmdAdminRun(cmd *cobra.Command, args []string) (err error) {
err = db.CheckVersion(ctx) err = db.CheckVersion(ctx)
if err != nil { if err != nil {
log.Fatal("Failed satellite database version check.", zap.Error(err)) log.Error("Failed satellite database version check.", zap.Error(err))
return errs.New("Error checking version for satellitedb: %+v", err) return errs.New("Error checking version for satellitedb: %+v", err)
} }

View File

@ -27,7 +27,8 @@ func cmdAPIRun(cmd *cobra.Command, args []string) (err error) {
identity, err := runCfg.Identity.Load() identity, err := runCfg.Identity.Load()
if err != nil { if err != nil {
log.Fatal("Failed to load identity.", zap.Error(err)) log.Error("Failed to load identity.", zap.Error(err))
return errs.New("Failed to load identity: %+v", err)
} }
db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{ db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{
@ -91,7 +92,7 @@ func cmdAPIRun(cmd *cobra.Command, args []string) (err error) {
err = db.CheckVersion(ctx) err = db.CheckVersion(ctx)
if err != nil { if err != nil {
log.Fatal("Failed satellite database version check.", zap.Error(err)) log.Error("Failed satellite database version check.", zap.Error(err))
return errs.New("Error checking version for satellitedb: %+v", err) return errs.New("Error checking version for satellitedb: %+v", err)
} }

View File

@ -24,7 +24,8 @@ func cmdGCRun(cmd *cobra.Command, args []string) (err error) {
identity, err := runCfg.Identity.Load() identity, err := runCfg.Identity.Load()
if err != nil { if err != nil {
log.Fatal("Failed to load identity.", zap.Error(err)) log.Error("Failed to load identity.", zap.Error(err))
return errs.New("Failed to load identity: %+v", err)
} }
db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{}) db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{})
@ -72,8 +73,8 @@ func cmdGCRun(cmd *cobra.Command, args []string) (err error) {
err = db.CheckVersion(ctx) err = db.CheckVersion(ctx)
if err != nil { if err != nil {
log.Fatal("Failed satellite database version check for GC.", zap.Error(err)) log.Error("Failed satellite database version check.", zap.Error(err))
return errs.New("Error checking version for satellitedb for GC: %+v", err) return errs.New("Error checking version for satellitedb: %+v", err)
} }
runError := peer.Run(ctx) runError := peer.Run(ctx)

View File

@ -346,7 +346,8 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
identity, err := runCfg.Identity.Load() identity, err := runCfg.Identity.Load()
if err != nil { if err != nil {
log.Fatal("Failed to load identity.", zap.Error(err)) log.Error("Failed to load identity.", zap.Error(err))
return errs.New("Failed to load identity: %+v", err)
} }
db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{ db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{
@ -410,7 +411,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
err = db.CheckVersion(ctx) err = db.CheckVersion(ctx)
if err != nil { if err != nil {
log.Fatal("Failed satellite database version check.", zap.Error(err)) log.Error("Failed satellite database version check.", zap.Error(err))
return errs.New("Error checking version for satellitedb: %+v", err) return errs.New("Error checking version for satellitedb: %+v", err)
} }

View File

@ -7,7 +7,6 @@ import (
"bufio" "bufio"
"context" "context"
"encoding/hex" "encoding/hex"
"log"
"os" "os"
"github.com/zeebo/errs" "github.com/zeebo/errs"
@ -59,14 +58,15 @@ func runMetainfoCmd(ctx context.Context, cmdFunc func(*metainfo.Service) error)
} }
func runVerifierCmd(ctx context.Context, cmdFunc func(*audit.Verifier) error) error { func runVerifierCmd(ctx context.Context, cmdFunc func(*audit.Verifier) error) error {
logger := zap.L() log := zap.L()
identity, err := dryRunCfg.Identity.Load() identity, err := dryRunCfg.Identity.Load()
if err != nil { if err != nil {
log.Fatal("Failed to load identity.", zap.Error(err)) log.Error("Failed to load identity.", zap.Error(err))
return errs.New("Failed to load identity: %+v", err)
} }
db, err := satellitedb.New(logger.Named("db"), dryRunCfg.Database, satellitedb.Options{}) db, err := satellitedb.New(log.Named("db"), dryRunCfg.Database, satellitedb.Options{})
if err != nil { if err != nil {
return errs.New("error connecting to master database on satellite: %+v", err) return errs.New("error connecting to master database on satellite: %+v", err)
} }
@ -79,7 +79,7 @@ func runVerifierCmd(ctx context.Context, cmdFunc func(*audit.Verifier) error) er
return errs.New("Error checking version for satellitedb: %+v", err) return errs.New("Error checking version for satellitedb: %+v", err)
} }
pointerDB, err := metainfo.NewStore(logger.Named("pointerdb"), dryRunCfg.Metainfo.DatabaseURL) pointerDB, err := metainfo.NewStore(log.Named("pointerdb"), dryRunCfg.Metainfo.DatabaseURL)
if err != nil { if err != nil {
return errs.New("Error creating metainfo database connection: %+v", err) return errs.New("Error creating metainfo database connection: %+v", err)
} }
@ -103,19 +103,19 @@ func runVerifierCmd(ctx context.Context, cmdFunc func(*audit.Verifier) error) er
dialer := rpc.NewDefaultDialer(tlsOptions) dialer := rpc.NewDefaultDialer(tlsOptions)
metainfoService := metainfo.NewService( metainfoService := metainfo.NewService(
logger.Named("metainfo:service"), log.Named("metainfo:service"),
pointerDB, pointerDB,
db.Buckets(), db.Buckets(),
) )
overlayService := overlay.NewService( overlayService := overlay.NewService(
logger.Named("overlay"), log.Named("overlay"),
db.OverlayCache(), db.OverlayCache(),
runCfg.Overlay, runCfg.Overlay,
) )
ordersService, err := orders.NewService( ordersService, err := orders.NewService(
logger.Named("orders:service"), log.Named("orders:service"),
signing.SignerFromFullIdentity(identity), signing.SignerFromFullIdentity(identity),
overlayService, overlayService,
db.Orders(), db.Orders(),
@ -131,7 +131,7 @@ func runVerifierCmd(ctx context.Context, cmdFunc func(*audit.Verifier) error) er
} }
verifier := audit.NewVerifier( verifier := audit.NewVerifier(
logger.Named("audit:verifier"), log.Named("audit:verifier"),
metainfoService, metainfoService,
dialer, dialer,
overlayService, overlayService,

View File

@ -26,7 +26,8 @@ func cmdRepairerRun(cmd *cobra.Command, args []string) (err error) {
identity, err := runCfg.Identity.Load() identity, err := runCfg.Identity.Load()
if err != nil { if err != nil {
log.Fatal("Failed to load identity.", zap.Error(err)) log.Error("Failed to load identity.", zap.Error(err))
return errs.New("Failed to load identity: %+v", err)
} }
db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{}) db, err := satellitedb.New(log.Named("db"), runCfg.Database, satellitedb.Options{})
@ -92,7 +93,7 @@ func cmdRepairerRun(cmd *cobra.Command, args []string) (err error) {
err = db.CheckVersion(ctx) err = db.CheckVersion(ctx)
if err != nil { if err != nil {
log.Fatal("Failed satellite database version check.", zap.Error(err)) log.Error("Failed satellite database version check.", zap.Error(err))
return errs.New("Error checking version for satellitedb: %+v", err) return errs.New("Error checking version for satellitedb: %+v", err)
} }

View File

@ -140,7 +140,8 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
identity, err := runCfg.Identity.Load() identity, err := runCfg.Identity.Load()
if err != nil { if err != nil {
log.Fatal("Failed to load identity.", zap.Error(err)) log.Error("Failed to load identity.", zap.Error(err))
return errs.New("Failed to load identity: %+v", err)
} }
if err := runCfg.Verify(log); err != nil { if err := runCfg.Verify(log); err != nil {

View File

@ -738,7 +738,7 @@ func (peer *Peer) Run(ctx context.Context) (err error) {
} }
if err := peer.Preflight.LocalTime.Check(ctx); err != nil { if err := peer.Preflight.LocalTime.Check(ctx); err != nil {
peer.Log.Fatal("Failed preflight check.", zap.Error(err)) peer.Log.Error("Failed preflight check.", zap.Error(err))
return err return err
} }

View File

@ -180,7 +180,8 @@ func New(log *zap.Logger, config *Config) (peer *Peer, err error) {
peer.response, err = json.Marshal(peer.Versions) peer.response, err = json.Marshal(peer.Versions)
if err != nil { if err != nil {
peer.Log.Fatal("Error marshalling version info.", zap.Error(err)) peer.Log.Error("Error marshalling version info.", zap.Error(err))
return nil, RolloutErr.Wrap(err)
} }
peer.Log.Debug("Setting version info.", zap.ByteString("Value", peer.response)) peer.Log.Debug("Setting version info.", zap.ByteString("Value", peer.response))