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
|
|
|
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'),
|
2021-08-20 09:19:44 -07: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;
|
2021-06-03 12:20:44 -07:00
|
|
|
if (!safe.fs.writeFileSync(paths.UPDATE_CHECKER_FILE, JSON.stringify(state, null, 4), 'utf8')) debug(`setUpdateInfo: Error writing to update checker file: ${safe.error.message}`);
|
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
|
|
|
}
|
|
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
async function checkAppUpdates(options) {
|
2020-05-06 16:50:27 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
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
|
|
|
|
2021-09-21 19:53:05 -07:00
|
|
|
const state = getUpdateInfo();
|
|
|
|
|
const newState = { }; // create new state so that old app ids are removed
|
2016-01-23 11:08:01 -08:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const result = await apps.list();
|
2016-01-23 05:56:05 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
for (const app of result) {
|
|
|
|
|
if (app.appStoreId === '') continue; // appStoreId can be '' for dev apps
|
2017-06-19 22:17:43 -07:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
const [error, updateInfo] = await safe(appstore.getAppUpdate(app, options));
|
|
|
|
|
if (error) {
|
|
|
|
|
debug('checkAppUpdates: Error getting app update info for %s', app.id, error);
|
|
|
|
|
continue; // continue to next
|
|
|
|
|
}
|
2019-05-06 22:09:18 +02:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
if (!updateInfo) continue; // skip if no next version is found
|
|
|
|
|
newState[app.id] = updateInfo;
|
|
|
|
|
}
|
2019-05-06 22:09:18 +02:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
if ('box' in state) newState.box = state.box; // preserve the latest box state information
|
|
|
|
|
setUpdateInfo(newState);
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
async function checkBoxUpdates(options) {
|
2020-05-06 16:50:27 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
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
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
const updateInfo = await appstore.getBoxUpdate(options);
|
2015-09-22 15:34:19 -07:00
|
|
|
|
2021-09-21 19:53:05 -07:00
|
|
|
const state = getUpdateInfo();
|
2021-01-13 16:57:55 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
if (!updateInfo) { // no update
|
|
|
|
|
if ('box' in state) {
|
|
|
|
|
delete state.box;
|
|
|
|
|
setUpdateInfo(state);
|
2017-10-25 20:52:05 -07:00
|
|
|
}
|
2021-08-18 15:54:53 -07:00
|
|
|
debug('checkBoxUpdates: no updates');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
if (state.box && state.box.version === updateInfo.version) {
|
|
|
|
|
debug(`checkBoxUpdates: Skipping notification of box update ${updateInfo.version} as user was already notified`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-03 13:49:22 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
debug(`checkBoxUpdates: ${updateInfo.version} is available`);
|
2019-03-07 14:32:31 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
|
|
|
|
|
const message = `Changelog:\n${changelog}\n\nGo to the settings view to update.\n\n`;
|
2017-01-27 08:03:55 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
await notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message);
|
2017-01-27 08:03:55 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
state.box = updateInfo;
|
|
|
|
|
setUpdateInfo(state);
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
2020-08-19 21:39:58 -07:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
async function checkForUpdates(options) {
|
2020-08-19 21:39:58 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
const [boxError] = await safe(checkBoxUpdates(options));
|
|
|
|
|
if (boxError) debug('checkForUpdates: error checking for box updates:', boxError);
|
2020-08-19 21:39:58 -07:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
const [appError] = await safe(checkAppUpdates(options));
|
|
|
|
|
if (appError) debug('checkForUpdates: error checking for app updates:', appError);
|
2020-08-19 21:39:58 -07:00
|
|
|
}
|