mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
config-patch: Inverse profiles
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
},
|
||||
"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",
|
||||
}
|
||||
|
||||
c.Swarm.DisableNatPortMap = true
|
||||
|
||||
c.Bootstrap = []string{}
|
||||
c.Discovery.MDNS.Enabled = false
|
||||
return nil
|
||||
},
|
||||
Revert: func(c *Config) error {
|
||||
c.Addresses = addressesConfig()
|
||||
|
||||
c.Swarm.DisableNatPortMap = false
|
||||
c.Discovery.MDNS.Enabled = true
|
||||
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
|
||||
},
|
||||
"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
|
||||
},
|
||||
"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.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
|
||||
},
|
||||
"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
|
||||
},
|
||||
"default-datastore": func(c *Config) error {
|
||||
c.Datastore.Spec = DefaultDatastoreConfig().Spec
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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 {} \;
|
||||
'
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user