2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2020-08-19 21:39:58 -07:00
|
|
|
checkForUpdates,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
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
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
const apps = require('./apps.js'),
|
2017-04-13 01:23:11 -07:00
|
|
|
appstore = require('./appstore.js'),
|
2019-03-07 14:27:23 -08:00
|
|
|
assert = require('assert'),
|
2015-07-20 00:09:47 -07:00
|
|
|
async = require('async'),
|
|
|
|
|
debug = require('debug')('box:updatechecker'),
|
2019-03-07 13:34:46 -08:00
|
|
|
notifications = require('./notifications.js'),
|
2015-09-22 15:34:19 -07:00
|
|
|
paths = require('./paths.js'),
|
2020-12-29 12:43:53 -08:00
|
|
|
safe = require('safetydance');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2020-12-20 14:17:39 -08:00
|
|
|
function setUpdateInfo(state) {
|
|
|
|
|
// appid -> update info { creationDate, manifest }
|
|
|
|
|
// box -> { version, changelog, upgrade, sourceTarballUrl }
|
2020-12-21 12:39:21 -08:00
|
|
|
state.version = 2;
|
2020-09-09 19:26:45 -07:00
|
|
|
safe.fs.writeFileSync(paths.UPDATE_CHECKER_FILE, JSON.stringify(state, null, 4), 'utf8');
|
2015-09-22 15:34:19 -07:00
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
function getUpdateInfo() {
|
2020-12-20 14:17:39 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-05-06 16:50:27 -07: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');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2020-12-20 14:17:39 -08:00
|
|
|
let state = getUpdateInfo();
|
|
|
|
|
let newState = { }; // create new state so that old app ids are removed
|
2016-01-23 11:08:01 -08:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
apps.getAll(function (error, result) {
|
|
|
|
|
if (error) return callback(error);
|
2016-11-14 17:00:30 +01:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
async.eachSeries(result, function (app, iteratorDone) {
|
|
|
|
|
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
|
2016-01-23 05:56:05 -08:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
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);
|
2020-12-29 12:43:53 -08:00
|
|
|
return iteratorDone(); // continue to next
|
|
|
|
|
}
|
2017-06-19 22:17:43 -07:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
if (!updateInfo) return iteratorDone(); // skip if no next version is found
|
2019-05-03 19:56:25 -07:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
newState[app.id] = updateInfo;
|
2019-05-06 16:31:57 +02:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
iteratorDone();
|
|
|
|
|
});
|
|
|
|
|
}, function () {
|
|
|
|
|
if ('box' in state) newState.box = state.box; // preserve the latest box state information
|
2019-05-06 22:09:18 +02:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
setUpdateInfo(newState);
|
2019-05-06 22:09:18 +02:00
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
callback();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 16:50:27 -07:00
|
|
|
function checkBoxUpdates(options, callback) {
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
2019-03-07 14:27:23 -08:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
2016-01-23 05:31:55 -08:00
|
|
|
|
2021-03-03 13:49:22 -08:00
|
|
|
debug('checkBoxUpdates: checking for updates');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2020-05-06 16:50:27 -07:00
|
|
|
appstore.getBoxUpdate(options, function (error, updateInfo) {
|
2021-01-13 16:57:55 -08:00
|
|
|
if (error) return callback(error);
|
2016-01-23 05:31:55 -08:00
|
|
|
|
2020-12-20 14:17:39 -08:00
|
|
|
let state = getUpdateInfo();
|
2015-09-22 15:34:19 -07:00
|
|
|
|
2021-01-13 16:57:55 -08:00
|
|
|
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');
|
2021-01-13 16:57:55 -08:00
|
|
|
return callback(null);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 14:17:39 -08:00
|
|
|
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();
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-03-03 13:49:22 -08:00
|
|
|
debug(`checkBoxUpdates: ${updateInfo.version} is available`);
|
|
|
|
|
|
2019-03-07 14:32:31 -08:00
|
|
|
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
|
|
|
|
|
|
2020-03-06 19:17:54 -08:00
|
|
|
const message = `Changelog:\n${changelog}\n\nGo to the settings view to update.\n\n`;
|
2017-01-27 08:03:55 -08:00
|
|
|
|
2019-03-07 14:49:38 -08:00
|
|
|
notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message, function (error) {
|
2019-03-07 14:32:31 -08:00
|
|
|
if (error) return callback(error);
|
2017-01-27 08:03:55 -08:00
|
|
|
|
2020-12-20 14:17:39 -08:00
|
|
|
state.box = updateInfo;
|
|
|
|
|
setUpdateInfo(state);
|
2017-04-13 23:19:37 -07:00
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
callback();
|
2016-01-23 05:31:55 -08:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
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
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|