diff --git a/src/appstore.js b/src/appstore.js index fcc858217..7a5b182d9 100644 --- a/src/appstore.js +++ b/src/appstore.js @@ -56,23 +56,12 @@ var NOOP_CALLBACK = function (error) { if (error) debug(error); }; function getAppstoreConfig(callback) { assert.strictEqual(typeof callback, 'function'); - // Caas Cloudrons do not store appstore credentials in their local database - if (config.provider() === 'caas') { - var url = config.apiServerOrigin() + '/api/v1/exchangeBoxTokenWithUserToken'; - superagent.post(url).query({ token: config.token() }).timeout(30 * 1000).end(function (error, result) { - if (error && !error.response) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, error)); - if (result.statusCode !== 201) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, util.format('App unpurchase failed. %s %j', result.status, result.body))); + settings.getAppstoreConfig(function (error, result) { + if (error) return callback(new AppstoreError(AppstoreError.INTERNAL_ERROR, error)); + if (!result.token) return callback(new AppstoreError(AppstoreError.BILLING_REQUIRED)); - callback(null, result.body); - }); - } else { - settings.getAppstoreConfig(function (error, result) { - if (error) return callback(new AppstoreError(AppstoreError.INTERNAL_ERROR, error)); - if (!result.token) return callback(new AppstoreError(AppstoreError.BILLING_REQUIRED)); - - callback(null, result); - }); - } + callback(null, result); + }); } function getSubscription(callback) { diff --git a/src/caas.js b/src/caas.js index 829663846..70ef95bc6 100644 --- a/src/caas.js +++ b/src/caas.js @@ -15,6 +15,7 @@ var assert = require('assert'), locker = require('./locker.js'), path = require('path'), progress = require('./progress.js'), + settings = require('./settings.js'), shell = require('./shell.js'), superagent = require('superagent'), util = require('util'), @@ -62,8 +63,19 @@ function retire(reason, info, callback) { shell.sudo('retire', [ RETIRE_CMD, reason, JSON.stringify(info), JSON.stringify(data) ], callback); } -function doMigrate(options, callback) { +function getCaasConfig(callback) { + assert.strictEqual(typeof callback, 'function'); + + settings.getCaasConfig(function (error, result) { + if (error) return callback(new CaasError(CaasError.INTERNAL_ERROR, error)); + + callback(null, result); + }); +} + +function doMigrate(options, caasConfig, callback) { assert.strictEqual(typeof options, 'object'); + assert.strictEqual(typeof caasConfig, 'object'); assert.strictEqual(typeof callback, 'function'); var error = locker.lock(locker.OP_MIGRATE); @@ -84,8 +96,8 @@ function doMigrate(options, callback) { debug('migrate: domain: %s size %s region %s', options.domain, options.size, options.region); superagent - .post(config.apiServerOrigin() + '/api/v1/boxes/' + config.fqdn() + '/migrate') - .query({ token: config.token() }) + .post(config.apiServerOrigin() + '/api/v1/boxes/' + caasConfig.boxId + '/migrate') + .query({ token: caasConfig.token }) .send(options) .timeout(30 * 1000) .end(function (error, result) { @@ -109,7 +121,11 @@ function changePlan(options, callback) { if (config.isDemo()) return callback(new CaasError(CaasError.BAD_FIELD, 'Not allowed in demo mode')); - doMigrate(options, callback); + getCaasConfig(function (error, result) { + if (error) return callback(error); + + doMigrate(options, result, callback); + }); } // this function expects a lock @@ -126,32 +142,40 @@ function upgrade(boxUpdateInfo, callback) { backups.backupBoxAndApps({ userId: null, username: 'upgrader' }, function (error) { if (error) return upgradeError(error); - superagent.post(config.apiServerOrigin() + '/api/v1/boxes/' + config.fqdn() + '/upgrade') - .query({ token: config.token() }) - .send({ version: boxUpdateInfo.version }) - .timeout(30 * 1000) - .end(function (error, result) { - if (error && !error.response) return upgradeError(new Error('Network error making upgrade request: ' + error)); - if (result.statusCode !== 202) return upgradeError(new Error(util.format('Server not ready to upgrade. statusCode: %s body: %j', result.status, result.body))); + getCaasConfig(function (error, result) { + if (error) return upgradeError(error); - progress.set(progress.UPDATE, 10, 'Updating base system'); + superagent.post(config.apiServerOrigin() + '/api/v1/boxes/' + result.boxId + '/upgrade') + .query({ token: result.token }) + .send({ version: boxUpdateInfo.version }) + .timeout(30 * 1000) + .end(function (error, result) { + if (error && !error.response) return upgradeError(new Error('Network error making upgrade request: ' + error)); + if (result.statusCode !== 202) return upgradeError(new Error(util.format('Server not ready to upgrade. statusCode: %s body: %j', result.status, result.body))); - // no need to unlock since this is the last thing we ever do on this box - callback(); + progress.set(progress.UPDATE, 10, 'Updating base system'); - retire('upgrade'); - }); + // no need to unlock since this is the last thing we ever do on this box + callback(); + + retire('upgrade'); + }); + }); }); } function sendHeartbeat() { assert(config.provider() === 'caas', 'Heartbeat is only sent for managed cloudrons'); - var url = config.apiServerOrigin() + '/api/v1/boxes/' + config.fqdn() + '/heartbeat'; - superagent.post(url).query({ token: config.token(), version: config.version() }).timeout(30 * 1000).end(function (error, result) { - if (error && !error.response) debug('Network error sending heartbeat.', error); - else if (result.statusCode !== 200) debug('Server responded to heartbeat with %s %s', result.statusCode, result.text); - else debug('Heartbeat sent to %s', url); + getCaasConfig(function (error, result) { + if (error) return debug('Caas config missing', error); + + var url = config.apiServerOrigin() + '/api/v1/boxes/' + result.boxId + '/heartbeat'; + superagent.post(url).query({ token: result.token, version: config.version() }).timeout(30 * 1000).end(function (error, result) { + if (error && !error.response) debug('Network error sending heartbeat.', error); + else if (result.statusCode !== 200) debug('Server responded to heartbeat with %s %s', result.statusCode, result.text); + else debug('Heartbeat sent to %s', url); + }); }); } @@ -180,15 +204,19 @@ function setPtrRecord(domain, callback) { assert.strictEqual(typeof domain, 'string'); assert.strictEqual(typeof callback, 'function'); - superagent - .post(config.apiServerOrigin() + '/api/v1/boxes/' + config.fqdn() + '/ptr') - .query({ token: config.token() }) - .send({ domain: domain }) - .timeout(5 * 1000) - .end(function (error, result) { - if (error && !error.response) return callback(new CaasError(CaasError.EXTERNAL_ERROR, 'Cannot reach appstore')); - if (result.statusCode !== 202) return callback(new CaasError(CaasError.EXTERNAL_ERROR, util.format('%s %j', result.statusCode, result.body))); + getCaasConfig(function (error, result) { + if (error) return callback(error); - return callback(null); - }); + superagent + .post(config.apiServerOrigin() + '/api/v1/boxes/' + result.boxId + '/ptr') + .query({ token: result.token }) + .send({ domain: domain }) + .timeout(5 * 1000) + .end(function (error, result) { + if (error && !error.response) return callback(new CaasError(CaasError.EXTERNAL_ERROR, 'Cannot reach appstore')); + if (result.statusCode !== 202) return callback(new CaasError(CaasError.EXTERNAL_ERROR, util.format('%s %j', result.statusCode, result.body))); + + return callback(null); + }); + }); } diff --git a/src/cloudron.js b/src/cloudron.js index 25f25f8ec..8fa703af0 100644 --- a/src/cloudron.js +++ b/src/cloudron.js @@ -175,6 +175,8 @@ function autoprovision(callback) { async.eachSeries(Object.keys(conf), function (key, iteratorDone) { var name; switch (key) { + case 'appstoreConfig': name = settings.APPSTORE_CONFIG_KEY; break; + case 'caasConfig': name = settings.CAAS_CONFIG_KEY; break; case 'tlsConfig': name = settings.TLS_CONFIG_KEY; break; case 'backupConfig': name = settings.BACKUP_CONFIG_KEY; break; case 'tlsCert': diff --git a/src/settings.js b/src/settings.js index d9093046c..dfb60214c 100644 --- a/src/settings.js +++ b/src/settings.js @@ -27,6 +27,8 @@ exports = module.exports = { getTlsConfig: getTlsConfig, setTlsConfig: setTlsConfig, + getCaasConfig: getCaasConfig, + getAppstoreConfig: getAppstoreConfig, setAppstoreConfig: setAppstoreConfig, @@ -57,6 +59,7 @@ exports = module.exports = { TLS_CONFIG_KEY: 'tls_config', UPDATE_CONFIG_KEY: 'update_config', APPSTORE_CONFIG_KEY: 'appstore_config', + CAAS_CONFIG_KEY: 'caas_config', MAIL_CONFIG_KEY: 'mail_config', MAIL_RELAY_KEY: 'mail_relay', CATCH_ALL_ADDRESS_KEY: 'catch_all_address', @@ -475,6 +478,17 @@ function setCatchAllAddress(address, callback) { }); } +function getCaasConfig(callback) { + assert.strictEqual(typeof callback, 'function'); + + settingsdb.get(exports.CAAS_CONFIG_KEY, function (error, value) { + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(null, gDefaults[exports.APPSTORE_CONFIG_KEY]); + if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error)); + + callback(null, JSON.parse(value)); + }); +} + function getAppstoreConfig(callback) { assert.strictEqual(typeof callback, 'function');