Skip to content

Commit

Permalink
feat: add ability to use existing config during init
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Jul 8, 2019
1 parent c014d1e commit f1f726c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
31 changes: 25 additions & 6 deletions cmd/ipfs/daemon.go
Expand Up @@ -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"
Expand All @@ -26,18 +27,19 @@ 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"
)

const (
adjustFDLimitKwd = "manage-fdlimit"
enableGCKwd = "enable-gc"
initOptionKwd = "init"
initConfigOptionKwd = "init-config-file"
initProfileOptionKwd = "init-profile"
ipfsMountKwd = "mount-ipfs"
ipnsMountKwd = "mount-ipns"
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
}
}
}
}

Expand Down
38 changes: 29 additions & 9 deletions cmd/ipfs/init.go
Expand Up @@ -7,25 +7,34 @@ 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 (
nBitsForKeypairDefault = 2048
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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, ",")
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit f1f726c

Please sign in to comment.