Skip to content

Commit 0546f1f

Browse files
committed
config-patch: Inverse profiles
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent acb4edc commit 0546f1f

File tree

5 files changed

+69
-96
lines changed

5 files changed

+69
-96
lines changed

cmd/ipfs/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
165165
return fmt.Errorf("invalid configuration profile: %s", profile)
166166
}
167167

168-
if err := transformer.Apply(conf); err != nil {
168+
if err := transformer(conf); err != nil {
169169
return err
170170
}
171171
}

core/commands/config.go

+2-30
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ var configProfileCmd = &cmds.Command{
300300
},
301301

302302
Subcommands: map[string]*cmds.Command{
303-
"apply": configProfileApplyCmd,
304-
"revert": configProfileRevertCmd,
303+
"apply": configProfileApplyCmd,
305304
},
306305
}
307306

@@ -319,34 +318,7 @@ var configProfileApplyCmd = &cmds.Command{
319318
return
320319
}
321320

322-
err := transformConfig(req.InvocContext().ConfigRoot, "apply-"+req.Arguments()[0], profile.Apply)
323-
if err != nil {
324-
res.SetError(err, cmdkit.ErrNormal)
325-
return
326-
}
327-
res.SetOutput(nil)
328-
},
329-
}
330-
331-
var configProfileRevertCmd = &cmds.Command{
332-
Helptext: cmdkit.HelpText{
333-
Tagline: "Revert profile changes.",
334-
ShortDescription: `Reverts profile-related changes to the default values.
335-
336-
Reverting some profiles may damage the configuration or not be possible.
337-
Backing up the config before running this command is advised.`,
338-
},
339-
Arguments: []cmdkit.Argument{
340-
cmdkit.StringArg("profile", true, false, "The profile to apply to the config."),
341-
},
342-
Run: func(req cmds.Request, res cmds.Response) {
343-
profile, ok := config.Profiles[req.Arguments()[0]]
344-
if !ok {
345-
res.SetError(fmt.Errorf("%s is not a profile", req.Arguments()[0]), cmdkit.ErrNormal)
346-
return
347-
}
348-
349-
err := transformConfig(req.InvocContext().ConfigRoot, "revert-"+req.Arguments()[0], profile.Revert)
321+
err := transformConfig(req.InvocContext().ConfigRoot, req.Arguments()[0], profile)
350322
if err != nil {
351323
res.SetError(err, cmdkit.ErrNormal)
352324
return

docs/config.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,33 @@ Available profiles:
1515
Recommended for nodes with public IPv4 address (servers, VPSes, etc.),
1616
disables host and content discovery in local networks.
1717

18+
- `local-discovery`
19+
20+
Sets default values to fields affected by `server` profile, enables
21+
discovery in local networks.
22+
1823
- `test`
1924

2025
Reduces external interference, useful for running ipfs in test environments.
2126
Note that with these settings node won't be able to talk to the rest of the
2227
network without manual bootstrap.
2328

29+
- `default-networking`
30+
31+
Restores default network settings. Inverse profile of the `test` profile.
32+
2433
- `badgerds`
2534

2635
Replaces default datastore configuration with experimental badger datastore.
2736
If you apply this profile after `ipfs init`, you will need to convert your
2837
datastore to the new configuration. You can do this using [ipfs-ds-convert](https://github.com/ipfs/ipfs-ds-convert)
2938

3039
WARNING: badger datastore is experimental. Make sure to backup your data
31-
frequently
40+
frequently.
41+
42+
- `default-datastore`
3243

44+
Restores default datastore configuration.
3345

3446
## Table of Contents
3547

repo/config/profile.go

+43-55
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ package config
33
// Transformer is a function which takes configuration and applies some filter to it
44
type Transformer func(c *Config) error
55

6-
// Profile applies some set of changes to the configuration
7-
type Profile struct {
8-
Apply Transformer
9-
Revert Transformer
10-
}
11-
126
// defaultServerFilters has a list of non-routable IPv4 prefixes
137
// according to http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
148
var defaultServerFilters = []string{
@@ -30,60 +24,54 @@ var defaultServerFilters = []string{
3024
}
3125

3226
// Profiles is a map holding configuration transformers. Docs are in docs/config.md
33-
var Profiles = map[string]*Profile{
34-
"server": {
35-
Apply: func(c *Config) error {
36-
c.Addresses.NoAnnounce = appendSingle(c.Addresses.NoAnnounce, defaultServerFilters)
37-
c.Swarm.AddrFilters = appendSingle(c.Swarm.AddrFilters, defaultServerFilters)
38-
c.Discovery.MDNS.Enabled = false
39-
return nil
40-
},
41-
Revert: func(c *Config) error {
42-
c.Addresses.NoAnnounce = deleteEntries(c.Addresses.NoAnnounce, defaultServerFilters)
43-
c.Swarm.AddrFilters = deleteEntries(c.Swarm.AddrFilters, defaultServerFilters)
44-
c.Discovery.MDNS.Enabled = true
45-
return nil
46-
},
27+
var Profiles = map[string]Transformer{
28+
"server": func(c *Config) error {
29+
c.Addresses.NoAnnounce = appendSingle(c.Addresses.NoAnnounce, defaultServerFilters)
30+
c.Swarm.AddrFilters = appendSingle(c.Swarm.AddrFilters, defaultServerFilters)
31+
c.Discovery.MDNS.Enabled = false
32+
return nil
33+
},
34+
"local-discovery": func(c *Config) error {
35+
c.Addresses.NoAnnounce = deleteEntries(c.Addresses.NoAnnounce, defaultServerFilters)
36+
c.Swarm.AddrFilters = deleteEntries(c.Swarm.AddrFilters, defaultServerFilters)
37+
c.Discovery.MDNS.Enabled = true
38+
return nil
4739
},
48-
"test": {
49-
Apply: func(c *Config) error {
50-
c.Addresses.API = "/ip4/127.0.0.1/tcp/0"
51-
c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0"
52-
c.Addresses.Swarm = []string{
53-
"/ip4/127.0.0.1/tcp/0",
54-
}
40+
"test": func(c *Config) error {
41+
c.Addresses.API = "/ip4/127.0.0.1/tcp/0"
42+
c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0"
43+
c.Addresses.Swarm = []string{
44+
"/ip4/127.0.0.1/tcp/0",
45+
}
5546

56-
c.Swarm.DisableNatPortMap = true
47+
c.Swarm.DisableNatPortMap = true
5748

58-
c.Bootstrap = []string{}
59-
c.Discovery.MDNS.Enabled = false
60-
return nil
61-
},
62-
Revert: func(c *Config) error {
63-
c.Addresses = addressesConfig()
49+
c.Bootstrap = []string{}
50+
c.Discovery.MDNS.Enabled = false
51+
return nil
52+
},
53+
"default-networking": func(c *Config) error {
54+
c.Addresses = addressesConfig()
6455

65-
c.Swarm.DisableNatPortMap = false
66-
c.Discovery.MDNS.Enabled = true
67-
return nil
68-
},
56+
c.Swarm.DisableNatPortMap = false
57+
c.Discovery.MDNS.Enabled = true
58+
return nil
59+
},
60+
"badgerds": func(c *Config) error {
61+
c.Datastore.Spec = map[string]interface{}{
62+
"type": "measure",
63+
"prefix": "badger.datastore",
64+
"child": map[string]interface{}{
65+
"type": "badgerds",
66+
"path": "badgerds",
67+
"syncWrites": true,
68+
},
69+
}
70+
return nil
6971
},
70-
"badgerds": {
71-
Apply: func(c *Config) error {
72-
c.Datastore.Spec = map[string]interface{}{
73-
"type": "measure",
74-
"prefix": "badger.datastore",
75-
"child": map[string]interface{}{
76-
"type": "badgerds",
77-
"path": "badgerds",
78-
"syncWrites": true,
79-
},
80-
}
81-
return nil
82-
},
83-
Revert: func(c *Config) error {
84-
c.Datastore.Spec = DefaultDatastoreConfig().Spec
85-
return nil
86-
},
72+
"default-datastore": func(c *Config) error {
73+
c.Datastore.Spec = DefaultDatastoreConfig().Spec
74+
return nil
8775
},
8876
}
8977

test/sharness/t0021-config.sh

+10-9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ CONFIG_SET_JSON_TEST='{
5050

5151
test_profile_apply_revert() {
5252
profile=$1
53+
inverse_profile=$2
5354

5455
test_expect_success "save expected config" '
5556
ipfs config show >expected
@@ -64,11 +65,11 @@ test_profile_apply_revert() {
6465
test_must_fail test_cmp expected actual
6566
'
6667

67-
test_expect_success "'ipfs config profile revert ${profile}' works" '
68-
ipfs config profile revert '${profile}'
68+
test_expect_success "'ipfs config profile apply ${inverse_profile}' works" '
69+
ipfs config profile apply '${inverse_profile}'
6970
'
7071

71-
test_expect_success "config is back to previous state after ${profile} revert" '
72+
test_expect_success "config is back to previous state after ${inverse_profile} was applied" '
7273
ipfs config show >actual &&
7374
test_cmp expected actual
7475
'
@@ -192,24 +193,24 @@ test_config_cmd() {
192193
'
193194

194195
test_expect_success "backup was created and looks good" '
195-
test_cmp "$(find "$IPFS_PATH" -name "config-profile*")" before_patch
196+
test_cmp "$(find "$IPFS_PATH" -name "config-*")" before_patch
196197
'
197198

198199
test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" '
199200
ipfs config Swarm.AddrFilters > actual_config &&
200201
test $(cat actual_config | wc -l) = 17
201202
'
202203

203-
test_expect_success "'ipfs config profile revert server' works" '
204-
ipfs config profile revert server
204+
test_expect_success "'ipfs config profile apply local-discovery' works" '
205+
ipfs config profile apply local-discovery
205206
'
206207

207-
test_expect_success "'ipfs config Swarm.AddrFilters' looks good with reverted server profile" '
208+
test_expect_success "'ipfs config Swarm.AddrFilters' looks good with applied local-discovery profile" '
208209
ipfs config Swarm.AddrFilters > actual_config &&
209210
test $(cat actual_config | wc -l) = 1
210211
'
211212

212-
test_profile_apply_revert server
213+
test_profile_apply_revert server local-discovery
213214

214215
# won't work as we already have this profile applied
215216
# test_profile_apply_revert test
@@ -219,7 +220,7 @@ test_config_cmd() {
219220
# test_profile_apply_revert badgerds
220221

221222
test_expect_success "cleanup config backups" '
222-
find "$IPFS_PATH" -name "config-profile*" -exec rm {} \;
223+
find "$IPFS_PATH" -name "config-*" -exec rm {} \;
223224
'
224225
}
225226

0 commit comments

Comments
 (0)