Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: daemon should recognize previously created repos. #212

Merged
merged 11 commits into from
Mar 16, 2018
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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