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

Implement more bootstrap commands #669

Merged
merged 3 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"ncp": "^2.0.0",
"nexpect": "^0.5.0",
"pre-commit": "^1.2.1",
"qs": "^6.3.0",
"rimraf": "^2.5.4",
"stream-to-promise": "^2.2.0",
"transform-loader": "^0.2.3"
Expand Down
14 changes: 11 additions & 3 deletions src/cli/commands/bootstrap/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ const utils = require('../../utils')
log.error = debug('cli:bootstrap:error')

module.exports = {
command: 'add <peer>',
command: 'add [<peer>]',

describe: 'Add peers to the bootstrap list',

builder: {},
builder: {
default: {
describe: 'Add default bootstrap nodes.',
type: 'boolean',
default: false
}
},

handler (argv) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}

ipfs.bootstrap.add(argv.peer, (err, list) => {
ipfs.bootstrap.add(argv.peer, {default: argv.default}, (err, list) => {
if (err) {
throw err
}

list.Peers.forEach((l) => console.log(l))
})
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/bootstrap/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module.exports = {
if (err) {
throw err
}
list.forEach((node) => {

list.Peers.forEach((node) => {
console.log(node)
})
})
Expand Down
15 changes: 12 additions & 3 deletions src/cli/commands/bootstrap/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ log.error = debug('cli:bootstrap:error')
const utils = require('../../utils')

module.exports = {
command: 'rm <peer>',
command: 'rm [<peer>]',

describe: 'Removes peers from the bootstrap list',

builder: {},
builder: {
all: {
type: 'boolean',
describe: 'Remove all bootstrap peers.',
default: false
}
},

handler (argv) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
ipfs.bootstrap.rm(argv.peer, (err, list) => {

ipfs.bootstrap.rm(argv.peer, {all: argv.all}, (err, list) => {
if (err) {
throw err
}

list.Peers.forEach((l) => console.log(l))
})
})
}
Expand Down
53 changes: 42 additions & 11 deletions src/core/components/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,69 @@
'use strict'

const defaultNodes = require('../../init-files/default-config.json').Bootstrap

module.exports = function bootstrap (self) {
return {
list: (callback) => {
self._repo.config.get((err, config) => {
if (err) {
return callback(err)
}
callback(null, config.Bootstrap)
callback(null, {Peers: config.Bootstrap})
})
},
add: (multiaddr, callback) => {
add: (multiaddr, args, callback) => {
if (typeof args === 'function') {
callback = args
args = {default: false}
}
self._repo.config.get((err, config) => {
if (err) {
return callback(err)
}
config.Bootstrap.push(multiaddr)
self._repo.config.set(config, callback)
if (args.default) {
config.Bootstrap = defaultNodes
} else if (multiaddr) {
config.Bootstrap.push(multiaddr)
}
self._repo.config.set(config, (err) => {
if (err) {
return callback(err)
}

callback(null, {
Peers: args.default ? defaultNodes : [multiaddr]
})
})
})
},
rm: (multiaddr, callback) => {
rm: (multiaddr, args, callback) => {
if (typeof args === 'function') {
callback = args
args = {all: false}
}
self._repo.config.get((err, config) => {
if (err) {
return callback(err)
}
if (args.all) {
config.Bootstrap = []
} else {
config.Bootstrap = config.Bootstrap.filter((mh) => mh !== multiaddr)
}

self._repo.config.set(config, (err) => {
if (err) {
return callback(err)
}

config.Bootstrap = config.Bootstrap.filter((mh) => {
if (mh === multiaddr) {
return false
} else {
return true
const res = []
if (!args.all && multiaddr) {
res.push(multiaddr)
}

callback(null, {Peers: res})
})
self._repo.config.set(config, callback)
})
}
}
Expand Down
79 changes: 52 additions & 27 deletions src/http-api/resources/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,90 @@
'use strict'

const boom = require('boom')
const multiaddr = require('multiaddr')

exports = module.exports

// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
exports.parseKey = (request, reply) => {
if (!request.query.arg) {
return reply("Argument 'multiaddr' is required").code(400).takeover()
}

try {
return reply({
addr: multiaddr(request.query.arg)
})
} catch (err) {
return reply({
Message: 'Not a valid multiaddr',
Code: 0
}).code(500).takeover()
}
function applyError (reply, err) {
reply({
Message: err.message,
Code: 0
}).code(500).takeover()
}

exports.list = (request, reply) => {
const ipfs = request.server.app.ipfs

ipfs.bootstrap.list((err, list) => {
if (err) {
return reply(boom.badRequest(err))
return applyError(reply, err)
}

return reply(list)
})
}

exports.add = {
parseArgs: exports.parseKey,
parseArgs (request, reply) {
const q = request.query
const def = q.default === 'true'

if (q.arg != null) {
try {
return reply({
addr: multiaddr(q.arg),
default: def
})
} catch (err) {
return applyError(reply, new Error('Not a valid multiaddr'))
}
} else {
reply({default: def})
}
},
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr
console.log('Handler is called', addr.toString())
const def = request.pre.args.default

ipfs.bootstrap.add(addr.toString(), (err, list) => {
ipfs.bootstrap.add(addr && addr.toString(), {default: def}, (err, list) => {
if (err) {
return reply(boom.badRequest(err))
return applyError(reply, err)
}
return reply()

return reply(list)
})
}
}

exports.rm = {
parseArgs: exports.parseKey,
parseArgs (request, reply) {
const q = request.query
const all = q.all === 'true'

if (q.arg != null) {
try {
return reply({
addr: multiaddr(q.arg),
all: all
})
} catch (err) {
return applyError(reply, new Error('Not a valid multiaddr'))
}
} else {
reply({all: all})
}
},
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr
const all = request.pre.args.all

ipfs.bootstrap.rm(addr.toString(), (err, list) => {
ipfs.bootstrap.rm(addr && addr.toString(), {all: all}, (err, list) => {
if (err) {
return reply(boom.badRequest(err))
return applyError(reply, err)
}
return reply()

return reply(list)
})
}
}
23 changes: 5 additions & 18 deletions src/http-api/routes/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const Joi = require('joi')
const resources = require('./../resources')

module.exports = (server) => {
Expand All @@ -10,7 +9,9 @@ module.exports = (server) => {
api.route({
method: '*',
path: '/api/v0/bootstrap',
handler: resources.bootstrap.list
config: {
handler: resources.bootstrap.list
}
})

// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L866
Expand All @@ -21,14 +22,7 @@ module.exports = (server) => {
pre: [
{ method: resources.bootstrap.add.parseArgs, assign: 'args' }
],
handler: resources.bootstrap.add.handler,
validate: {
query: {
arg: Joi.string().required(),
default: Joi.boolean(),
'stream-channels': Joi.boolean()
}
}
handler: resources.bootstrap.add.handler
}
})

Expand All @@ -47,14 +41,7 @@ module.exports = (server) => {
pre: [
{ method: resources.bootstrap.rm.parseArgs, assign: 'args' }
],
handler: resources.bootstrap.rm.handler,
validate: {
query: {
arg: Joi.string().required(),
default: Joi.boolean(),
'stream-channels': Joi.boolean()
}
}
handler: resources.bootstrap.rm.handler
}
})
}
23 changes: 19 additions & 4 deletions test/cli/test-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,43 @@ describe('bootstrap', () => {
'/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD'
]

it('add default', () => {
return ipfs('bootstrap add --default').then((out) => {
expect(out).to.be.eql(defaultList.join('\n'))
})
})

it('list the bootstrap nodes', () => {
return ipfs('bootstrap list').then((out) => {
expect(out).to.eql(defaultList.join('\n'))
})
})

// TODO need https://github.com/ipfs/interface-ipfs-core/issues/97
// to happen, otherwise it is a cat an mouse game
it.skip('add another bootstrap node', () => {
it('add another bootstrap node', () => {
return ipfs('bootstrap add /ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD').then((out) => {
expect(out).to.be.eql('/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD')
return ipfs('bootstrap list')
}).then((out) => {
expect(out).to.be.eql(updatedList.join('\n'))
})
})

it.skip('rm a bootstrap node', () => {
it('rm a bootstrap node', () => {
return ipfs('bootstrap rm /ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD').then((out) => {
expect(out).to.be.eql('/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD')
return ipfs('bootstrap list')
}).then((out) => {
expect(out).to.deep.equal(defaultList.join('\n'))
})
})

it('rm all bootstrap nodes', () => {
return ipfs('bootstrap rm --all').then((out) => {
expect(out).to.be.eql('')
return ipfs('bootstrap list')
}).then((out) => {
expect(out).to.deep.equal('')
})
})
})
})
Loading