2015-07-20 00:09:47 -07:00
#!/usr/bin/env node
/* jslint node:true */
'use strict' ;
exports = module . exports = {
initialize : initialize ,
startTask : startTask ,
// exported for testing
2015-12-10 15:21:10 -08:00
_reserveHttpPort : reserveHttpPort ,
2015-07-20 00:09:47 -07:00
_configureNginx : configureNginx ,
_unconfigureNginx : unconfigureNginx ,
_createVolume : createVolume ,
_deleteVolume : deleteVolume ,
_allocateOAuthProxyCredentials : allocateOAuthProxyCredentials ,
_removeOAuthProxyCredentials : removeOAuthProxyCredentials ,
_verifyManifest : verifyManifest ,
_registerSubdomain : registerSubdomain ,
_unregisterSubdomain : unregisterSubdomain ,
_waitForDnsPropagation : waitForDnsPropagation
} ;
require ( 'supererror' ) ( { splatchError : true } ) ;
2015-09-21 09:05:14 -07:00
// remove timestamp from debug() based output
require ( 'debug' ) . formatArgs = function formatArgs ( ) {
arguments [ 0 ] = this . namespace + ' ' + arguments [ 0 ] ;
return arguments ;
} ;
2015-07-20 00:09:47 -07:00
var addons = require ( './addons.js' ) ,
appdb = require ( './appdb.js' ) ,
apps = require ( './apps.js' ) ,
assert = require ( 'assert' ) ,
async = require ( 'async' ) ,
2015-12-11 14:37:55 -08:00
certificates = require ( './certificates.js' ) ,
2015-07-20 00:09:47 -07:00
clientdb = require ( './clientdb.js' ) ,
config = require ( './config.js' ) ,
database = require ( './database.js' ) ,
DatabaseError = require ( './databaseerror.js' ) ,
debug = require ( 'debug' ) ( 'box:apptask' ) ,
2015-10-19 14:09:20 -07:00
docker = require ( './docker.js' ) ,
2015-07-20 00:09:47 -07:00
ejs = require ( 'ejs' ) ,
fs = require ( 'fs' ) ,
hat = require ( 'hat' ) ,
manifestFormat = require ( 'cloudron-manifestformat' ) ,
net = require ( 'net' ) ,
2015-12-11 14:48:02 -08:00
nginx = require ( './nginx.js' ) ,
2015-07-20 00:09:47 -07:00
path = require ( 'path' ) ,
paths = require ( './paths.js' ) ,
safe = require ( 'safetydance' ) ,
shell = require ( './shell.js' ) ,
2015-11-06 17:50:22 -08:00
SubdomainError = require ( './subdomains.js' ) . SubdomainError ,
2015-08-26 16:14:51 -07:00
subdomains = require ( './subdomains.js' ) ,
2015-07-20 00:09:47 -07:00
superagent = require ( 'superagent' ) ,
sysinfo = require ( './sysinfo.js' ) ,
util = require ( 'util' ) ,
uuid = require ( 'node-uuid' ) ,
_ = require ( 'underscore' ) ;
2015-12-11 14:48:02 -08:00
var COLLECTD _CONFIG _EJS = fs . readFileSync ( _ _dirname + '/collectd.config.ejs' , { encoding : 'utf8' } ) ,
2015-07-20 00:09:47 -07:00
RELOAD _COLLECTD _CMD = path . join ( _ _dirname , 'scripts/reloadcollectd.sh' ) ,
RMAPPDIR _CMD = path . join ( _ _dirname , 'scripts/rmappdir.sh' ) ,
CREATEAPPDIR _CMD = path . join ( _ _dirname , 'scripts/createappdir.sh' ) ;
function initialize ( callback ) {
database . initialize ( callback ) ;
}
function debugApp ( app , args ) {
assert ( ! app || typeof app === 'object' ) ;
var prefix = app ? ( app . location || '(bare)' ) : '(no app)' ;
debug ( prefix + ' ' + util . format . apply ( util , Array . prototype . slice . call ( arguments , 1 ) ) ) ;
}
2015-12-10 15:21:10 -08:00
function reserveHttpPort ( app , callback ) {
2015-07-20 00:09:47 -07:00
var server = net . createServer ( ) ;
server . listen ( 0 , function ( ) {
var port = server . address ( ) . port ;
2015-12-10 15:21:10 -08:00
updateApp ( app , { httpPort : port } , function ( error ) {
if ( error ) {
server . close ( ) ;
return callback ( error ) ;
}
server . close ( callback ) ;
2015-07-20 00:09:47 -07:00
} ) ;
} ) ;
}
2015-12-11 14:48:02 -08:00
function configureNginx ( app , callback ) {
2015-12-10 15:21:10 -08:00
var vhost = config . appFqdn ( app . location ) ;
2015-07-20 00:09:47 -07:00
2015-12-11 14:37:55 -08:00
certificates . ensureCertificate ( vhost , function ( error , certFilePath , keyFilePath ) {
if ( error ) return callback ( error ) ;
2015-12-11 14:48:02 -08:00
nginx . configureApp ( app , certFilePath , keyFilePath , callback ) ;
2015-12-11 14:37:55 -08:00
} ) ;
2015-07-20 00:09:47 -07:00
}
function unconfigureNginx ( app , callback ) {
2015-12-11 14:48:02 -08:00
// TODO: maybe revoke the cert
nginx . unconfigureApp ( app , callback ) ;
2015-07-20 00:09:47 -07:00
}
function createContainer ( app , callback ) {
2015-10-19 16:22:35 -07:00
assert ( ! app . containerId ) ; // otherwise, it will trigger volumeFrom
2015-10-19 18:48:56 -07:00
debugApp ( app , 'creating container' ) ;
2015-10-19 21:33:53 -07:00
docker . createContainer ( app , function ( error , container ) {
2015-10-19 16:00:40 -07:00
if ( error ) return callback ( new Error ( 'Error creating container: ' + error ) ) ;
2015-07-20 00:09:47 -07:00
2015-10-19 16:00:40 -07:00
updateApp ( app , { containerId : container . id } , callback ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}
2015-10-19 18:48:56 -07:00
function deleteContainers ( app , callback ) {
debugApp ( app , 'deleting containers' ) ;
docker . deleteContainers ( app . id , function ( error ) {
2015-10-19 11:40:19 -07:00
if ( error ) return callback ( new Error ( 'Error deleting container: ' + error ) ) ;
2015-07-20 00:09:47 -07:00
2015-10-19 11:40:19 -07:00
updateApp ( app , { containerId : null } , callback ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}
function createVolume ( app , callback ) {
shell . sudo ( 'createVolume' , [ CREATEAPPDIR _CMD , app . id ] , callback ) ;
}
function deleteVolume ( app , callback ) {
shell . sudo ( 'deleteVolume' , [ RMAPPDIR _CMD , app . id ] , callback ) ;
}
function allocateOAuthProxyCredentials ( app , callback ) {
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2015-10-13 09:49:05 +02:00
if ( ! app . oauthProxy ) return callback ( null ) ;
2015-07-20 00:09:47 -07:00
2015-10-15 16:31:45 -07:00
var id = 'cid-' + uuid . v4 ( ) ;
2015-07-20 00:09:47 -07:00
var clientSecret = hat ( 256 ) ;
var redirectURI = 'https://' + config . appFqdn ( app . location ) ;
2015-10-15 12:50:48 +02:00
var scope = 'profile' ;
2015-07-20 00:09:47 -07:00
2015-10-15 16:31:45 -07:00
clientdb . add ( id , app . id , clientdb . TYPE _PROXY , clientSecret , redirectURI , scope , callback ) ;
2015-07-20 00:09:47 -07:00
}
function removeOAuthProxyCredentials ( app , callback ) {
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2015-10-15 16:31:45 -07:00
clientdb . delByAppIdAndType ( app . id , clientdb . TYPE _PROXY , function ( error ) {
2015-07-20 00:09:47 -07:00
if ( error && error . reason !== DatabaseError . NOT _FOUND ) {
debugApp ( app , 'Error removing OAuth client id' , error ) ;
return callback ( error ) ;
}
callback ( null ) ;
} ) ;
}
function addCollectdProfile ( app , callback ) {
var collectdConf = ejs . render ( COLLECTD _CONFIG _EJS , { appId : app . id , containerId : app . containerId } ) ;
fs . writeFile ( path . join ( paths . COLLECTD _APPCONFIG _DIR , app . id + '.conf' ) , collectdConf , function ( error ) {
if ( error ) return callback ( error ) ;
shell . sudo ( 'addCollectdProfile' , [ RELOAD _COLLECTD _CMD ] , callback ) ;
} ) ;
}
function removeCollectdProfile ( app , callback ) {
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' , [ RELOAD _COLLECTD _CMD ] , callback ) ;
} ) ;
}
function verifyManifest ( app , callback ) {
debugApp ( app , 'Verifying manifest' ) ;
var manifest = app . manifest ;
var error = manifestFormat . parse ( manifest ) ;
if ( error ) return callback ( new Error ( util . format ( 'Manifest error: %s' , error . message ) ) ) ;
error = apps . checkManifestConstraints ( manifest ) ;
if ( error ) return callback ( error ) ;
return callback ( null ) ;
}
function downloadIcon ( app , callback ) {
debugApp ( app , 'Downloading icon of %s@%s' , app . appStoreId , app . manifest . version ) ;
var iconUrl = config . apiServerOrigin ( ) + '/api/v1/apps/' + app . appStoreId + '/versions/' + app . manifest . version + '/icon' ;
superagent
. get ( iconUrl )
. buffer ( true )
. end ( function ( error , res ) {
2015-12-15 09:12:52 -08:00
if ( error && ! error . response ) return callback ( new Error ( 'Network error downloading icon:' + error . message ) ) ;
if ( res . statusCode !== 200 ) return callback ( null ) ; // ignore error. this can also happen for apps installed with cloudron-cli
2015-07-20 00:09:47 -07:00
if ( ! safe . fs . writeFileSync ( path . join ( paths . APPICONS _DIR , app . id + '.png' ) , res . body ) ) return callback ( new Error ( 'Error saving icon:' + safe . error . message ) ) ;
callback ( null ) ;
} ) ;
}
function registerSubdomain ( app , callback ) {
// even though the bare domain is already registered in the appstore, we still
// need to register it so that we have a dnsRecordId to wait for it to complete
2015-09-15 18:56:11 -07:00
async . retry ( { times : 200 , interval : 5000 } , function ( retryCallback ) {
2015-09-08 12:51:25 -07:00
debugApp ( app , 'Registering subdomain location [%s]' , app . location ) ;
2015-10-30 13:16:07 -07:00
subdomains . add ( app . location , 'A' , [ sysinfo . getIp ( ) ] , function ( error , changeId ) {
2015-09-14 21:45:27 -07:00
if ( error && ( error . reason === SubdomainError . STILL _BUSY || error . reason === SubdomainError . EXTERNAL _ERROR ) ) return retryCallback ( error ) ; // try again
2015-07-20 00:09:47 -07:00
2015-09-08 12:51:25 -07:00
retryCallback ( null , error || changeId ) ;
} ) ;
} , function ( error , result ) {
if ( error || result instanceof Error ) return callback ( error || result ) ;
2015-07-20 00:09:47 -07:00
2015-09-08 12:51:25 -07:00
updateApp ( app , { dnsRecordId : result } , callback ) ;
2015-08-26 16:14:51 -07:00
} ) ;
2015-07-20 00:09:47 -07:00
}
2015-08-30 16:54:33 -07:00
function unregisterSubdomain ( app , location , callback ) {
2015-07-20 00:09:47 -07:00
// do not unregister bare domain because we show a error/cloudron info page there
2015-09-08 12:51:25 -07:00
if ( location === '' ) {
debugApp ( app , 'Skip unregister of empty subdomain' ) ;
return callback ( null ) ;
}
2015-07-20 00:09:47 -07:00
2015-09-08 12:51:25 -07:00
async . retry ( { times : 30 , interval : 5000 } , function ( retryCallback ) {
debugApp ( app , 'Unregistering subdomain: %s' , location ) ;
2015-10-30 13:30:19 -07:00
subdomains . remove ( location , 'A' , [ sysinfo . getIp ( ) ] , function ( error ) {
2015-10-27 18:53:06 -07:00
if ( error && ( error . reason === SubdomainError . STILL _BUSY || error . reason === SubdomainError . EXTERNAL _ERROR ) ) return retryCallback ( error ) ; // try again
2015-09-08 12:51:25 -07:00
2015-10-27 18:53:06 -07:00
retryCallback ( null , error ) ;
2015-09-08 12:51:25 -07:00
} ) ;
2015-10-27 18:53:06 -07:00
} , function ( error , result ) {
if ( error || result instanceof Error ) return callback ( error || result ) ;
2015-07-20 00:09:47 -07:00
2015-08-26 16:14:51 -07:00
updateApp ( app , { dnsRecordId : null } , callback ) ;
} ) ;
2015-07-20 00:09:47 -07:00
}
function removeIcon ( app , callback ) {
fs . unlink ( path . join ( paths . APPICONS _DIR , app . id + '.png' ) , function ( error ) {
if ( error && error . code !== 'ENOENT' ) debugApp ( app , 'cannot remove icon : %s' , error ) ;
callback ( null ) ;
} ) ;
}
function waitForDnsPropagation ( app , callback ) {
if ( ! config . CLOUDRON ) {
debugApp ( app , 'Skipping dns propagation check for development' ) ;
return callback ( null ) ;
}
function retry ( error ) {
debugApp ( app , 'waitForDnsPropagation: ' , error ) ;
setTimeout ( waitForDnsPropagation . bind ( null , app , callback ) , 5000 ) ;
}
2015-08-30 15:58:54 -07:00
subdomains . status ( app . dnsRecordId , function ( error , result ) {
2015-08-26 16:14:51 -07:00
if ( error ) return retry ( new Error ( 'Failed to get dns record status : ' + error . message ) ) ;
2015-07-20 00:09:47 -07:00
2015-08-26 16:14:51 -07:00
debugApp ( app , 'waitForDnsPropagation: dnsRecordId:%s status:%s' , app . dnsRecordId , result ) ;
2015-07-20 00:09:47 -07:00
2015-08-26 16:14:51 -07:00
if ( result !== 'done' ) return retry ( new Error ( util . format ( 'app:%s not ready yet: %s' , app . id , result ) ) ) ;
2015-07-20 00:09:47 -07:00
2015-08-26 16:14:51 -07:00
callback ( null ) ;
} ) ;
2015-07-20 00:09:47 -07:00
}
// updates the app object and the database
function updateApp ( app , values , callback ) {
2015-10-11 13:45:02 -07:00
debugApp ( app , 'updating app with values: %j' , values ) ;
2015-07-20 00:09:47 -07:00
appdb . update ( app . id , values , function ( error ) {
if ( error ) return callback ( error ) ;
for ( var value in values ) {
app [ value ] = values [ value ] ;
}
return 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)
function install ( app , callback ) {
async . series ( [
verifyManifest . bind ( null , app ) ,
// teardown for re-installs
updateApp . bind ( null , app , { installationProgress : '10, Cleaning up old install' } ) ,
2015-12-07 22:03:16 -08:00
unconfigureNginx . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
removeCollectdProfile . bind ( null , app ) ,
stopApp . bind ( null , app ) ,
2015-10-19 18:48:56 -07:00
deleteContainers . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
addons . teardownAddons . bind ( null , app , app . manifest . addons ) ,
deleteVolume . bind ( null , app ) ,
2015-08-30 16:54:33 -07:00
unregisterSubdomain . bind ( null , app , app . location ) ,
2015-07-20 00:09:47 -07:00
removeOAuthProxyCredentials . bind ( null , app ) ,
2015-08-17 19:37:48 -07:00
// removeIcon.bind(null, app), // do not remove icon for non-appstore installs
2015-07-20 00:09:47 -07:00
2015-12-10 15:21:10 -08:00
reserveHttpPort . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '20, Downloading icon' } ) ,
downloadIcon . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '25, Creating OAuth proxy credentials' } ) ,
allocateOAuthProxyCredentials . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '30, Registering subdomain' } ) ,
registerSubdomain . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '40, Downloading image' } ) ,
2015-10-19 15:37:57 -07:00
docker . downloadImage . bind ( null , app . manifest ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '50, Creating volume' } ) ,
createVolume . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '60, Setting up addons' } ) ,
addons . setupAddons . bind ( null , app , app . manifest . addons ) ,
updateApp . bind ( null , app , { installationProgress : '70, Creating container' } ) ,
createContainer . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '80, Setting up collectd profile' } ) ,
addCollectdProfile . bind ( null , app ) ,
runApp . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '90, Waiting for DNS propagation' } ) ,
exports . _waitForDnsPropagation . bind ( null , app ) ,
2015-12-07 22:03:16 -08:00
updateApp . bind ( null , app , { installationProgress : '95, Configure nginx' } ) ,
configureNginx . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
// done!
function ( callback ) {
debugApp ( app , 'installed' ) ;
updateApp ( app , { installationState : appdb . ISTATE _INSTALLED , installationProgress : '' , health : null } , callback ) ;
}
] , function seriesDone ( error ) {
if ( error ) {
debugApp ( app , 'error installing app: %s' , error ) ;
return updateApp ( app , { installationState : appdb . ISTATE _ERROR , installationProgress : error . message } , callback . bind ( null , error ) ) ;
}
callback ( null ) ;
} ) ;
}
function backup ( app , callback ) {
async . series ( [
updateApp . bind ( null , app , { installationProgress : '10, Backing up' } ) ,
2015-07-20 00:50:36 -07:00
apps . backupApp . bind ( null , app , app . manifest . addons ) ,
2015-07-20 00:09:47 -07:00
// done!
function ( callback ) {
debugApp ( app , 'installed' ) ;
updateApp ( app , { installationState : appdb . ISTATE _INSTALLED , installationProgress : '' } , callback ) ;
}
] , function seriesDone ( error ) {
if ( error ) {
debugApp ( app , 'error backing up app: %s' , error ) ;
return updateApp ( app , { installationState : appdb . ISTATE _INSTALLED , installationProgress : error . message } , callback . bind ( null , error ) ) ; // return to installed state intentionally
}
callback ( null ) ;
} ) ;
}
// restore is also called for upgrades and infra updates. note that in those cases it is possible there is no backup
function restore ( app , callback ) {
2015-07-20 09:54:17 -07:00
// we don't have a backup, same as re-install. this allows us to install from install failures (update failures always
// have a backupId)
2015-07-20 11:03:11 -07:00
if ( ! app . lastBackupId ) {
debugApp ( app , 'No lastBackupId. reinstalling' ) ;
return install ( app , callback ) ;
}
2015-07-20 00:09:47 -07:00
async . series ( [
updateApp . bind ( null , app , { installationProgress : '10, Cleaning up old install' } ) ,
2015-12-07 22:03:16 -08:00
unconfigureNginx . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
removeCollectdProfile . bind ( null , app ) ,
stopApp . bind ( null , app ) ,
2015-10-19 18:48:56 -07:00
deleteContainers . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
// oldConfig can be null during upgrades
addons . teardownAddons . bind ( null , app , app . oldConfig ? app . oldConfig . manifest . addons : null ) ,
deleteVolume . bind ( null , app ) ,
2015-08-19 14:16:01 -07:00
function deleteImageIfChanged ( done ) {
2015-08-25 12:24:02 -07:00
if ( ! app . oldConfig || ( app . oldConfig . manifest . dockerImage === app . manifest . dockerImage ) ) return done ( ) ;
2015-08-19 14:16:01 -07:00
2015-10-19 15:39:26 -07:00
docker . deleteImage ( app . oldConfig . manifest , done ) ;
2015-08-19 14:16:01 -07:00
} ,
2015-07-20 00:09:47 -07:00
removeOAuthProxyCredentials . bind ( null , app ) ,
removeIcon . bind ( null , app ) ,
2015-12-10 15:21:10 -08:00
reserveHttpPort . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '40, Downloading icon' } ) ,
downloadIcon . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '50, Create OAuth proxy credentials' } ) ,
allocateOAuthProxyCredentials . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '55, Registering subdomain' } ) , // ip might change during upgrades
registerSubdomain . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '60, Downloading image' } ) ,
2015-10-19 15:37:57 -07:00
docker . downloadImage . bind ( null , app . manifest ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '65, Creating volume' } ) ,
createVolume . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '70, Download backup and restore addons' } ) ,
2015-07-20 00:50:36 -07:00
apps . restoreApp . bind ( null , app , app . manifest . addons ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '75, Creating container' } ) ,
createContainer . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '80, Setting up collectd profile' } ) ,
addCollectdProfile . bind ( null , app ) ,
runApp . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '90, Waiting for DNS propagation' } ) ,
exports . _waitForDnsPropagation . bind ( null , app ) ,
2015-12-07 22:03:16 -08:00
updateApp . bind ( null , app , { installationProgress : '95, Configuring Nginx' } ) ,
configureNginx . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
// done!
function ( callback ) {
debugApp ( app , 'restored' ) ;
updateApp ( app , { installationState : appdb . ISTATE _INSTALLED , installationProgress : '' , health : null } , callback ) ;
}
] , function seriesDone ( error ) {
if ( error ) {
debugApp ( app , 'Error installing app: %s' , error ) ;
return updateApp ( app , { installationState : appdb . ISTATE _ERROR , installationProgress : error . message } , callback . bind ( null , error ) ) ;
}
callback ( null ) ;
} ) ;
}
// note that configure is called after an infra update as well
function configure ( app , callback ) {
async . series ( [
updateApp . bind ( null , app , { installationProgress : '10, Cleaning up old install' } ) ,
2015-12-07 22:03:16 -08:00
unconfigureNginx . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
removeCollectdProfile . bind ( null , app ) ,
stopApp . bind ( null , app ) ,
2015-10-19 18:48:56 -07:00
deleteContainers . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
function ( next ) {
2015-10-09 11:59:23 -07:00
// oldConfig can be null during an infra update
if ( ! app . oldConfig || app . oldConfig . location === app . location ) return next ( ) ;
2015-08-30 16:54:33 -07:00
unregisterSubdomain ( app , app . oldConfig . location , next ) ;
2015-07-20 00:09:47 -07:00
} ,
removeOAuthProxyCredentials . bind ( null , app ) ,
2015-12-10 15:21:10 -08:00
reserveHttpPort . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '30, Create OAuth proxy credentials' } ) ,
allocateOAuthProxyCredentials . bind ( null , app ) ,
2015-08-30 16:54:33 -07:00
updateApp . bind ( null , app , { installationProgress : '35, Registering subdomain' } ) ,
registerSubdomain . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
// re-setup addons since they rely on the app's fqdn (e.g oauth)
updateApp . bind ( null , app , { installationProgress : '50, Setting up addons' } ) ,
addons . setupAddons . bind ( null , app , app . manifest . addons ) ,
updateApp . bind ( null , app , { installationProgress : '60, Creating container' } ) ,
createContainer . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '70, Add collectd profile' } ) ,
addCollectdProfile . bind ( null , app ) ,
runApp . bind ( null , app ) ,
2015-08-30 17:00:23 -07:00
updateApp . bind ( null , app , { installationProgress : '80, Waiting for DNS propagation' } ) ,
exports . _waitForDnsPropagation . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
2015-12-07 22:03:16 -08:00
updateApp . bind ( null , app , { installationProgress : '90, Configuring Nginx' } ) ,
configureNginx . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
// done!
function ( callback ) {
debugApp ( app , 'configured' ) ;
updateApp ( app , { installationState : appdb . ISTATE _INSTALLED , installationProgress : '' , health : null } , callback ) ;
}
] , function seriesDone ( error ) {
if ( error ) {
debugApp ( app , 'error reconfiguring : %s' , error ) ;
return updateApp ( app , { installationState : appdb . ISTATE _ERROR , installationProgress : error . message } , callback . bind ( null , error ) ) ;
}
callback ( null ) ;
} ) ;
}
// nginx configuration is skipped because app.httpPort is expected to be available
function update ( app , callback ) {
debugApp ( app , 'Updating to %s' , safe . query ( app , 'manifest.version' ) ) ;
// app does not want these addons anymore
2015-07-20 10:50:44 -07:00
var unusedAddons = _ . omit ( app . oldConfig . manifest . addons , Object . keys ( app . manifest . addons ) ) ;
2015-07-20 00:09:47 -07:00
async . series ( [
updateApp . bind ( null , app , { installationProgress : '0, Verify manifest' } ) ,
verifyManifest . bind ( null , app ) ,
2015-07-20 00:55:27 -07:00
// 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
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '10, Cleaning up old install' } ) ,
removeCollectdProfile . bind ( null , app ) ,
stopApp . bind ( null , app ) ,
2015-10-19 18:48:56 -07:00
deleteContainers . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
addons . teardownAddons . bind ( null , app , unusedAddons ) ,
2015-08-19 14:16:01 -07:00
function deleteImageIfChanged ( done ) {
if ( app . oldConfig . manifest . dockerImage === app . manifest . dockerImage ) return done ( ) ;
2015-10-19 15:39:26 -07:00
docker . deleteImage ( app . oldConfig . manifest , done ) ;
2015-08-19 14:16:01 -07:00
} ,
2015-07-20 11:01:52 -07:00
// removeIcon.bind(null, app), // do not remove icon, otherwise the UI breaks for a short time...
2015-07-20 00:09:47 -07:00
2015-07-20 10:09:02 -07:00
function ( next ) {
if ( app . installationState === appdb . ISTATE _PENDING _FORCE _UPDATE ) return next ( null ) ;
async . series ( [
updateApp . bind ( null , app , { installationProgress : '20, Backup app' } ) ,
apps . backupApp . bind ( null , app , app . oldConfig . manifest . addons )
] , next ) ;
} ,
2015-07-20 00:55:27 -07:00
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '35, Downloading icon' } ) ,
downloadIcon . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '45, Downloading image' } ) ,
2015-10-19 15:37:57 -07:00
docker . downloadImage . bind ( null , app . manifest ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '70, Updating addons' } ) ,
addons . setupAddons . bind ( null , app , app . manifest . addons ) ,
updateApp . bind ( null , app , { installationProgress : '80, Creating container' } ) ,
createContainer . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '90, Add collectd profile' } ) ,
addCollectdProfile . bind ( null , app ) ,
runApp . bind ( null , app ) ,
// done!
function ( callback ) {
debugApp ( app , 'updated' ) ;
updateApp ( app , { installationState : appdb . ISTATE _INSTALLED , installationProgress : '' , health : null } , callback ) ;
}
] , function seriesDone ( error ) {
2015-07-20 00:55:27 -07:00
if ( error ) {
2015-07-20 00:09:47 -07:00
debugApp ( app , 'Error updating app: %s' , error ) ;
return updateApp ( app , { installationState : appdb . ISTATE _ERROR , installationProgress : error . message } , callback . bind ( null , error ) ) ;
}
callback ( null ) ;
} ) ;
}
function uninstall ( app , callback ) {
debugApp ( app , 'uninstalling' ) ;
async . series ( [
updateApp . bind ( null , app , { installationProgress : '0, Remove collectd profile' } ) ,
removeCollectdProfile . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '10, Stopping app' } ) ,
stopApp . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '20, Deleting container' } ) ,
2015-10-19 18:48:56 -07:00
deleteContainers . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '30, Teardown addons' } ) ,
addons . teardownAddons . bind ( null , app , app . manifest . addons ) ,
updateApp . bind ( null , app , { installationProgress : '40, Deleting volume' } ) ,
deleteVolume . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '50, Deleting image' } ) ,
2015-10-19 15:39:26 -07:00
docker . deleteImage . bind ( null , app . manifest ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '60, Unregistering subdomain' } ) ,
2015-08-30 16:54:33 -07:00
unregisterSubdomain . bind ( null , app , app . location ) ,
2015-07-20 00:09:47 -07:00
updateApp . bind ( null , app , { installationProgress : '70, Remove OAuth credentials' } ) ,
removeOAuthProxyCredentials . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '80, Cleanup icon' } ) ,
removeIcon . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '90, Unconfiguring Nginx' } ) ,
unconfigureNginx . bind ( null , app ) ,
updateApp . bind ( null , app , { installationProgress : '95, Remove app from database' } ) ,
appdb . del . bind ( null , app . id )
] , callback ) ;
}
function runApp ( app , callback ) {
2015-10-19 15:39:26 -07:00
docker . startContainer ( app . containerId , function ( error ) {
2015-10-11 11:15:09 -07:00
if ( error ) return callback ( error ) ;
2015-07-20 00:09:47 -07:00
updateApp ( app , { runState : appdb . RSTATE _RUNNING } , callback ) ;
} ) ;
}
function stopApp ( app , callback ) {
2015-10-20 00:05:07 -07:00
docker . stopContainers ( app . id , function ( error ) {
2015-07-20 00:09:47 -07:00
if ( error ) return callback ( error ) ;
2015-11-30 15:41:56 -08:00
updateApp ( app , { runState : appdb . RSTATE _STOPPED , health : null } , callback ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}
function handleRunCommand ( app , callback ) {
if ( app . runState === appdb . RSTATE _PENDING _STOP ) {
return stopApp ( app , callback ) ;
}
if ( app . runState === appdb . RSTATE _PENDING _START || app . runState === appdb . RSTATE _RUNNING ) {
debugApp ( app , 'Resuming app with state : %s' , app . runState ) ;
return runApp ( app , callback ) ;
}
debugApp ( app , 'handleRunCommand - doing nothing: %s' , app . runState ) ;
return callback ( null ) ;
}
function startTask ( appId , callback ) {
// determine what to do
appdb . get ( appId , function ( error , app ) {
if ( error ) return callback ( error ) ;
debugApp ( app , 'startTask installationState: %s runState: %s' , app . installationState , app . runState ) ;
2015-07-20 10:03:55 -07:00
switch ( app . installationState ) {
case appdb . ISTATE _PENDING _UNINSTALL : return uninstall ( app , callback ) ;
case appdb . ISTATE _PENDING _CONFIGURE : return configure ( app , callback ) ;
case appdb . ISTATE _PENDING _UPDATE : return update ( app , callback ) ;
case appdb . ISTATE _PENDING _RESTORE : return restore ( app , callback ) ;
case appdb . ISTATE _PENDING _BACKUP : return backup ( app , callback ) ;
case appdb . ISTATE _INSTALLED : return handleRunCommand ( app , callback ) ;
case appdb . ISTATE _PENDING _INSTALL : return install ( app , callback ) ;
2015-07-20 10:09:02 -07:00
case appdb . ISTATE _PENDING _FORCE _UPDATE : return update ( app , callback ) ;
2015-07-20 10:03:55 -07:00
case appdb . ISTATE _ERROR :
debugApp ( app , 'Apptask launched with error states.' ) ;
return callback ( null ) ;
default :
debugApp ( app , 'apptask launched with invalid command' ) ;
return callback ( new Error ( 'Unknown command in apptask:' + app . installationState ) ) ;
2015-07-20 00:09:47 -07:00
}
} ) ;
}
if ( require . main === module ) {
assert . strictEqual ( process . argv . length , 3 , 'Pass the appid as argument' ) ;
debug ( 'Apptask for %s' , process . argv [ 2 ] ) ;
initialize ( function ( error ) {
if ( error ) throw error ;
startTask ( process . argv [ 2 ] , function ( error ) {
2015-10-28 16:08:12 -07:00
if ( error ) debug ( 'Apptask completed with error' , error ) ;
2015-07-20 00:09:47 -07:00
debug ( 'Apptask completed for %s' , process . argv [ 2 ] ) ;
// https://nodejs.org/api/process.html are exit codes used by node. apps.js uses the value below
// to check apptask crashes
process . exit ( error ? 50 : 0 ) ;
} ) ;
} ) ;
}