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

Commit

Permalink
fix: add profiles docs, support in validation and tests (#2545)
Browse files Browse the repository at this point in the history
* fix: add profiles docs, support in validation and tests

* feat: support ipfs daemon --init-config
  • Loading branch information
hugomrdias authored and achingbrain committed Oct 17, 2019
1 parent 82ed328 commit e081e16
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ Instead of a boolean, you may provide an object with custom initialization optio
})
```
- `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).
- `profiles` (Array) Apply profile settings to config.

##### `options.start`

Expand Down
44 changes: 41 additions & 3 deletions doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@

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

#### Profiles

Configuration profiles allow to tweak configuration quickly. Profiles can be
applied with `--profile` flag to `ipfs init` or with the `ipfs config profile
apply` command. When a profile is applied a backup of the configuration file
will be created in `$IPFS_PATH`.

Available profiles:

- `server`

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.

- `lowpower`

Reduces daemon overhead on the system. May affect node functionality,
performance of content discovery and data fetching may be degraded.

- `default-power`

Inverse of "lowpower" profile.

## Table of Contents

- [`Addresses`](#addresses)
Expand Down Expand Up @@ -121,18 +159,18 @@ Options for Multicast DNS peer discovery:
- `Enabled`

A boolean value for whether or not MDNS should be active.

Default: `true`

- `Interval`

A number of seconds to wait between discovery checks.

Default: `10`

### `webRTCStar`

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

Options for webRTCstar peer discovery:

Expand Down
10 changes: 9 additions & 1 deletion src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ module.exports = {
type: 'string',
desc: 'Path to existing configuration file to be loaded during --init.'
})
.option('init-profile', {
type: 'string',
desc: 'Configuration profiles to apply for --init. See ipfs init --help for more.',
coerce: (value) => {
return (value || '').split(',')
}
})
.option('enable-sharding-experiment', {
type: 'boolean',
default: false
Expand Down Expand Up @@ -73,7 +80,8 @@ module.exports = {
ipnsPubsub: argv.enableNamesysPubsub,
dht: argv.enableDhtExperiment,
sharding: argv.enableShardingExperiment
}
},
init: argv.initProfile ? { profiles: argv.initProfile } : true
})

try {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Daemon {
}

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

await new Promise((resolve, reject) => {
Expand Down
51 changes: 26 additions & 25 deletions src/core/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function listProfiles (options) { // eslint-disable-line require-await

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

return config
}
},
lowpower: {
description: 'Reduces daemon overhead on the system - recommended for low power systems.',
transform: (config) => {
config.Swarm = config.Swarm || {}
config.Swarm.ConnMgr = config.Swarm.ConnMgr || {}
config.Swarm.ConnMgr.LowWater = 20
config.Swarm.ConnMgr.HighWater = 40

return config
}
},
'default-power': {
description: 'Inverse of "lowpower" profile.',
transform: (config) => {
const defaultConfig = getDefaultConfig()

config.Swarm = defaultConfig.Swarm

return config
}
},
test: {
description: 'Reduces external interference of IPFS daemon - for running the daemon in test environments.',
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.',
transform: (config) => {
const defaultConfig = getDefaultConfig()

Expand All @@ -110,7 +89,7 @@ const profiles = {
}
},
'default-networking': {
description: 'Restores default network settings - inverse of "test" profile.',
description: 'Restores default network settings. Inverse profile of the `test` profile.',
transform: (config) => {
const defaultConfig = getDefaultConfig()

Expand All @@ -121,9 +100,31 @@ const profiles = {
config.Discovery.MDNS.Enabled = defaultConfig.Discovery.MDNS.Enabled
config.Discovery.webRTCStar.Enabled = defaultConfig.Discovery.webRTCStar.Enabled

return config
}
},
lowpower: {
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.',
transform: (config) => {
config.Swarm = config.Swarm || {}
config.Swarm.ConnMgr = config.Swarm.ConnMgr || {}
config.Swarm.ConnMgr.LowWater = 20
config.Swarm.ConnMgr.HighWater = 40

return config
}
},
'default-power': {
description: 'Inverse of "lowpower" profile.',
transform: (config) => {
const defaultConfig = getDefaultConfig()

config.Swarm = defaultConfig.Swarm

return config
}
}

}

module.exports.profiles = profiles
3 changes: 2 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const configSchema = s({
bits: 'number?',
emptyRepo: 'boolean?',
privateKey: optional(s('object|string')), // object should be a custom type for PeerId using 'kind-of'
pass: 'string?'
pass: 'string?',
profiles: 'array?'
})])),
start: 'boolean?',
offline: 'boolean?',
Expand Down
9 changes: 9 additions & 0 deletions test/cli/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,13 @@ describe('daemon', () => {
const out = await ipfs('config \'Addresses.API\'')
expect(out).to.be.eq('/ip4/127.0.0.1/tcp/9999\n')
})

it('should init with profiles', async function () {
this.timeout(100 * 1000)
const daemon = ipfs('daemon --init-profile test')

await daemonReady(daemon)
const out = await ipfs('config Bootstrap')
expect(out).to.be.eq('[]\n')
})
})
11 changes: 11 additions & 0 deletions test/core/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,15 @@ describe('config', () => {

cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw())
})

it('should validate valid profiles', () => {
expect(
() => config.validate({ init: { profiles: ['test'] } })
).to.not.throw()
})
it('should validate invalid profiles', () => {
expect(
() => config.validate({ init: { profiles: 'test' } })
).to.throw()
})
})
15 changes: 15 additions & 0 deletions test/core/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,19 @@ describe('init', () => {
})
})
})

it('profiles apply one', async () => {
await ipfs.init({ profiles: ['test'] })

const config = await repo.config.get()
expect(config.Bootstrap).to.be.empty()
})

it('profiles apply multiple', async () => {
await ipfs.init({ profiles: ['test', 'local-discovery'] })

const config = await repo.config.get()
expect(config.Bootstrap).to.be.empty()
expect(config.Discovery.MDNS.Enabled).to.be.true()
})
})

0 comments on commit e081e16

Please sign in to comment.