提交 ac26cf19 作者: Łukasz Magiera

config-patch: Inverse profiles

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 acb4edcc
...@@ -165,7 +165,7 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con ...@@ -165,7 +165,7 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
return fmt.Errorf("invalid configuration profile: %s", profile) return fmt.Errorf("invalid configuration profile: %s", profile)
} }
if err := transformer.Apply(conf); err != nil { if err := transformer(conf); err != nil {
return err return err
} }
} }
......
...@@ -301,7 +301,6 @@ var configProfileCmd = &cmds.Command{ ...@@ -301,7 +301,6 @@ var configProfileCmd = &cmds.Command{
Subcommands: map[string]*cmds.Command{ Subcommands: map[string]*cmds.Command{
"apply": configProfileApplyCmd, "apply": configProfileApplyCmd,
"revert": configProfileRevertCmd,
}, },
} }
...@@ -319,7 +318,7 @@ var configProfileApplyCmd = &cmds.Command{ ...@@ -319,7 +318,7 @@ var configProfileApplyCmd = &cmds.Command{
return return
} }
err := transformConfig(req.InvocContext().ConfigRoot, "apply-"+req.Arguments()[0], profile.Apply) err := transformConfig(req.InvocContext().ConfigRoot, req.Arguments()[0], profile)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
return return
...@@ -328,34 +327,7 @@ var configProfileApplyCmd = &cmds.Command{ ...@@ -328,34 +327,7 @@ var configProfileApplyCmd = &cmds.Command{
}, },
} }
var configProfileRevertCmd = &cmds.Command{ func transformConfig(configRoot string, configName string, transformer config.Transformer) error {
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 {
r, err := fsrepo.Open(configRoot) r, err := fsrepo.Open(configRoot)
if err != nil { if err != nil {
return err return err
...@@ -372,7 +344,7 @@ func transformConfig(configRoot string, backupName string, transformer config.Tr ...@@ -372,7 +344,7 @@ func transformConfig(configRoot string, backupName string, transformer config.Tr
return err return err
} }
_, err = r.BackupConfig(backupName + "-") _, err = r.BackupConfig("pre-" + configName + "-")
if err != nil { if err != nil {
return err return err
} }
......
...@@ -7,7 +7,8 @@ on a running daemon do not read the config file at runtime. ...@@ -7,7 +7,8 @@ on a running daemon do not read the config file at runtime.
#### Profiles #### Profiles
Configuration profiles allow to tweak configuration quickly. Profiles can be Configuration profiles allow to tweak configuration quickly. Profiles can be
applied with `--profile` flag to `ipfs init` or with `ipfs config profile apply` 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: Available profiles:
- `server` - `server`
...@@ -15,12 +16,21 @@ Available profiles: ...@@ -15,12 +16,21 @@ Available profiles:
Recommended for nodes with public IPv4 address (servers, VPSes, etc.), Recommended for nodes with public IPv4 address (servers, VPSes, etc.),
disables host and content discovery in local networks. 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` - `test`
Reduces external interference, useful for running ipfs in test environments. 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 Note that with these settings node won't be able to talk to the rest of the
network without manual bootstrap. network without manual bootstrap.
- `default-networking`
Restores default network settings. Inverse profile of the `test` profile.
- `badgerds` - `badgerds`
Replaces default datastore configuration with experimental badger datastore. Replaces default datastore configuration with experimental badger datastore.
...@@ -28,8 +38,11 @@ Available profiles: ...@@ -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) 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 WARNING: badger datastore is experimental. Make sure to backup your data
frequently frequently.
- `default-datastore`
Restores default datastore configuration.
## Table of Contents ## Table of Contents
......
...@@ -3,12 +3,6 @@ package config ...@@ -3,12 +3,6 @@ package config
// Transformer is a function which takes configuration and applies some filter to it // Transformer is a function which takes configuration and applies some filter to it
type Transformer func(c *Config) error 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 // 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 // according to http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
var defaultServerFilters = []string{ var defaultServerFilters = []string{
...@@ -30,23 +24,20 @@ var defaultServerFilters = []string{ ...@@ -30,23 +24,20 @@ var defaultServerFilters = []string{
} }
// Profiles is a map holding configuration transformers. Docs are in docs/config.md // Profiles is a map holding configuration transformers. Docs are in docs/config.md
var Profiles = map[string]*Profile{ var Profiles = map[string]Transformer{
"server": { "server": func(c *Config) error {
Apply: func(c *Config) error {
c.Addresses.NoAnnounce = appendSingle(c.Addresses.NoAnnounce, defaultServerFilters) c.Addresses.NoAnnounce = appendSingle(c.Addresses.NoAnnounce, defaultServerFilters)
c.Swarm.AddrFilters = appendSingle(c.Swarm.AddrFilters, defaultServerFilters) c.Swarm.AddrFilters = appendSingle(c.Swarm.AddrFilters, defaultServerFilters)
c.Discovery.MDNS.Enabled = false c.Discovery.MDNS.Enabled = false
return nil return nil
}, },
Revert: func(c *Config) error { "local-discovery": func(c *Config) error {
c.Addresses.NoAnnounce = deleteEntries(c.Addresses.NoAnnounce, defaultServerFilters) c.Addresses.NoAnnounce = deleteEntries(c.Addresses.NoAnnounce, defaultServerFilters)
c.Swarm.AddrFilters = deleteEntries(c.Swarm.AddrFilters, defaultServerFilters) c.Swarm.AddrFilters = deleteEntries(c.Swarm.AddrFilters, defaultServerFilters)
c.Discovery.MDNS.Enabled = true c.Discovery.MDNS.Enabled = true
return nil return nil
}, },
}, "test": func(c *Config) error {
"test": {
Apply: func(c *Config) error {
c.Addresses.API = "/ip4/127.0.0.1/tcp/0" c.Addresses.API = "/ip4/127.0.0.1/tcp/0"
c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0" c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0"
c.Addresses.Swarm = []string{ c.Addresses.Swarm = []string{
...@@ -59,16 +50,14 @@ var Profiles = map[string]*Profile{ ...@@ -59,16 +50,14 @@ var Profiles = map[string]*Profile{
c.Discovery.MDNS.Enabled = false c.Discovery.MDNS.Enabled = false
return nil return nil
}, },
Revert: func(c *Config) error { "default-networking": func(c *Config) error {
c.Addresses = addressesConfig() c.Addresses = addressesConfig()
c.Swarm.DisableNatPortMap = false c.Swarm.DisableNatPortMap = false
c.Discovery.MDNS.Enabled = true c.Discovery.MDNS.Enabled = true
return nil return nil
}, },
}, "badgerds": func(c *Config) error {
"badgerds": {
Apply: func(c *Config) error {
c.Datastore.Spec = map[string]interface{}{ c.Datastore.Spec = map[string]interface{}{
"type": "measure", "type": "measure",
"prefix": "badger.datastore", "prefix": "badger.datastore",
...@@ -80,11 +69,10 @@ var Profiles = map[string]*Profile{ ...@@ -80,11 +69,10 @@ var Profiles = map[string]*Profile{
} }
return nil return nil
}, },
Revert: func(c *Config) error { "default-datastore": func(c *Config) error {
c.Datastore.Spec = DefaultDatastoreConfig().Spec c.Datastore.Spec = DefaultDatastoreConfig().Spec
return nil return nil
}, },
},
} }
func appendSingle(a []string, b []string) []string { func appendSingle(a []string, b []string) []string {
......
...@@ -50,6 +50,7 @@ CONFIG_SET_JSON_TEST='{ ...@@ -50,6 +50,7 @@ CONFIG_SET_JSON_TEST='{
test_profile_apply_revert() { test_profile_apply_revert() {
profile=$1 profile=$1
inverse_profile=$2
test_expect_success "save expected config" ' test_expect_success "save expected config" '
ipfs config show >expected ipfs config show >expected
...@@ -64,11 +65,11 @@ test_profile_apply_revert() { ...@@ -64,11 +65,11 @@ test_profile_apply_revert() {
test_must_fail test_cmp expected actual test_must_fail test_cmp expected actual
' '
test_expect_success "'ipfs config profile revert ${profile}' works" ' test_expect_success "'ipfs config profile apply ${inverse_profile}' works" '
ipfs config profile revert '${profile}' 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 && ipfs config show >actual &&
test_cmp expected actual test_cmp expected actual
' '
...@@ -192,7 +193,7 @@ test_config_cmd() { ...@@ -192,7 +193,7 @@ test_config_cmd() {
' '
test_expect_success "backup was created and looks good" ' 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" ' test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" '
...@@ -200,16 +201,16 @@ test_config_cmd() { ...@@ -200,16 +201,16 @@ test_config_cmd() {
test $(cat actual_config | wc -l) = 17 test $(cat actual_config | wc -l) = 17
' '
test_expect_success "'ipfs config profile revert server' works" ' test_expect_success "'ipfs config profile apply local-discovery' works" '
ipfs config profile revert server 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 && ipfs config Swarm.AddrFilters > actual_config &&
test $(cat actual_config | wc -l) = 1 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 # won't work as we already have this profile applied
# test_profile_apply_revert test # test_profile_apply_revert test
...@@ -219,7 +220,7 @@ test_config_cmd() { ...@@ -219,7 +220,7 @@ test_config_cmd() {
# test_profile_apply_revert badgerds # test_profile_apply_revert badgerds
test_expect_success "cleanup config backups" ' test_expect_success "cleanup config backups" '
find "$IPFS_PATH" -name "config-profile*" -exec rm {} \; find "$IPFS_PATH" -name "config-*" -exec rm {} \;
' '
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论