Skip to content

Commit

Permalink
config-patch: Inverse profiles
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Dec 16, 2017
1 parent acb4edc commit 0546f1f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 96 deletions.
2 changes: 1 addition & 1 deletion cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
32 changes: 2 additions & 30 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ var configProfileCmd = &cmds.Command{
},

Subcommands: map[string]*cmds.Command{
"apply": configProfileApplyCmd,
"revert": configProfileRevertCmd,
"apply": configProfileApplyCmd,
},
}

Expand All @@ -319,34 +318,7 @@ var configProfileApplyCmd = &cmds.Command{
return
}

err := transformConfig(req.InvocContext().ConfigRoot, "apply-"+req.Arguments()[0], profile.Apply)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
res.SetOutput(nil)
},
}

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)
err := transformConfig(req.InvocContext().ConfigRoot, req.Arguments()[0], profile)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down
14 changes: 13 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,33 @@ 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.
If you apply this profile after `ipfs init`, you will need to convert your
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

Expand Down
98 changes: 43 additions & 55 deletions repo/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
},
}

Expand Down
19 changes: 10 additions & 9 deletions test/sharness/t0021-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
'
Expand Down Expand Up @@ -192,24 +193,24 @@ 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" '
ipfs config Swarm.AddrFilters > actual_config &&
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
Expand All @@ -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 {} \;
'
}

Expand Down

0 comments on commit 0546f1f

Please sign in to comment.