2015-07-20 00:09:47 -07:00
#!/usr/bin/env node
'use strict' ;
exports = module . exports = {
2019-08-26 15:55:57 -07:00
run : run ,
2015-07-20 00:09:47 -07:00
// exported for testing
2015-12-10 15:21:10 -08:00
_reserveHttpPort : reserveHttpPort ,
2018-01-30 12:23:27 -08:00
_configureReverseProxy : configureReverseProxy ,
_unconfigureReverseProxy : unconfigureReverseProxy ,
2018-09-14 10:18:43 -07:00
_createAppDir : createAppDir ,
_deleteAppDir : deleteAppDir ,
2015-07-20 00:09:47 -07:00
_verifyManifest : verifyManifest ,
_registerSubdomain : registerSubdomain ,
_unregisterSubdomain : unregisterSubdomain ,
2018-02-07 20:54:43 +01:00
_waitForDnsPropagation : waitForDnsPropagation
2015-07-20 00:09:47 -07:00
} ;
require ( 'supererror' ) ( { splatchError : true } ) ;
var addons = require ( './addons.js' ) ,
appdb = require ( './appdb.js' ) ,
apps = require ( './apps.js' ) ,
assert = require ( 'assert' ) ,
async = require ( 'async' ) ,
2019-05-07 12:04:43 +02:00
auditsource = require ( './auditsource.js' ) ,
2016-04-10 19:17:44 -07:00
backups = require ( './backups.js' ) ,
2019-07-26 10:10:14 -07:00
constants = require ( './constants.js' ) ,
2017-10-23 22:11:33 +02:00
DatabaseError = require ( './databaseerror.js' ) ,
2015-07-20 00:09:47 -07:00
debug = require ( 'debug' ) ( 'box:apptask' ) ,
2019-08-12 20:38:24 -07:00
df = require ( '@sindresorhus/df' ) ,
2015-10-19 14:09:20 -07:00
docker = require ( './docker.js' ) ,
2017-10-29 00:15:11 +02:00
domains = require ( './domains.js' ) ,
2018-04-29 11:20:12 -07:00
DomainsError = domains . DomainsError ,
2015-07-20 00:09:47 -07:00
ejs = require ( 'ejs' ) ,
2019-05-07 12:04:43 +02:00
eventlog = require ( './eventlog.js' ) ,
2015-07-20 00:09:47 -07:00
fs = require ( 'fs' ) ,
manifestFormat = require ( 'cloudron-manifestformat' ) ,
2018-09-13 13:55:49 -07:00
mkdirp = require ( 'mkdirp' ) ,
2015-07-20 00:09:47 -07:00
net = require ( 'net' ) ,
2017-08-11 22:05:31 +02:00
os = require ( 'os' ) ,
2015-07-20 00:09:47 -07:00
path = require ( 'path' ) ,
paths = require ( './paths.js' ) ,
2018-01-30 12:23:27 -08:00
reverseProxy = require ( './reverseproxy.js' ) ,
2018-06-04 11:45:22 +02:00
rimraf = require ( 'rimraf' ) ,
2015-07-20 00:09:47 -07:00
safe = require ( 'safetydance' ) ,
2019-07-26 10:49:29 -07:00
settings = require ( './settings.js' ) ,
2015-07-20 00:09:47 -07:00
shell = require ( './shell.js' ) ,
superagent = require ( 'superagent' ) ,
sysinfo = require ( './sysinfo.js' ) ,
util = require ( 'util' ) ,
_ = require ( 'underscore' ) ;
2015-12-11 14:48:02 -08:00
var COLLECTD _CONFIG _EJS = fs . readFileSync ( _ _dirname + '/collectd.config.ejs' , { encoding : 'utf8' } ) ,
2017-09-12 21:16:44 -07:00
CONFIGURE _COLLECTD _CMD = path . join ( _ _dirname , 'scripts/configurecollectd.sh' ) ,
2018-12-20 14:33:29 -08:00
MV _VOLUME _CMD = path . join ( _ _dirname , 'scripts/mvvolume.sh' ) ,
2017-08-11 22:05:31 +02:00
LOGROTATE _CONFIG _EJS = fs . readFileSync ( _ _dirname + '/logrotate.ejs' , { encoding : 'utf8' } ) ,
2018-09-13 13:55:49 -07:00
CONFIGURE _LOGROTATE _CMD = path . join ( _ _dirname , 'scripts/configurelogrotate.sh' ) ;
2015-07-20 00:09:47 -07:00
2016-01-05 12:14:39 +01:00
function debugApp ( app ) {
assert . strictEqual ( typeof app , 'object' ) ;
2015-07-20 00:09:47 -07:00
2018-02-08 15:07:49 +01:00
debug ( app . fqdn + ' ' + util . format . apply ( util , Array . prototype . slice . call ( arguments , 1 ) ) ) ;
2015-07-20 00:09:47 -07:00
}
2017-12-21 01:04:38 -08:00
// 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 ( error ) ;
for ( var value in values ) {
app [ value ] = values [ value ] ;
}
return callback ( null ) ;
} ) ;
}
2015-12-10 15:21:10 -08:00
function reserveHttpPort ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
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
} ) ;
} ) ;
}
2018-01-30 12:23:27 -08:00
function configureReverseProxy ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2018-01-30 15:16:34 -08:00
reverseProxy . configureApp ( app , { userId : null , username : 'apptask' } , callback ) ;
2015-07-20 00:09:47 -07:00
}
2018-01-30 12:23:27 -08:00
function unconfigureReverseProxy ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2015-12-11 14:48:02 -08:00
// TODO: maybe revoke the cert
2018-01-30 12:23:27 -08:00
reverseProxy . unconfigureApp ( app , callback ) ;
2015-07-20 00:09:47 -07:00
}
function createContainer ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
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
} ) ;
}
2019-01-17 23:32:24 -08:00
function deleteContainers ( app , options , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-01-17 23:32:24 -08:00
assert . strictEqual ( typeof options , 'object' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2017-09-07 20:09:46 -07:00
debugApp ( app , 'deleting app containers (app, scheduler)' ) ;
2015-10-19 18:48:56 -07:00
2019-01-17 23:32:24 -08:00
docker . deleteContainers ( app . id , options , 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
} ) ;
}
2018-09-14 10:18:43 -07:00
function createAppDir ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2018-09-13 13:55:49 -07:00
mkdirp ( path . join ( paths . APPS _DATA _DIR , app . id ) , callback ) ;
2015-07-20 00:09:47 -07:00
}
2018-09-14 10:18:43 -07:00
function deleteAppDir ( app , options , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2017-09-07 15:43:57 -07:00
assert . strictEqual ( typeof options , 'object' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2018-09-15 17:05:04 -07:00
const appDataDir = path . join ( paths . APPS _DATA _DIR , app . id ) ;
2018-09-13 13:55:49 -07:00
2018-10-18 19:47:40 -07:00
// resolve any symlinked data dir
const stat = safe . fs . lstatSync ( appDataDir ) ;
if ( ! stat ) return callback ( null ) ;
2018-09-14 14:37:20 +02:00
2018-10-18 19:47:40 -07:00
const resolvedAppDataDir = stat . isSymbolicLink ( ) ? safe . fs . readlinkSync ( appDataDir ) : appDataDir ;
2018-09-13 13:55:49 -07:00
2018-10-18 19:47:40 -07:00
if ( safe . fs . existsSync ( resolvedAppDataDir ) ) {
const entries = safe . fs . readdirSync ( resolvedAppDataDir ) ;
if ( ! entries ) return callback ( ` Error listing ${ resolvedAppDataDir } : ${ safe . error . message } ` ) ;
// remove only files. directories inside app dir are currently volumes managed by the addons
2019-01-18 14:48:31 -08:00
// we cannot delete those dirs anyway because of perms
2018-10-18 19:47:40 -07:00
entries . forEach ( function ( entry ) {
let stat = safe . fs . statSync ( path . join ( resolvedAppDataDir , entry ) ) ;
if ( stat && ! stat . isDirectory ( ) ) safe . fs . unlinkSync ( path . join ( resolvedAppDataDir , entry ) ) ;
} ) ;
}
2018-09-15 17:05:04 -07:00
// if this fails, it's probably because the localstorage/redis addons have not cleaned up properly
2018-10-18 19:47:40 -07:00
if ( options . removeDirectory ) {
if ( stat . isSymbolicLink ( ) ) {
if ( ! safe . fs . unlinkSync ( appDataDir ) ) return callback ( safe . error . code === 'ENOENT' ? null : safe . error ) ;
} else {
if ( ! safe . fs . rmdirSync ( appDataDir ) ) return callback ( safe . error . code === 'ENOENT' ? null : safe . error ) ;
}
}
2018-09-15 17:05:04 -07:00
callback ( null ) ;
2015-07-20 00:09:47 -07:00
}
function addCollectdProfile ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-08-17 03:37:22 -07:00
var collectdConf = ejs . render ( COLLECTD _CONFIG _EJS , { appId : app . id , containerId : app . containerId , appDataDir : apps . getDataDir ( app , app . dataDir ) } ) ;
2015-07-20 00:09:47 -07:00
fs . writeFile ( path . join ( paths . COLLECTD _APPCONFIG _DIR , app . id + '.conf' ) , collectdConf , function ( error ) {
if ( error ) return callback ( error ) ;
2018-11-25 14:57:17 -08:00
shell . sudo ( 'addCollectdProfile' , [ CONFIGURE _COLLECTD _CMD , 'add' , app . id ] , { } , callback ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}
function removeCollectdProfile ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2015-07-20 00:09:47 -07:00
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 ) ;
2018-11-25 14:57:17 -08:00
shell . sudo ( 'removeCollectdProfile' , [ CONFIGURE _COLLECTD _CMD , 'remove' , app . id ] , { } , callback ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}
2017-08-11 22:05:31 +02:00
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 ( error ) ;
var runVolume = result . Mounts . find ( function ( mount ) { return mount . Destination === '/run' ; } ) ;
if ( ! runVolume ) return callback ( new Error ( 'App does not have /run mounted' ) ) ;
2017-09-12 21:45:42 -07:00
// logrotate configs can have arbitrary commands, so the config files must be owned by root
2019-07-30 14:47:33 -07:00
var logrotateConf = ejs . render ( LOGROTATE _CONFIG _EJS , { volumePath : runVolume . Source , appId : app . id } ) ;
2017-08-11 22:05:31 +02:00
var tmpFilePath = path . join ( os . tmpdir ( ) , app . id + '.logrotate' ) ;
fs . writeFile ( tmpFilePath , logrotateConf , function ( error ) {
if ( error ) return callback ( error ) ;
2018-11-25 14:57:17 -08:00
shell . sudo ( 'addLogrotateConfig' , [ CONFIGURE _LOGROTATE _CMD , 'add' , app . id , tmpFilePath ] , { } , callback ) ;
2017-08-11 22:05:31 +02:00
} ) ;
} ) ;
}
function removeLogrotateConfig ( app , callback ) {
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2018-11-25 14:57:17 -08:00
shell . sudo ( 'removeLogrotateConfig' , [ CONFIGURE _LOGROTATE _CMD , 'remove' , app . id ] , { } , callback ) ;
2017-08-11 22:05:31 +02:00
}
2017-10-12 17:46:15 -07:00
function verifyManifest ( manifest , callback ) {
assert . strictEqual ( typeof manifest , 'object' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2015-07-20 00:09:47 -07:00
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 ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2017-01-19 12:38:41 +01:00
// nothing to download if we dont have an appStoreId
if ( ! app . appStoreId ) return callback ( null ) ;
2015-07-20 00:09:47 -07:00
debugApp ( app , 'Downloading icon of %s@%s' , app . appStoreId , app . manifest . version ) ;
2019-07-26 10:49:29 -07:00
var iconUrl = settings . apiServerOrigin ( ) + '/api/v1/apps/' + app . appStoreId + '/versions/' + app . manifest . version + '/icon' ;
2015-07-20 00:09:47 -07:00
2016-04-20 00:40:20 -07:00
async . retry ( { times : 10 , interval : 5000 } , function ( retryCallback ) {
superagent
. get ( iconUrl )
. buffer ( true )
2016-09-12 12:53:51 -07:00
. timeout ( 30 * 1000 )
2016-04-20 00:40:20 -07:00
. end ( function ( error , res ) {
if ( error && ! error . response ) return retryCallback ( new 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
2015-07-20 00:09:47 -07:00
2017-01-24 10:13:25 -08:00
if ( ! safe . fs . writeFileSync ( path . join ( paths . APP _ICONS _DIR , app . id + '.png' ) , res . body ) ) return retryCallback ( new Error ( 'Error saving icon:' + safe . error . message ) ) ;
2015-07-20 00:09:47 -07:00
2016-04-20 00:40:20 -07:00
retryCallback ( null ) ;
2017-09-17 21:25:07 -07:00
} ) ;
2016-04-20 00:40:20 -07:00
} , callback ) ;
2015-07-20 00:09:47 -07:00
}
2017-02-02 10:40:10 -08:00
function registerSubdomain ( app , overwrite , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2017-02-02 10:40:10 -08:00
assert . strictEqual ( typeof overwrite , 'boolean' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2017-02-23 22:03:44 -08:00
sysinfo . getPublicIp ( function ( error , ip ) {
2016-01-05 12:16:39 +01:00
if ( error ) return callback ( error ) ;
2015-09-08 12:51:25 -07:00
2016-01-05 12:16:39 +01:00
async . retry ( { times : 200 , interval : 5000 } , function ( retryCallback ) {
2018-02-08 15:07:49 +01:00
debugApp ( app , 'Registering subdomain location [%s] overwrite: %s' , app . fqdn , overwrite ) ;
2015-07-20 00:09:47 -07:00
2016-09-05 17:14:17 -07:00
// get the current record before updating it
2018-02-08 12:05:29 -08:00
domains . getDnsRecords ( app . location , app . domain , 'A' , function ( error , values ) {
2019-03-13 10:48:42 -07:00
if ( error && error . reason === DomainsError . EXTERNAL _ERROR ) return retryCallback ( error ) ; // try again
if ( error ) return retryCallback ( null , error ) ; // give up for access and other errors
2015-07-20 00:09:47 -07:00
2016-09-05 17:39:10 -07:00
// refuse to update any existing DNS record for custom domains that we did not create
2018-01-10 20:52:59 -08:00
if ( values . length !== 0 && ! overwrite ) return retryCallback ( null , new Error ( 'DNS Record already exists' ) ) ;
2016-09-05 17:14:17 -07:00
2018-06-29 22:25:34 +02:00
domains . upsertDnsRecords ( app . location , app . domain , 'A' , [ ip ] , function ( error ) {
2018-10-05 17:07:08 +02:00
if ( error && ( error . reason === DomainsError . STILL _BUSY || error . reason === DomainsError . EXTERNAL _ERROR ) ) {
debug ( 'Upsert error. Will retry.' , error . message ) ;
return retryCallback ( error ) ; // try again
}
2016-09-05 17:14:17 -07:00
2018-06-29 22:25:34 +02:00
retryCallback ( null , error ) ;
2016-09-05 17:14:17 -07:00
} ) ;
2016-01-05 12:16:39 +01:00
} ) ;
} , function ( error , result ) {
if ( error || result instanceof Error ) return callback ( error || result ) ;
2018-06-30 14:02:40 +02:00
callback ( null ) ;
2016-01-05 12:16:39 +01:00
} ) ;
2015-08-26 16:14:51 -07:00
} ) ;
2015-07-20 00:09:47 -07:00
}
2017-11-10 00:11:28 +01:00
function unregisterSubdomain ( app , location , domain , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2017-11-10 00:11:28 +01:00
assert . strictEqual ( typeof domain , 'string' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2017-02-23 22:03:44 -08:00
sysinfo . getPublicIp ( function ( error , ip ) {
2016-01-05 12:16:39 +01:00
if ( error ) return callback ( error ) ;
2015-09-08 12:51:25 -07:00
2016-01-05 12:16:39 +01:00
async . retry ( { times : 30 , interval : 5000 } , function ( retryCallback ) {
2018-02-08 15:07:49 +01:00
debugApp ( app , 'Unregistering subdomain: %s' , app . fqdn ) ;
2015-09-08 12:51:25 -07:00
2018-02-08 12:05:29 -08:00
domains . removeDnsRecords ( location , domain , 'A' , [ ip ] , function ( error ) {
2018-04-29 11:20:12 -07:00
if ( error && error . reason === DomainsError . NOT _FOUND ) return retryCallback ( null , null ) ; // domain can be not found if oldConfig.domain or restoreConfig.domain was removed
if ( error && ( error . reason === DomainsError . STILL _BUSY || error . reason === DomainsError . EXTERNAL _ERROR ) ) return retryCallback ( error ) ; // try again
2015-07-20 00:09:47 -07:00
2016-01-05 12:16:39 +01:00
retryCallback ( null , error ) ;
} ) ;
} , function ( error , result ) {
if ( error || result instanceof Error ) return callback ( error || result ) ;
2018-06-30 14:02:40 +02:00
callback ( null ) ;
2016-01-05 12:16:39 +01:00
} ) ;
2015-08-26 16:14:51 -07:00
} ) ;
2015-07-20 00:09:47 -07:00
}
2018-06-29 15:07:20 +02:00
function registerAlternateDomains ( 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 ) ;
async . eachSeries ( app . alternateDomains , function ( domain , callback ) {
async . retry ( { times : 200 , interval : 5000 } , function ( retryCallback ) {
2018-06-29 17:00:03 +02:00
debugApp ( app , 'Registering alternate subdomain [%s] overwrite: %s' , ( domain . subdomain ? ( domain . subdomain + '.' ) : '' ) + domain . domain , overwrite ) ;
2018-06-29 15:07:20 +02:00
// get the current record before updating it
domains . getDnsRecords ( domain . subdomain , domain . domain , 'A' , function ( error , values ) {
if ( error ) return retryCallback ( error ) ;
// refuse to update any existing DNS record for custom domains that we did not create
if ( values . length !== 0 && ! overwrite ) return retryCallback ( null , new Error ( 'DNS Record already exists' ) ) ;
2018-06-29 22:25:34 +02:00
domains . upsertDnsRecords ( domain . subdomain , domain . domain , 'A' , [ ip ] , function ( error ) {
2018-10-05 17:07:08 +02:00
if ( error && ( error . reason === DomainsError . STILL _BUSY || error . reason === DomainsError . EXTERNAL _ERROR ) ) {
debug ( 'Upsert error. Will retry.' , error . message ) ;
return retryCallback ( error ) ; // try again
}
2018-06-29 22:25:34 +02:00
retryCallback ( null , error ) ;
2018-06-29 15:07:20 +02:00
} ) ;
} ) ;
} , function ( error , result ) {
if ( error || result instanceof Error ) return callback ( error || result ) ;
2018-06-29 22:25:34 +02:00
callback ( ) ;
2018-06-29 15:07:20 +02:00
} ) ;
} , callback ) ;
} ) ;
}
2018-06-29 19:04:48 +02:00
function unregisterAlternateDomains ( app , all , callback ) {
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof all , 'boolean' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2018-10-15 16:29:30 -07:00
let obsoleteDomains = [ ] ;
if ( all ) {
obsoleteDomains = app . alternateDomains ;
} else if ( app . oldConfig ) { // oldConfig can be null during an infra update
obsoleteDomains = app . oldConfig . alternateDomains . filter ( function ( o ) {
return ! app . alternateDomains . some ( function ( n ) { return n . subdomain === o . subdomain && n . domain === o . domain ; } ) ;
} ) ;
}
2018-06-29 19:04:48 +02:00
if ( obsoleteDomains . length === 0 ) return callback ( ) ;
sysinfo . getPublicIp ( function ( error , ip ) {
if ( error ) return callback ( error ) ;
async . eachSeries ( obsoleteDomains , function ( domain , callback ) {
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 ) ) return retryCallback ( error ) ; // try again
retryCallback ( null , error ) ;
} ) ;
} , function ( error , result ) {
if ( error || result instanceof Error ) return callback ( error || result ) ;
2018-06-29 22:25:34 +02:00
callback ( ) ;
2018-06-29 19:04:48 +02:00
} ) ;
} , callback ) ;
} ) ;
}
2015-07-20 00:09:47 -07:00
function removeIcon ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-05-17 09:47:11 -07:00
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 ) ;
2015-07-20 00:09:47 -07:00
}
2018-06-04 11:45:22 +02:00
function cleanupLogs ( app , callback ) {
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2018-09-18 14:15:23 -07:00
// note that redis container logs are cleaned up by the addon
2018-06-04 11:48:22 +02:00
rimraf ( path . join ( paths . LOG _DIR , app . id ) , function ( error ) {
if ( error ) debugApp ( app , 'cannot cleanup logs: %s' , error ) ;
2018-09-18 14:15:23 -07:00
2018-06-04 11:45:22 +02:00
callback ( null ) ;
} ) ;
}
2015-07-20 00:09:47 -07:00
function waitForDnsPropagation ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-07-26 10:10:14 -07:00
if ( ! constants . CLOUDRON ) {
2015-07-20 00:09:47 -07:00
debugApp ( app , 'Skipping dns propagation check for development' ) ;
return callback ( null ) ;
}
2017-02-23 22:03:44 -08:00
sysinfo . getPublicIp ( function ( error , ip ) {
2016-11-30 18:34:11 +01:00
if ( error ) return callback ( error ) ;
2018-09-22 11:25:58 -07:00
domains . waitForDnsRecord ( app . location , app . domain , 'A' , ip , { interval : 5000 , times : 240 } , function ( error ) {
2018-06-29 22:32:47 +02:00
if ( error ) return callback ( error ) ;
// now wait for alternateDomains, if any
2018-09-22 11:25:58 -07:00
async . eachSeries ( app . alternateDomains , function ( domain , iteratorCallback ) {
domains . waitForDnsRecord ( domain . subdomain , domain . domain , 'A' , ip , { interval : 5000 , times : 240 } , iteratorCallback ) ;
2018-06-29 22:32:47 +02:00
} , callback ) ;
} ) ;
2016-11-30 18:34:11 +01:00
} ) ;
2015-07-20 00:09:47 -07:00
}
2018-12-20 14:33:29 -08:00
function migrateDataDir ( 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 ( ` migrateDataDir: migrating data from ${ resolvedSourceDir } to ${ resolvedTargetDir } ` ) ;
shell . sudo ( 'migrateDataDir' , [ MV _VOLUME _CMD , resolvedSourceDir , resolvedTargetDir ] , { } , callback ) ;
}
2019-08-12 20:38:24 -07:00
function downloadImage ( manifest , callback ) {
assert . strictEqual ( typeof manifest , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
docker . info ( function ( error , info ) {
if ( error ) return callback ( error ) ;
const dfAsync = util . callbackify ( df . file ) ;
dfAsync ( info . DockerRootDir , function ( error , diskUsage ) {
if ( error ) return callback ( error ) ;
if ( diskUsage . available < ( 1024 * 1024 * 1024 ) ) return callback ( new Error ( 'Not enough disk space to pull docker image. See https://cloudron.io/documentation/storage/#docker-image-location' ) ) ;
docker . downloadImage ( manifest , callback ) ;
} ) ;
} ) ;
}
2015-07-20 00:09:47 -07:00
// 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)
2017-10-12 17:46:15 -07:00
// restore is also handled here since restore is just an install with some oldConfig to clean up
2019-08-27 15:18:16 -07:00
function install ( app , restoreConfig , progressCallback , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-08-27 15:18:16 -07:00
assert . strictEqual ( typeof restoreConfig , 'object' ) ;
2019-08-26 20:43:15 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-08-27 15:18:16 -07:00
const isRestoring = app . installationState === appdb . ISTATE _PENDING _RESTORE ;
2017-04-11 12:49:21 -07:00
2015-07-20 00:09:47 -07:00
async . series ( [
2017-10-12 17:46:15 -07:00
// 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 ) ,
2015-07-20 00:09:47 -07:00
// teardown for re-installs
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 10 , message : 'Cleaning up old install' } ) ,
2018-01-30 12:23:27 -08:00
unconfigureReverseProxy . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
removeCollectdProfile . bind ( null , app ) ,
2017-08-11 22:05:31 +02:00
removeLogrotateConfig . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
stopApp . bind ( null , app ) ,
2019-01-17 23:32:24 -08:00
deleteContainers . bind ( null , app , { managedOnly : true } ) ,
2018-05-22 12:05:55 -07:00
function teardownAddons ( next ) {
// when restoring, app does not require these addons anymore. remove carefully to preserve the db passwords
2019-08-27 15:18:16 -07:00
var addonsToRemove = ! isRestoring ? app . manifest . addons : _ . omit ( restoreConfig . oldManifest . addons , Object . keys ( app . manifest . addons ) ) ;
2018-05-22 12:05:55 -07:00
addons . teardownAddons ( app , addonsToRemove , next ) ;
} ,
2018-12-18 21:16:23 -08:00
deleteAppDir . bind ( null , app , { removeDirectory : false } ) , // do not remove any symlinked appdata dir
2017-04-11 15:16:42 -07:00
// for restore case
function deleteImageIfChanged ( done ) {
2019-08-27 15:18:16 -07:00
if ( ! restoreConfig . oldManifest ) return done ( ) ;
2017-04-11 15:16:42 -07:00
2019-08-27 15:18:16 -07:00
if ( restoreConfig . oldManifest . dockerImage === app . manifest . dockerImage ) return done ( ) ;
docker . deleteImage ( restoreConfig . oldManifest , done ) ;
2017-04-11 15:16:42 -07:00
} ,
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
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 20 , message : 'Downloading icon' } ) ,
2015-07-20 00:09:47 -07:00
downloadIcon . bind ( null , app ) ,
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 30 , message : 'Registering subdomain' } ) ,
2017-04-12 13:02:39 -07:00
registerSubdomain . bind ( null , app , isRestoring /* overwrite */ ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 35 , message : 'Registering alternate domains' } ) ,
2018-09-22 10:09:46 -07:00
registerAlternateDomains . bind ( null , app , isRestoring /* overwrite */ ) ,
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 40 , message : 'Downloading image' } ) ,
2019-08-12 20:38:24 -07:00
downloadImage . bind ( null , app . manifest ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 50 , message : 'Creating app data directory' } ) ,
2018-09-14 10:18:43 -07:00
createAppDir . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
2017-04-11 12:49:21 -07:00
function restoreFromBackup ( next ) {
2019-08-27 15:18:16 -07:00
if ( ! restoreConfig . backupId ) {
2017-04-20 17:55:47 -07:00
async . series ( [
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 60 , message : 'Setting up addons' } ) ,
2017-04-20 17:55:47 -07:00
addons . setupAddons . bind ( null , app , app . manifest . addons ) ,
] , next ) ;
} else {
async . series ( [
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 65 , message : 'Download backup and restoring addons' } ) ,
2018-09-15 17:05:04 -07:00
addons . setupAddons . bind ( null , app , app . manifest . addons ) ,
addons . clearAddons . bind ( null , app , app . manifest . addons ) ,
2019-08-26 22:11:27 -07:00
backups . restoreApp . bind ( null , app , app . manifest . addons , restoreConfig , ( progress ) => {
progressCallback ( { percent : 65 , message : ` Restore - ${ progress . message } ` } ) ;
} )
2017-04-20 17:55:47 -07:00
] , next ) ;
}
2017-04-11 12:49:21 -07:00
} ,
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 70 , message : 'Creating container' } ) ,
2015-07-20 00:09:47 -07:00
createContainer . bind ( null , app ) ,
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 75 , message : 'Setting up logrotate config' } ) ,
2017-08-11 22:05:31 +02:00
addLogrotateConfig . bind ( null , app ) ,
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 80 , message : 'Setting up collectd profile' } ) ,
2015-07-20 00:09:47 -07:00
addCollectdProfile . bind ( null , app ) ,
runApp . bind ( null , app ) ,
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 85 , message : 'Waiting for DNS propagation' } ) ,
2015-07-20 00:09:47 -07:00
exports . _waitForDnsPropagation . bind ( null , app ) ,
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 95 , message : 'Configuring reverse proxy' } ) ,
2018-01-30 12:23:27 -08:00
configureReverseProxy . bind ( null , app ) ,
2015-12-07 22:03:16 -08:00
2019-08-26 20:43:15 -07:00
progressCallback . bind ( null , { percent : 100 , message : 'Done' } ) ,
updateApp . bind ( null , app , { installationState : appdb . ISTATE _INSTALLED , health : null , taskId : null } )
2015-07-20 00:09:47 -07:00
] , function seriesDone ( error ) {
if ( error ) {
debugApp ( app , 'error installing app: %s' , error ) ;
2019-08-27 20:08:35 -07:00
return updateApp ( app , { installationState : appdb . ISTATE _ERROR , errorMessage : error . message , taskId : null } , callback . bind ( null , error ) ) ;
2015-07-20 00:09:47 -07:00
}
callback ( null ) ;
} ) ;
}
2019-08-26 22:11:27 -07:00
function backup ( app , progressCallback , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-08-26 22:11:27 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2015-07-20 00:09:47 -07:00
async . series ( [
2019-08-26 22:11:27 -07:00
progressCallback . bind ( null , { percent : 10 , message : 'Backing up' } ) ,
backups . backupApp . bind ( null , app , { /* options */ } , ( progress ) => {
progressCallback ( { percent : 30 , message : progress . message } ) ;
} ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 22:11:27 -07:00
progressCallback . bind ( null , { percent : 100 , message : 'Done' } ) ,
2019-08-27 20:08:35 -07:00
updateApp . bind ( null , app , { installationState : appdb . ISTATE _INSTALLED , errorMessage : '' , taskId : null } )
2015-07-20 00:09:47 -07:00
] , function seriesDone ( error ) {
if ( error ) {
debugApp ( app , 'error backing up app: %s' , error ) ;
2019-08-27 20:08:35 -07:00
return updateApp ( app , { installationState : appdb . ISTATE _INSTALLED , errorMessage : error . message , taskId : null } , callback . bind ( null , error ) ) ; // return to installed state intentionally
2015-07-20 00:09:47 -07:00
}
callback ( null ) ;
} ) ;
}
// note that configure is called after an infra update as well
2019-08-27 15:18:16 -07:00
function configure ( app , oldConfig , progressCallback , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-08-27 15:18:16 -07:00
assert . strictEqual ( typeof oldConfig , 'object' ) ;
2019-08-26 21:36:18 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2017-09-14 19:33:07 -07:00
// oldConfig can be null during an infra update
2019-08-27 15:18:16 -07:00
const locationChanged = oldConfig . fqdn !== app . fqdn ;
const dataDirChanged = oldConfig . dataDir !== app . dataDir ;
2017-09-14 19:33:07 -07:00
2015-07-20 00:09:47 -07:00
async . series ( [
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 10 , message : 'Cleaning up old install' } ) ,
2018-01-30 12:23:27 -08:00
unconfigureReverseProxy . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
removeCollectdProfile . bind ( null , app ) ,
2017-08-11 22:05:31 +02:00
removeLogrotateConfig . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
stopApp . bind ( null , app ) ,
2019-01-17 23:32:24 -08:00
deleteContainers . bind ( null , app , { managedOnly : true } ) ,
2018-06-29 19:04:48 +02:00
unregisterAlternateDomains . bind ( null , app , false /* all */ ) ,
2015-07-20 00:09:47 -07:00
function ( next ) {
2017-09-14 19:33:07 -07:00
if ( ! locationChanged ) return next ( ) ;
2018-01-10 21:22:44 -08:00
2019-08-27 15:18:16 -07:00
unregisterSubdomain ( app , oldConfig . location , oldConfig . domain , next ) ;
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
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 20 , message : 'Downloading icon' } ) ,
2017-03-15 20:24:03 -07:00
downloadIcon . bind ( null , app ) ,
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 30 , message : 'Registering subdomain' } ) ,
2017-09-14 19:33:07 -07:00
registerSubdomain . bind ( null , app , ! locationChanged /* overwrite */ ) , // if location changed, do not overwrite to detect conflicts
2015-07-20 00:09:47 -07:00
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 35 , message : 'Registering alternate domains' } ) ,
2018-06-29 15:07:20 +02:00
registerAlternateDomains . bind ( null , app , true /* overwrite */ ) , // figure out when to overwrite
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 40 , message : 'Downloading image' } ) ,
2019-08-12 20:38:24 -07:00
downloadImage . bind ( null , app . manifest ) ,
2017-02-17 15:00:55 +01:00
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 45 , message : 'Ensuring app data directory' } ) ,
2018-09-14 10:18:43 -07:00
createAppDir . bind ( null , app ) ,
2017-02-17 15:00:55 +01:00
2015-07-20 00:09:47 -07:00
// re-setup addons since they rely on the app's fqdn (e.g oauth)
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 50 , message : 'Setting up addons' } ) ,
2015-07-20 00:09:47 -07:00
addons . setupAddons . bind ( null , app , app . manifest . addons ) ,
2018-12-20 14:33:29 -08:00
// migrate dataDir
function ( next ) {
if ( ! dataDirChanged ) return next ( ) ;
2019-08-27 15:18:16 -07:00
migrateDataDir ( app , oldConfig . dataDir , next ) ;
2018-12-20 14:33:29 -08:00
} ,
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 60 , message : 'Creating container' } ) ,
2015-07-20 00:09:47 -07:00
createContainer . bind ( null , app ) ,
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 65 , message : 'Setting up logrotate config' } ) ,
2017-08-11 22:05:31 +02:00
addLogrotateConfig . bind ( null , app ) ,
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 70 , message : 'Add collectd profile' } ) ,
2015-07-20 00:09:47 -07:00
addCollectdProfile . bind ( null , app ) ,
runApp . bind ( null , app ) ,
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 80 , message : 'Waiting for DNS propagation' } ) ,
2015-08-30 17:00:23 -07:00
exports . _waitForDnsPropagation . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 90 , message : 'Configuring reverse proxy' } ) ,
2018-01-30 12:23:27 -08:00
configureReverseProxy . bind ( null , app ) ,
2015-12-07 22:03:16 -08:00
2019-08-26 21:36:18 -07:00
progressCallback . bind ( null , { percent : 100 , message : 'Done' } ) ,
2019-08-27 20:08:35 -07:00
updateApp . bind ( null , app , { installationState : appdb . ISTATE _INSTALLED , errorMessage : '' , health : null , taskId : null } )
2015-07-20 00:09:47 -07:00
] , function seriesDone ( error ) {
if ( error ) {
debugApp ( app , 'error reconfiguring : %s' , error ) ;
2019-08-27 20:08:35 -07:00
return updateApp ( app , { installationState : appdb . ISTATE _ERROR , errorMessage : error . message , taskId : null } , callback . bind ( null , error ) ) ;
2015-07-20 00:09:47 -07:00
}
callback ( null ) ;
} ) ;
}
// nginx configuration is skipped because app.httpPort is expected to be available
2019-08-27 15:18:16 -07:00
function update ( app , updateConfig , progressCallback , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-08-27 15:18:16 -07:00
assert . strictEqual ( typeof updateConfig , 'object' ) ;
2019-08-26 22:08:40 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-08-27 15:18:16 -07:00
debugApp ( app , ` Updating to ${ updateConfig . manifest . version } ` ) ;
2015-07-20 00:09:47 -07:00
// app does not want these addons anymore
2016-02-02 08:37:48 -08:00
// FIXME: this does not handle option changes (like multipleDatabases)
2019-08-27 15:18:16 -07:00
var unusedAddons = _ . omit ( app . manifest . addons , Object . keys ( updateConfig . manifest . addons ) ) ;
2015-07-20 00:09:47 -07:00
2019-05-15 19:15:21 +02:00
const FORCED _UPDATE = ( app . installationState === appdb . ISTATE _PENDING _FORCE _UPDATE ) ;
2015-07-20 00:09:47 -07:00
async . series ( [
2017-10-12 17:46:15 -07:00
// this protects against the theoretical possibility of an app being marked for update from
// a previous version of box code
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 0 , message : 'Verify manifest' } ) ,
2019-08-27 15:18:16 -07:00
verifyManifest . bind ( null , updateConfig . manifest ) ,
2017-10-12 17:46:15 -07:00
function ( next ) {
2019-05-15 19:15:21 +02:00
if ( FORCED _UPDATE ) return next ( null ) ;
2017-10-12 17:46:15 -07:00
async . series ( [
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 15 , message : 'Backing up app' } ) ,
2019-04-13 17:09:15 -07:00
// preserve update backups for 3 weeks
2019-08-26 22:11:27 -07:00
backups . backupApp . bind ( null , app , { preserveSecs : 3 * 7 * 24 * 60 * 60 } , ( progress ) => {
progressCallback ( { percent : 15 , message : ` Backup - ${ progress . message } ` } ) ;
} )
2017-11-21 15:26:55 -08:00
] , function ( error ) {
if ( error ) error . backupError = true ;
next ( error ) ;
} ) ;
2017-10-12 17:46:15 -07:00
} ,
2015-07-20 00:09:47 -07:00
2016-02-04 19:53:39 -08:00
// 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
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 25 , message : 'Downloading image' } ) ,
2019-08-27 15:18:16 -07:00
downloadImage . bind ( null , updateConfig . manifest ) ,
2016-02-04 19:53:39 -08:00
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
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 35 , message : 'Cleaning up old install' } ) ,
2015-07-20 00:09:47 -07:00
removeCollectdProfile . bind ( null , app ) ,
2017-08-11 22:05:31 +02:00
removeLogrotateConfig . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
stopApp . bind ( null , app ) ,
2019-01-17 23:32:24 -08:00
deleteContainers . bind ( null , app , { managedOnly : true } ) ,
2015-08-19 14:16:01 -07:00
function deleteImageIfChanged ( done ) {
2019-08-27 15:18:16 -07:00
if ( app . manifest . dockerImage === updateConfig . manifest . dockerImage ) return done ( ) ;
2015-08-19 14:16:01 -07:00
2017-10-12 17:46:15 -07:00
docker . deleteImage ( app . manifest , done ) ;
2015-07-20 10:09:02 -07:00
} ,
2015-07-20 00:55:27 -07:00
2016-06-13 23:07:41 -07:00
// only delete unused addons after backup
addons . teardownAddons . bind ( null , app , unusedAddons ) ,
2017-10-23 22:11:33 +02:00
// free unused ports
function ( next ) {
2018-08-12 22:47:59 -07:00
const currentPorts = app . portBindings || { } ;
2019-08-27 15:18:16 -07:00
const newTcpPorts = updateConfig . manifest . tcpPorts || { } ;
const newUdpPorts = updateConfig . manifest . udpPorts || { } ;
2017-10-23 22:11:33 +02:00
async . each ( Object . keys ( currentPorts ) , function ( portName , callback ) {
2018-08-12 22:47:59 -07:00
if ( newTcpPorts [ portName ] || newUdpPorts [ portName ] ) return callback ( ) ; // port still in use
2017-10-23 22:11:33 +02:00
2018-08-12 22:08:19 -07:00
appdb . delPortBinding ( currentPorts [ portName ] , apps . PORT _TYPE _TCP , function ( error ) {
2017-10-23 22:11:33 +02:00
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 ( ) ;
} ) ;
} , next ) ;
} ,
2017-10-12 17:46:15 -07:00
// switch over to the new config. manifest, memoryLimit, portBindings, appstoreId are updated here
2019-08-27 15:18:16 -07:00
updateApp . bind ( null , app , updateConfig ) ,
2017-10-12 17:46:15 -07:00
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 45 , message : 'Downloading icon' } ) ,
2015-07-20 00:09:47 -07:00
downloadIcon . bind ( null , app ) ,
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 70 , message : 'Updating addons' } ) ,
2019-08-27 15:18:16 -07:00
addons . setupAddons . bind ( null , app , updateConfig . manifest . addons ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 80 , message : 'Creating container' } ) ,
2015-07-20 00:09:47 -07:00
createContainer . bind ( null , app ) ,
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 85 , message : 'Setting up logrotate config' } ) ,
2017-08-11 22:05:31 +02:00
addLogrotateConfig . bind ( null , app ) ,
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 90 , message : 'Add collectd profile' } ) ,
2015-07-20 00:09:47 -07:00
addCollectdProfile . bind ( null , app ) ,
runApp . bind ( null , app ) ,
2019-08-26 22:08:40 -07:00
progressCallback . bind ( null , { percent : 100 , message : 'Done' } ) ,
2019-08-27 22:03:07 -07:00
updateApp . bind ( null , app , { installationState : appdb . ISTATE _INSTALLED , errorMessage : '' , health : null , taskId : null , updateTime : new Date ( ) } )
2015-07-20 00:09:47 -07:00
] , function seriesDone ( error ) {
2017-11-21 15:26:55 -08:00
if ( error && error . backupError ) {
debugApp ( app , 'update aborted because backup failed' , error ) ;
2019-08-27 22:03:07 -07:00
updateApp ( app , { installationState : appdb . ISTATE _INSTALLED , errorMessage : '' , health : null , taskId : null } , callback . bind ( null , error ) ) ;
2017-11-21 15:26:55 -08:00
} else if ( error ) {
2015-07-20 00:09:47 -07:00
debugApp ( app , 'Error updating app: %s' , error ) ;
2019-08-27 20:08:35 -07:00
updateApp ( app , { installationState : appdb . ISTATE _ERROR , errorMessage : error . message , updateTime : new Date ( ) } , callback . bind ( null , error ) ) ;
2017-11-21 15:26:55 -08:00
} else {
2019-05-15 19:15:21 +02:00
// do not spam the notifcation view
if ( FORCED _UPDATE ) return callback ( ) ;
2019-05-07 12:04:43 +02:00
eventlog . add ( eventlog . ACTION _APP _UPDATE _FINISH , auditsource . APP _TASK , { app : app , success : true } , callback ) ;
2015-07-20 00:09:47 -07:00
}
} ) ;
}
2019-08-26 21:56:55 -07:00
function uninstall ( app , progressCallback , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-08-26 21:56:55 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2015-07-20 00:09:47 -07:00
async . series ( [
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 0 , message : 'Remove collectd profile' } ) ,
2015-07-20 00:09:47 -07:00
removeCollectdProfile . bind ( null , app ) ,
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 5 , message : 'Remove logrotate config' } ) ,
2017-08-11 22:05:31 +02:00
removeLogrotateConfig . bind ( null , app ) ,
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 10 , message : 'Stopping app' } ) ,
2015-07-20 00:09:47 -07:00
stopApp . bind ( null , app ) ,
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 20 , message : 'Deleting container' } ) ,
2019-01-17 23:32:24 -08:00
deleteContainers . bind ( null , app , { } ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 30 , message : 'Teardown addons' } ) ,
2015-07-20 00:09:47 -07:00
addons . teardownAddons . bind ( null , app , app . manifest . addons ) ,
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 40 , message : 'Deleting app data directory' } ) ,
2018-09-14 10:18:43 -07:00
deleteAppDir . bind ( null , app , { removeDirectory : true } ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 50 , message : 'Deleting image' } ) ,
2015-10-19 15:39:26 -07:00
docker . deleteImage . bind ( null , app . manifest ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 60 , message : 'Unregistering domains' } ) ,
2018-06-29 19:04:48 +02:00
unregisterAlternateDomains . bind ( null , app , true /* all */ ) ,
2017-11-09 22:56:15 +01:00
unregisterSubdomain . bind ( null , app , app . location , app . domain ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 70 , message : 'Cleanup icon' } ) ,
2015-07-20 00:09:47 -07:00
removeIcon . bind ( null , app ) ,
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 80 , message : 'Unconfiguring reverse proxy' } ) ,
2018-01-30 12:23:27 -08:00
unconfigureReverseProxy . bind ( null , app ) ,
2015-07-20 00:09:47 -07:00
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 90 , message : 'Cleanup logs' } ) ,
2018-06-04 11:45:22 +02:00
cleanupLogs . bind ( null , app ) ,
2019-08-26 21:56:55 -07:00
progressCallback . bind ( null , { percent : 95 , message : 'Remove app from database' } ) ,
2015-07-20 00:09:47 -07:00
appdb . del . bind ( null , app . id )
2016-02-24 17:53:21 +01:00
] , function seriesDone ( error ) {
if ( error ) {
debugApp ( app , 'error uninstalling app: %s' , error ) ;
2019-08-27 20:08:35 -07:00
return updateApp ( app , { installationState : appdb . ISTATE _ERROR , errorMessage : error . message , taskId : null } , callback . bind ( null , error ) ) ;
2016-02-24 17:53:21 +01:00
}
callback ( null ) ;
} ) ;
2015-07-20 00:09:47 -07:00
}
function runApp ( app , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
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 ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
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
} ) ;
}
2019-08-26 22:16:42 -07:00
function handleRunCommand ( app , progressCallback , callback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-08-26 22:16:42 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2015-07-20 00:09:47 -07:00
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 ) ;
}
2019-08-27 15:18:16 -07:00
function run ( appId , args , progressCallback , callback ) {
2019-08-26 15:55:57 -07:00
assert . strictEqual ( typeof appId , 'string' ) ;
2019-08-27 15:18:16 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-08-26 15:55:57 -07:00
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 ) ;
2015-07-20 00:09:47 -07:00
2015-07-20 10:03:55 -07:00
switch ( app . installationState ) {
2019-08-27 15:18:16 -07:00
case appdb . ISTATE _PENDING _INSTALL : return install ( app , args . restoreConfig || { } , progressCallback , callback ) ;
case appdb . ISTATE _PENDING _CONFIGURE : return configure ( app , args . oldConfig , progressCallback , callback ) ;
2019-08-26 21:56:55 -07:00
case appdb . ISTATE _PENDING _UNINSTALL : return uninstall ( app , progressCallback , callback ) ;
2019-08-27 15:18:16 -07:00
case appdb . ISTATE _PENDING _CLONE : return install ( app , args . restoreConfig || { } , progressCallback , callback ) ;
case appdb . ISTATE _PENDING _RESTORE : return install ( app , args . restoreConfig || { } , progressCallback , callback ) ;
case appdb . ISTATE _PENDING _UPDATE : return update ( app , args . updateConfig , progressCallback , callback ) ;
case appdb . ISTATE _PENDING _FORCE _UPDATE : return update ( app , args . updateConfig , progressCallback , callback ) ;
2019-08-26 22:11:27 -07:00
case appdb . ISTATE _PENDING _BACKUP : return backup ( app , progressCallback , callback ) ;
2019-08-26 22:16:42 -07:00
case appdb . ISTATE _INSTALLED : return handleRunCommand ( app , progressCallback , callback ) ;
2017-04-11 15:16:42 -07:00
2015-07-20 10:03:55 -07:00
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
}
} ) ;
}