2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
checkAppUpdates: checkAppUpdates,
|
|
|
|
|
checkBoxUpdates: checkBoxUpdates,
|
|
|
|
|
|
2016-01-25 16:46:54 -08:00
|
|
|
getUpdateInfo: getUpdateInfo,
|
2016-11-07 15:14:06 +01:00
|
|
|
resetUpdateInfo: resetUpdateInfo,
|
|
|
|
|
resetAppUpdateInfo: resetAppUpdateInfo
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var apps = require('./apps.js'),
|
|
|
|
|
async = require('async'),
|
|
|
|
|
config = require('./config.js'),
|
2017-01-27 09:24:36 -08:00
|
|
|
constants = require('./constants.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
debug = require('debug')('box:updatechecker'),
|
|
|
|
|
mailer = require('./mailer.js'),
|
2015-09-22 15:34:19 -07:00
|
|
|
paths = require('./paths.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
safe = require('safetydance'),
|
|
|
|
|
semver = require('semver'),
|
2016-01-23 05:31:55 -08:00
|
|
|
settings = require('./settings.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
superagent = require('superagent'),
|
|
|
|
|
util = require('util');
|
|
|
|
|
|
|
|
|
|
var gAppUpdateInfo = { }, // id -> update info { creationDate, manifest }
|
2016-01-25 13:57:59 -08:00
|
|
|
gBoxUpdateInfo = null; // { version, changelog, upgrade, sourceTarballUrl }
|
2015-09-22 15:34:19 -07:00
|
|
|
|
2016-01-23 05:31:55 -08:00
|
|
|
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
|
|
|
|
|
2015-09-22 15:34:19 -07:00
|
|
|
function loadState() {
|
|
|
|
|
var state = safe.JSON.parse(safe.fs.readFileSync(paths.UPDATE_CHECKER_FILE, 'utf8'));
|
|
|
|
|
return state || { };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveState(mailedUser) {
|
|
|
|
|
safe.fs.writeFileSync(paths.UPDATE_CHECKER_FILE, JSON.stringify(mailedUser, null, 4), 'utf8');
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
function getUpdateInfo() {
|
|
|
|
|
return {
|
|
|
|
|
apps: gAppUpdateInfo,
|
|
|
|
|
box: gBoxUpdateInfo
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-25 16:46:54 -08:00
|
|
|
function resetUpdateInfo() {
|
|
|
|
|
gBoxUpdateInfo = null;
|
2016-11-07 15:14:06 +01:00
|
|
|
resetAppUpdateInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no appId provided all apps are reset
|
|
|
|
|
function resetAppUpdateInfo(appId) {
|
|
|
|
|
if (!appId) {
|
|
|
|
|
gAppUpdateInfo = {};
|
|
|
|
|
} else {
|
|
|
|
|
delete gAppUpdateInfo[appId];
|
|
|
|
|
}
|
2016-01-25 16:46:54 -08:00
|
|
|
}
|
|
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
function getAppUpdate(app, callback) {
|
|
|
|
|
superagent
|
|
|
|
|
.get(config.apiServerOrigin() + '/api/v1/apps/' + app.appStoreId + '/versions/' + app.manifest.version + '/update')
|
|
|
|
|
.query({ boxVersion: config.version() })
|
|
|
|
|
.timeout(10 * 1000)
|
|
|
|
|
.end(function (error, result) {
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
if (error && !error.response) return callback(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
if (result.statusCode !== 200 || !('update' in result.body)) return callback(new Error(util.format('Bad response: %s %s', result.statusCode, result.text)));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
callback(null, result.body.update);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getBoxUpdates(callback) {
|
|
|
|
|
var currentVersion = config.version();
|
|
|
|
|
|
2015-10-13 13:20:28 +02:00
|
|
|
// do not crash if boxVersionsUrl is not set
|
|
|
|
|
if (!config.get('boxVersionsUrl')) return callback(null, null);
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
superagent
|
|
|
|
|
.get(config.get('boxVersionsUrl'))
|
|
|
|
|
.timeout(10 * 1000)
|
|
|
|
|
.end(function (error, result) {
|
2015-12-15 09:12:52 -08:00
|
|
|
if (error && !error.response) return callback(error);
|
|
|
|
|
if (result.statusCode !== 200) return callback(new Error(util.format('Bad status: %s %s', result.statusCode, result.text)));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
var versions = safe.JSON.parse(result.text);
|
|
|
|
|
|
|
|
|
|
if (!versions || typeof versions !== 'object') return callback(new Error('versions is not in valid format:' + safe.error));
|
|
|
|
|
|
|
|
|
|
var latestVersion = Object.keys(versions).sort(semver.compare).pop();
|
|
|
|
|
debug('checkBoxUpdates: Latest version is %s etag:%s', latestVersion, result.header['etag']);
|
|
|
|
|
|
|
|
|
|
if (!latestVersion) return callback(new Error('No version available'));
|
|
|
|
|
|
|
|
|
|
var nextVersion = null, nextVersionInfo = null;
|
|
|
|
|
var currentVersionInfo = versions[currentVersion];
|
|
|
|
|
if (!currentVersionInfo) {
|
|
|
|
|
debug('Cloudron runs on unknown version %s. Offering to update to latest version', currentVersion);
|
|
|
|
|
nextVersion = latestVersion;
|
|
|
|
|
nextVersionInfo = versions[latestVersion];
|
|
|
|
|
} else {
|
|
|
|
|
nextVersion = currentVersionInfo.next;
|
|
|
|
|
nextVersionInfo = nextVersion ? versions[nextVersion] : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nextVersionInfo && typeof nextVersionInfo === 'object') {
|
|
|
|
|
debug('new version %s available. imageId: %d code: %s', nextVersion, nextVersionInfo.imageId, nextVersionInfo.sourceTarballUrl);
|
|
|
|
|
callback(null, {
|
|
|
|
|
version: nextVersion,
|
|
|
|
|
changelog: nextVersionInfo.changelog,
|
2016-01-12 16:50:14 +01:00
|
|
|
upgrade: nextVersionInfo.upgrade,
|
|
|
|
|
sourceTarballUrl: nextVersionInfo.sourceTarballUrl
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
debug('no new version available.');
|
|
|
|
|
callback(null, null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 05:35:57 -08:00
|
|
|
function checkAppUpdates(callback) {
|
|
|
|
|
callback = callback || NOOP_CALLBACK; // null when called from a timer task
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
debug('Checking App Updates');
|
|
|
|
|
|
2016-01-23 11:08:01 -08:00
|
|
|
gAppUpdateInfo = { };
|
2016-07-27 18:25:53 -07:00
|
|
|
var oldState = loadState();
|
2016-08-02 17:38:59 -07:00
|
|
|
var newState = { }; // create new state so that old app ids are removed
|
2016-01-23 11:08:01 -08:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
apps.getAll(function (error, apps) {
|
2016-01-23 05:35:57 -08:00
|
|
|
if (error) return callback(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
async.eachSeries(apps, function (app, iteratorDone) {
|
|
|
|
|
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
|
|
|
|
|
|
|
|
|
|
getAppUpdate(app, function (error, updateInfo) {
|
|
|
|
|
if (error) {
|
|
|
|
|
debug('Error getting app update info for %s', app.id, error);
|
|
|
|
|
return iteratorDone(); // continue to next
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-11-14 17:00:30 +01:00
|
|
|
// skip if no next version is found
|
|
|
|
|
if (!updateInfo) {
|
|
|
|
|
delete gAppUpdateInfo[app.id];
|
|
|
|
|
return iteratorDone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!safe.query(updateInfo, 'manifest.version')) {
|
|
|
|
|
debug('Skipping malformed update of app %s. got %j', app.id, updateInfo);
|
2016-07-27 18:25:53 -07:00
|
|
|
delete gAppUpdateInfo[app.id];
|
|
|
|
|
return iteratorDone();
|
|
|
|
|
}
|
2015-09-22 22:46:14 -07:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
gAppUpdateInfo[app.id] = updateInfo;
|
2016-07-27 17:46:58 -07:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
// decide whether to send email
|
|
|
|
|
newState[app.id] = updateInfo.manifest.version;
|
2016-01-23 05:56:05 -08:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
if (oldState[app.id] === newState[app.id]) {
|
|
|
|
|
debug('Skipping notification of app update %s since user was already notified', app.id);
|
2016-08-01 15:35:58 -07:00
|
|
|
} else if (semver.satisfies(newState[app.id], '~' + app.manifest.version)) {
|
|
|
|
|
debug('Skipping notification of app update as this is a patch release');
|
2016-07-27 17:46:58 -07:00
|
|
|
} else {
|
2017-01-27 09:24:36 -08:00
|
|
|
// only send notifications if update pattern is 'never'
|
|
|
|
|
settings.getAutoupdatePattern(function (error, result) {
|
2017-02-07 10:48:51 -08:00
|
|
|
if (error) return debug(error);
|
2017-01-27 09:24:36 -08:00
|
|
|
if (result !== constants.AUTOUPDATE_PATTERN_NEVER) return;
|
|
|
|
|
|
|
|
|
|
debug('Notifying user of app update for %s from %s to %s', app.id, app.manifest.version, updateInfo.manifest.version);
|
|
|
|
|
|
|
|
|
|
mailer.appUpdateAvailable(app, updateInfo);
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-27 17:46:58 -07:00
|
|
|
iteratorDone();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
2016-07-27 17:46:58 -07:00
|
|
|
}, function () {
|
2016-08-02 17:38:59 -07:00
|
|
|
newState.box = loadState().box; // preserve the latest box state information
|
2016-07-27 17:46:58 -07:00
|
|
|
saveState(newState);
|
|
|
|
|
callback();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 05:31:55 -08:00
|
|
|
function checkBoxUpdates(callback) {
|
|
|
|
|
callback = callback || NOOP_CALLBACK; // null when called from a timer task
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
debug('Checking Box Updates');
|
|
|
|
|
|
2016-01-23 11:08:01 -08:00
|
|
|
gBoxUpdateInfo = null;
|
|
|
|
|
|
2016-01-23 05:31:55 -08:00
|
|
|
getBoxUpdates(function (error, updateInfo) {
|
2016-01-23 05:37:22 -08:00
|
|
|
if (error || !updateInfo) return callback(error);
|
2016-01-23 05:31:55 -08:00
|
|
|
|
|
|
|
|
settings.getUpdateConfig(function (error, updateConfig) {
|
2016-01-23 05:37:22 -08:00
|
|
|
if (error) return callback(error);
|
2016-01-23 05:31:55 -08:00
|
|
|
|
|
|
|
|
var isPrerelease = semver.parse(updateInfo.version).prerelease.length !== 0;
|
|
|
|
|
|
|
|
|
|
if (isPrerelease && !updateConfig.prerelease) {
|
2016-01-23 13:37:21 -08:00
|
|
|
debug('Skipping update %s since this box does not want prereleases', updateInfo.version);
|
2016-01-23 05:31:55 -08:00
|
|
|
return callback();
|
|
|
|
|
}
|
2015-09-22 15:34:19 -07:00
|
|
|
|
2016-01-23 05:31:55 -08:00
|
|
|
gBoxUpdateInfo = updateInfo;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-01-27 08:03:55 -08:00
|
|
|
// decide whether to send email
|
|
|
|
|
var state = loadState();
|
|
|
|
|
|
|
|
|
|
if (state.box === gBoxUpdateInfo.version) {
|
|
|
|
|
debug('Skipping notification of box update as user was already notified');
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (semver.satisfies(gBoxUpdateInfo.version, '~' + config.version())) {
|
|
|
|
|
debug('Skipping notification of box update as this is a patch release');
|
|
|
|
|
} else {
|
2017-01-27 09:24:36 -08:00
|
|
|
// only send notifications if update pattern is 'never'
|
|
|
|
|
settings.getAutoupdatePattern(function (error, result) {
|
2017-02-07 10:48:51 -08:00
|
|
|
if (error) return debug(error);
|
2017-01-27 09:24:36 -08:00
|
|
|
if (result !== constants.AUTOUPDATE_PATTERN_NEVER) return;
|
|
|
|
|
|
|
|
|
|
mailer.boxUpdateAvailable(updateInfo.version, updateInfo.changelog);
|
|
|
|
|
});
|
2017-01-27 08:03:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.box = updateInfo.version;
|
|
|
|
|
|
|
|
|
|
saveState(state);
|
|
|
|
|
|
2016-01-23 15:57:56 -08:00
|
|
|
callback();
|
2016-01-23 05:31:55 -08:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|