#!/usr/bin/env node 'use strict'; exports = module.exports = { run: run, // exported for testing _reserveHttpPort: reserveHttpPort, _configureReverseProxy: configureReverseProxy, _unconfigureReverseProxy: unconfigureReverseProxy, _createAppDir: createAppDir, _deleteAppDir: deleteAppDir, _verifyManifest: verifyManifest, _registerSubdomains: registerSubdomains, _unregisterSubdomains: unregisterSubdomains, _waitForDnsPropagation: waitForDnsPropagation }; require('supererror')({ splatchError: true }); var addons = require('./addons.js'), appdb = require('./appdb.js'), apps = require('./apps.js'), assert = require('assert'), async = require('async'), auditsource = require('./auditsource.js'), backups = require('./backups.js'), BoxError = require('./boxerror.js'), constants = require('./constants.js'), DatabaseError = require('./databaseerror.js'), debug = require('debug')('box:apptask'), df = require('@sindresorhus/df'), docker = require('./docker.js'), domains = require('./domains.js'), DomainsError = domains.DomainsError, ejs = require('ejs'), eventlog = require('./eventlog.js'), fs = require('fs'), manifestFormat = require('cloudron-manifestformat'), mkdirp = require('mkdirp'), net = require('net'), os = require('os'), path = require('path'), paths = require('./paths.js'), reverseProxy = require('./reverseproxy.js'), rimraf = require('rimraf'), safe = require('safetydance'), settings = require('./settings.js'), shell = require('./shell.js'), superagent = require('superagent'), sysinfo = require('./sysinfo.js'), util = require('util'), _ = require('underscore'); const COLLECTD_CONFIG_EJS = fs.readFileSync(__dirname + '/collectd.config.ejs', { encoding: 'utf8' }), CONFIGURE_COLLECTD_CMD = path.join(__dirname, 'scripts/configurecollectd.sh'), MV_VOLUME_CMD = path.join(__dirname, 'scripts/mvvolume.sh'), LOGROTATE_CONFIG_EJS = fs.readFileSync(__dirname + '/logrotate.ejs', { encoding: 'utf8' }), CONFIGURE_LOGROTATE_CMD = path.join(__dirname, 'scripts/configurelogrotate.sh'); function debugApp(app) { assert.strictEqual(typeof app, 'object'); debug(app.fqdn + ' ' + util.format.apply(util, Array.prototype.slice.call(arguments, 1))); } // updates the app object and the database function updateApp(app, values, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof values, 'object'); assert.strictEqual(typeof callback, 'function'); debugApp(app, 'updating app with values: %j', values); appdb.update(app.id, values, function (error) { if (error) return callback(new BoxError(BoxError.INTERNAL_ERROR, error)); for (var value in values) { app[value] = values[value]; } callback(null); }); } function reserveHttpPort(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); let server = net.createServer(); server.listen(0, function () { let port = server.address().port; updateApp(app, { httpPort: port }, function (error) { server.close(function (/* closeError */) { if (error) return callback(new BoxError(BoxError.NETWORK_ERROR, `Failed to allocate http port ${port}: ${error.message}`)); callback(null); }); }); }); } function configureReverseProxy(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); reverseProxy.configureApp(app, { userId: null, username: 'apptask' }, function (error) { if (error) return callback(new BoxError(BoxError.REVERSEPROXY_ERROR, `Error configuring nginx: ${error.message}`)); callback(null); }); } function unconfigureReverseProxy(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); reverseProxy.unconfigureApp(app, function (error) { if (error) return callback(new BoxError(BoxError.REVERSEPROXY_ERROR, `Error unconfiguring nginx: ${error.message}`)); callback(null); }); } function createContainer(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); assert(!app.containerId); // otherwise, it will trigger volumeFrom debugApp(app, 'creating container'); docker.createContainer(app, function (error, container) { if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, `Error creating container: ${error.message}`)); updateApp(app, { containerId: container.id }, callback); }); } function deleteContainers(app, options, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof options, 'object'); assert.strictEqual(typeof callback, 'function'); debugApp(app, 'deleting app containers (app, scheduler)'); docker.deleteContainers(app.id, options, function (error) { if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, `Error deleting container: ${error.message}`)); updateApp(app, { containerId: null }, callback); }); } function createAppDir(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); const appDir = path.join(paths.APPS_DATA_DIR, app.id); mkdirp(appDir, function (error) { if (error) return callback(new BoxError(BoxError.FS_ERROR, `Error creating directory: ${error.message}`, { appDir })); callback(null); }); } function deleteAppDir(app, options, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof options, 'object'); assert.strictEqual(typeof callback, 'function'); const appDataDir = path.join(paths.APPS_DATA_DIR, app.id); // resolve any symlinked data dir const stat = safe.fs.lstatSync(appDataDir); if (!stat) return callback(null); const resolvedAppDataDir = stat.isSymbolicLink() ? safe.fs.readlinkSync(appDataDir) : appDataDir; if (safe.fs.existsSync(resolvedAppDataDir)) { const entries = safe.fs.readdirSync(resolvedAppDataDir); if (!entries) return callback(new BoxError(BoxError.FS_ERROR, `Error listing ${resolvedAppDataDir}: ${safe.error.message}`)); // remove only files. directories inside app dir are currently volumes managed by the addons // we cannot delete those dirs anyway because of perms entries.forEach(function (entry) { let stat = safe.fs.statSync(path.join(resolvedAppDataDir, entry)); if (stat && !stat.isDirectory()) safe.fs.unlinkSync(path.join(resolvedAppDataDir, entry)); }); } // if this fails, it's probably because the localstorage/redis addons have not cleaned up properly if (options.removeDirectory) { if (stat.isSymbolicLink()) { if (!safe.fs.unlinkSync(appDataDir)) { if (safe.error.code !== 'ENOENT') return callback(new BoxError(BoxError.FS_ERROR, `Error unlinking dir ${appDataDir} : ${safe.error.message}`)); } } else { if (!safe.fs.rmdirSync(appDataDir)) { if (safe.error.code !== 'ENOENT') return callback(new BoxError(BoxError.FS_ERROR, `Error removing dir ${appDataDir} : ${safe.error.message}`)); } } } callback(null); } function addCollectdProfile(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); var collectdConf = ejs.render(COLLECTD_CONFIG_EJS, { appId: app.id, containerId: app.containerId, appDataDir: apps.getDataDir(app, app.dataDir) }); fs.writeFile(path.join(paths.COLLECTD_APPCONFIG_DIR, app.id + '.conf'), collectdConf, function (error) { if (error) return callback(new BoxError(BoxError.FS_ERROR, `Error writing collectd config: ${error.message}`)); shell.sudo('addCollectdProfile', [ CONFIGURE_COLLECTD_CMD, 'add', app.id ], {}, function (error) { if (error) return callback(new BoxError(BoxError.COLLECTD_ERROR, 'Culd not add collectd config')); callback(null); }); }); } function removeCollectdProfile(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); fs.unlink(path.join(paths.COLLECTD_APPCONFIG_DIR, app.id + '.conf'), function (error) { if (error && error.code !== 'ENOENT') debugApp(app, 'Error removing collectd profile', error); shell.sudo('removeCollectdProfile', [ CONFIGURE_COLLECTD_CMD, 'remove', app.id ], {}, function (error) { if (error) return callback(new BoxError(BoxError.COLLECTD_ERROR, 'Culd not remove collectd config')); callback(null); }); }); } function addLogrotateConfig(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); docker.inspect(app.containerId, function (error, result) { if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, `Error inspecting app container: ${error.message}`, { containerId: app.containerId })); var runVolume = result.Mounts.find(function (mount) { return mount.Destination === '/run'; }); if (!runVolume) return callback(new BoxError(BoxError.DOCKER_ERROR, 'App does not have /run mounted')); // logrotate configs can have arbitrary commands, so the config files must be owned by root var logrotateConf = ejs.render(LOGROTATE_CONFIG_EJS, { volumePath: runVolume.Source, appId: app.id }); var tmpFilePath = path.join(os.tmpdir(), app.id + '.logrotate'); fs.writeFile(tmpFilePath, logrotateConf, function (error) { if (error) return callback(new BoxError(BoxError.FS_ERROR, `Error writing logrotate config: ${error.message}`)); shell.sudo('addLogrotateConfig', [ CONFIGURE_LOGROTATE_CMD, 'add', app.id, tmpFilePath ], {}, function (error) { if (error) return callback(new BoxError(BoxError.LOGROTATE_ERROR, `Error adding logrotate config: ${error.message}`)); callback(null); }); }); }); } function removeLogrotateConfig(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); shell.sudo('removeLogrotateConfig', [ CONFIGURE_LOGROTATE_CMD, 'remove', app.id ], {}, function (error) { if (error) return callback(new BoxError(BoxError.LOGROTATE_ERROR, `Error removing logrotate config: ${error.message}`)); callback(null); }); } function cleanupLogs(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); // note that redis container logs are cleaned up by the addon rimraf(path.join(paths.LOG_DIR, app.id), function (error) { if (error) debugApp(app, 'cannot cleanup logs: %s', error); callback(null); }); } function verifyManifest(manifest, callback) { assert.strictEqual(typeof manifest, 'object'); assert.strictEqual(typeof callback, 'function'); var error = manifestFormat.parse(manifest); if (error) return callback(new BoxError(BoxError.BAD_FIELD, `Manifest error: ${error.message}`, { field: 'manifest' })); error = apps.checkManifestConstraints(manifest); if (error) return callback(new BoxError(BoxError.CONFLICT, `Manifest constraint check failed: ${error.message}`, { field: 'manifest' })); callback(null); } function downloadIcon(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); // nothing to download if we dont have an appStoreId if (!app.appStoreId) return callback(null); debugApp(app, 'Downloading icon of %s@%s', app.appStoreId, app.manifest.version); var iconUrl = settings.apiServerOrigin() + '/api/v1/apps/' + app.appStoreId + '/versions/' + app.manifest.version + '/icon'; async.retry({ times: 10, interval: 5000 }, function (retryCallback) { superagent .get(iconUrl) .buffer(true) .timeout(30 * 1000) .end(function (error, res) { if (error && !error.response) return retryCallback(new BoxError(BoxError.NETWORK_ERROR, `Network error downloading icon : ${error.message}`)); if (res.statusCode !== 200) return retryCallback(null); // ignore error. this can also happen for apps installed with cloudron-cli const iconPath = path.join(paths.APP_ICONS_DIR, app.id + '.png'); if (!safe.fs.writeFileSync(iconPath, res.body)) return retryCallback(new BoxError(BoxError.FS_ERROR, `Error saving icon to ${iconPath}: ${safe.error.message}`)); retryCallback(null); }); }, callback); } function removeIcon(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); if (!safe.fs.unlinkSync(path.join(paths.APP_ICONS_DIR, app.id + '.png'))) { if (safe.error.code !== 'ENOENT') debugApp(app, 'cannot remove icon : %s', safe.error); } if (!safe.fs.unlinkSync(path.join(paths.APP_ICONS_DIR, app.id + '.user.png'))) { if (safe.error.code !== 'ENOENT') debugApp(app, 'cannot remove user icon : %s', safe.error); } callback(null); } function registerSubdomains(app, overwrite, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof overwrite, 'boolean'); assert.strictEqual(typeof callback, 'function'); sysinfo.getPublicIp(function (error, ip) { if (error) return callback(error); const allDomains = [ { subdomain: app.location, domain: app.domain }].concat(app.alternateDomains); async.eachSeries(allDomains, function (domain, iteratorDone) { async.retry({ times: 200, interval: 5000 }, function (retryCallback) { debugApp(app, 'Registering subdomain: %s%s', domain.subdomain ? (domain.subdomain + '.') : '', domain.domain); // get the current record before updating it domains.getDnsRecords(domain.subdomain, domain.domain, 'A', function (error, values) { if (error && error.reason === DomainsError.EXTERNAL_ERROR) return retryCallback(new BoxError(BoxError.EXTERNAL_ERROR, error.message, domain)); // try again if (error && error.reason === DomainsError.ACCESS_DENIED) return retryCallback(null, new BoxError(BoxError.ACCESS_DENIED, error.message, domain)); if (error && error.reason === DomainsError.NOT_FOUND) return retryCallback(null, new BoxError(BoxError.NOT_FOUND, error.message, domain)); if (error) return retryCallback(null, new BoxError(BoxError.EXTERNAL_ERROR, error.message, domain)); // give up for access and other errors // refuse to update any existing DNS record for custom domains that we did not create if (values.length !== 0 && !overwrite) return retryCallback(null, new BoxError(BoxError.ALREADY_EXISTS, 'DNS Record already exists', domain)); domains.upsertDnsRecords(domain.subdomain, domain.domain, 'A', [ ip ], function (error) { if (error && (error.reason === DomainsError.STILL_BUSY || error.reason === DomainsError.EXTERNAL_ERROR)) { debug('registerSubdomains: Upsert error. Will retry.', error.message); return retryCallback(new BoxError(BoxError.EXTERNAL_ERROR, error.message, domain)); // try again } retryCallback(null, error ? new BoxError(BoxError.EXTERNAL_ERROR, error.message, domain) : null); }); }); }, function (error, result) { if (error || result) return iteratorDone(error || result); iteratorDone(null); }); }, callback); }); } function unregisterSubdomains(app, allDomains, callback) { assert.strictEqual(typeof app, 'object'); assert(Array.isArray(allDomains)); assert.strictEqual(typeof callback, 'function'); sysinfo.getPublicIp(function (error, ip) { if (error) return callback(error); async.eachSeries(allDomains, function (domain, iteratorDone) { async.retry({ times: 30, interval: 5000 }, function (retryCallback) { debugApp(app, 'Unregistering subdomain: %s%s', domain.subdomain ? (domain.subdomain + '.') : '', domain.domain); domains.removeDnsRecords(domain.subdomain, domain.domain, 'A', [ ip ], function (error) { if (error && error.reason === DomainsError.NOT_FOUND) return retryCallback(null, null); if (error && (error.reason === DomainsError.STILL_BUSY || error.reason === DomainsError.EXTERNAL_ERROR)) { debug('registerSubdomains: Remove error. Will retry.', error.message); return retryCallback(new BoxError(BoxError.EXTERNAL_ERROR, error.message, domain)); // try again } retryCallback(null, error ? new BoxError(BoxError.EXTERNAL_ERROR, error.message, domain) : null); }); }, function (error, result) { if (error || result) return iteratorDone(error || result); iteratorDone(); }); }, callback); }); } function waitForDnsPropagation(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); if (!constants.CLOUDRON) { debugApp(app, 'Skipping dns propagation check for development'); return callback(null); } sysinfo.getPublicIp(function (error, ip) { if (error) return callback(new BoxError(BoxError.NETWORK_ERROR, `Error getting public IP: ${error.message}`)); domains.waitForDnsRecord(app.location, app.domain, 'A', ip, { interval: 5000, times: 240 }, function (error) { if (error) return callback(new BoxError(BoxError.DNS_ERROR, `DNS Record is not synced yet: ${error.message}`, { ip: ip, subdomain: app.location, domain: app.domain })); // now wait for alternateDomains, if any async.eachSeries(app.alternateDomains, function (domain, iteratorCallback) { domains.waitForDnsRecord(domain.subdomain, domain.domain, 'A', ip, { interval: 5000, times: 240 }, function (error) { if (error) return callback(new BoxError(BoxError.DNS_ERROR, `DNS Record is not synced yet: ${error.message}`, { ip: ip, subdomain: domain.subdomain, domain: domain.domain })); iteratorCallback(); }); }, callback); }); }); } function moveDataDir(app, sourceDir, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof sourceDir, 'string'); assert.strictEqual(typeof callback, 'function'); let resolvedSourceDir = apps.getDataDir(app, sourceDir); let resolvedTargetDir = apps.getDataDir(app, app.dataDir); debug(`moveDataDir: migrating data from ${resolvedSourceDir} to ${resolvedTargetDir}`); shell.sudo('moveDataDir', [ MV_VOLUME_CMD, resolvedSourceDir, resolvedTargetDir ], {}, function (error) { if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Error migrating data directory: ${error.message}`)); callback(null); }); } function downloadImage(manifest, callback) { assert.strictEqual(typeof manifest, 'object'); assert.strictEqual(typeof callback, 'function'); docker.info(function (error, info) { if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, `Error getting docker info: ${error.message}`)); const dfAsync = util.callbackify(df.file); dfAsync(info.DockerRootDir, function (error, diskUsage) { if (error) return callback(new BoxError(BoxError.FS_ERROR, `Error getting file system info: ${error.message}`)); if (diskUsage.available < (1024*1024*1024)) return callback(new BoxError(BoxError.DOCKER_ERROR, 'Not enough disk space to pull docker image', { diskUsage: diskUsage, dockerRootDir: info.DockerRootDir })); docker.downloadImage(manifest, function (error) { if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, `Error downloading image: ${error.message}`, { image: manifest.dockerImage })); callback(null); }); }); }); } // Ordering is based on the following rationale: // - configure nginx, icon, oauth // - register subdomain. // at this point, the user can visit the site and the above nginx config can show some install screen. // the icon can be displayed in this nginx page and oauth proxy means the page can be protected // - download image // - setup volumes // - setup addons (requires the above volume) // - setup the container (requires image, volumes, addons) // - setup collectd (requires container id) // restore is also handled here since restore is just an install with some oldConfig to clean up function install(app, restoreConfig, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof restoreConfig, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); const isInstalling = app.installationState !== apps.ISTATE_PENDING_RESTORE; // install or clone async.series([ // this protects against the theoretical possibility of an app being marked for install/restore from // a previous version of box code verifyManifest.bind(null, app.manifest), // teardown for re-installs progressCallback.bind(null, { percent: 10, message: 'Cleaning up old install' }), unconfigureReverseProxy.bind(null, app), removeCollectdProfile.bind(null, app), removeLogrotateConfig.bind(null, app), stopApp.bind(null, app, progressCallback), deleteContainers.bind(null, app, { managedOnly: true }), function teardownAddons(next) { // when restoring, app does not require these addons anymore. remove carefully to preserve the db passwords var addonsToRemove = isInstalling ? app.manifest.addons : _.omit(restoreConfig.oldManifest.addons, Object.keys(app.manifest.addons)); addons.teardownAddons(app, addonsToRemove, next); }, deleteAppDir.bind(null, app, { removeDirectory: false }), // do not remove any symlinked appdata dir // for restore case function deleteImageIfChanged(done) { if (isInstalling) return done(); if (restoreConfig.oldManifest.dockerImage === app.manifest.dockerImage) return done(); docker.deleteImage(restoreConfig.oldManifest, done); }, reserveHttpPort.bind(null, app), progressCallback.bind(null, { percent: 20, message: 'Downloading icon' }), downloadIcon.bind(null, app), progressCallback.bind(null, { percent: 30, message: 'Registering subdomains' }), registerSubdomains.bind(null, app, !isInstalling /* overwrite */), progressCallback.bind(null, { percent: 40, message: 'Downloading image' }), downloadImage.bind(null, app.manifest), progressCallback.bind(null, { percent: 50, message: 'Creating app data directory' }), createAppDir.bind(null, app), function restoreFromBackup(next) { if (!restoreConfig.backupId) { async.series([ progressCallback.bind(null, { percent: 60, message: 'Setting up addons' }), addons.setupAddons.bind(null, app, app.manifest.addons), ], next); } else { async.series([ progressCallback.bind(null, { percent: 65, message: 'Download backup and restoring addons' }), addons.setupAddons.bind(null, app, app.manifest.addons), addons.clearAddons.bind(null, app, app.manifest.addons), backups.restoreApp.bind(null, app, app.manifest.addons, restoreConfig, (progress) => { progressCallback({ percent: 65, message: `Restore - ${progress.message}` }); }) ], next); } }, progressCallback.bind(null, { percent: 70, message: 'Creating container' }), createContainer.bind(null, app), progressCallback.bind(null, { percent: 75, message: 'Setting up logrotate config' }), addLogrotateConfig.bind(null, app), progressCallback.bind(null, { percent: 80, message: 'Setting up collectd profile' }), addCollectdProfile.bind(null, app), runApp.bind(null, app, progressCallback), progressCallback.bind(null, { percent: 85, message: 'Waiting for DNS propagation' }), exports._waitForDnsPropagation.bind(null, app), progressCallback.bind(null, { percent: 95, message: 'Configuring reverse proxy' }), configureReverseProxy.bind(null, app), progressCallback.bind(null, { percent: 100, message: 'Done' }), updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, health: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error installing app: %s', error); return updateApp(app, { installationState: apps.ISTATE_ERROR, error: error.toPlainObject ? error.toPlainObject() : error.message }, callback.bind(null, error)); } callback(null); }); } function backup(app, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); async.series([ progressCallback.bind(null, { percent: 10, message: 'Backing up' }), backups.backupApp.bind(null, app, { /* options */ }, (progress) => { progressCallback({ percent: 30, message: progress.message }); }), progressCallback.bind(null, { percent: 100, message: 'Done' }), updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error backing up app: %s', error); // return to installed state intentionally return updateApp(app, { installationState: apps.ISTATE_INSTALLED, error: error.toPlainObject ? error.toPlainObject() : error.message }, callback.bind(null, error)); } callback(null); }); } function create(app, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); async.series([ progressCallback.bind(null, { percent: 10, message: 'Cleaning up old install' }), stopApp.bind(null, app, progressCallback), deleteContainers.bind(null, app, { managedOnly: true }), progressCallback.bind(null, { percent: 60, message: 'Creating container' }), createContainer.bind(null, app), progressCallback.bind(null, { percent: 80, message: 'Starting app' }), runApp.bind(null, app, progressCallback), progressCallback.bind(null, { percent: 100, message: 'Done' }), updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error creating : %s', error); return updateApp(app, { installationState: apps.ISTATE_ERROR, error: error.toPlainObject ? error.toPlainObject() : error.message }, callback.bind(null, error)); } callback(null); }); } function changeLocation(app, oldConfig, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof oldConfig, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); const locationChanged = oldConfig.fqdn !== app.fqdn; async.series([ progressCallback.bind(null, { percent: 10, message: 'Cleaning up old install' }), unconfigureReverseProxy.bind(null, app), stopApp.bind(null, app, progressCallback), deleteContainers.bind(null, app, { managedOnly: true }), function (next) { let obsoleteDomains = oldConfig.alternateDomains.filter(function (o) { return !app.alternateDomains.some(function (n) { return n.subdomain === o.subdomain && n.domain === o.domain; }); }); if (locationChanged) obsoleteDomains.push({ subdomain: oldConfig.location, domain: oldConfig.domain }); if (obsoleteDomains.length === 0) return next(); unregisterSubdomains(app, obsoleteDomains, next); }, progressCallback.bind(null, { percent: 30, message: 'Registering subdomains' }), registerSubdomains.bind(null, app, !locationChanged /* overwrite */), // if location changed, do not overwrite to detect conflicts // re-setup addons since they rely on the app's fqdn (e.g oauth) progressCallback.bind(null, { percent: 50, message: 'Setting up addons' }), addons.setupAddons.bind(null, app, app.manifest.addons), progressCallback.bind(null, { percent: 60, message: 'Creating container' }), createContainer.bind(null, app), runApp.bind(null, app, progressCallback), progressCallback.bind(null, { percent: 80, message: 'Waiting for DNS propagation' }), exports._waitForDnsPropagation.bind(null, app), progressCallback.bind(null, { percent: 90, message: 'Configuring reverse proxy' }), configureReverseProxy.bind(null, app), progressCallback.bind(null, { percent: 100, message: 'Done' }), updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error reconfiguring : %s', error); return updateApp(app, { installationState: apps.ISTATE_ERROR, error: error.toPlainObject ? error.toPlainObject() : error.message }, callback.bind(null, error)); } callback(null); }); } function migrateDataDir(app, oldConfig, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof oldConfig, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); const dataDirChanged = oldConfig.dataDir !== app.dataDir; async.series([ progressCallback.bind(null, { percent: 10, message: 'Cleaning up old install' }), stopApp.bind(null, app, progressCallback), deleteContainers.bind(null, app, { managedOnly: true }), progressCallback.bind(null, { percent: 45, message: 'Ensuring app data directory' }), createAppDir.bind(null, app), // migrate dataDir function (next) { if (!dataDirChanged) return next(); moveDataDir(app, oldConfig.dataDir, next); }, progressCallback.bind(null, { percent: 60, message: 'Creating container' }), createContainer.bind(null, app), progressCallback.bind(null, { percent: 60, message: 'Starting app' }), runApp.bind(null, app, progressCallback), progressCallback.bind(null, { percent: 100, message: 'Done' }), updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error reconfiguring : %s', error); return updateApp(app, { installationState: apps.ISTATE_ERROR, error: error.toPlainObject ? error.toPlainObject() : error.message }, callback.bind(null, error)); } callback(null); }); } // note that configure is called after an infra update as well function configure(app, oldConfig, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof oldConfig, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); const locationChanged = oldConfig.fqdn !== app.fqdn; const dataDirChanged = oldConfig.dataDir !== app.dataDir; async.series([ progressCallback.bind(null, { percent: 10, message: 'Cleaning up old install' }), unconfigureReverseProxy.bind(null, app), removeCollectdProfile.bind(null, app), removeLogrotateConfig.bind(null, app), stopApp.bind(null, app, progressCallback), deleteContainers.bind(null, app, { managedOnly: true }), function (next) { let obsoleteDomains = oldConfig.alternateDomains.filter(function (o) { return !app.alternateDomains.some(function (n) { return n.subdomain === o.subdomain && n.domain === o.domain; }); }); if (locationChanged) obsoleteDomains.push({ subdomain: oldConfig.location, domain: oldConfig.domain }); if (obsoleteDomains.length === 0) return next(); unregisterSubdomains(app, obsoleteDomains, next); }, reserveHttpPort.bind(null, app), progressCallback.bind(null, { percent: 20, message: 'Downloading icon' }), downloadIcon.bind(null, app), progressCallback.bind(null, { percent: 30, message: 'Registering subdomains' }), registerSubdomains.bind(null, app, !locationChanged /* overwrite */), // if location changed, do not overwrite to detect conflicts progressCallback.bind(null, { percent: 40, message: 'Downloading image' }), downloadImage.bind(null, app.manifest), progressCallback.bind(null, { percent: 45, message: 'Ensuring app data directory' }), createAppDir.bind(null, app), // re-setup addons since they rely on the app's fqdn (e.g oauth) progressCallback.bind(null, { percent: 50, message: 'Setting up addons' }), addons.setupAddons.bind(null, app, app.manifest.addons), // migrate dataDir function (next) { if (!dataDirChanged) return next(); moveDataDir(app, oldConfig.dataDir, next); }, progressCallback.bind(null, { percent: 60, message: 'Creating container' }), createContainer.bind(null, app), progressCallback.bind(null, { percent: 65, message: 'Setting up logrotate config' }), addLogrotateConfig.bind(null, app), progressCallback.bind(null, { percent: 70, message: 'Add collectd profile' }), addCollectdProfile.bind(null, app), runApp.bind(null, app, progressCallback), progressCallback.bind(null, { percent: 80, message: 'Waiting for DNS propagation' }), exports._waitForDnsPropagation.bind(null, app), progressCallback.bind(null, { percent: 90, message: 'Configuring reverse proxy' }), configureReverseProxy.bind(null, app), progressCallback.bind(null, { percent: 100, message: 'Done' }), updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }) ], function seriesDone(error) { if (error) { debugApp(app, 'error reconfiguring : %s', error); return updateApp(app, { installationState: apps.ISTATE_ERROR, error: error.toPlainObject ? error.toPlainObject() : error.message }, callback.bind(null, error)); } callback(null); }); } // nginx configuration is skipped because app.httpPort is expected to be available function update(app, updateConfig, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof updateConfig, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); debugApp(app, `Updating to ${updateConfig.manifest.version}`); // app does not want these addons anymore // FIXME: this does not handle option changes (like multipleDatabases) var unusedAddons = _.omit(app.manifest.addons, Object.keys(updateConfig.manifest.addons)); async.series([ // this protects against the theoretical possibility of an app being marked for update from // a previous version of box code progressCallback.bind(null, { percent: 0, message: 'Verify manifest' }), verifyManifest.bind(null, updateConfig.manifest), function (next) { if (updateConfig.skipBackup) return next(null); async.series([ progressCallback.bind(null, { percent: 15, message: 'Backing up app' }), // preserve update backups for 3 weeks backups.backupApp.bind(null, app, { preserveSecs: 3*7*24*60*60 }, (progress) => { progressCallback({ percent: 15, message: `Backup - ${progress.message}` }); }) ], function (error) { if (error) error.backupError = true; next(error); }); }, // download new image before app is stopped. this is so we can reduce downtime // and also not remove the 'common' layers when the old image is deleted progressCallback.bind(null, { percent: 25, message: 'Downloading image' }), downloadImage.bind(null, updateConfig.manifest), // note: we cleanup first and then backup. this is done so that the app is not running should backup fail // we cannot easily 'recover' from backup failures because we have to revert manfest and portBindings progressCallback.bind(null, { percent: 35, message: 'Cleaning up old install' }), removeCollectdProfile.bind(null, app), removeLogrotateConfig.bind(null, app), stopApp.bind(null, app, progressCallback), deleteContainers.bind(null, app, { managedOnly: true }), function deleteImageIfChanged(done) { if (app.manifest.dockerImage === updateConfig.manifest.dockerImage) return done(); docker.deleteImage(app.manifest, done); }, // only delete unused addons after backup addons.teardownAddons.bind(null, app, unusedAddons), // free unused ports function (next) { const currentPorts = app.portBindings || {}; const newTcpPorts = updateConfig.manifest.tcpPorts || {}; const newUdpPorts = updateConfig.manifest.udpPorts || {}; async.each(Object.keys(currentPorts), function (portName, callback) { if (newTcpPorts[portName] || newUdpPorts[portName]) return callback(null); // port still in use appdb.delPortBinding(currentPorts[portName], apps.PORT_TYPE_TCP, function (error) { if (error && error.reason === DatabaseError.NOT_FOUND) console.error('Portbinding does not exist in database.'); else if (error) return next(error); // also delete from app object for further processing (the db is updated in the next step) delete app.portBindings[portName]; callback(null); }); }, next); }, updateApp.bind(null, app, _.pick(updateConfig, 'manifest', 'appStoreId', 'memoryLimit')), // switch over to the new config progressCallback.bind(null, { percent: 45, message: 'Downloading icon' }), downloadIcon.bind(null, app), progressCallback.bind(null, { percent: 70, message: 'Updating addons' }), addons.setupAddons.bind(null, app, updateConfig.manifest.addons), progressCallback.bind(null, { percent: 80, message: 'Creating container' }), createContainer.bind(null, app), progressCallback.bind(null, { percent: 85, message: 'Setting up logrotate config' }), addLogrotateConfig.bind(null, app), progressCallback.bind(null, { percent: 90, message: 'Add collectd profile' }), addCollectdProfile.bind(null, app), runApp.bind(null, app, progressCallback), progressCallback.bind(null, { percent: 100, message: 'Done' }), updateApp.bind(null, app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null, updateTime: new Date() }) ], function seriesDone(error) { if (error && error.backupError) { debugApp(app, 'update aborted because backup failed', error); updateApp(app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }, callback.bind(null, error)); } else if (error) { debugApp(app, 'Error updating app: %s', error); updateApp(app, { installationState: apps.ISTATE_ERROR, error: error.toPlainObject ? error.toPlainObject() : error.message, updateTime: new Date() }, callback.bind(null, error)); } else { if (updateConfig.skipNotification) return callback(null); eventlog.add(eventlog.ACTION_APP_UPDATE_FINISH, auditsource.APP_TASK, { app: app, success: true }, callback); } }); } function uninstall(app, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); async.series([ progressCallback.bind(null, { percent: 0, message: 'Remove collectd profile' }), removeCollectdProfile.bind(null, app), progressCallback.bind(null, { percent: 5, message: 'Remove logrotate config' }), removeLogrotateConfig.bind(null, app), progressCallback.bind(null, { percent: 10, message: 'Stopping app' }), stopApp.bind(null, app, progressCallback), progressCallback.bind(null, { percent: 20, message: 'Deleting container' }), deleteContainers.bind(null, app, {}), progressCallback.bind(null, { percent: 30, message: 'Teardown addons' }), addons.teardownAddons.bind(null, app, app.manifest.addons), progressCallback.bind(null, { percent: 40, message: 'Deleting app data directory' }), deleteAppDir.bind(null, app, { removeDirectory: true }), progressCallback.bind(null, { percent: 50, message: 'Deleting image' }), docker.deleteImage.bind(null, app.manifest), progressCallback.bind(null, { percent: 60, message: 'Unregistering domains' }), unregisterSubdomains.bind(null, app, [ { subdomain: app.location, domain: app.domain } ].concat(app.alternateDomains)), progressCallback.bind(null, { percent: 70, message: 'Cleanup icon' }), removeIcon.bind(null, app), progressCallback.bind(null, { percent: 80, message: 'Unconfiguring reverse proxy' }), unconfigureReverseProxy.bind(null, app), progressCallback.bind(null, { percent: 90, message: 'Cleanup logs' }), cleanupLogs.bind(null, app), progressCallback.bind(null, { percent: 95, message: 'Remove app from database' }), appdb.del.bind(null, app.id) ], function seriesDone(error) { if (error) { debugApp(app, 'error uninstalling app: %s', error); return updateApp(app, { installationState: apps.ISTATE_ERROR, error: error.toPlainObject ? error.toPlainObject() : error.message }, callback.bind(null, error)); } callback(null); }); } function runApp(app, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); progressCallback({ message: 'Starting app' }); docker.startContainer(app.containerId, function (error) { if (error) return callback(error); updateApp(app, { runState: apps.RSTATE_RUNNING }, callback); }); } function stopApp(app, progressCallback, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); progressCallback({ message: 'Stopping app' }); docker.stopContainers(app.id, function (error) { if (error) return callback(error); updateApp(app, { runState: apps.RSTATE_STOPPED, health: null }, callback); }); } function run(appId, args, progressCallback, callback) { assert.strictEqual(typeof appId, 'string'); assert.strictEqual(typeof args, 'object'); assert.strictEqual(typeof progressCallback, 'function'); assert.strictEqual(typeof callback, 'function'); // determine what to do apps.get(appId, function (error, app) { if (error) return callback(error); debugApp(app, 'startTask installationState: %s runState: %s', app.installationState, app.runState); switch (app.installationState) { case apps.ISTATE_PENDING_INSTALL: return install(app, args.restoreConfig || {}, progressCallback, callback); case apps.ISTATE_PENDING_CONFIGURE: return configure(app, args.oldConfig, progressCallback, callback); case apps.ISTATE_PENDING_CREATE_CONTAINER: return create(app, progressCallback, callback); case apps.ISTATE_PENDING_LOCATION_CHANGE: return changeLocation(app, args.oldConfig, progressCallback, callback); case apps.ISTATE_PENDING_DATA_DIR_MIGRATION: return migrateDataDir(app, args.oldConfig, progressCallback, callback); case apps.ISTATE_PENDING_UNINSTALL: return uninstall(app, progressCallback, callback); case apps.ISTATE_PENDING_CLONE: return install(app, args.restoreConfig || {}, progressCallback, callback); case apps.ISTATE_PENDING_RESTORE: return install(app, args.restoreConfig || {}, progressCallback, callback); case apps.ISTATE_PENDING_UPDATE: return update(app, args.updateConfig, progressCallback, callback); case apps.ISTATE_PENDING_BACKUP: return backup(app, progressCallback, callback); case apps.ISTATE_INSTALLED: switch (app.runState) { case apps.RSTATE_PENDING_STOP: return stopApp(app, progressCallback, callback); case apps.RSTATE_PENDING_START: return runApp(app, progressCallback, callback); default: return callback(new Error('Unknown run command in apptask:' + app.runState)); } default: debugApp(app, 'apptask launched with invalid command'); return callback(new Error('Unknown install command in apptask:' + app.installationState)); } }); }