From f1f726cd70ea7cf1c62d0c20a2a8613fb889b89e 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 | 31 +++++++++++++++++++++++++------ cmd/ipfs/init.go | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index ec5a979e16dc..6270a6997ee4 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -13,6 +13,7 @@ import ( "sync" version "github.com/ipfs/go-ipfs" + 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 +27,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 +39,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 +156,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"), @@ -230,15 +233,31 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment // running in an uninitialized state. initialize, _ := req.Options[initOptionKwd].(bool) if initialize { + cfgLocation, _ := req.Options[initConfigOptionKwd].(string) + profiles, _ := req.Options[initProfileOptionKwd].(string) - cfg := cctx.ConfigRoot - if !fsrepo.IsInitialized(cfg) { - profiles, _ := req.Options[initProfileOptionKwd].(string) + if cfgLocation != "" { + if profiles != "" { + return errInitConfigArgs + } - err := initWithDefaults(os.Stdout, cfg, profiles) + conf, err := cserial.Load(cfgLocation) if err != nil { return err } + + if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, nil, conf); err != nil { + return err + } + } else { + cfgLocation = cctx.ConfigRoot + + if !fsrepo.IsInitialized(cfgLocation) { + err := initWithDefaults(os.Stdout, cfgLocation, profiles) + if err != nil { + return err + } + } } } diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 4fa5087fa6e0..686ea6fd4da2 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,6 +27,14 @@ 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. +`) + errInitConfigArgs = errors.New("Config file <-> Profile merging not implemented") ) var initCmd = &cmds.Command{ @@ -53,6 +62,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 instead of generating one"), // 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,8 +112,22 @@ environment variable: } } + cfgLocation, _ := req.Options[configOptionName].(string) profile, _ := req.Options[profileOptionName].(string) + if cfgLocation != "" { + if profile != "" { + return errInitConfigArgs + } + + conf, err := cserial.Load(cfgLocation) + if err != nil { + return err + } + + return doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, nil, conf) + } + var profiles []string if profile != "" { profiles = strings.Split(profile, ",") @@ -113,10 +137,6 @@ environment variable: }, } -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 != "" { @@ -175,7 +195,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) {