@cryptotaxi247 / kubo / commits / ac26cf19c

config-patch: Inverse profiles

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Dec 16, 2017 at 18:59 UTC ac26cf19cd65ab9ec5ecc031a0e0a7fb797c5439
5 files changed +73 -99
cmd/ipfs/init.go
+1 -1
@@ -165,7 +165,7 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
165 return fmt.Errorf("invalid configuration profile: %s", profile)
166 }
167
168 - if err := transformer.Apply(conf); err != nil {
168 + if err := transformer(conf); err != nil {
169 return err
170 }
171 }
core/commands/config.go
+4 -32
@@ -300,8 +300,7 @@ var configProfileCmd = &cmds.Command{
300 },
301
302 Subcommands: map[string]*cmds.Command{
303 - "apply": configProfileApplyCmd,
304 - "revert": configProfileRevertCmd,
303 + "apply": configProfileApplyCmd,
304 },
305 }
306
@@ -319,7 +318,7 @@ var configProfileApplyCmd = &cmds.Command{
318 return
319 }
320
322 - err := transformConfig(req.InvocContext().ConfigRoot, "apply-"+req.Arguments()[0], profile.Apply)
321 + err := transformConfig(req.InvocContext().ConfigRoot, req.Arguments()[0], profile)
322 if err != nil {
323 res.SetError(err, cmdkit.ErrNormal)
324 return
@@ -328,34 +327,7 @@ var configProfileApplyCmd = &cmds.Command{
327 },
328 }
329
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)
350 - if err != nil {
351 - res.SetError(err, cmdkit.ErrNormal)
352 - return
353 - }
354 - res.SetOutput(nil)
355 - },
356 -}
357 -
358 -func transformConfig(configRoot string, backupName string, transformer config.Transformer) error {
330 +func transformConfig(configRoot string, configName string, transformer config.Transformer) error {
331 r, err := fsrepo.Open(configRoot)
332 if err != nil {
333 return err
@@ -372,7 +344,7 @@ func transformConfig(configRoot string, backupName string, transformer config.Tr
344 return err
345 }
346
375 - _, err = r.BackupConfig(backupName + "-")
347 + _, err = r.BackupConfig("pre-" + configName + "-")
348 if err != nil {
349 return err
350 }
docs/config.md
+15 -2
@@ -7,7 +7,8 @@ on a running daemon do not read the config file at runtime.
7 #### Profiles
8 Configuration profiles allow to tweak configuration quickly. Profiles can be
9 applied with `--profile` flag to `ipfs init` or with `ipfs config profile apply`
10 -command.
10 +command. When a profile is applied a backup of the configuration file will
11 +be created in $IPFS_PATH
12
13 Available profiles:
14 - `server`
@@ -15,12 +16,21 @@ Available profiles:
16 Recommended for nodes with public IPv4 address (servers, VPSes, etc.),
17 disables host and content discovery in local networks.
18
19 +- `local-discovery`
20 +
21 + Sets default values to fields affected by `server` profile, enables
22 + discovery in local networks.
23 +
24 - `test`
25
26 Reduces external interference, useful for running ipfs in test environments.
27 Note that with these settings node won't be able to talk to the rest of the
28 network without manual bootstrap.
29
30 +- `default-networking`
31 +
32 + Restores default network settings. Inverse profile of the `test` profile.
33 +
34 - `badgerds`
35
36 Replaces default datastore configuration with experimental badger datastore.
@@ -28,8 +38,11 @@ Available profiles:
38 datastore to the new configuration. You can do this using [ipfs-ds-convert](https://github.com/ipfs/ipfs-ds-convert)
39
40 WARNING: badger datastore is experimental. Make sure to backup your data
31 - frequently
41 + frequently.
42 +
43 +- `default-datastore`
44
45 + Restores default datastore configuration.
46
47 ## Table of Contents
48
repo/config/profile.go
+43 -55
@@ -3,12 +3,6 @@ package config
3 // Transformer is a function which takes configuration and applies some filter to it
4 type Transformer func(c *Config) error
5
6 -// Profile applies some set of changes to the configuration
7 -type Profile struct {
8 - Apply Transformer
9 - Revert Transformer
10 -}
11 -
6 // defaultServerFilters has a list of non-routable IPv4 prefixes
7 // according to http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
8 var defaultServerFilters = []string{
@@ -30,60 +24,54 @@ var defaultServerFilters = []string{
24 }
25
26 // 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
39 },
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 + }
46
56 - c.Swarm.DisableNatPortMap = true
47 + c.Swarm.DisableNatPortMap = true
48
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()
55
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
71 },
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
75 },
76 }
77
test/sharness/t0021-config.sh
+10 -9
@@ -50,6 +50,7 @@ CONFIG_SET_JSON_TEST='{
50
51 test_profile_apply_revert() {
52 profile=$1
53 + inverse_profile=$2
54
55 test_expect_success "save expected config" '
56 ipfs config show >expected
@@ -64,11 +65,11 @@ test_profile_apply_revert() {
65 test_must_fail test_cmp expected actual
66 '
67
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}'
70 '
71
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" '
73 ipfs config show >actual &&
74 test_cmp expected actual
75 '
@@ -192,7 +193,7 @@ test_config_cmd() {
193 '
194
195 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
197 '
198
199 test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" '
@@ -200,16 +201,16 @@ test_config_cmd() {
201 test $(cat actual_config | wc -l) = 17
202 '
203
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
206 '
207
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" '
209 ipfs config Swarm.AddrFilters > actual_config &&
210 test $(cat actual_config | wc -l) = 1
211 '
212
212 - test_profile_apply_revert server
213 + test_profile_apply_revert server local-discovery
214
215 # won't work as we already have this profile applied
216 # test_profile_apply_revert test
@@ -219,7 +220,7 @@ test_config_cmd() {
220 # test_profile_apply_revert badgerds
221
222 test_expect_success "cleanup config backups" '
222 - find "$IPFS_PATH" -name "config-profile*" -exec rm {} \;
223 + find "$IPFS_PATH" -name "config-*" -exec rm {} \;
224 '
225 }
226