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

Fixing #430 Added backup import functionality

parent ce3a1681
No related branches found
No related tags found
No related merge requests found
......@@ -48,7 +48,10 @@ function WebAdmin (dbConf, overConf) {
let pluggedConfP = plugForConf();
let pluggedDALP = co(function *() {
let pluggedDALP = replugDAL();
function replugDAL() {
return co(function *() {
yield pluggedConfP;
// Routing documents
......@@ -56,6 +59,7 @@ function WebAdmin (dbConf, overConf) {
return plugForDAL();
});
}
this.summary = () => co(function *() {
yield pluggedDALP;
......@@ -422,6 +426,34 @@ function WebAdmin (dbConf, overConf) {
return server.exportAllDataAsZIP();
});
this.importData = (req) => co(function *() {
yield that.stopHTTP();
yield that.stopAllServices();
yield server.unplugFileSystem();
yield pluggedDALP;
if (!req.files.importData) {
throw "Wrong upload file name";
}
const importZipPath = server.home + '/import.zip';
yield new Promise((resolve, reject) => {
req.files.importData.mv(importZipPath, (err) => {
err ? reject(err) : resolve();
});
});
yield server.importAllDataFromZIP(importZipPath);
pluggedConfP = plugForConf();
pluggedDALP = replugDAL();
return {};
});
this.loadData = (dunFile) => co(function *() {
yield pluggedDALP;
// We have to wait for a non-breaking window to process reset
yield server.unplugFileSystem();
yield server.cleanDBData();
return {};
});
function plugForConf() {
return co(function *() {
yield server.plugFileSystem();
......
......@@ -7,6 +7,8 @@ const network = require('../system/network');
const dtos = require('../../lib/streams/dtos');
const logger = require('../logger')('webmin');
const ENABLE_FILE_UPLOAD = true;
let WebSocketServer = require('ws').Server;
module.exports = function(dbConf, overConf, interfaces, httpLogs) {
......@@ -35,6 +37,7 @@ module.exports = function(dbConf, overConf, interfaces, httpLogs) {
httpMethods.httpGET( '/webmin/server/reset/data', webminCtrl.resetData, dtos.Boolean);
httpMethods.httpGET( '/webmin/network/interfaces', webminCtrl.listInterfaces, dtos.NetworkInterfaces);
httpMethods.httpGETFile('/webmin/data/duniter_export', webminCtrl.exportData);
httpMethods.httpPOST( '/webmin/data/duniter_import', webminCtrl.importData);
}, (httpServer) => {
// Socket for synchronization events
......@@ -127,7 +130,7 @@ module.exports = function(dbConf, overConf, interfaces, httpLogs) {
}));
}
}));
});
}, ENABLE_FILE_UPLOAD);
return {
httpLayer: httpLayer,
......
......@@ -11,6 +11,7 @@ const morgan = require('morgan');
const errorhandler = require('errorhandler');
const bodyParser = require('body-parser');
const cors = require('cors');
const fileUpload = require('express-fileupload');
const constants = require('../constants');
const sanitize = require('../streams/sanitize');
const logger = require('../logger')('network');
......@@ -74,7 +75,7 @@ module.exports = {
}
},
createServersAndListen: (name, interfaces, httpLogs, staticPath, routingCallback, listenWebSocket) => co(function *() {
createServersAndListen: (name, interfaces, httpLogs, staticPath, routingCallback, listenWebSocket, enableFileUpload) => co(function *() {
const app = express();
......@@ -92,6 +93,11 @@ module.exports = {
// CORS for **any** HTTP request
app.use(cors());
if (enableFileUpload) {
// File upload for backup API
app.use(fileUpload());
}
app.use(bodyParser.urlencoded({
extended: true
}));
......
......@@ -47,6 +47,7 @@
"event-stream": "3.1.5",
"express": "4.13.4",
"express-cors": "0.0.3",
"express-fileupload": "0.0.5",
"inquirer": "0.8.5",
"jison": "0.4.17",
"merkle": "0.4.0",
......@@ -66,6 +67,7 @@
"superagent": "1.4.0",
"tweetnacl": "0.14.1",
"underscore": "1.8.3",
"unzip": "0.1.11",
"vucoin": "0.30.4",
"winston": "2.1.1",
"wotb": "0.4.10",
......
......@@ -7,6 +7,8 @@ const co = require('co');
const _ = require('underscore');
const Q = require('q');
const archiver = require('archiver');
const unzip = require('unzip');
const fs = require('fs');
const parsers = require('./app/lib/streams/parsers');
const constants = require('./app/lib/constants');
const fileDAL = require('./app/lib/dal/fileDAL');
......@@ -319,6 +321,17 @@ function Server (dbConf, overrideConf) {
return archive;
});
this.importAllDataFromZIP = (zipFile) => co(function *() {
const params = yield paramsP;
yield that.resetData();
const output = unzip.Extract({ path: params.home });
fs.createReadStream(zipFile).pipe(output);
return new Promise((resolve, reject) => {
output.on('error', reject);
output.on('close', resolve);
});
});
this.cleanDBData = () => co(function *() {
yield _.values(that.dal.newDals).map((dal) => dal.cleanData && dal.cleanData());
that.dal.wotb.resetWoT();
......
"use strict";
const should = require('should');
const fs = require('fs');
const co = require('co');
const unzip = require('unzip');
const toolbox = require('../integration/tools/toolbox');
const user = require('../integration/tools/user');
const bma = require('../../app/lib/streams/bma');
......@@ -47,4 +49,8 @@ describe('Import/Export', () => {
});
});
}));
it('should be able to import data', () => co(function *() {
yield s1.importAllDataFromZIP(s1.home + '/export.zip');
}));
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment