Skip to content
Snippets Groups Projects
Commit a5a2f0ca authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Fix: could not start Duniter after fresh install

parent 9fec922c
Branches
Tags
No related merge requests found
...@@ -168,7 +168,7 @@ function CFSCore(rootPath, qfs, parent) { ...@@ -168,7 +168,7 @@ function CFSCore(rootPath, qfs, parent) {
} }
} }
} catch (e) { } catch (e) {
if (e && e.code !== "EISDIR") throw e; if (e && e.code !== "EISDIR" && e.code !== "EEXIST") throw e;
} }
}); });
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
var Q = require('q'); var Q = require('q');
var co = require('co'); var co = require('co');
var _ = require('underscore'); var _ = require('underscore');
var qfs = require('q-io/fs');
var path = require('path'); var path = require('path');
var hashf = require('../hashf'); var hashf = require('../hashf');
var wotb = require('../wot'); var wotb = require('../wot');
var directory = require('../directory');
var Configuration = require('../entity/configuration'); var Configuration = require('../entity/configuration');
var Membership = require('../entity/membership'); var Membership = require('../entity/membership');
var Merkle = require('../entity/merkle'); var Merkle = require('../entity/merkle');
...@@ -15,59 +15,21 @@ var ConfDAL = require('./fileDALs/confDAL'); ...@@ -15,59 +15,21 @@ var ConfDAL = require('./fileDALs/confDAL');
var StatDAL = require('./fileDALs/statDAL'); var StatDAL = require('./fileDALs/statDAL');
var IndicatorsDAL = require('./fileDALs/IndicatorsDAL'); var IndicatorsDAL = require('./fileDALs/IndicatorsDAL');
var CFSStorage = require('./fileDALs/AbstractCFS'); var CFSStorage = require('./fileDALs/AbstractCFS');
var sqlite3 = require("sqlite3b").verbose();
var logger = require('../../lib/logger')('database');
const UCOIN_DB_NAME = 'ucoin';
const WOTB_FILE = 'wotb.bin';
module.exports = {
memory: function(home) {
return getHomeFS(true, home)
.then(function(params) {
let sqlite = new sqlite3.Database(':memory:');
return Q(new FileDAL(params.home, "", params.fs, 'fileDal', sqlite, wotb.memoryInstance()));
});
},
file: function(home) {
return getHomeFS(false, home)
.then(function(params) {
let sqlitePath = path.join(params.home, UCOIN_DB_NAME + '.db');
let sqlite = new sqlite3.Database(sqlitePath);
return new FileDAL(params.home, "", params.fs, 'fileDal', sqlite, wotb.fileInstance(path.join(params.home, WOTB_FILE)));
});
},
FileDAL: FileDAL
};
function someDelayFix() { module.exports = (params) => {
return Q.Promise(function(resolve){ return new FileDAL(params);
setTimeout(resolve, 100); };
});
}
function getHomeFS(isMemory, home) {
let myfs;
return someDelayFix()
.then(function() {
myfs = (isMemory ? require('q-io/fs-mock')({}) : qfs);
return myfs.makeTree(home);
})
.then(function(){
return { fs: myfs, home: home };
});
}
function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) {
var that = this; function FileDAL(params) {
let localHome = path.join(home, localDir); let rootPath = params.home;
let myFS = params.fs;
let sqlite = params.db;
let wotbInstance = params.wotb;
let that = this;
this.name = dalName;
this.profile = 'DAL'; this.profile = 'DAL';
this.wotb = wotbInstance; this.wotb = wotbInstance;
var rootPath = home;
// DALs // DALs
this.confDAL = new ConfDAL(rootPath, myFS, null, that, CFSStorage); this.confDAL = new ConfDAL(rootPath, myFS, null, that, CFSStorage);
...@@ -121,12 +83,6 @@ function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) { ...@@ -121,12 +83,6 @@ function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) {
return currency; return currency;
}; };
this.removeHome = function() {
return myFS.removeTree(localHome)
.catch(function(){
});
};
that.writeFileOfBlock = function(block) { that.writeFileOfBlock = function(block) {
return that.blockDAL.saveBlock(block); return that.blockDAL.saveBlock(block);
}; };
...@@ -201,7 +157,7 @@ function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) { ...@@ -201,7 +157,7 @@ function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) {
else return block; else return block;
}) })
.catch(function(){ .catch(function(){
throw 'Block ' + [number, hash].join('-') + ' not found in ' + that.name; throw 'Block ' + [number, hash].join('-') + ' not found';
}) })
.then(function(block){ .then(function(block){
done && done(null, block); done && done(null, block);
...@@ -1165,13 +1121,13 @@ function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) { ...@@ -1165,13 +1121,13 @@ function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) {
}); });
this.resetAll = function(done) { this.resetAll = function(done) {
var files = ['stats', 'cores', 'current', 'conf', UCOIN_DB_NAME, UCOIN_DB_NAME + '.db', WOTB_FILE]; var files = ['stats', 'cores', 'current', 'conf', directory.UCOIN_DB_NAME, directory.UCOIN_DB_NAME + '.db', directory.WOTB_FILE];
var dirs = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb']; var dirs = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb'];
return resetFiles(files, dirs, done); return resetFiles(files, dirs, done);
}; };
this.resetData = function(done) { this.resetData = function(done) {
var files = ['stats', 'cores', 'current', UCOIN_DB_NAME, UCOIN_DB_NAME + '.db', WOTB_FILE]; var files = ['stats', 'cores', 'current', directory.UCOIN_DB_NAME, directory.UCOIN_DB_NAME + '.db', directory.WOTB_FILE];
var dirs = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb']; var dirs = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb'];
return resetFiles(files, dirs, done); return resetFiles(files, dirs, done);
}; };
......
...@@ -12,6 +12,4 @@ function AbstractCFS(rootPath, qioFS, parentDAL, localDAL) { ...@@ -12,6 +12,4 @@ function AbstractCFS(rootPath, qioFS, parentDAL, localDAL) {
this.coreFS = cfs(rootPath, qioFS, parentDAL); this.coreFS = cfs(rootPath, qioFS, parentDAL);
this.dal = localDAL; this.dal = localDAL;
this.changeParentCore = (newParent) => this.coreFS.changeParent(newParent);
} }
"use strict"; "use strict";
var co = require('co');
var opts = require('optimist').argv; var opts = require('optimist').argv;
var path = require('path'); var path = require('path');
var cfs = require('./cfs');
var Q = require('q');
var qfs = require('q-io/fs');
var sqlite3 = require("sqlite3b").verbose();
const DEFAULT_DOMAIN = "ucoin_default"; const DEFAULT_DOMAIN = "ucoin_default";
const DEFAULT_HOME = (process.platform == 'win32' ? process.env.USERPROFILE : process.env.HOME) + '/.config/ucoin/'; const DEFAULT_HOME = (process.platform == 'win32' ? process.env.USERPROFILE : process.env.HOME) + '/.config/ucoin/';
module.exports = { let dir = module.exports = {
INSTANCE_NAME: getDomain(opts.mdb), INSTANCE_NAME: getDomain(opts.mdb),
INSTANCE_HOME: getHomePath(opts.mdb, opts.home), INSTANCE_HOME: getHomePath(opts.mdb, opts.home),
INSTANCE_HOMELOG_FILE: getLogsPath(opts.mdb, opts.home), INSTANCE_HOMELOG_FILE: getLogsPath(opts.mdb, opts.home),
UCOIN_DB_NAME: 'ucoin',
WOTB_FILE: 'wotb.bin',
getHome: (profile, dir) => getHomePath(profile, dir) getHome: (profile, dir) => getHomePath(profile, dir),
getHomeFS: (isMemory, theHome) => co(function *() {
let home = theHome || dir.getHome();
yield someDelayFix();
let params = {
home: home
};
if (isMemory) {
params.fs = require('q-io/fs-mock')({});
} else {
params.fs = qfs;
}
yield params.fs.makeTree(home);
return params;
}),
getHomeParams: (isMemory, theHome) => co(function *() {
let params = yield dir.getHomeFS(isMemory, theHome);
let home = params.home;
yield someDelayFix();
if (isMemory) {
params.db = new sqlite3.Database(':memory:');
params.wotb = require('./wot').memoryInstance();
} else {
let sqlitePath = path.join(home, dir.UCOIN_DB_NAME + '.db');
params.db = new sqlite3.Database(sqlitePath);
params.wotb = require('./wot').fileInstance(path.join(home, dir.WOTB_FILE));
}
return params;
}),
createHomeIfNotExists: (fs, theHome) => co(function *() {
let fsHandler = cfs(theHome, fs);
return fsHandler.makeTree('');
})
}; };
function someDelayFix() {
return Q.Promise(function(resolve){
setTimeout(resolve, 100);
});
}
function getLogsPath(profile, dir) { function getLogsPath(profile, dir) {
return path.join(getHomePath(profile, dir), 'ucoin.log'); return path.join(getHomePath(profile, dir), 'ucoin.log');
} }
......
#!/usr/bin/env node #!/usr/bin/env node
"use strict"; "use strict";
var logger = require('../app/lib/logger')('ucoind');
process.on('uncaughtException', function (err) { process.on('uncaughtException', function (err) {
// Dunno why this specific exception is not caught // Dunno why this specific exception is not caught
if (err.code !== "EADDRNOTAVAIL") { if (err.code !== "EADDRNOTAVAIL") {
...@@ -9,7 +11,6 @@ process.on('uncaughtException', function (err) { ...@@ -9,7 +11,6 @@ process.on('uncaughtException', function (err) {
} }
}); });
var logger = require('../app/lib/logger')('ucoind');
var async = require('async'); var async = require('async');
var co = require('co'); var co = require('co');
var Q = require('q'); var Q = require('q');
...@@ -785,15 +786,19 @@ program ...@@ -785,15 +786,19 @@ program
}); });
function webStart() { function webStart() {
return co(function *() {
var dbName = program.mdb; var dbName = program.mdb;
var dbHome = program.home; var dbHome = program.home;
if (!program.memory) {
let params = yield directory.getHomeFS(program.memory, dbHome);
yield directory.createHomeIfNotExists(params.fs, params.home);
// Add log files for this instance // Add log files for this instance
logger.addHomeLogs(directory.getHome(dbName, dbHome)); logger.addHomeLogs(params.home);
}
co(function *() {
let webminapi = yield ucoin.statics.enableHttpAdmin({ home: dbHome, name: dbName, memory: program.memory }, commandLineConf(), false); let webminapi = yield ucoin.statics.enableHttpAdmin({ home: dbHome, name: dbName, memory: program.memory }, commandLineConf(), false);
webminapi.openConnections(); return webminapi.openConnections();
}); });
} }
...@@ -802,5 +807,12 @@ program.parse(process.argv); ...@@ -802,5 +807,12 @@ program.parse(process.argv);
if (program.args.length == 0) { if (program.args.length == 0) {
console.log('No command given, using default: ucoind webstart'); console.log('No command given, using default: ucoind webstart');
webStart(); return co(function *() {
}; try {
yield webStart();
} catch (e) {
logger.error(e);
process.exit();
}
});
}
...@@ -53,8 +53,8 @@ function Server (dbConf, overrideConf) { ...@@ -53,8 +53,8 @@ function Server (dbConf, overrideConf) {
this.plugFileSystem = () => co(function *() { this.plugFileSystem = () => co(function *() {
logger.debug('Plugging file system...'); logger.debug('Plugging file system...');
var dbType = dbConf && dbConf.memory ? fileDAL.memory : fileDAL.file; var params = yield directory.getHomeParams(dbConf && dbConf.memory, home);
that.dal = yield dbType(home); that.dal = fileDAL(params);
}); });
this.unPlugFileSystem = () => co(function *() { this.unPlugFileSystem = () => co(function *() {
......
...@@ -4,6 +4,7 @@ var _ = require('underscore'); ...@@ -4,6 +4,7 @@ var _ = require('underscore');
var should = require('should'); var should = require('should');
var assert = require('assert'); var assert = require('assert');
var dal = require('../../app/lib/dal/fileDAL'); var dal = require('../../app/lib/dal/fileDAL');
var dir = require('../../app/lib/directory');
var constants = require('../../app/lib/constants'); var constants = require('../../app/lib/constants');
var Peer = require('../../app/lib/entity/peer'); var Peer = require('../../app/lib/entity/peer');
...@@ -162,16 +163,12 @@ var fileDAL = null; ...@@ -162,16 +163,12 @@ var fileDAL = null;
describe("DAL", function(){ describe("DAL", function(){
before(function() { before(() => co(function *() {
return dal.memory('db0') let params = yield dir.getHomeParams(true, 'db0');
.then(function(dal){ fileDAL = dal(params);
fileDAL = dal; yield fileDAL.init();
return dal.init();
})
.then(function() {
return fileDAL.saveConf({ currency: "meta_brouzouf" }); return fileDAL.saveConf({ currency: "meta_brouzouf" });
}); }));
});
it('should have no peer in a first time', function(){ it('should have no peer in a first time', function(){
return fileDAL.listAllPeers().then(function(peers){ return fileDAL.listAllPeers().then(function(peers){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment