Files
cloudron-box/src/routes/sysadmin.js

73 lines
2.5 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
backup: backup,
2016-01-25 20:21:04 -08:00
update: update,
2018-11-11 21:58:02 -08:00
retire: retire,
importAppDatabase: importAppDatabase
};
2018-11-11 21:58:02 -08:00
var apps = require('../apps.js'),
AppsError = apps.AppsError,
addons = require('../addons.js'),
backups = require('../backups.js'),
2016-04-15 11:53:53 -07:00
BackupsError = require('../backups.js').BackupsError,
cloudron = require('../cloudron.js'),
2016-04-15 12:33:54 -07:00
debug = require('debug')('box:routes/sysadmin'),
HttpError = require('connect-lastmile').HttpError,
2018-07-31 11:35:23 -07:00
HttpSuccess = require('connect-lastmile').HttpSuccess,
updater = require('../updater.js'),
UpdaterError = require('../updater.js').UpdaterError;
function backup(req, res, next) {
debug('triggering backup');
// note that cloudron.backup only waits for backup initiation and not for backup to complete
// backup progress can be checked up ny polling the progress api call
var auditSource = { userId: null, username: 'sysadmin' };
backups.startBackupTask(auditSource, function (error) {
2016-04-15 11:53:53 -07:00
if (error && error.reason === BackupsError.BAD_STATE) return next(new HttpError(409, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(202, {}));
});
}
function update(req, res, next) {
debug('triggering update');
// this only initiates the update, progress can be checked via the progress route
var auditSource = { userId: null, username: 'sysadmin' };
2018-07-31 11:35:23 -07:00
updater.updateToLatest(auditSource, function (error) {
if (error && error.reason === UpdaterError.ALREADY_UPTODATE) return next(new HttpError(422, error.message));
if (error && error.reason === UpdaterError.BAD_STATE) return next(new HttpError(409, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(202, {}));
});
}
2016-01-25 20:21:04 -08:00
function retire(req, res, next) {
debug('triggering retire');
2016-07-06 13:12:09 -05:00
cloudron.retire('migrate', { }, function (error) {
2018-11-11 21:58:02 -08:00
if (error) debug('Retire failed.', error);
2016-01-25 20:21:04 -08:00
});
next(new HttpSuccess(202, {}));
2016-01-25 20:21:04 -08:00
}
2018-11-11 21:58:02 -08:00
function importAppDatabase(req, res, next) {
apps.get(req.params.id, function (error, app) {
if (error && error.reason === AppsError.NOT_FOUND) return next(new HttpError(404, 'No such app'));
if (error) return next(new HttpError(500, error));
addons.importAppDatabase(app, req.query.addon || '', function (error) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(202, {}));
});
});
}