Skip to content

Commit 7b3e818

Browse files
committed
feat: add ability to use existing config during init
1 parent c014d1e commit 7b3e818

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

cmd/ipfs/daemon.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"sync"
1414

1515
version "github.com/ipfs/go-ipfs"
16+
config "github.com/ipfs/go-ipfs-config"
17+
cserial "github.com/ipfs/go-ipfs-config/serialize"
1618
utilmain "github.com/ipfs/go-ipfs/cmd/ipfs/util"
1719
oldcmds "github.com/ipfs/go-ipfs/commands"
1820
"github.com/ipfs/go-ipfs/core"
@@ -26,18 +28,19 @@ import (
2628
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
2729

2830
"github.com/hashicorp/go-multierror"
29-
"github.com/ipfs/go-ipfs-cmds"
31+
cmds "github.com/ipfs/go-ipfs-cmds"
3032
mprome "github.com/ipfs/go-metrics-prometheus"
3133
goprocess "github.com/jbenet/goprocess"
3234
ma "github.com/multiformats/go-multiaddr"
33-
"github.com/multiformats/go-multiaddr-net"
35+
manet "github.com/multiformats/go-multiaddr-net"
3436
"github.com/prometheus/client_golang/prometheus"
3537
)
3638

3739
const (
3840
adjustFDLimitKwd = "manage-fdlimit"
3941
enableGCKwd = "enable-gc"
4042
initOptionKwd = "init"
43+
initConfigOptionKwd = "init-config-file"
4144
initProfileOptionKwd = "init-profile"
4245
ipfsMountKwd = "mount-ipfs"
4346
ipnsMountKwd = "mount-ipns"
@@ -154,6 +157,7 @@ Headers.
154157

155158
Options: []cmds.Option{
156159
cmds.BoolOption(initOptionKwd, "Initialize ipfs with default settings if not already initialized"),
160+
cmds.StringOption(initConfigOptionKwd, "Path to existing configuration file to be loaded during --init"),
157161
cmds.StringOption(initProfileOptionKwd, "Configuration profiles to apply for --init. See ipfs init --help for more"),
158162
cmds.StringOption(routingOptionKwd, "Overrides the routing option").WithDefault(routingOptionDefaultKwd),
159163
cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem"),
@@ -229,17 +233,20 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
229233
// first, whether user has provided the initialization flag. we may be
230234
// running in an uninitialized state.
231235
initialize, _ := req.Options[initOptionKwd].(bool)
232-
if initialize {
236+
if initialize && !fsrepo.IsInitialized(cctx.ConfigRoot) {
237+
cfgLocation, _ := req.Options[initConfigOptionKwd].(string)
238+
profiles, _ := req.Options[initProfileOptionKwd].(string)
239+
var conf *config.Config
233240

234-
cfg := cctx.ConfigRoot
235-
if !fsrepo.IsInitialized(cfg) {
236-
profiles, _ := req.Options[initProfileOptionKwd].(string)
237-
238-
err := initWithDefaults(os.Stdout, cfg, profiles)
239-
if err != nil {
241+
if cfgLocation != "" {
242+
if conf, err = cserial.Load(cfgLocation); err != nil {
240243
return err
241244
}
242245
}
246+
247+
if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, profiles, conf); err != nil {
248+
return err
249+
}
243250
}
244251

245252
// acquire the repo lock _before_ constructing a node. we need to make

cmd/ipfs/init.go

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,33 @@ import (
77
"fmt"
88
"io"
99
"os"
10-
"path"
10+
"path/filepath"
1111
"strings"
1212

13+
cserial "github.com/ipfs/go-ipfs-config/serialize"
1314
assets "github.com/ipfs/go-ipfs/assets"
1415
oldcmds "github.com/ipfs/go-ipfs/commands"
1516
core "github.com/ipfs/go-ipfs/core"
1617
namesys "github.com/ipfs/go-ipfs/namesys"
1718
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
1819

19-
"github.com/ipfs/go-ipfs-cmds"
20-
"github.com/ipfs/go-ipfs-config"
21-
"github.com/ipfs/go-ipfs-files"
20+
cmds "github.com/ipfs/go-ipfs-cmds"
21+
config "github.com/ipfs/go-ipfs-config"
22+
files "github.com/ipfs/go-ipfs-files"
2223
)
2324

2425
const (
2526
nBitsForKeypairDefault = 2048
2627
bitsOptionName = "bits"
2728
emptyRepoOptionName = "empty-repo"
2829
profileOptionName = "profile"
30+
configOptionName = "config-file"
2931
)
3032

33+
var errRepoExists = errors.New(`ipfs configuration file already exists!
34+
Reinitializing would overwrite your keys.
35+
`)
36+
3137
var initCmd = &cmds.Command{
3238
Helptext: cmds.HelpText{
3339
Tagline: "Initializes ipfs config file.",
@@ -53,6 +59,7 @@ environment variable:
5359
cmds.IntOption(bitsOptionName, "b", "Number of bits to use in the generated RSA private key.").WithDefault(nBitsForKeypairDefault),
5460
cmds.BoolOption(emptyRepoOptionName, "e", "Don't add and pin help files to the local storage."),
5561
cmds.StringOption(profileOptionName, "p", "Apply profile settings to config. Multiple profiles can be separated by ','"),
62+
cmds.StringOption(configOptionName, "Use supplied config as a base instead of the default"),
5663

5764
// TODO need to decide whether to expose the override as a file or a
5865
// directory. That is: should we allow the user to also specify the
@@ -102,31 +109,38 @@ environment variable:
102109
}
103110
}
104111

105-
profile, _ := req.Options[profileOptionName].(string)
106-
107-
var profiles []string
108-
if profile != "" {
109-
profiles = strings.Split(profile, ",")
112+
cfgLocation, _ := req.Options[configOptionName].(string)
113+
profiles, _ := req.Options[profileOptionName].(string)
114+
if cfgLocation != "" {
115+
var err error
116+
if conf, err = cserial.Load(cfgLocation); err != nil {
117+
return err
118+
}
110119
}
111120

112121
return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf)
113122
},
114123
}
115124

116-
var errRepoExists = errors.New(`ipfs configuration file already exists!
117-
Reinitializing would overwrite your keys.
118-
`)
119-
120-
func initWithDefaults(out io.Writer, repoRoot string, profile string) error {
121-
var profiles []string
122-
if profile != "" {
123-
profiles = strings.Split(profile, ",")
125+
func applyProfiles(conf *config.Config, profiles string) error {
126+
if profiles == "" {
127+
return nil
124128
}
125129

126-
return doInit(out, repoRoot, false, nBitsForKeypairDefault, profiles, nil)
130+
for _, profile := range strings.Split(profiles, ",") {
131+
transformer, ok := config.Profiles[profile]
132+
if !ok {
133+
return fmt.Errorf("invalid configuration profile: %s", profile)
134+
}
135+
136+
if err := transformer.Transform(conf); err != nil {
137+
return err
138+
}
139+
}
140+
return nil
127141
}
128142

129-
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles []string, conf *config.Config) error {
143+
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles string, conf *config.Config) error {
130144
if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil {
131145
return err
132146
}
@@ -147,15 +161,8 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
147161
}
148162
}
149163

150-
for _, profile := range confProfiles {
151-
transformer, ok := config.Profiles[profile]
152-
if !ok {
153-
return fmt.Errorf("invalid configuration profile: %s", profile)
154-
}
155-
156-
if err := transformer.Transform(conf); err != nil {
157-
return err
158-
}
164+
if err := applyProfiles(conf, confProfiles); err != nil {
165+
return err
159166
}
160167

161168
if err := fsrepo.Init(repoRoot, conf); err != nil {
@@ -175,7 +182,7 @@ func checkWritable(dir string) error {
175182
_, err := os.Stat(dir)
176183
if err == nil {
177184
// dir exists, make sure we can write to it
178-
testfile := path.Join(dir, "test")
185+
testfile := filepath.Join(dir, "test")
179186
fi, err := os.Create(testfile)
180187
if err != nil {
181188
if os.IsPermission(err) {

0 commit comments

Comments
 (0)