trilium/src/services/setup.js

122 lines
3.5 KiB
JavaScript
Raw Normal View History

const syncService = require('./sync');
const log = require('./log');
const sqlInit = require('./sql_init');
2018-07-25 15:46:57 +08:00
const repository = require('./repository');
const optionService = require('./options');
2018-09-11 15:35:29 +08:00
const syncOptions = require('./sync_options');
const request = require('./request');
const appInfo = require('./app_info');
const utils = require('./utils');
2018-09-11 16:01:40 +08:00
async function hasSyncServerSchemaAndSeed() {
const response = await requestToSyncServer('GET', '/api/setup/status');
if (response.syncVersion !== appInfo.syncVersion) {
throw new Error(`Could not setup sync since local sync protocol version is ${appInfo.syncVersion} while remote is ${response.syncVersion}. To fix this issue, use same Trilium version on all instances.`);
}
2018-09-11 16:01:40 +08:00
return response.schemaExists;
}
function triggerSync() {
log.info("Triggering sync.");
// it's ok to not wait for it here
2020-06-20 18:31:38 +08:00
syncService.sync().then(res => {
if (res.success) {
2020-06-21 03:42:41 +08:00
sqlInit.setDbAsInitialized();
}
});
}
2018-09-11 16:01:40 +08:00
async function sendSeedToSyncServer() {
log.info("Initiating sync to server");
await requestToSyncServer('POST', '/api/setup/sync-seed', {
2020-06-20 18:31:38 +08:00
options: getSyncSeedOptions(),
syncVersion: appInfo.syncVersion
});
// this is completely new sync, need to reset counters. If this would not be new sync,
// the previous request would have failed.
2020-06-20 18:31:38 +08:00
optionService.setOption('lastSyncedPush', 0);
optionService.setOption('lastSyncedPull', 0);
}
async function requestToSyncServer(method, path, body = null) {
2020-06-20 18:31:38 +08:00
const timeout = syncOptions.getSyncTimeout();
2020-06-20 18:31:38 +08:00
return await utils.timeLimit(request.exec({
method,
2020-06-20 18:31:38 +08:00
url: syncOptions.getSyncServerHost() + path,
body,
2020-06-20 18:31:38 +08:00
proxy: syncOptions.getSyncProxy(),
timeout: timeout
}), timeout);
}
2018-07-25 14:30:41 +08:00
async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, password) {
2020-06-20 18:31:38 +08:00
if (sqlInit.isDbInitialized()) {
return {
result: 'failure',
error: 'DB is already initialized.'
};
}
try {
log.info("Getting document options from sync server.");
// response is expected to contain documentId and documentSecret options
const resp = await request.exec({
method: 'get',
url: syncServerHost + '/api/setup/sync-seed',
auth: {
'user': username,
'pass': password
},
proxy: syncProxy,
timeout: 30000 // seed request should not take long
});
if (resp.syncVersion !== appInfo.syncVersion) {
const message = `Could not setup sync since local sync protocol version is ${appInfo.syncVersion} while remote is ${resp.syncVersion}. To fix this issue, use same Trilium version on all instances.`;
log.error(message);
return {
result: 'failure',
error: message
}
}
2020-06-20 18:31:38 +08:00
sqlInit.createDatabaseForSync(resp.options, syncServerHost, syncProxy);
triggerSync();
return { result: 'success' };
}
catch (e) {
log.error("Sync failed: " + e.message);
return {
result: 'failure',
error: e.message
};
}
}
2020-06-20 18:31:38 +08:00
function getSyncSeedOptions() {
2018-07-25 15:46:57 +08:00
return [
2020-06-20 18:31:38 +08:00
repository.getOption('documentId'),
repository.getOption('documentSecret')
2018-07-25 15:46:57 +08:00
];
}
module.exports = {
2018-09-11 16:01:40 +08:00
hasSyncServerSchemaAndSeed,
triggerSync,
2018-09-11 16:01:40 +08:00
sendSeedToSyncServer,
setupSyncFromSyncServer,
getSyncSeedOptions
};