Files
cloudron-box/src/updatechecker.js

144 lines
5.1 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2020-08-19 21:39:58 -07:00
checkForUpdates,
2020-08-19 21:39:58 -07:00
getUpdateInfo,
2017-07-23 10:53:16 -07:00
2020-08-19 22:40:04 -07:00
_checkAppUpdates: checkAppUpdates
};
var apps = require('./apps.js'),
appstore = require('./appstore.js'),
2019-03-07 14:27:23 -08:00
assert = require('assert'),
async = require('async'),
constants = require('./constants.js'),
debug = require('debug')('box:updatechecker'),
mailer = require('./mailer.js'),
2019-03-07 13:34:46 -08:00
notifications = require('./notifications.js'),
paths = require('./paths.js'),
safe = require('safetydance'),
2019-03-10 16:05:27 -07:00
settings = require('./settings.js'),
users = require('./users.js');
function setUpdateInfo(state) {
console.dir(state);
// appid -> update info { creationDate, manifest }
// box -> { version, changelog, upgrade, sourceTarballUrl }
safe.fs.writeFileSync(paths.UPDATE_CHECKER_FILE, JSON.stringify(state, null, 4), 'utf8');
}
function getUpdateInfo() {
const state = safe.JSON.parse(safe.fs.readFileSync(paths.UPDATE_CHECKER_FILE, 'utf8'));
return state || {};
2016-01-25 16:46:54 -08:00
}
function checkAppUpdates(options, callback) {
assert.strictEqual(typeof options, 'object');
2019-03-07 14:27:23 -08:00
assert.strictEqual(typeof callback, 'function');
2016-01-23 05:35:57 -08:00
debug('Checking App Updates');
let state = getUpdateInfo();
let newState = { }; // create new state so that old app ids are removed
2020-08-19 22:40:04 -07:00
settings.getAutoupdatePattern(function (error, result) {
2016-01-23 05:35:57 -08:00
if (error) return callback(error);
const autoupdatesEnabled = (result !== constants.AUTOUPDATE_PATTERN_NEVER);
var notificationPending = [];
2019-05-06 17:42:26 +02:00
apps.getAll(function (error, result) {
if (error) return callback(error);
2019-05-06 17:42:26 +02:00
async.eachSeries(result, function (app, iteratorDone) {
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
appstore.getAppUpdate(app, options, function (error, updateInfo) {
if (error) {
debug('Error getting app update info for %s', app.id, error);
return iteratorDone(); // continue to next
}
2016-11-14 17:00:30 +01:00
if (!updateInfo) return iteratorDone(); // skip if no next version is found
newState[app.id] = updateInfo;
2017-06-19 22:17:43 -07:00
if (safe.query(state[app.id], 'manifest.version') === updateInfo.manifest.version) {
debug(`Skipping app update notification of ${app.id} since user was already notified of ${updateInfo.manifest.version}`);
return iteratorDone();
}
const canAutoupdateApp = apps.canAutoupdateApp(app, updateInfo);
2019-12-04 13:17:58 -08:00
if (autoupdatesEnabled && canAutoupdateApp) return iteratorDone();
debug(`Notifying of app update for ${app.id} from ${app.manifest.version} to ${updateInfo.manifest.version}`);
notificationPending.push({ app, updateInfo });
iteratorDone();
2017-06-19 22:17:43 -07:00
});
2019-05-06 22:35:01 +02:00
}, function () {
if ('box' in state) newState.box = state.box; // preserve the latest box state information
setUpdateInfo(newState);
2019-05-06 22:35:01 +02:00
if (notificationPending.length === 0) return callback();
users.getAdmins(function (error, admins) {
2019-05-06 22:35:01 +02:00
if (error) {
debug('checkAppUpdates: failed to get admins', error);
2019-05-06 22:35:01 +02:00
return callback();
}
2019-05-06 22:35:01 +02:00
async.eachSeries(admins, (admin, done) => mailer.appUpdatesAvailable(admin.email, notificationPending, true /* subscription */, done), callback);
});
});
});
});
}
function checkBoxUpdates(options, callback) {
assert.strictEqual(typeof options, 'object');
2019-03-07 14:27:23 -08:00
assert.strictEqual(typeof callback, 'function');
debug('Checking Box Updates');
appstore.getBoxUpdate(options, function (error, updateInfo) {
2016-01-23 05:37:22 -08:00
if (error || !updateInfo) return callback(error);
let state = getUpdateInfo();
if (state.box && state.box.version === updateInfo.version) {
2017-10-25 20:52:05 -07:00
debug('Skipping notification of box update as user was already notified');
return callback();
}
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
const message = `Changelog:\n${changelog}\n\nGo to the settings view to update.\n\n`;
notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message, function (error) {
if (error) return callback(error);
state.box = updateInfo;
setUpdateInfo(state);
2017-04-13 23:19:37 -07:00
callback();
});
});
}
2020-08-19 21:39:58 -07:00
function checkForUpdates(options, callback) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
2020-08-19 22:40:04 -07:00
checkBoxUpdates(options, function (boxError) {
if (boxError) debug('checkForUpdates: error checking for box updates:', boxError);
2020-08-19 21:39:58 -07:00
2020-08-19 22:40:04 -07:00
checkAppUpdates(options, function (appError) {
if (appError) debug('checkForUpdates: error checking for app updates:', appError);
2020-08-19 21:39:58 -07:00
2020-08-19 22:40:04 -07:00
callback(boxError || appError || null);
2020-08-19 21:39:58 -07:00
});
});
}