From d7150d5556e3e74ad1f5cd866a8252f85a69ed8c Mon Sep 17 00:00:00 2001 From: carla Date: Wed, 12 Aug 2020 16:28:11 +0200 Subject: [PATCH] loopd: add data, loop and config options --- loopd/config.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ loopd/run.go | 23 ++++++++++++++------ loopd/utils.go | 19 +--------------- 3 files changed, 75 insertions(+), 25 deletions(-) diff --git a/loopd/config.go b/loopd/config.go index a694ef5..fad68db 100644 --- a/loopd/config.go +++ b/loopd/config.go @@ -1,10 +1,13 @@ package loopd import ( + "fmt" + "os" "path/filepath" "github.com/btcsuite/btcutil" "github.com/lightninglabs/loop/lsat" + "github.com/lightningnetwork/lnd/lncfg" ) var ( @@ -14,6 +17,7 @@ var ( defaultLogDirname = "logs" defaultLogFilename = "loopd.log" defaultLogDir = filepath.Join(loopDirBase, defaultLogDirname) + defaultConfigFile = filepath.Join(loopDirBase, defaultConfigFilename) defaultMaxLogFiles = 3 defaultMaxLogFileSize = 10 @@ -43,6 +47,9 @@ type Config struct { RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"` CORSOrigin string `long:"corsorigin" description:"The value to send in the Access-Control-Allow-Origin header. Header will be omitted if empty."` + LoopDir string `long:"loopdir" description:"The directory for all of loop's data."` + ConfigFile string `long:"configfile" description:"Path to configuration file."` + DataDir string `long:"datadir" description:"Directory for loopdb."` LogDir string `long:"logdir" description:"Directory to log output."` MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"` MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB"` @@ -74,6 +81,9 @@ func DefaultConfig() Config { Server: &loopServerConfig{ NoTLS: false, }, + LoopDir: loopDirBase, + ConfigFile: defaultConfigFile, + DataDir: loopDirBase, LogDir: defaultLogDir, MaxLogFiles: defaultMaxLogFiles, MaxLogFileSize: defaultMaxLogFileSize, @@ -86,3 +96,51 @@ func DefaultConfig() Config { }, } } + +// Validate cleans up paths in the config provided and validates it. +func Validate(cfg *Config) error { + // Cleanup any paths before we use them. + cfg.LoopDir = lncfg.CleanAndExpandPath(cfg.LoopDir) + cfg.DataDir = lncfg.CleanAndExpandPath(cfg.DataDir) + cfg.LogDir = lncfg.CleanAndExpandPath(cfg.LogDir) + + // Since our loop directory overrides our log/data dir values, make sure + // that they are not set when loop dir is set. We hard here rather than + // overwriting and potentially confusing the user. + logDirSet := cfg.LogDir != defaultLogDir + dataDirSet := cfg.DataDir != loopDirBase + loopDirSet := cfg.LoopDir != loopDirBase + + if loopDirSet { + if logDirSet { + return fmt.Errorf("loopdir overwrites logdir, please " + + "only set one value") + } + + if dataDirSet { + return fmt.Errorf("loopdir overwrites datadir, please " + + "only set one value") + } + + // Once we are satisfied that neither config value was set, we + // replace them with our loop dir. + cfg.DataDir = cfg.LoopDir + cfg.LogDir = filepath.Join(cfg.LoopDir, defaultLogDirname) + } + + // Append the network type to the data and log directory so they are + // "namespaced" per network. + cfg.DataDir = filepath.Join(cfg.DataDir, cfg.Network) + cfg.LogDir = filepath.Join(cfg.LogDir, cfg.Network) + + // If either of these directories do not exist, create them. + if err := os.MkdirAll(cfg.DataDir, os.ModePerm); err != nil { + return err + } + + if err := os.MkdirAll(cfg.LogDir, os.ModePerm); err != nil { + return err + } + + return nil +} diff --git a/loopd/run.go b/loopd/run.go index 35647a6..d13cbdf 100644 --- a/loopd/run.go +++ b/loopd/run.go @@ -12,6 +12,7 @@ import ( "github.com/lightninglabs/lndclient" "github.com/lightninglabs/loop" "github.com/lightningnetwork/lnd/build" + "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lnrpc/verrpc" "github.com/lightningnetwork/lnd/signal" ) @@ -109,12 +110,19 @@ func Run(rpcCfg RPCConfig) error { } // Parse ini file. - loopDir := filepath.Join(loopDirBase, config.Network) - if err := os.MkdirAll(loopDir, os.ModePerm); err != nil { - return err + loopDir := lncfg.CleanAndExpandPath(config.LoopDir) + configFile := lncfg.CleanAndExpandPath(config.ConfigFile) + + // If our loop directory is set and the config file parameter is not + // set, we assume that they want to point to a config file in their + // loop dir. However, if the config file has a non-default value, then + // we leave the config parameter as its custom value. + if loopDir != loopDirBase && configFile == defaultConfigFile { + configFile = filepath.Join( + loopDir, defaultConfigFilename, + ) } - configFile := filepath.Join(loopDir, defaultConfigFilename) if err := flags.IniParse(configFile, &config); err != nil { // If it's a parsing related error, then we'll return // immediately, otherwise we can proceed as possibly the config @@ -146,9 +154,10 @@ func Run(rpcCfg RPCConfig) error { os.Exit(0) } - // Append the network type to the log directory so it is - // "namespaced" per network in the same fashion as the data directory. - config.LogDir = filepath.Join(config.LogDir, config.Network) + // Validate our config before we proceed. + if err := Validate(&config); err != nil { + return err + } // Initialize logging at the default logging level. err = logWriter.InitLogRotator( diff --git a/loopd/utils.go b/loopd/utils.go index 2df499e..c7bc62e 100644 --- a/loopd/utils.go +++ b/loopd/utils.go @@ -1,9 +1,6 @@ package loopd import ( - "os" - "path/filepath" - "github.com/btcsuite/btcutil" "github.com/lightninglabs/lndclient" "github.com/lightninglabs/loop" @@ -13,11 +10,6 @@ import ( func getClient(config *Config, lnd *lndclient.LndServices) (*loop.Client, func(), error) { - storeDir, err := getStoreDir(config.Network) - if err != nil { - return nil, nil, err - } - clientConfig := &loop.ClientConfig{ ServerAddress: config.Server.Host, ProxyAddress: config.Server.Proxy, @@ -29,19 +21,10 @@ func getClient(config *Config, lnd *lndclient.LndServices) (*loop.Client, LoopOutMaxParts: config.LoopOutMaxParts, } - swapClient, cleanUp, err := loop.NewClient(storeDir, clientConfig) + swapClient, cleanUp, err := loop.NewClient(config.DataDir, clientConfig) if err != nil { return nil, nil, err } return swapClient, cleanUp, nil } - -func getStoreDir(network string) (string, error) { - dir := filepath.Join(loopDirBase, network) - if err := os.MkdirAll(dir, os.ModePerm); err != nil { - return "", err - } - - return dir, nil -}