From 20d6803dc872a9e8f17f93bbfb4dc310298c1cdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 1 Sep 2017 03:23:31 +0200 Subject: [PATCH 01/11] config: option to apply profile after init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/commands/config.go | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/core/commands/config.go b/core/commands/config.go index 9bfcf035d51..0195f93171f 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -142,6 +142,7 @@ Set the value of the 'Datastore.Path' key: "show": configShowCmd, "edit": configEditCmd, "replace": configReplaceCmd, + "profile": configProfileCmd, }, } @@ -293,6 +294,59 @@ can't be undone. }, } +var configProfileCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Apply profiles to config.", + }, + + Subcommands: map[string]*cmds.Command{ + "apply": configProfileApplyCmd, + }, +} + +var configProfileApplyCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Apply profile to config.", + }, + Arguments: []cmds.Argument{ + cmds.StringArg("profile", true, false, "The profile to apply to the config."), + }, + Run: func(req cmds.Request, res cmds.Response) { + args := req.Arguments() + + profile, ok := config.ConfigProfiles[args[0]] + if !ok { + res.SetError(fmt.Errorf("%s in not a profile", args[0]), cmds.ErrNormal) + return + } + + r, err := fsrepo.Open(req.InvocContext().ConfigRoot) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + defer r.Close() + + cfg, err := r.Config() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + err = profile(cfg) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + err = r.SetConfig(cfg) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + }, +} + func getConfig(r repo.Repo, key string) (*ConfigField, error) { value, err := r.GetConfigKey(key) if err != nil { From b59354bf6a51b24ac220f50d18847c1516c4d37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 5 Sep 2017 23:44:37 +0200 Subject: [PATCH 02/11] config: revert profile subcommand MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- cmd/ipfs/init.go | 4 +- core/commands/config.go | 56 +++++++++++++----- repo/config/init.go | 26 +++++---- repo/config/profile.go | 126 ++++++++++++++++++++++++---------------- 4 files changed, 137 insertions(+), 75 deletions(-) diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 489e74a4d9f..47c51571496 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -160,12 +160,12 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con } for _, profile := range confProfiles { - transformer, ok := config.ConfigProfiles[profile] + transformer, ok := config.Profiles[profile] if !ok { return fmt.Errorf("invalid configuration profile: %s", profile) } - if err := transformer(conf); err != nil { + if err := transformer.Apply(conf); err != nil { return err } } diff --git a/core/commands/config.go b/core/commands/config.go index 0195f93171f..e80185ad2b3 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -300,7 +300,8 @@ var configProfileCmd = &cmds.Command{ }, Subcommands: map[string]*cmds.Command{ - "apply": configProfileApplyCmd, + "apply": configProfileApplyCmd, + "revert": configProfileRevertCmd, }, } @@ -314,32 +315,41 @@ var configProfileApplyCmd = &cmds.Command{ Run: func(req cmds.Request, res cmds.Response) { args := req.Arguments() - profile, ok := config.ConfigProfiles[args[0]] + profile, ok := config.Profiles[args[0]] if !ok { res.SetError(fmt.Errorf("%s in not a profile", args[0]), cmds.ErrNormal) return } - r, err := fsrepo.Open(req.InvocContext().ConfigRoot) + err := transformConfig(req.InvocContext().ConfigRoot, profile.Apply) if err != nil { res.SetError(err, cmds.ErrNormal) return } - defer r.Close() + }, +} - cfg, err := r.Config() - if err != nil { - res.SetError(err, cmds.ErrNormal) - return - } +var configProfileRevertCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Revert profile changes.", + ShortDescription: `Reverts profile-related changes to the config. - err = profile(cfg) - if err != nil { - res.SetError(err, cmds.ErrNormal) +Reverting some profiles may damage the configuration or not be possible. +Backing up the config before running this command is advised.`, + }, + Arguments: []cmds.Argument{ + cmds.StringArg("profile", true, false, "The profile to apply to the config."), + }, + Run: func(req cmds.Request, res cmds.Response) { + args := req.Arguments() + + profile, ok := config.Profiles[args[0]] + if !ok { + res.SetError(fmt.Errorf("%s in not a profile", args[0]), cmds.ErrNormal) return } - err = r.SetConfig(cfg) + err := transformConfig(req.InvocContext().ConfigRoot, profile.Unapply) if err != nil { res.SetError(err, cmds.ErrNormal) return @@ -347,6 +357,26 @@ var configProfileApplyCmd = &cmds.Command{ }, } +func transformConfig(configRoot string, transformer config.Transformer) error { + r, err := fsrepo.Open(configRoot) + if err != nil { + return err + } + defer r.Close() + + cfg, err := r.Config() + if err != nil { + return err + } + + err = transformer(cfg) + if err != nil { + return err + } + + return r.SetConfig(cfg) +} + func getConfig(r repo.Repo, key string) (*ConfigField, error) { value, err := r.GetConfigKey(key) if err != nil { diff --git a/repo/config/init.go b/repo/config/init.go index 1c7874e3d90..2b1def8b522 100644 --- a/repo/config/init.go +++ b/repo/config/init.go @@ -28,17 +28,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) { // setup the node's default addresses. // NOTE: two swarm listen addrs, one tcp, one utp. - Addresses: Addresses{ - Swarm: []string{ - "/ip4/0.0.0.0/tcp/4001", - // "/ip4/0.0.0.0/udp/4002/utp", // disabled for now. - "/ip6/::/tcp/4001", - }, - Announce: []string{}, - NoAnnounce: []string{}, - API: "/ip4/127.0.0.1/tcp/5001", - Gateway: "/ip4/127.0.0.1/tcp/8080", - }, + Addresses: addressesConfig(), Datastore: datastore, Bootstrap: BootstrapPeerStrings(bootstrapPeers), @@ -97,6 +87,20 @@ const DefaultConnMgrLowWater = 600 // grace period const DefaultConnMgrGracePeriod = time.Second * 20 +func addressesConfig() Addresses { + return Addresses{ + Swarm: []string{ + "/ip4/0.0.0.0/tcp/4001", + // "/ip4/0.0.0.0/udp/4002/utp", // disabled for now. + "/ip6/::/tcp/4001", + }, + Announce: []string{}, + NoAnnounce: []string{}, + API: "/ip4/127.0.0.1/tcp/5001", + Gateway: "/ip4/127.0.0.1/tcp/8080", + } +} + // DefaultDatastoreConfig is an internal function exported to aid in testing. func DefaultDatastoreConfig() Datastore { return Datastore{ diff --git a/repo/config/profile.go b/repo/config/profile.go index 3a7bf3694f3..a4160306a8b 100644 --- a/repo/config/profile.go +++ b/repo/config/profile.go @@ -1,56 +1,84 @@ package config -// ConfigProfiles is a map holding configuration transformers -var ConfigProfiles = map[string]func(*Config) error{ - "server": func(c *Config) error { - - // 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 - defaultServerFilters := []string{ - "/ip4/10.0.0.0/ipcidr/8", - "/ip4/100.64.0.0/ipcidr/10", - "/ip4/169.254.0.0/ipcidr/16", - "/ip4/172.16.0.0/ipcidr/12", - "/ip4/192.0.0.0/ipcidr/24", - "/ip4/192.0.0.0/ipcidr/29", - "/ip4/192.0.0.8/ipcidr/32", - "/ip4/192.0.0.170/ipcidr/32", - "/ip4/192.0.0.171/ipcidr/32", - "/ip4/192.0.2.0/ipcidr/24", - "/ip4/192.168.0.0/ipcidr/16", - "/ip4/198.18.0.0/ipcidr/15", - "/ip4/198.51.100.0/ipcidr/24", - "/ip4/203.0.113.0/ipcidr/24", - "/ip4/240.0.0.0/ipcidr/4", - } - - c.Swarm.AddrFilters = append(c.Swarm.AddrFilters, defaultServerFilters...) - c.Discovery.MDNS.Enabled = false - return nil +type Transformer func(c *Config) error + +type Profile struct { + Apply Transformer + Unapply Transformer +} + +// Profiles is a map holding configuration transformers +var Profiles = map[string]*Profile{ + "server": { + Apply: func(c *Config) error { + + // 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 + defaultServerFilters := []string{ + "/ip4/10.0.0.0/ipcidr/8", + "/ip4/100.64.0.0/ipcidr/10", + "/ip4/169.254.0.0/ipcidr/16", + "/ip4/172.16.0.0/ipcidr/12", + "/ip4/192.0.0.0/ipcidr/24", + "/ip4/192.0.0.0/ipcidr/29", + "/ip4/192.0.0.8/ipcidr/32", + "/ip4/192.0.0.170/ipcidr/32", + "/ip4/192.0.0.171/ipcidr/32", + "/ip4/192.0.2.0/ipcidr/24", + "/ip4/192.168.0.0/ipcidr/16", + "/ip4/198.18.0.0/ipcidr/15", + "/ip4/198.51.100.0/ipcidr/24", + "/ip4/203.0.113.0/ipcidr/24", + "/ip4/240.0.0.0/ipcidr/4", + } + + c.Swarm.AddrFilters = append(c.Swarm.AddrFilters, defaultServerFilters...) + c.Discovery.MDNS.Enabled = false + return nil + }, + Unapply: func(c *Config) error { + c.Swarm.AddrFilters = []string{} + c.Discovery.MDNS.Enabled = true + 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.Swarm.DisableNatPortMap = true - c.Addresses.Swarm = []string{ - "/ip4/127.0.0.1/tcp/0", - } - - c.Bootstrap = []string{} - 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 + }, + Unapply: func(c *Config) error { + c.Addresses = addressesConfig() + + c.Swarm.DisableNatPortMap = false + 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, + "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 }, - } - return nil + Unapply: func(c *Config) error { + c.Datastore.Spec = DefaultDatastoreConfig().Spec + return nil + }, }, } From d7376cdab054b5f62a17da3909449c0b89104d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 6 Sep 2017 13:54:45 +0200 Subject: [PATCH 03/11] config: profile tests, docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- docs/config.md | 15 +++++++++++++++ repo/config/profile.go | 24 ++++++++++++------------ test/sharness/t0021-config.sh | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/docs/config.md b/docs/config.md index 6c129e44f23..e1da0da9b09 100644 --- a/docs/config.md +++ b/docs/config.md @@ -17,6 +17,7 @@ on a running daemon do not read the config file at runtime. - [`Mounts`](#mounts) - [`Reprovider`](#reprovider) - [`Swarm`](#swarm) +- [`Profiles`](#profiles) ## `Addresses` Contains information about various listener addresses to be used by this node. @@ -287,3 +288,17 @@ LowWater is the minimum number of connections to maintain. HighWater is the number of connections that, when exceeded, will trigger a connection GC operation. - `GracePeriod` GracePeriod is a time duration that new connections are immune from being closed by the connection manager. + +## 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. + +- `server` profile +Recommended for nodes with public IPv4 address, disables host and content +discovery in local networks. + +- `test` profile +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. diff --git a/repo/config/profile.go b/repo/config/profile.go index a4160306a8b..7711d9e7c4f 100644 --- a/repo/config/profile.go +++ b/repo/config/profile.go @@ -7,7 +7,7 @@ type Profile struct { Unapply Transformer } -// Profiles is a map holding configuration transformers +// Profiles is a map holding configuration transformers. Docs are in docs/config.md var Profiles = map[string]*Profile{ "server": { Apply: func(c *Config) error { @@ -65,17 +65,17 @@ var Profiles = map[string]*Profile{ }, "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 - }, + c.Datastore.Spec = map[string]interface{}{ + "type": "measure", + "prefix": "badger.datastore", + "child": map[string]interface{}{ + "type": "badgerds", + "path": "badgerds", + "syncWrites": true, + }, + } + return nil + }, Unapply: 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 b7b985a8963..be1ce81585b 100755 --- a/test/sharness/t0021-config.sh +++ b/test/sharness/t0021-config.sh @@ -151,6 +151,30 @@ test_config_cmd() { echo "Error: setting private key with API is not supported" > replace_expected test_cmp replace_out replace_expected ' + + test_expect_success "'ipfs config Swarm.AddrFilters' looks good" ' + ipfs config Swarm.AddrFilters > actual_config && + test $(cat actual_config | wc -l) = 1 + ' + + test_expect_success "'ipfs config profile apply server' works" ' + ipfs config profile apply server + ' + + 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 Swarm.AddrFilters' looks good with reverted server profile" ' + ipfs config Swarm.AddrFilters > actual_config && + test $(cat actual_config | wc -l) = 1 + ' + } test_init_ipfs From ed8817961ec723af89aad5403d968f1be97831b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 22 Sep 2017 00:13:58 +0200 Subject: [PATCH 04/11] conifg-patch: apply review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/commands/config.go | 12 ++++-------- repo/config/profile.go | 4 ++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/commands/config.go b/core/commands/config.go index e80185ad2b3..51093d601e4 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -313,11 +313,9 @@ var configProfileApplyCmd = &cmds.Command{ cmds.StringArg("profile", true, false, "The profile to apply to the config."), }, Run: func(req cmds.Request, res cmds.Response) { - args := req.Arguments() - - profile, ok := config.Profiles[args[0]] + profile, ok := config.Profiles[req.Arguments()[0]] if !ok { - res.SetError(fmt.Errorf("%s in not a profile", args[0]), cmds.ErrNormal) + res.SetError(fmt.Errorf("%s is not a profile", req.Arguments()[0]), cmds.ErrNormal) return } @@ -341,11 +339,9 @@ Backing up the config before running this command is advised.`, cmds.StringArg("profile", true, false, "The profile to apply to the config."), }, Run: func(req cmds.Request, res cmds.Response) { - args := req.Arguments() - - profile, ok := config.Profiles[args[0]] + profile, ok := config.Profiles[req.Arguments()[0]] if !ok { - res.SetError(fmt.Errorf("%s in not a profile", args[0]), cmds.ErrNormal) + res.SetError(fmt.Errorf("%s is not a profile", req.Arguments()[0]), cmds.ErrNormal) return } diff --git a/repo/config/profile.go b/repo/config/profile.go index 7711d9e7c4f..92fdbbc8e71 100644 --- a/repo/config/profile.go +++ b/repo/config/profile.go @@ -1,7 +1,9 @@ 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 Unapply Transformer @@ -32,11 +34,13 @@ var Profiles = map[string]*Profile{ "/ip4/240.0.0.0/ipcidr/4", } + c.Addresses.NoAnnounce = append(c.Addresses.NoAnnounce, defaultServerFilters...) c.Swarm.AddrFilters = append(c.Swarm.AddrFilters, defaultServerFilters...) c.Discovery.MDNS.Enabled = false return nil }, Unapply: func(c *Config) error { + c.Addresses.NoAnnounce = []string{} c.Swarm.AddrFilters = []string{} c.Discovery.MDNS.Enabled = true return nil From 75b235d05cc2bcbec52fa7843bc348332461ad94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sun, 29 Oct 2017 20:54:07 +0100 Subject: [PATCH 05/11] config docs: Move profile section to the top MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- docs/config.md | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/docs/config.md b/docs/config.md index e1da0da9b09..bae5ed80a21 100644 --- a/docs/config.md +++ b/docs/config.md @@ -4,6 +4,20 @@ The go-ipfs config file is a json document. It is read once at node instantiatio either for an offline command, or when starting the daemon. Commands that execute 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. + +- `server` profile +Recommended for nodes with public IPv4 address, disables host and content +discovery in local networks. + +- `test` profile +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. + ## Table of Contents - [`Addresses`](#addresses) @@ -17,7 +31,6 @@ on a running daemon do not read the config file at runtime. - [`Mounts`](#mounts) - [`Reprovider`](#reprovider) - [`Swarm`](#swarm) -- [`Profiles`](#profiles) ## `Addresses` Contains information about various listener addresses to be used by this node. @@ -288,17 +301,3 @@ LowWater is the minimum number of connections to maintain. HighWater is the number of connections that, when exceeded, will trigger a connection GC operation. - `GracePeriod` GracePeriod is a time duration that new connections are immune from being closed by the connection manager. - -## 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. - -- `server` profile -Recommended for nodes with public IPv4 address, disables host and content -discovery in local networks. - -- `test` profile -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. From 9312fa5f0f85c59ddefe558c04e21451b4fd0fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sun, 5 Nov 2017 14:21:14 +0100 Subject: [PATCH 06/11] config: rename profile.Unapply to Revert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/commands/config.go | 2 +- repo/config/profile.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/commands/config.go b/core/commands/config.go index 51093d601e4..ba400416bbc 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -345,7 +345,7 @@ Backing up the config before running this command is advised.`, return } - err := transformConfig(req.InvocContext().ConfigRoot, profile.Unapply) + err := transformConfig(req.InvocContext().ConfigRoot, profile.Revert) if err != nil { res.SetError(err, cmds.ErrNormal) return diff --git a/repo/config/profile.go b/repo/config/profile.go index 92fdbbc8e71..061a41408eb 100644 --- a/repo/config/profile.go +++ b/repo/config/profile.go @@ -5,8 +5,8 @@ type Transformer func(c *Config) error // Profile applies some set of changes to the configuration type Profile struct { - Apply Transformer - Unapply Transformer + Apply Transformer + Revert Transformer } // Profiles is a map holding configuration transformers. Docs are in docs/config.md @@ -39,7 +39,7 @@ var Profiles = map[string]*Profile{ c.Discovery.MDNS.Enabled = false return nil }, - Unapply: func(c *Config) error { + Revert: func(c *Config) error { c.Addresses.NoAnnounce = []string{} c.Swarm.AddrFilters = []string{} c.Discovery.MDNS.Enabled = true @@ -60,7 +60,7 @@ var Profiles = map[string]*Profile{ c.Discovery.MDNS.Enabled = false return nil }, - Unapply: func(c *Config) error { + Revert: func(c *Config) error { c.Addresses = addressesConfig() c.Swarm.DisableNatPortMap = false @@ -80,7 +80,7 @@ var Profiles = map[string]*Profile{ } return nil }, - Unapply: func(c *Config) error { + Revert: func(c *Config) error { c.Datastore.Spec = DefaultDatastoreConfig().Spec return nil }, From c573d3d051bae5570404b77756592ba1689ae205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sat, 25 Nov 2017 03:01:35 +0100 Subject: [PATCH 07/11] config-patch: update to new commands lib MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/commands/config.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/core/commands/config.go b/core/commands/config.go index ba400416bbc..b4ba6e7f2f5 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -295,7 +295,7 @@ can't be undone. } var configProfileCmd = &cmds.Command{ - Helptext: cmds.HelpText{ + Helptext: cmdkit.HelpText{ Tagline: "Apply profiles to config.", }, @@ -306,48 +306,48 @@ var configProfileCmd = &cmds.Command{ } var configProfileApplyCmd = &cmds.Command{ - Helptext: cmds.HelpText{ + Helptext: cmdkit.HelpText{ Tagline: "Apply profile to config.", }, - Arguments: []cmds.Argument{ - cmds.StringArg("profile", true, false, "The profile to apply to the config."), + 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]), cmds.ErrNormal) + res.SetError(fmt.Errorf("%s is not a profile", req.Arguments()[0]), cmdkit.ErrNormal) return } err := transformConfig(req.InvocContext().ConfigRoot, profile.Apply) if err != nil { - res.SetError(err, cmds.ErrNormal) + res.SetError(err, cmdkit.ErrNormal) return } }, } var configProfileRevertCmd = &cmds.Command{ - Helptext: cmds.HelpText{ + Helptext: cmdkit.HelpText{ Tagline: "Revert profile changes.", ShortDescription: `Reverts profile-related changes to the config. Reverting some profiles may damage the configuration or not be possible. Backing up the config before running this command is advised.`, }, - Arguments: []cmds.Argument{ - cmds.StringArg("profile", true, false, "The profile to apply to the config."), + 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]), cmds.ErrNormal) + res.SetError(fmt.Errorf("%s is not a profile", req.Arguments()[0]), cmdkit.ErrNormal) return } err := transformConfig(req.InvocContext().ConfigRoot, profile.Revert) if err != nil { - res.SetError(err, cmds.ErrNormal) + res.SetError(err, cmdkit.ErrNormal) return } }, From 2514c747505b034c488f529692418c9266135970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sat, 25 Nov 2017 03:16:30 +0100 Subject: [PATCH 08/11] config-patch: apply review suggestions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/commands/config.go | 4 +++- docs/config.md | 29 +++++++++++++++++++++-------- repo/config/profile.go | 1 + test/sharness/t0021-config.sh | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/core/commands/config.go b/core/commands/config.go index b4ba6e7f2f5..8736dd77f67 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -324,13 +324,14 @@ var configProfileApplyCmd = &cmds.Command{ 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 config. + 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.`, @@ -350,6 +351,7 @@ Backing up the config before running this command is advised.`, res.SetError(err, cmdkit.ErrNormal) return } + res.SetOutput(nil) }, } diff --git a/docs/config.md b/docs/config.md index bae5ed80a21..e4cff76f8bd 100644 --- a/docs/config.md +++ b/docs/config.md @@ -9,14 +9,27 @@ Configuration profiles allow to tweak configuration quickly. Profiles can be applied with `--profile` flag to `ipfs init` or with `ipfs config profile apply` command. -- `server` profile -Recommended for nodes with public IPv4 address, disables host and content -discovery in local networks. - -- `test` profile -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. +Available profiles: +- `server` + + Recommended for nodes with public IPv4 address, disables host and content + 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. + +- `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 experimantal. Make sure to backup your data + frequently + ## Table of Contents diff --git a/repo/config/profile.go b/repo/config/profile.go index 061a41408eb..529cf3a97d3 100644 --- a/repo/config/profile.go +++ b/repo/config/profile.go @@ -64,6 +64,7 @@ var Profiles = map[string]*Profile{ c.Addresses = addressesConfig() c.Swarm.DisableNatPortMap = false + c.Discovery.MDNS.Enabled = true return nil }, }, diff --git a/test/sharness/t0021-config.sh b/test/sharness/t0021-config.sh index be1ce81585b..a7ff59bcfcb 100755 --- a/test/sharness/t0021-config.sh +++ b/test/sharness/t0021-config.sh @@ -48,6 +48,32 @@ CONFIG_SET_JSON_TEST='{ } }' +test_profile_apply_revert() { + profile=$1 + + test_expect_success "save expected config" ' + ipfs config show >expected + ' + + test_expect_success "'ipfs config profile apply ${profile}' works" ' + ipfs config profile apply '${profile}' + ' + + test_expect_success "profile ${profile} changed something" ' + ipfs config show >actual && + test_must_fail test_cmp expected actual + ' + + test_expect_success "'ipfs config profile revert ${profile}' works" ' + ipfs config profile revert '${profile}' + ' + + test_expect_success "config is back to previous state after ${profile} revert" ' + ipfs config show >actual && + test_cmp expected actual + ' +} + test_config_cmd() { test_config_cmd_set "beep" "boop" test_config_cmd_set "beep1" "boop2" @@ -175,6 +201,14 @@ test_config_cmd() { test $(cat actual_config | wc -l) = 1 ' + test_profile_apply_revert server + + # won't work as we already have this profile applied + # test_profile_apply_revert test + + # won't work as it changes datastore definition, which makes ipfs not launch + # without converting first + # test_profile_apply_revert badgerds } test_init_ipfs From 0ff9b24a32fcaa33a4d11f087542704c38daa904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 15 Dec 2017 20:55:31 +0100 Subject: [PATCH 09/11] config-patch: backup config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/commands/config.go | 5 +++++ repo/fsrepo/fsrepo.go | 26 ++++++++++++++++++++++++++ repo/mock.go | 4 ++++ repo/repo.go | 1 + test/sharness/t0021-config.sh | 12 ++++++++++++ 5 files changed, 48 insertions(+) diff --git a/core/commands/config.go b/core/commands/config.go index 8736dd77f67..24875c3ea1c 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -372,6 +372,11 @@ func transformConfig(configRoot string, transformer config.Transformer) error { return err } + _, err = r.BackupConfig("profile-") + if err != nil { + return err + } + return r.SetConfig(cfg) } diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index 757c36438ae..51823042a77 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -480,6 +480,32 @@ func (r *FSRepo) FileManager() *filestore.FileManager { return r.filemgr } +func (r *FSRepo) BackupConfig(prefix string) (string, error) { + temp, err := ioutil.TempFile(r.path, "config-"+prefix) + if err != nil { + return "", err + } + defer temp.Close() + + configFilename, err := config.Filename(r.path) + if err != nil { + return "", err + } + + orig, err := os.OpenFile(configFilename, os.O_RDONLY, 0600) + if err != nil { + return "", err + } + defer orig.Close() + + _, err = io.Copy(temp, orig) + if err != nil { + return "", err + } + + return orig.Name(), nil +} + // setConfigUnsynced is for private use. func (r *FSRepo) setConfigUnsynced(updated *config.Config) error { configFilename, err := config.Filename(r.path) diff --git a/repo/mock.go b/repo/mock.go index 030c2ff28d1..ad9ddcf6cb0 100644 --- a/repo/mock.go +++ b/repo/mock.go @@ -28,6 +28,10 @@ func (m *Mock) SetConfig(updated *config.Config) error { return nil } +func (m *Mock) BackupConfig(prefix string) (string, error) { + return "", errTODO +} + func (m *Mock) SetConfigKey(key string, value interface{}) error { return errTODO } diff --git a/repo/repo.go b/repo/repo.go index 0cbf8f53669..3403482c9ea 100644 --- a/repo/repo.go +++ b/repo/repo.go @@ -18,6 +18,7 @@ var ( type Repo interface { Config() (*config.Config, error) + BackupConfig(prefix string) (string, error) SetConfig(*config.Config) error SetConfigKey(key string, value interface{}) error diff --git a/test/sharness/t0021-config.sh b/test/sharness/t0021-config.sh index a7ff59bcfcb..9a086b1d8dd 100755 --- a/test/sharness/t0021-config.sh +++ b/test/sharness/t0021-config.sh @@ -183,10 +183,18 @@ test_config_cmd() { test $(cat actual_config | wc -l) = 1 ' + test_expect_success "copy ipfs config" ' + cp "$IPFS_PATH/config" before_patch + ' + test_expect_success "'ipfs config profile apply server' works" ' ipfs config profile apply server ' + test_expect_success "backup was created and looks good" ' + test_cmp "$(find "$IPFS_PATH" -name "config-profile*")" 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 @@ -209,6 +217,10 @@ test_config_cmd() { # won't work as it changes datastore definition, which makes ipfs not launch # without converting first # test_profile_apply_revert badgerds + + test_expect_success "cleanup config backups" ' + find "$IPFS_PATH" -name "config-profile*" -exec rm {} \; + ' } test_init_ipfs From acb4edcce4ede6ef0f0509d06e7c80e4ac26960c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sat, 16 Dec 2017 18:16:43 +0100 Subject: [PATCH 10/11] config-patch: docs typo, fix server profile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/commands/config.go | 8 ++--- docs/config.md | 6 ++-- repo/config/profile.go | 79 ++++++++++++++++++++++++++++------------- 3 files changed, 61 insertions(+), 32 deletions(-) diff --git a/core/commands/config.go b/core/commands/config.go index 24875c3ea1c..a5011f25872 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -319,7 +319,7 @@ var configProfileApplyCmd = &cmds.Command{ return } - err := transformConfig(req.InvocContext().ConfigRoot, profile.Apply) + err := transformConfig(req.InvocContext().ConfigRoot, "apply-"+req.Arguments()[0], profile.Apply) if err != nil { res.SetError(err, cmdkit.ErrNormal) return @@ -346,7 +346,7 @@ Backing up the config before running this command is advised.`, return } - err := transformConfig(req.InvocContext().ConfigRoot, profile.Revert) + err := transformConfig(req.InvocContext().ConfigRoot, "revert-"+req.Arguments()[0], profile.Revert) if err != nil { res.SetError(err, cmdkit.ErrNormal) return @@ -355,7 +355,7 @@ Backing up the config before running this command is advised.`, }, } -func transformConfig(configRoot string, transformer config.Transformer) error { +func transformConfig(configRoot string, backupName string, transformer config.Transformer) error { r, err := fsrepo.Open(configRoot) if err != nil { return err @@ -372,7 +372,7 @@ func transformConfig(configRoot string, transformer config.Transformer) error { return err } - _, err = r.BackupConfig("profile-") + _, err = r.BackupConfig(backupName + "-") if err != nil { return err } diff --git a/docs/config.md b/docs/config.md index e4cff76f8bd..1376414bf3a 100644 --- a/docs/config.md +++ b/docs/config.md @@ -12,8 +12,8 @@ command. Available profiles: - `server` - Recommended for nodes with public IPv4 address, disables host and content - discovery in local networks. + Recommended for nodes with public IPv4 address (servers, VPSes, etc.), + disables host and content discovery in local networks. - `test` @@ -27,7 +27,7 @@ Available profiles: 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 experimantal. Make sure to backup your data + WARNING: badger datastore is experimental. Make sure to backup your data frequently diff --git a/repo/config/profile.go b/repo/config/profile.go index 529cf3a97d3..87668357f66 100644 --- a/repo/config/profile.go +++ b/repo/config/profile.go @@ -9,39 +9,38 @@ type Profile struct { 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{ + "/ip4/10.0.0.0/ipcidr/8", + "/ip4/100.64.0.0/ipcidr/10", + "/ip4/169.254.0.0/ipcidr/16", + "/ip4/172.16.0.0/ipcidr/12", + "/ip4/192.0.0.0/ipcidr/24", + "/ip4/192.0.0.0/ipcidr/29", + "/ip4/192.0.0.8/ipcidr/32", + "/ip4/192.0.0.170/ipcidr/32", + "/ip4/192.0.0.171/ipcidr/32", + "/ip4/192.0.2.0/ipcidr/24", + "/ip4/192.168.0.0/ipcidr/16", + "/ip4/198.18.0.0/ipcidr/15", + "/ip4/198.51.100.0/ipcidr/24", + "/ip4/203.0.113.0/ipcidr/24", + "/ip4/240.0.0.0/ipcidr/4", +} + // Profiles is a map holding configuration transformers. Docs are in docs/config.md var Profiles = map[string]*Profile{ "server": { Apply: func(c *Config) error { - - // 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 - defaultServerFilters := []string{ - "/ip4/10.0.0.0/ipcidr/8", - "/ip4/100.64.0.0/ipcidr/10", - "/ip4/169.254.0.0/ipcidr/16", - "/ip4/172.16.0.0/ipcidr/12", - "/ip4/192.0.0.0/ipcidr/24", - "/ip4/192.0.0.0/ipcidr/29", - "/ip4/192.0.0.8/ipcidr/32", - "/ip4/192.0.0.170/ipcidr/32", - "/ip4/192.0.0.171/ipcidr/32", - "/ip4/192.0.2.0/ipcidr/24", - "/ip4/192.168.0.0/ipcidr/16", - "/ip4/198.18.0.0/ipcidr/15", - "/ip4/198.51.100.0/ipcidr/24", - "/ip4/203.0.113.0/ipcidr/24", - "/ip4/240.0.0.0/ipcidr/4", - } - - c.Addresses.NoAnnounce = append(c.Addresses.NoAnnounce, defaultServerFilters...) - c.Swarm.AddrFilters = append(c.Swarm.AddrFilters, defaultServerFilters...) + 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 = []string{} - c.Swarm.AddrFilters = []string{} + c.Addresses.NoAnnounce = deleteEntries(c.Addresses.NoAnnounce, defaultServerFilters) + c.Swarm.AddrFilters = deleteEntries(c.Swarm.AddrFilters, defaultServerFilters) c.Discovery.MDNS.Enabled = true return nil }, @@ -87,3 +86,33 @@ var Profiles = map[string]*Profile{ }, }, } + +func appendSingle(a []string, b []string) []string { + m := map[string]struct{}{} + for _, f := range a { + m[f] = struct{}{} + } + for _, f := range b { + m[f] = struct{}{} + } + return mapKeys(m) +} + +func deleteEntries(arr []string, del []string) []string { + m := map[string]struct{}{} + for _, f := range arr { + m[f] = struct{}{} + } + for _, f := range del { + delete(m, f) + } + return mapKeys(m) +} + +func mapKeys(m map[string]struct{}) []string { + out := make([]string, 0, len(m)) + for f := range m { + out = append(out, f) + } + return out +} From ac26cf19cd65ab9ec5ecc031a0e0a7fb797c5439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sat, 16 Dec 2017 18:59:46 +0100 Subject: [PATCH 11/11] config-patch: Inverse profiles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- cmd/ipfs/init.go | 2 +- core/commands/config.go | 36 ++----------- docs/config.md | 17 +++++- repo/config/profile.go | 98 +++++++++++++++-------------------- test/sharness/t0021-config.sh | 19 +++---- 5 files changed, 73 insertions(+), 99 deletions(-) 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 {} \; ' }