Files
cloudron-box/src/updatechecker.js

127 lines
4.0 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
};
const apps = require('./apps.js'),
appstore = require('./appstore.js'),
2019-03-07 14:27:23 -08:00
assert = require('assert'),
async = require('async'),
debug = require('debug')('box:updatechecker'),
2019-03-07 13:34:46 -08:00
notifications = require('./notifications.js'),
paths = require('./paths.js'),
safe = require('safetydance');
function setUpdateInfo(state) {
// appid -> update info { creationDate, manifest }
// box -> { version, changelog, upgrade, sourceTarballUrl }
2020-12-21 12:39:21 -08:00
state.version = 2;
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'));
2020-12-21 12:39:21 -08:00
if (!state || state.version !== 2) return {};
delete state.version;
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
2021-03-03 13:49:22 -08:00
debug('checkAppUpdates: checking for updates');
let state = getUpdateInfo();
let newState = { }; // create new state so that old app ids are removed
apps.getAll(function (error, result) {
if (error) return callback(error);
2016-11-14 17:00:30 +01: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) {
2021-03-03 13:49:22 -08:00
debug('checkAppUpdates: Error getting app update info for %s', app.id, error);
return iteratorDone(); // continue to next
}
2017-06-19 22:17:43 -07:00
if (!updateInfo) return iteratorDone(); // skip if no next version is found
newState[app.id] = updateInfo;
iteratorDone();
});
}, function () {
if ('box' in state) newState.box = state.box; // preserve the latest box state information
setUpdateInfo(newState);
callback();
});
});
}
function checkBoxUpdates(options, callback) {
assert.strictEqual(typeof options, 'object');
2019-03-07 14:27:23 -08:00
assert.strictEqual(typeof callback, 'function');
2021-03-03 13:49:22 -08:00
debug('checkBoxUpdates: checking for updates');
appstore.getBoxUpdate(options, function (error, updateInfo) {
if (error) return callback(error);
let state = getUpdateInfo();
if (!updateInfo) { // no update
if ('box' in state) {
delete state.box;
setUpdateInfo(state);
}
2021-03-03 13:49:22 -08:00
debug('checkBoxUpdates: no updates');
return callback(null);
}
if (state.box && state.box.version === updateInfo.version) {
2021-03-03 13:49:22 -08:00
debug(`checkBoxUpdates: Skipping notification of box update ${updateInfo.version} as user was already notified`);
2017-10-25 20:52:05 -07:00
return callback();
}
2021-03-03 13:49:22 -08:00
debug(`checkBoxUpdates: ${updateInfo.version} is available`);
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
});
});
}