Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 37073e6

Browse files
hugomrdiasachingbrain
authored andcommitted
fix: add profiles docs, support in validation and tests (#2545)
* fix: add profiles docs, support in validation and tests * feat: support ipfs daemon --init-config
1 parent cdc77a3 commit 37073e6

File tree

9 files changed

+115
-31
lines changed

9 files changed

+115
-31
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ Instead of a boolean, you may provide an object with custom initialization optio
307307
})
308308
```
309309
- `pass` (string) A passphrase to encrypt keys. You should generally use the [top-level `pass` option](#optionspass) instead of the `init.pass` option (this one will take its value from the top-level option if not set).
310+
- `profiles` (Array) Apply profile settings to config.
310311

311312
##### `options.start`
312313

docs/config.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
22

33
The js-ipfs config file is a JSON document located in the root directory of the js-ipfs repository.
44

5+
#### Profiles
6+
7+
Configuration profiles allow to tweak configuration quickly. Profiles can be
8+
applied with `--profile` flag to `ipfs init` or with the `ipfs config profile
9+
apply` command. When a profile is applied a backup of the configuration file
10+
will be created in `$IPFS_PATH`.
11+
12+
Available profiles:
13+
14+
- `server`
15+
16+
Recommended for nodes with public IPv4 address (servers, VPSes, etc.),
17+
disables host and content discovery in local networks.
18+
19+
- `local-discovery`
20+
21+
Sets default values to fields affected by `server` profile, enables
22+
discovery in local networks.
23+
24+
- `test`
25+
26+
Reduces external interference, useful for running ipfs in test environments.
27+
Note that with these settings node won't be able to talk to the rest of the
28+
network without manual bootstrap.
29+
30+
- `default-networking`
31+
32+
Restores default network settings. Inverse profile of the `test` profile.
33+
34+
- `lowpower`
35+
36+
Reduces daemon overhead on the system. May affect node functionality,
37+
performance of content discovery and data fetching may be degraded.
38+
39+
- `default-power`
40+
41+
Inverse of "lowpower" profile.
42+
543
## Table of Contents
644

745
- [`Addresses`](#addresses)
@@ -121,18 +159,18 @@ Options for Multicast DNS peer discovery:
121159
- `Enabled`
122160

123161
A boolean value for whether or not MDNS should be active.
124-
162+
125163
Default: `true`
126164

127165
- `Interval`
128166

129167
A number of seconds to wait between discovery checks.
130-
168+
131169
Default: `10`
132170

133171
### `webRTCStar`
134172

135-
WebRTCStar is a discovery mechanism prvided by a signalling-star that allows peer-to-peer communications in the browser.
173+
WebRTCStar is a discovery mechanism prvided by a signalling-star that allows peer-to-peer communications in the browser.
136174

137175
Options for webRTCstar peer discovery:
138176

src/cli/commands/daemon.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ module.exports = {
1919
type: 'string',
2020
desc: 'Path to existing configuration file to be loaded during --init.'
2121
})
22+
.option('init-profile', {
23+
type: 'string',
24+
desc: 'Configuration profiles to apply for --init. See ipfs init --help for more.',
25+
coerce: (value) => {
26+
return (value || '').split(',')
27+
}
28+
})
2229
.option('enable-sharding-experiment', {
2330
type: 'boolean',
2431
default: false
@@ -73,7 +80,8 @@ module.exports = {
7380
ipnsPubsub: argv.enableNamesysPubsub,
7481
dht: argv.enableDhtExperiment,
7582
sharding: argv.enableShardingExperiment
76-
}
83+
},
84+
init: argv.initProfile ? { profiles: argv.initProfile } : true
7785
})
7886

7987
try {

src/cli/daemon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Daemon {
5454
}
5555

5656
// start the daemon
57-
const ipfsOpts = Object.assign({ }, this._options, { init: true, start: true, libp2p })
57+
const ipfsOpts = Object.assign({ }, { init: true, start: true, libp2p }, this._options)
5858
const ipfs = new IPFS(ipfsOpts)
5959

6060
await new Promise((resolve, reject) => {

src/core/components/config.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async function listProfiles (options) { // eslint-disable-line require-await
5656

5757
const profiles = {
5858
server: {
59-
description: 'Disables local host discovery - recommended when running IPFS on machines with public IPv4 addresses.',
59+
description: 'Recommended for nodes with public IPv4 address (servers, VPSes, etc.), disables host and content discovery in local networks.',
6060
transform: (config) => {
6161
config.Discovery.MDNS.Enabled = false
6262
config.Discovery.webRTCStar.Enabled = false
@@ -65,37 +65,16 @@ const profiles = {
6565
}
6666
},
6767
'local-discovery': {
68-
description: 'Enables local host discovery - inverse of "server" profile.',
68+
description: 'Sets default values to fields affected by `server` profile, enables discovery in local networks.',
6969
transform: (config) => {
7070
config.Discovery.MDNS.Enabled = true
7171
config.Discovery.webRTCStar.Enabled = true
7272

7373
return config
7474
}
7575
},
76-
lowpower: {
77-
description: 'Reduces daemon overhead on the system - recommended for low power systems.',
78-
transform: (config) => {
79-
config.Swarm = config.Swarm || {}
80-
config.Swarm.ConnMgr = config.Swarm.ConnMgr || {}
81-
config.Swarm.ConnMgr.LowWater = 20
82-
config.Swarm.ConnMgr.HighWater = 40
83-
84-
return config
85-
}
86-
},
87-
'default-power': {
88-
description: 'Inverse of "lowpower" profile.',
89-
transform: (config) => {
90-
const defaultConfig = getDefaultConfig()
91-
92-
config.Swarm = defaultConfig.Swarm
93-
94-
return config
95-
}
96-
},
9776
test: {
98-
description: 'Reduces external interference of IPFS daemon - for running the daemon in test environments.',
77+
description: '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.',
9978
transform: (config) => {
10079
const defaultConfig = getDefaultConfig()
10180

@@ -110,7 +89,7 @@ const profiles = {
11089
}
11190
},
11291
'default-networking': {
113-
description: 'Restores default network settings - inverse of "test" profile.',
92+
description: 'Restores default network settings. Inverse profile of the `test` profile.',
11493
transform: (config) => {
11594
const defaultConfig = getDefaultConfig()
11695

@@ -121,9 +100,31 @@ const profiles = {
121100
config.Discovery.MDNS.Enabled = defaultConfig.Discovery.MDNS.Enabled
122101
config.Discovery.webRTCStar.Enabled = defaultConfig.Discovery.webRTCStar.Enabled
123102

103+
return config
104+
}
105+
},
106+
lowpower: {
107+
description: 'Reduces daemon overhead on the system. May affect node functionality,performance of content discovery and data fetching may be degraded. Recommended for low power systems.',
108+
transform: (config) => {
109+
config.Swarm = config.Swarm || {}
110+
config.Swarm.ConnMgr = config.Swarm.ConnMgr || {}
111+
config.Swarm.ConnMgr.LowWater = 20
112+
config.Swarm.ConnMgr.HighWater = 40
113+
114+
return config
115+
}
116+
},
117+
'default-power': {
118+
description: 'Inverse of "lowpower" profile.',
119+
transform: (config) => {
120+
const defaultConfig = getDefaultConfig()
121+
122+
config.Swarm = defaultConfig.Swarm
123+
124124
return config
125125
}
126126
}
127+
127128
}
128129

129130
module.exports.profiles = profiles

src/core/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const configSchema = s({
3737
bits: 'number?',
3838
emptyRepo: 'boolean?',
3939
privateKey: optional(s('object|string')), // object should be a custom type for PeerId using 'kind-of'
40-
pass: 'string?'
40+
pass: 'string?',
41+
profiles: 'array?'
4142
})])),
4243
start: 'boolean?',
4344
offline: 'boolean?',

test/cli/daemon.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,13 @@ describe('daemon', () => {
286286
const out = await ipfs('config \'Addresses.API\'')
287287
expect(out).to.be.eq('/ip4/127.0.0.1/tcp/9999\n')
288288
})
289+
290+
it('should init with profiles', async function () {
291+
this.timeout(100 * 1000)
292+
const daemon = ipfs('daemon --init-profile test')
293+
294+
await daemonReady(daemon)
295+
const out = await ipfs('config Bootstrap')
296+
expect(out).to.be.eq('[]\n')
297+
})
289298
})

test/core/config.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,15 @@ describe('config', () => {
209209

210210
cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw())
211211
})
212+
213+
it('should validate valid profiles', () => {
214+
expect(
215+
() => config.validate({ init: { profiles: ['test'] } })
216+
).to.not.throw()
217+
})
218+
it('should validate invalid profiles', () => {
219+
expect(
220+
() => config.validate({ init: { profiles: 'test' } })
221+
).to.throw()
222+
})
212223
})

test/core/init.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ describe('init', () => {
8585
})
8686
})
8787
})
88+
89+
it('profiles apply one', async () => {
90+
await ipfs.init({ profiles: ['test'] })
91+
92+
const config = await repo.config.get()
93+
expect(config.Bootstrap).to.be.empty()
94+
})
95+
96+
it('profiles apply multiple', async () => {
97+
await ipfs.init({ profiles: ['test', 'local-discovery'] })
98+
99+
const config = await repo.config.get()
100+
expect(config.Bootstrap).to.be.empty()
101+
expect(config.Discovery.MDNS.Enabled).to.be.true()
102+
})
88103
})

0 commit comments

Comments
 (0)