diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 47c51571496..20e15b27f27 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -165,7 +165,7 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con return fmt.Errorf("invalid configuration profile: %s", profile) } - if err := transformer.Apply(conf); err != nil { + if err := transformer(conf); err != nil { return err } } diff --git a/core/commands/config.go b/core/commands/config.go index a5011f25872..75e64b564d1 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -300,8 +300,7 @@ var configProfileCmd = &cmds.Command{ }, Subcommands: map[string]*cmds.Command{ - "apply": configProfileApplyCmd, - "revert": configProfileRevertCmd, + "apply": configProfileApplyCmd, }, } @@ -319,7 +318,7 @@ var configProfileApplyCmd = &cmds.Command{ return } - err := transformConfig(req.InvocContext().ConfigRoot, "apply-"+req.Arguments()[0], profile.Apply) + err := transformConfig(req.InvocContext().ConfigRoot, req.Arguments()[0], profile) if err != nil { res.SetError(err, cmdkit.ErrNormal) return @@ -328,34 +327,7 @@ var configProfileApplyCmd = &cmds.Command{ }, } -var configProfileRevertCmd = &cmds.Command{ - Helptext: cmdkit.HelpText{ - Tagline: "Revert profile changes.", - ShortDescription: `Reverts profile-related changes to the default values. - -Reverting some profiles may damage the configuration or not be possible. -Backing up the config before running this command is advised.`, - }, - Arguments: []cmdkit.Argument{ - cmdkit.StringArg("profile", true, false, "The profile to apply to the config."), - }, - Run: func(req cmds.Request, res cmds.Response) { - profile, ok := config.Profiles[req.Arguments()[0]] - if !ok { - res.SetError(fmt.Errorf("%s is not a profile", req.Arguments()[0]), cmdkit.ErrNormal) - return - } - - err := transformConfig(req.InvocContext().ConfigRoot, "revert-"+req.Arguments()[0], profile.Revert) - if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return - } - res.SetOutput(nil) - }, -} - -func transformConfig(configRoot string, backupName string, transformer config.Transformer) error { +func transformConfig(configRoot string, configName string, transformer config.Transformer) error { r, err := fsrepo.Open(configRoot) if err != nil { return err @@ -372,7 +344,7 @@ func transformConfig(configRoot string, backupName string, transformer config.Tr return err } - _, err = r.BackupConfig(backupName + "-") + _, err = r.BackupConfig("pre-" + configName + "-") if err != nil { return err } diff --git a/docs/config.md b/docs/config.md index 1376414bf3a..33dc9d3aae4 100644 --- a/docs/config.md +++ b/docs/config.md @@ -7,7 +7,8 @@ on a running daemon do not read the config file at runtime. #### Profiles Configuration profiles allow to tweak configuration quickly. Profiles can be applied with `--profile` flag to `ipfs init` or with `ipfs config profile apply` -command. +command. When a profile is applied a backup of the configuration file will +be created in $IPFS_PATH Available profiles: - `server` @@ -15,12 +16,21 @@ Available profiles: Recommended for nodes with public IPv4 address (servers, VPSes, etc.), disables host and content discovery in local networks. +- `local-discovery` + + Sets default values to fields affected by `server` profile, enables + discovery in local networks. + - `test` Reduces external interference, useful for running ipfs in test environments. Note that with these settings node won't be able to talk to the rest of the network without manual bootstrap. +- `default-networking` + + Restores default network settings. Inverse profile of the `test` profile. + - `badgerds` Replaces default datastore configuration with experimental badger datastore. @@ -28,8 +38,11 @@ Available profiles: datastore to the new configuration. You can do this using [ipfs-ds-convert](https://github.com/ipfs/ipfs-ds-convert) WARNING: badger datastore is experimental. Make sure to backup your data - frequently + frequently. + +- `default-datastore` + Restores default datastore configuration. ## Table of Contents diff --git a/repo/config/profile.go b/repo/config/profile.go index 87668357f66..74383cda63f 100644 --- a/repo/config/profile.go +++ b/repo/config/profile.go @@ -3,12 +3,6 @@ package config // Transformer is a function which takes configuration and applies some filter to it type Transformer func(c *Config) error -// Profile applies some set of changes to the configuration -type Profile struct { - Apply Transformer - Revert Transformer -} - // defaultServerFilters has a list of non-routable IPv4 prefixes // according to http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml var defaultServerFilters = []string{ @@ -30,60 +24,54 @@ var defaultServerFilters = []string{ } // Profiles is a map holding configuration transformers. Docs are in docs/config.md -var Profiles = map[string]*Profile{ - "server": { - Apply: func(c *Config) error { - c.Addresses.NoAnnounce = appendSingle(c.Addresses.NoAnnounce, defaultServerFilters) - c.Swarm.AddrFilters = appendSingle(c.Swarm.AddrFilters, defaultServerFilters) - c.Discovery.MDNS.Enabled = false - return nil - }, - Revert: func(c *Config) error { - c.Addresses.NoAnnounce = deleteEntries(c.Addresses.NoAnnounce, defaultServerFilters) - c.Swarm.AddrFilters = deleteEntries(c.Swarm.AddrFilters, defaultServerFilters) - c.Discovery.MDNS.Enabled = true - return nil - }, +var Profiles = map[string]Transformer{ + "server": func(c *Config) error { + c.Addresses.NoAnnounce = appendSingle(c.Addresses.NoAnnounce, defaultServerFilters) + c.Swarm.AddrFilters = appendSingle(c.Swarm.AddrFilters, defaultServerFilters) + c.Discovery.MDNS.Enabled = false + return nil + }, + "local-discovery": func(c *Config) error { + c.Addresses.NoAnnounce = deleteEntries(c.Addresses.NoAnnounce, defaultServerFilters) + c.Swarm.AddrFilters = deleteEntries(c.Swarm.AddrFilters, defaultServerFilters) + c.Discovery.MDNS.Enabled = true + return nil }, - "test": { - Apply: func(c *Config) error { - c.Addresses.API = "/ip4/127.0.0.1/tcp/0" - c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0" - c.Addresses.Swarm = []string{ - "/ip4/127.0.0.1/tcp/0", - } + "test": func(c *Config) error { + c.Addresses.API = "/ip4/127.0.0.1/tcp/0" + c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0" + c.Addresses.Swarm = []string{ + "/ip4/127.0.0.1/tcp/0", + } - c.Swarm.DisableNatPortMap = true + c.Swarm.DisableNatPortMap = true - c.Bootstrap = []string{} - c.Discovery.MDNS.Enabled = false - return nil - }, - Revert: func(c *Config) error { - c.Addresses = addressesConfig() + c.Bootstrap = []string{} + c.Discovery.MDNS.Enabled = false + return nil + }, + "default-networking": func(c *Config) error { + c.Addresses = addressesConfig() - c.Swarm.DisableNatPortMap = false - c.Discovery.MDNS.Enabled = true - return nil - }, + c.Swarm.DisableNatPortMap = false + c.Discovery.MDNS.Enabled = true + return nil + }, + "badgerds": func(c *Config) error { + c.Datastore.Spec = map[string]interface{}{ + "type": "measure", + "prefix": "badger.datastore", + "child": map[string]interface{}{ + "type": "badgerds", + "path": "badgerds", + "syncWrites": true, + }, + } + return nil }, - "badgerds": { - Apply: func(c *Config) error { - c.Datastore.Spec = map[string]interface{}{ - "type": "measure", - "prefix": "badger.datastore", - "child": map[string]interface{}{ - "type": "badgerds", - "path": "badgerds", - "syncWrites": true, - }, - } - return nil - }, - Revert: func(c *Config) error { - c.Datastore.Spec = DefaultDatastoreConfig().Spec - return nil - }, + "default-datastore": func(c *Config) error { + c.Datastore.Spec = DefaultDatastoreConfig().Spec + return nil }, } diff --git a/test/sharness/t0021-config.sh b/test/sharness/t0021-config.sh index 9a086b1d8dd..5ade07d0326 100755 --- a/test/sharness/t0021-config.sh +++ b/test/sharness/t0021-config.sh @@ -50,6 +50,7 @@ CONFIG_SET_JSON_TEST='{ test_profile_apply_revert() { profile=$1 + inverse_profile=$2 test_expect_success "save expected config" ' ipfs config show >expected @@ -64,11 +65,11 @@ test_profile_apply_revert() { test_must_fail test_cmp expected actual ' - test_expect_success "'ipfs config profile revert ${profile}' works" ' - ipfs config profile revert '${profile}' + test_expect_success "'ipfs config profile apply ${inverse_profile}' works" ' + ipfs config profile apply '${inverse_profile}' ' - test_expect_success "config is back to previous state after ${profile} revert" ' + test_expect_success "config is back to previous state after ${inverse_profile} was applied" ' ipfs config show >actual && test_cmp expected actual ' @@ -192,7 +193,7 @@ test_config_cmd() { ' test_expect_success "backup was created and looks good" ' - test_cmp "$(find "$IPFS_PATH" -name "config-profile*")" before_patch + test_cmp "$(find "$IPFS_PATH" -name "config-*")" before_patch ' test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" ' @@ -200,16 +201,16 @@ test_config_cmd() { test $(cat actual_config | wc -l) = 17 ' - test_expect_success "'ipfs config profile revert server' works" ' - ipfs config profile revert server + test_expect_success "'ipfs config profile apply local-discovery' works" ' + ipfs config profile apply local-discovery ' - test_expect_success "'ipfs config Swarm.AddrFilters' looks good with reverted server profile" ' + test_expect_success "'ipfs config Swarm.AddrFilters' looks good with applied local-discovery profile" ' ipfs config Swarm.AddrFilters > actual_config && test $(cat actual_config | wc -l) = 1 ' - test_profile_apply_revert server + test_profile_apply_revert server local-discovery # won't work as we already have this profile applied # test_profile_apply_revert test @@ -219,7 +220,7 @@ test_config_cmd() { # test_profile_apply_revert badgerds test_expect_success "cleanup config backups" ' - find "$IPFS_PATH" -name "config-profile*" -exec rm {} \; + find "$IPFS_PATH" -name "config-*" -exec rm {} \; ' }