From 3f83090c3b319493b9c3eacc9fa8515e2f6f61d7 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 17 Apr 2020 14:01:24 +0100 Subject: [PATCH] wip: ipfsd ctl --- src/daemon/config.js | 13 +++++++++---- src/daemon/daemon.js | 20 +++++++++++++++----- src/daemon/index.js | 2 +- test/e2e/launch.e2e.js | 3 +-- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/daemon/config.js b/src/daemon/config.js index 7c3acc261..12e62358a 100644 --- a/src/daemon/config.js +++ b/src/daemon/config.js @@ -9,8 +9,8 @@ const { showDialog } = require('../dialogs') const store = require('../common/store') const logger = require('../common/logger') -function configExists (ipfsPath) { - return fs.pathExistsSync(join(ipfsPath, 'config')) +function configExists (ipfsd) { + return fs.pathExistsSync(join(ipfsd.path, 'config')) } function configPath (ipfsd) { @@ -188,8 +188,13 @@ async function checkPorts (ipfsd) { const apiPort = parseInt(configApiMa.nodeAddress().port, 10) const gatewayPort = parseInt(configGatewayMa.nodeAddress().port, 10) - const freeGatewayPort = await portfinder.getPortPromise({ port: gatewayPort, stopPort: gatewayPort + 100 }) - const freeApiPort = await portfinder.getPortPromise({ port: apiPort, stopPort: apiPort + 100 }) + const findFreePort = async (port, from) => { + port = Math.max(port, from, 1024) + return portfinder.getPortPromise({ port, stopPort: port + 100 }) + } + + const freeGatewayPort = await findFreePort(gatewayPort, 8080) + const freeApiPort = await findFreePort(apiPort, 5001) const busyApiPort = apiPort !== freeApiPort const busyGatewayPort = gatewayPort !== freeGatewayPort diff --git a/src/daemon/daemon.js b/src/daemon/daemon.js index de4c93d61..f7594278f 100644 --- a/src/daemon/daemon.js +++ b/src/daemon/daemon.js @@ -41,6 +41,12 @@ async function cleanup (ipfsd) { } async function spawn ({ flags, path, keysize }) { + // js-ipfsd-ctl stopped supporting IPFS_PATH after 1.x + // NOTE: https://github.com/ipfs/js-ipfsd-ctl/issues/497 + if (process.env.IPFS_PATH) { + path = process.env.IPFS_PATH + } + const ipfsd = await Ctl.createController({ ipfsHttpModule: require('ipfs-http-client'), ipfsBin: require('go-ipfs-dep').path(), @@ -53,14 +59,17 @@ async function spawn ({ flags, path, keysize }) { args: flags }) - // Init does not run if the repository is already initialized. We run this - // anyway because ipfsd.initialized is always false when spawning a new daemon. - // See https://github.com/ipfs/js-ipfsd-ctl/blob/master/src/ipfsd-daemon.js#L82 + // ipfsd-ctl is not setting .initialized properly after spawn. + // NOTE: https://github.com/ipfs/js-ipfsd-ctl/issues/500 + if (configExists(ipfsd)) { + checkCorsConfig(ipfsd) + return ipfsd + } + await ipfsd.init({ bits: keysize }) - checkCorsConfig(ipfsd) applyDefaults(ipfsd) return ipfsd } @@ -68,11 +77,12 @@ async function spawn ({ flags, path, keysize }) { module.exports = async function (opts) { const ipfsd = await spawn(opts) await checkPorts(ipfsd) - await ipfsd.start() try { + await ipfsd.start() const { id } = await ipfsd.api.id() logger.info(`[daemon] PeerID is ${id}`) + logger.info(`[daemon] Repo is at ${ipfsd.path}`) } catch (err) { if (!err.message.includes('ECONNREFUSED')) { throw err diff --git a/src/daemon/index.js b/src/daemon/index.js index 8e08f049e..89d85f712 100644 --- a/src/daemon/index.js +++ b/src/daemon/index.js @@ -90,7 +90,7 @@ module.exports = async function (ctx) { log.end() updateStatus(STATUS.STOPPING_FINISHED) } catch (err) { - logger.error(`[ipfsd] ${err.toString}`) + logger.error(`[ipfsd] ${err.toString()}`) updateStatus(STATUS.STOPPING_FAILED) } finally { ipfsd = null diff --git a/test/e2e/launch.e2e.js b/test/e2e/launch.e2e.js index 91110985f..56c83d963 100644 --- a/test/e2e/launch.e2e.js +++ b/test/e2e/launch.e2e.js @@ -164,8 +164,7 @@ describe('Application launch', function () { expect(app.isRunning()).to.be.true() await daemonReady(app) await ipfsd.stop() - }) - */ + }) */ it('starts with multiple api addresses', async function () { const { repoPath, configPath } = await makeRepository({ start: false })