Skip to content

Commit

Permalink
fix: daemon should recognize previously created repos. (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonKrone authored and daviddias committed Mar 16, 2018
1 parent 4d0aafb commit 2e9cf0d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/endpoint/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ module.exports = (server) => {
if (err) {
return reply(boom.badRequest(err))
}

const id = hat()
const initialized = ipfsd.initialized
nodes[id] = ipfsd

let api = null
Expand All @@ -80,7 +80,7 @@ module.exports = (server) => {
}
}

reply({ id: id, api: api })
reply({ id: id, api: api, initialized: initialized })
})
}
})
Expand Down
8 changes: 7 additions & 1 deletion src/factory-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ class FactoryClient {
const apiAddr = res.body.api ? res.body.api.apiAddr : ''
const gatewayAddr = res.body.api ? res.body.api.gatewayAddr : ''

const ipfsd = new DaemonClient(this.baseUrl, res.body.id, apiAddr, gatewayAddr)
const ipfsd = new DaemonClient(
this.baseUrl,
res.body.id,
res.body.initialized,
apiAddr,
gatewayAddr
)

callback(null, ipfsd)
})
Expand Down
26 changes: 16 additions & 10 deletions src/factory-in-proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,22 @@ class FactoryInProc {
}

const node = new Node(options)
node.once('ready', () => {
series([
(cb) => options.init
? node.init(cb)
: cb(),
(cb) => options.start
? node.start(options.args, cb)
: cb()
], (err) => callback(err, node))
})

series([
(cb) => node.once('ready', cb),
(cb) => node.repo._isInitialized(err => {
// if err exists, repo failed to find config or the ipfs-repo package
// version is different than that of the existing repo.
node.initialized = !err
cb()
}),
(cb) => options.init
? node.init(cb)
: cb(),
(cb) => options.start
? node.start(options.args, cb)
: cb()
], (err) => callback(err, node))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ipfsd-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ function createApi (apiAddr, gwAddr) {
}

class DaemonClient {
constructor (baseUrl, _id, apiAddr, gwAddrs) {
constructor (baseUrl, _id, initialized, apiAddr, gwAddrs) {
this.baseUrl = baseUrl
this._id = _id
this._apiAddr = multiaddr(apiAddr)
this._gwAddr = multiaddr(gwAddrs)
this.initialized = false
this.initialized = initialized
this.started = false
this.api = createApi(apiAddr, gwAddrs)
}
Expand Down
2 changes: 1 addition & 1 deletion src/ipfsd-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Daemon {
this.disposable = this.opts.disposable
this.exec = this.opts.exec || process.env.IPFS_EXEC || findIpfsExecutable(this.opts.type, rootPath)
this.subprocess = null
this.initialized = fs.existsSync(path)
this.initialized = fs.existsSync(this.path)
this.clean = true
this._apiAddr = null
this._gatewayAddr = null
Expand Down
3 changes: 1 addition & 2 deletions src/ipfsd-in-proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class Node extends EventEmitter {
this._apiAddr = null
this._gatewayAddr = null
this._started = false
this.initialized = false
this.api = null
this.bits = this.opts.initOptions ? this.opts.initOptions.bits : null

Expand Down Expand Up @@ -155,7 +154,7 @@ class Node extends EventEmitter {
(cb) => this.getConfig(cb),
(conf, cb) => this.replaceConfig(defaults({}, this.opts.config, conf), cb)
], (err) => {
if (err) { return callback }
if (err) { return callback(err) }
self.clean = false
self.initialized = true
return callback()
Expand Down
13 changes: 6 additions & 7 deletions test/spawn-options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('Spawn options', function () {
expect(err).to.not.exist()
expect(_ipfsd).to.exist()
expect(_ipfsd.api).to.not.exist()
expect(_ipfsd.initialized).to.eql(false)

ipfsd = _ipfsd
repoPath = _ipfsd.repoPath
Expand Down Expand Up @@ -114,12 +115,7 @@ describe('Spawn options', function () {
})
})

describe('spawn from a initialized repo', () => {
// TODO: figure out why `proc` IPFS refuses
// to start with a provided repo
// `Error: Not able to start from state: uninitalized`
if (fOpts.type === 'proc') { return }

describe('spawn from an initialized repo', () => {
let ipfsd

it('f.spawn', function (done) {
Expand All @@ -135,9 +131,12 @@ describe('Spawn options', function () {
f.spawn(options, (err, _ipfsd) => {
expect(err).to.not.exist()
expect(_ipfsd).to.exist()
expect(_ipfsd.api).to.not.exist()

ipfsd = _ipfsd

expect(ipfsd.api).to.not.exist()
expect(ipfsd.initialized).to.eql(true)

done()
})
})
Expand Down

0 comments on commit 2e9cf0d

Please sign in to comment.