config: revert profile subcommand
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Sep 5, 2017 at 23:44 UTC
b59354bf6a51b24ac220f50d18847c1516c4d37e
4 files changed
+137
-75
cmd/ipfs/init.go
+2
-2
@@ -160,12 +160,12 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
160
}
161
162
for _, profile := range confProfiles {
163
- transformer, ok := config.ConfigProfiles[profile]
163
+ transformer, ok := config.Profiles[profile]
164
if !ok {
165
return fmt.Errorf("invalid configuration profile: %s", profile)
166
}
167
168
- if err := transformer(conf); err != nil {
168
+ if err := transformer.Apply(conf); err != nil {
169
return err
170
}
171
}
core/commands/config.go
+43
-13
@@ -300,7 +300,8 @@ var configProfileCmd = &cmds.Command{
300
},
301
302
Subcommands: map[string]*cmds.Command{
303
- "apply": configProfileApplyCmd,
303
+ "apply": configProfileApplyCmd,
304
+ "revert": configProfileRevertCmd,
305
},
306
}
307
@@ -314,32 +315,41 @@ var configProfileApplyCmd = &cmds.Command{
315
Run: func(req cmds.Request, res cmds.Response) {
316
args := req.Arguments()
317
317
- profile, ok := config.ConfigProfiles[args[0]]
318
+ profile, ok := config.Profiles[args[0]]
319
if !ok {
320
res.SetError(fmt.Errorf("%s in not a profile", args[0]), cmds.ErrNormal)
321
return
322
}
323
323
- r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
324
+ err := transformConfig(req.InvocContext().ConfigRoot, profile.Apply)
325
if err != nil {
326
res.SetError(err, cmds.ErrNormal)
327
return
328
}
328
- defer r.Close()
329
+ },
330
+}
331
330
- cfg, err := r.Config()
331
- if err != nil {
332
- res.SetError(err, cmds.ErrNormal)
333
- return
334
- }
332
+var configProfileRevertCmd = &cmds.Command{
333
+ Helptext: cmds.HelpText{
334
+ Tagline: "Revert profile changes.",
335
+ ShortDescription: `Reverts profile-related changes to the config.
336
336
- err = profile(cfg)
337
- if err != nil {
338
- res.SetError(err, cmds.ErrNormal)
337
+Reverting some profiles may damage the configuration or not be possible.
338
+Backing up the config before running this command is advised.`,
339
+ },
340
+ Arguments: []cmds.Argument{
341
+ cmds.StringArg("profile", true, false, "The profile to apply to the config."),
342
+ },
343
+ Run: func(req cmds.Request, res cmds.Response) {
344
+ args := req.Arguments()
345
+
346
+ profile, ok := config.Profiles[args[0]]
347
+ if !ok {
348
+ res.SetError(fmt.Errorf("%s in not a profile", args[0]), cmds.ErrNormal)
349
return
350
}
351
342
- err = r.SetConfig(cfg)
352
+ err := transformConfig(req.InvocContext().ConfigRoot, profile.Unapply)
353
if err != nil {
354
res.SetError(err, cmds.ErrNormal)
355
return
@@ -347,6 +357,26 @@ var configProfileApplyCmd = &cmds.Command{
357
},
358
}
359
360
+func transformConfig(configRoot string, transformer config.Transformer) error {
361
+ r, err := fsrepo.Open(configRoot)
362
+ if err != nil {
363
+ return err
364
+ }
365
+ defer r.Close()
366
+
367
+ cfg, err := r.Config()
368
+ if err != nil {
369
+ return err
370
+ }
371
+
372
+ err = transformer(cfg)
373
+ if err != nil {
374
+ return err
375
+ }
376
+
377
+ return r.SetConfig(cfg)
378
+}
379
+
380
func getConfig(r repo.Repo, key string) (*ConfigField, error) {
381
value, err := r.GetConfigKey(key)
382
if err != nil {
repo/config/init.go
+15
-11
@@ -28,17 +28,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
28
29
// setup the node's default addresses.
30
// NOTE: two swarm listen addrs, one tcp, one utp.
31
- Addresses: Addresses{
32
- Swarm: []string{
33
- "/ip4/0.0.0.0/tcp/4001",
34
- // "/ip4/0.0.0.0/udp/4002/utp", // disabled for now.
35
- "/ip6/::/tcp/4001",
36
- },
37
- Announce: []string{},
38
- NoAnnounce: []string{},
39
- API: "/ip4/127.0.0.1/tcp/5001",
40
- Gateway: "/ip4/127.0.0.1/tcp/8080",
41
- },
31
+ Addresses: addressesConfig(),
32
33
Datastore: datastore,
34
Bootstrap: BootstrapPeerStrings(bootstrapPeers),
@@ -97,6 +87,20 @@ const DefaultConnMgrLowWater = 600
87
// grace period
88
const DefaultConnMgrGracePeriod = time.Second * 20
89
90
+func addressesConfig() Addresses {
91
+ return Addresses{
92
+ Swarm: []string{
93
+ "/ip4/0.0.0.0/tcp/4001",
94
+ // "/ip4/0.0.0.0/udp/4002/utp", // disabled for now.
95
+ "/ip6/::/tcp/4001",
96
+ },
97
+ Announce: []string{},
98
+ NoAnnounce: []string{},
99
+ API: "/ip4/127.0.0.1/tcp/5001",
100
+ Gateway: "/ip4/127.0.0.1/tcp/8080",
101
+ }
102
+}
103
+
104
// DefaultDatastoreConfig is an internal function exported to aid in testing.
105
func DefaultDatastoreConfig() Datastore {
106
return Datastore{
repo/config/profile.go
+77
-49
@@ -1,56 +1,84 @@
1
package config
2
3
-// ConfigProfiles is a map holding configuration transformers
4
-var ConfigProfiles = map[string]func(*Config) error{
5
- "server": func(c *Config) error {
6
-
7
- // defaultServerFilters has a list of non-routable IPv4 prefixes
8
- // according to http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
9
- defaultServerFilters := []string{
10
- "/ip4/10.0.0.0/ipcidr/8",
11
- "/ip4/100.64.0.0/ipcidr/10",
12
- "/ip4/169.254.0.0/ipcidr/16",
13
- "/ip4/172.16.0.0/ipcidr/12",
14
- "/ip4/192.0.0.0/ipcidr/24",
15
- "/ip4/192.0.0.0/ipcidr/29",
16
- "/ip4/192.0.0.8/ipcidr/32",
17
- "/ip4/192.0.0.170/ipcidr/32",
18
- "/ip4/192.0.0.171/ipcidr/32",
19
- "/ip4/192.0.2.0/ipcidr/24",
20
- "/ip4/192.168.0.0/ipcidr/16",
21
- "/ip4/198.18.0.0/ipcidr/15",
22
- "/ip4/198.51.100.0/ipcidr/24",
23
- "/ip4/203.0.113.0/ipcidr/24",
24
- "/ip4/240.0.0.0/ipcidr/4",
25
- }
26
-
27
- c.Swarm.AddrFilters = append(c.Swarm.AddrFilters, defaultServerFilters...)
28
- c.Discovery.MDNS.Enabled = false
29
- return nil
3
+type Transformer func(c *Config) error
4
+
5
+type Profile struct {
6
+ Apply Transformer
7
+ Unapply Transformer
8
+}
9
+
10
+// Profiles is a map holding configuration transformers
11
+var Profiles = map[string]*Profile{
12
+ "server": {
13
+ Apply: func(c *Config) error {
14
+
15
+ // defaultServerFilters has a list of non-routable IPv4 prefixes
16
+ // according to http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
17
+ defaultServerFilters := []string{
18
+ "/ip4/10.0.0.0/ipcidr/8",
19
+ "/ip4/100.64.0.0/ipcidr/10",
20
+ "/ip4/169.254.0.0/ipcidr/16",
21
+ "/ip4/172.16.0.0/ipcidr/12",
22
+ "/ip4/192.0.0.0/ipcidr/24",
23
+ "/ip4/192.0.0.0/ipcidr/29",
24
+ "/ip4/192.0.0.8/ipcidr/32",
25
+ "/ip4/192.0.0.170/ipcidr/32",
26
+ "/ip4/192.0.0.171/ipcidr/32",
27
+ "/ip4/192.0.2.0/ipcidr/24",
28
+ "/ip4/192.168.0.0/ipcidr/16",
29
+ "/ip4/198.18.0.0/ipcidr/15",
30
+ "/ip4/198.51.100.0/ipcidr/24",
31
+ "/ip4/203.0.113.0/ipcidr/24",
32
+ "/ip4/240.0.0.0/ipcidr/4",
33
+ }
34
+
35
+ c.Swarm.AddrFilters = append(c.Swarm.AddrFilters, defaultServerFilters...)
36
+ c.Discovery.MDNS.Enabled = false
37
+ return nil
38
+ },
39
+ Unapply: func(c *Config) error {
40
+ c.Swarm.AddrFilters = []string{}
41
+ c.Discovery.MDNS.Enabled = true
42
+ return nil
43
+ },
44
},
31
- "test": func(c *Config) error {
32
- c.Addresses.API = "/ip4/127.0.0.1/tcp/0"
33
- c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0"
34
-
35
- c.Swarm.DisableNatPortMap = true
36
- c.Addresses.Swarm = []string{
37
- "/ip4/127.0.0.1/tcp/0",
38
- }
39
-
40
- c.Bootstrap = []string{}
41
- c.Discovery.MDNS.Enabled = false
42
- return nil
45
+ "test": {
46
+ Apply: func(c *Config) error {
47
+ c.Addresses.API = "/ip4/127.0.0.1/tcp/0"
48
+ c.Addresses.Gateway = "/ip4/127.0.0.1/tcp/0"
49
+ c.Addresses.Swarm = []string{
50
+ "/ip4/127.0.0.1/tcp/0",
51
+ }
52
+
53
+ c.Swarm.DisableNatPortMap = true
54
+
55
+ c.Bootstrap = []string{}
56
+ c.Discovery.MDNS.Enabled = false
57
+ return nil
58
+ },
59
+ Unapply: func(c *Config) error {
60
+ c.Addresses = addressesConfig()
61
+
62
+ c.Swarm.DisableNatPortMap = false
63
+ return nil
64
+ },
65
},
44
- "badgerds": func(c *Config) error {
45
- c.Datastore.Spec = map[string]interface{}{
46
- "type": "measure",
47
- "prefix": "badger.datastore",
48
- "child": map[string]interface{}{
49
- "type": "badgerds",
50
- "path": "badgerds",
51
- "syncWrites": true,
66
+ "badgerds": {
67
+ Apply: func(c *Config) error {
68
+ c.Datastore.Spec = map[string]interface{}{
69
+ "type": "measure",
70
+ "prefix": "badger.datastore",
71
+ "child": map[string]interface{}{
72
+ "type": "badgerds",
73
+ "path": "badgerds",
74
+ "syncWrites": true,
75
+ },
76
+ }
77
+ return nil
78
},
53
- }
54
- return nil
79
+ Unapply: func(c *Config) error {
80
+ c.Datastore.Spec = DefaultDatastoreConfig().Spec
81
+ return nil
82
+ },
83
},
84
}