From 7b3e81883fee468f0fbe832311c0e0cc93051b7c Mon Sep 17 00:00:00 2001 From: Dominic Della Valle Date: Mon, 8 Jul 2019 14:35:31 -0400 Subject: [PATCH] feat: add ability to use existing config during init --- cmd/ipfs/daemon.go | 25 +++++++++++------- cmd/ipfs/init.go | 65 +++++++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index ec5a979e16dc..25b69b34065e 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -13,6 +13,8 @@ import ( "sync" version "github.com/ipfs/go-ipfs" + config "github.com/ipfs/go-ipfs-config" + cserial "github.com/ipfs/go-ipfs-config/serialize" utilmain "github.com/ipfs/go-ipfs/cmd/ipfs/util" oldcmds "github.com/ipfs/go-ipfs/commands" "github.com/ipfs/go-ipfs/core" @@ -26,11 +28,11 @@ import ( migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations" "github.com/hashicorp/go-multierror" - "github.com/ipfs/go-ipfs-cmds" + cmds "github.com/ipfs/go-ipfs-cmds" mprome "github.com/ipfs/go-metrics-prometheus" goprocess "github.com/jbenet/goprocess" ma "github.com/multiformats/go-multiaddr" - "github.com/multiformats/go-multiaddr-net" + manet "github.com/multiformats/go-multiaddr-net" "github.com/prometheus/client_golang/prometheus" ) @@ -38,6 +40,7 @@ const ( adjustFDLimitKwd = "manage-fdlimit" enableGCKwd = "enable-gc" initOptionKwd = "init" + initConfigOptionKwd = "init-config-file" initProfileOptionKwd = "init-profile" ipfsMountKwd = "mount-ipfs" ipnsMountKwd = "mount-ipns" @@ -154,6 +157,7 @@ Headers. Options: []cmds.Option{ cmds.BoolOption(initOptionKwd, "Initialize ipfs with default settings if not already initialized"), + cmds.StringOption(initConfigOptionKwd, "Path to existing configuration file to be loaded during --init"), cmds.StringOption(initProfileOptionKwd, "Configuration profiles to apply for --init. See ipfs init --help for more"), cmds.StringOption(routingOptionKwd, "Overrides the routing option").WithDefault(routingOptionDefaultKwd), cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem"), @@ -229,17 +233,20 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment // first, whether user has provided the initialization flag. we may be // running in an uninitialized state. initialize, _ := req.Options[initOptionKwd].(bool) - if initialize { + if initialize && !fsrepo.IsInitialized(cctx.ConfigRoot) { + cfgLocation, _ := req.Options[initConfigOptionKwd].(string) + profiles, _ := req.Options[initProfileOptionKwd].(string) + var conf *config.Config - cfg := cctx.ConfigRoot - if !fsrepo.IsInitialized(cfg) { - profiles, _ := req.Options[initProfileOptionKwd].(string) - - err := initWithDefaults(os.Stdout, cfg, profiles) - if err != nil { + if cfgLocation != "" { + if conf, err = cserial.Load(cfgLocation); err != nil { return err } } + + if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, profiles, conf); err != nil { + return err + } } // acquire the repo lock _before_ constructing a node. we need to make diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 4fa5087fa6e0..8fe2cbba371d 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -7,18 +7,19 @@ import ( "fmt" "io" "os" - "path" + "path/filepath" "strings" + cserial "github.com/ipfs/go-ipfs-config/serialize" assets "github.com/ipfs/go-ipfs/assets" oldcmds "github.com/ipfs/go-ipfs/commands" core "github.com/ipfs/go-ipfs/core" namesys "github.com/ipfs/go-ipfs/namesys" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" - "github.com/ipfs/go-ipfs-cmds" - "github.com/ipfs/go-ipfs-config" - "github.com/ipfs/go-ipfs-files" + cmds "github.com/ipfs/go-ipfs-cmds" + config "github.com/ipfs/go-ipfs-config" + files "github.com/ipfs/go-ipfs-files" ) const ( @@ -26,8 +27,13 @@ const ( bitsOptionName = "bits" emptyRepoOptionName = "empty-repo" profileOptionName = "profile" + configOptionName = "config-file" ) +var errRepoExists = errors.New(`ipfs configuration file already exists! +Reinitializing would overwrite your keys. +`) + var initCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Initializes ipfs config file.", @@ -53,6 +59,7 @@ environment variable: cmds.IntOption(bitsOptionName, "b", "Number of bits to use in the generated RSA private key.").WithDefault(nBitsForKeypairDefault), cmds.BoolOption(emptyRepoOptionName, "e", "Don't add and pin help files to the local storage."), cmds.StringOption(profileOptionName, "p", "Apply profile settings to config. Multiple profiles can be separated by ','"), + cmds.StringOption(configOptionName, "Use supplied config as a base instead of the default"), // TODO need to decide whether to expose the override as a file or a // directory. That is: should we allow the user to also specify the @@ -102,31 +109,38 @@ environment variable: } } - profile, _ := req.Options[profileOptionName].(string) - - var profiles []string - if profile != "" { - profiles = strings.Split(profile, ",") + cfgLocation, _ := req.Options[configOptionName].(string) + profiles, _ := req.Options[profileOptionName].(string) + if cfgLocation != "" { + var err error + if conf, err = cserial.Load(cfgLocation); err != nil { + return err + } } return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf) }, } -var errRepoExists = errors.New(`ipfs configuration file already exists! -Reinitializing would overwrite your keys. -`) - -func initWithDefaults(out io.Writer, repoRoot string, profile string) error { - var profiles []string - if profile != "" { - profiles = strings.Split(profile, ",") +func applyProfiles(conf *config.Config, profiles string) error { + if profiles == "" { + return nil } - return doInit(out, repoRoot, false, nBitsForKeypairDefault, profiles, nil) + for _, profile := range strings.Split(profiles, ",") { + transformer, ok := config.Profiles[profile] + if !ok { + return fmt.Errorf("invalid configuration profile: %s", profile) + } + + if err := transformer.Transform(conf); err != nil { + return err + } + } + return nil } -func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles []string, conf *config.Config) error { +func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles string, conf *config.Config) error { if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil { return err } @@ -147,15 +161,8 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con } } - for _, profile := range confProfiles { - transformer, ok := config.Profiles[profile] - if !ok { - return fmt.Errorf("invalid configuration profile: %s", profile) - } - - if err := transformer.Transform(conf); err != nil { - return err - } + if err := applyProfiles(conf, confProfiles); err != nil { + return err } if err := fsrepo.Init(repoRoot, conf); err != nil { @@ -175,7 +182,7 @@ func checkWritable(dir string) error { _, err := os.Stat(dir) if err == nil { // dir exists, make sure we can write to it - testfile := path.Join(dir, "test") + testfile := filepath.Join(dir, "test") fi, err := os.Create(testfile) if err != nil { if os.IsPermission(err) {