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 10, 2019
1 parent c014d1e commit 7b3e818
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
25 changes: 16 additions & 9 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,18 +28,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 +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"),
Expand Down Expand Up @@ -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
Expand Down
65 changes: 36 additions & 29 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,33 @@ 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.
`)

var initCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Initializes ipfs config file.",
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 7b3e818

Please sign in to comment.