diff --git a/src/appstore.js b/src/appstore.js index 9e5daea0b..a8b581a7b 100644 --- a/src/appstore.js +++ b/src/appstore.js @@ -7,6 +7,7 @@ exports = module.exports = { sendAliveStatus: sendAliveStatus, getAppUpdate: getAppUpdate, + getBoxUpdate: getBoxUpdate, AppstoreError: AppstoreError }; @@ -170,6 +171,25 @@ function sendAliveStatus(data, callback) { }); } +function getBoxUpdate(callback) { + assert.strictEqual(typeof callback, 'function'); + + getAppstoreConfig(function (error, appstoreConfig) { + if (error) return callback(error); + + var url = config.apiServerOrigin() + '/api/v1/users/' + appstoreConfig.userId + '/cloudrons/' + appstoreConfig.cloudronId + '/boxupdate'; + + superagent.get(url).query({ boxVersion: config.version() }).timeout(10 * 1000).end(function (error, result) { + if (error && !error.response) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, error)); + if (result.statusCode === 204) return callback(null); // no update + if (result.statusCode !== 200) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, util.format('Bad response: %s %s', result.statusCode, result.text))); + + // { version, changelog, upgrade, sourceTarballUrl} + callback(null, result.body); + }); + }); +} + function getAppUpdate(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); @@ -184,6 +204,7 @@ function getAppUpdate(app, callback) { if (result.statusCode === 204) return callback(null); // no update if (result.statusCode !== 200) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, util.format('Bad response: %s %s', result.statusCode, result.text))); + // { id, creationDate, manifest } callback(null, result.body); }); }); diff --git a/src/updatechecker.js b/src/updatechecker.js index 7b61b78d1..bb99d0f01 100644 --- a/src/updatechecker.js +++ b/src/updatechecker.js @@ -19,9 +19,7 @@ var apps = require('./apps.js'), paths = require('./paths.js'), safe = require('safetydance'), semver = require('semver'), - settings = require('./settings.js'), - superagent = require('superagent'), - util = require('util'); + settings = require('./settings.js'); var gAppUpdateInfo = { }, // id -> update info { creationDate, manifest } gBoxUpdateInfo = null; // { version, changelog, upgrade, sourceTarballUrl } @@ -58,54 +56,6 @@ function resetAppUpdateInfo(appId) { } } -function getBoxUpdates(callback) { - var currentVersion = config.version(); - - // do not crash if boxVersionsUrl is not set - if (!config.get('boxVersionsUrl')) return callback(null, null); - - superagent - .get(config.get('boxVersionsUrl')) - .timeout(10 * 1000) - .end(function (error, result) { - 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))); - - 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, - upgrade: nextVersionInfo.upgrade, - sourceTarballUrl: nextVersionInfo.sourceTarballUrl - }); - } else { - debug('no new version available.'); - callback(null, null); - } - }); -} - function checkAppUpdates(callback) { callback = callback || NOOP_CALLBACK; // null when called from a timer task @@ -177,7 +127,7 @@ function checkBoxUpdates(callback) { gBoxUpdateInfo = null; - getBoxUpdates(function (error, updateInfo) { + appstore.getBoxUpdate(function (error, updateInfo) { if (error || !updateInfo) return callback(error); settings.getUpdateConfig(function (error, updateConfig) {