Skip to content

Commit

Permalink
wip: ipfsd ctl
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 17, 2020
1 parent 682bc18 commit 3f83090
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
13 changes: 9 additions & 4 deletions src/daemon/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions src/daemon/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -53,26 +59,30 @@ 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
}

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
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/launch.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down

0 comments on commit 3f83090

Please sign in to comment.