2019-10-08 19:26:53 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Deprecated contains deprecated config structs.
|
2019-10-08 19:26:53 +01:00
|
|
|
type Deprecated struct {
|
|
|
|
Kademlia struct {
|
|
|
|
ExternalAddress string `default:"" hidden:"true"`
|
|
|
|
Operator struct {
|
|
|
|
Email string `default:"" hidden:"true"`
|
|
|
|
Wallet string `default:"" hidden:"true"`
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 17:19:37 +01:00
|
|
|
Storage2 struct {
|
|
|
|
Monitor struct {
|
|
|
|
VerifyDirInterval string `default:"" hidden:"true"`
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 19:26:53 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// maps deprecated config values to new values if applicable.
|
2023-04-05 22:05:07 +01:00
|
|
|
func mapDeprecatedConfigs(log *zap.Logger, cfg *StorageNodeFlags) {
|
|
|
|
if cfg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:26:53 +01:00
|
|
|
type migration struct {
|
|
|
|
newValue interface{}
|
|
|
|
newConfigString string
|
|
|
|
oldValue string
|
|
|
|
oldConfigString string
|
|
|
|
}
|
|
|
|
migrations := []migration{
|
|
|
|
{
|
2023-04-05 22:05:07 +01:00
|
|
|
newValue: &cfg.Contact.ExternalAddress,
|
2019-10-08 19:26:53 +01:00
|
|
|
newConfigString: "contact.external-address",
|
2023-04-05 22:05:07 +01:00
|
|
|
oldValue: cfg.Deprecated.Kademlia.ExternalAddress,
|
2019-10-08 19:26:53 +01:00
|
|
|
oldConfigString: "kademlia.external-address",
|
|
|
|
},
|
|
|
|
{
|
2023-04-05 22:05:07 +01:00
|
|
|
newValue: &cfg.Operator.Wallet,
|
2019-10-08 19:26:53 +01:00
|
|
|
newConfigString: "operator.wallet",
|
2023-04-05 22:05:07 +01:00
|
|
|
oldValue: cfg.Deprecated.Kademlia.Operator.Wallet,
|
2019-10-08 19:26:53 +01:00
|
|
|
oldConfigString: "kademlia.operator.wallet",
|
|
|
|
},
|
|
|
|
{
|
2023-04-05 22:05:07 +01:00
|
|
|
newValue: &cfg.Operator.Email,
|
2019-10-08 19:26:53 +01:00
|
|
|
newConfigString: "operator.email",
|
2023-04-05 22:05:07 +01:00
|
|
|
oldValue: cfg.Deprecated.Kademlia.Operator.Email,
|
2019-10-08 19:26:53 +01:00
|
|
|
oldConfigString: "kademlia.operator.email",
|
|
|
|
},
|
2020-08-07 17:19:37 +01:00
|
|
|
{
|
2023-04-05 22:05:07 +01:00
|
|
|
newValue: &cfg.Config.Storage2.Monitor.VerifyDirReadableInterval,
|
2020-08-07 17:19:37 +01:00
|
|
|
newConfigString: "storage2.monitor.verify-dir-readable-interval",
|
2023-04-05 22:05:07 +01:00
|
|
|
oldValue: cfg.Deprecated.Storage2.Monitor.VerifyDirInterval,
|
2020-08-07 17:19:37 +01:00
|
|
|
oldConfigString: "storage2.monitor.verify-dir-interval",
|
|
|
|
},
|
2019-10-08 19:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, migration := range migrations {
|
|
|
|
if migration.oldValue != "" {
|
|
|
|
typ := reflect.TypeOf(migration.newValue).Elem()
|
|
|
|
override := parseOverride(typ, migration.oldValue)
|
|
|
|
|
|
|
|
reflect.ValueOf(migration.newValue).Elem().Set(reflect.ValueOf(override))
|
2020-04-13 10:31:17 +01:00
|
|
|
log.Debug("Found deprecated flag. Migrating value.",
|
|
|
|
zap.Stringer("Value", reflect.ValueOf(migration.newValue).Elem()),
|
|
|
|
zap.String("From", migration.oldConfigString),
|
|
|
|
zap.String("To", migration.newConfigString),
|
|
|
|
)
|
2019-10-08 19:26:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseOverride(typ reflect.Type, value string) interface{} {
|
|
|
|
switch typ {
|
|
|
|
case reflect.TypeOf(int(0)):
|
|
|
|
val, err := strconv.ParseInt(value, 0, strconv.IntSize)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return int(val)
|
|
|
|
case reflect.TypeOf(int64(0)):
|
|
|
|
val, err := strconv.ParseInt(value, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
case reflect.TypeOf(uint(0)):
|
|
|
|
val, err := strconv.ParseUint(value, 0, strconv.IntSize)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return uint(val)
|
|
|
|
case reflect.TypeOf(uint64(0)):
|
|
|
|
val, err := strconv.ParseUint(value, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
case reflect.TypeOf(time.Duration(0)):
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
case reflect.TypeOf(float64(0)):
|
|
|
|
val, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
case reflect.TypeOf(string("")):
|
|
|
|
return value
|
|
|
|
case reflect.TypeOf(bool(false)):
|
|
|
|
val, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid field type: %s", typ.String()))
|
|
|
|
}
|
|
|
|
}
|