2015-07-20 00:09:47 -07:00
#!/usr/bin/env node
'use strict' ;
exports = module . exports = {
2021-02-24 14:47:05 -08:00
run ,
2019-08-26 15:55:57 -07:00
2015-07-20 00:09:47 -07:00
// exported for testing
2018-09-14 10:18:43 -07:00
_createAppDir : createAppDir ,
_deleteAppDir : deleteAppDir ,
2015-07-20 00:09:47 -07:00
_verifyManifest : verifyManifest ,
2018-02-07 20:54:43 +01:00
_waitForDnsPropagation : waitForDnsPropagation
2015-07-20 00:09:47 -07:00
} ;
2021-08-20 09:19:44 -07:00
const apps = require ( './apps.js' ) ,
2015-07-20 00:09:47 -07:00
assert = require ( 'assert' ) ,
2021-09-30 09:50:30 -07:00
AuditSource = require ( './auditsource.js' ) ,
2021-07-14 11:07:19 -07:00
backuptask = require ( './backuptask.js' ) ,
2019-09-05 17:13:07 -07:00
BoxError = require ( './boxerror.js' ) ,
2020-01-31 13:33:19 -08:00
collectd = require ( './collectd.js' ) ,
2019-07-26 10:10:14 -07:00
constants = require ( './constants.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' ) ,
2021-08-13 17:22:28 -07:00
dns = require ( './dns.js' ) ,
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' ) ,
2020-11-20 14:13:16 -08:00
iputils = require ( './iputils.js' ) ,
2015-07-20 00:09:47 -07:00
manifestFormat = require ( 'cloudron-manifestformat' ) ,
2021-09-29 20:16:06 -07:00
mounts = require ( './mounts.js' ) ,
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' ) ,
2021-08-25 19:41:46 -07:00
promiseRetry = require ( './promise-retry.js' ) ,
2018-01-30 12:23:27 -08:00
reverseProxy = require ( './reverseproxy.js' ) ,
2015-07-20 00:09:47 -07:00
safe = require ( 'safetydance' ) ,
2021-01-21 11:31:35 -08:00
services = require ( './services.js' ) ,
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' ) ,
_ = require ( 'underscore' ) ;
2022-04-27 18:41:20 -07:00
const 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
2022-04-27 18:41:20 -07:00
// https://rootlesscontaine.rs/getting-started/common/cgroup2/#checking-whether-cgroup-v2-is-already-enabled
const CGROUP _VERSION = fs . existsSync ( '/sys/fs/cgroup/cgroup.controllers' ) ? '2' : '1' ;
const COLLECTD _CONFIG _EJS = fs . readFileSync ( ` ${ _ _dirname } /collectd/app_cgroup_v ${ CGROUP _VERSION } .ejs ` , { encoding : 'utf8' } ) ;
2019-09-24 10:28:50 -07:00
function makeTaskError ( error , app ) {
2022-08-08 13:50:26 +02:00
assert ( error instanceof BoxError ) ;
2019-12-05 09:32:45 -08:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-24 10:28:50 -07:00
// track a few variables which helps 'repair' restart the task (see also scheduleTask in apps.js)
2019-11-22 14:27:41 -08:00
error . details . taskId = app . taskId ;
error . details . installationState = app . installationState ;
return error . toPlainObject ( ) ;
2019-09-21 19:45:55 -07:00
}
2017-12-21 01:04:38 -08:00
// updates the app object and the database
2021-08-25 19:41:46 -07:00
async function updateApp ( app , values ) {
2017-12-21 01:04:38 -08:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof values , 'object' ) ;
2021-08-25 19:41:46 -07:00
await apps . update ( app . id , values ) ;
2017-12-21 01:04:38 -08:00
2021-08-25 19:41:46 -07:00
for ( const value in values ) {
app [ value ] = values [ value ] ;
}
2017-12-21 01:04:38 -08:00
}
2021-08-25 19:41:46 -07:00
async function allocateContainerIp ( app ) {
2020-11-20 14:13:16 -08:00
assert . strictEqual ( typeof app , 'object' ) ;
2022-06-09 13:57:57 +02:00
if ( app . manifest . id === constants . PROXY _APP _APPSTORE _ID ) return ;
2022-06-06 20:04:22 +02:00
2021-12-07 11:18:26 -08:00
await promiseRetry ( { times : 10 , interval : 0 , debug } , async function ( ) {
2020-11-20 14:13:16 -08:00
const iprange = iputils . intFromIp ( '172.18.20.255' ) - iputils . intFromIp ( '172.18.16.1' ) ;
let rnd = Math . floor ( Math . random ( ) * iprange ) ;
const containerIp = iputils . ipFromInt ( iputils . intFromIp ( '172.18.16.1' ) + rnd ) ;
2022-04-14 15:40:51 -05:00
await updateApp ( app , { containerIp } ) ;
2021-08-25 19:41:46 -07:00
} ) ;
2020-11-20 14:13:16 -08:00
}
2021-08-25 19:41:46 -07:00
async function createContainer ( app ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2015-10-19 16:22:35 -07:00
assert ( ! app . containerId ) ; // otherwise, it will trigger volumeFrom
2022-06-09 13:57:57 +02:00
if ( app . manifest . id === constants . PROXY _APP _APPSTORE _ID ) return ;
2022-06-06 20:04:22 +02:00
2021-09-25 21:38:09 -07:00
debug ( 'createContainer: creating container' ) ;
2015-10-19 18:48:56 -07:00
2021-08-25 19:41:46 -07:00
const container = await docker . createContainer ( app ) ;
2015-07-20 00:09:47 -07:00
2021-08-25 19:41:46 -07:00
await updateApp ( app , { containerId : container . id } ) ;
2019-11-19 17:27:39 -08:00
2021-08-25 19:41:46 -07:00
// re-generate configs that rely on container id
await addLogrotateConfig ( app ) ;
await addCollectdProfile ( app ) ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
async function deleteContainers ( app , options ) {
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
2021-09-25 21:38:09 -07:00
debug ( 'deleteContainer: deleting app containers (app, scheduler)' ) ;
2015-10-19 18:48:56 -07:00
2021-09-16 13:59:03 -07:00
// remove configs that rely on container id
await removeCollectdProfile ( app ) ;
await removeLogrotateConfig ( app ) ;
await docker . stopContainers ( app . id ) ;
await docker . deleteContainers ( app . id , options ) ;
await updateApp ( app , { containerId : null } ) ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
async function createAppDir ( app ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-06 11:19:58 -07:00
const appDir = path . join ( paths . APPS _DATA _DIR , app . id ) ;
2021-09-16 13:59:03 -07:00
const [ error ] = await safe ( fs . promises . mkdir ( appDir , { recursive : true } ) ) ;
if ( error ) throw new BoxError ( BoxError . FS _ERROR , ` Error creating directory: ${ error . message } ` , { appDir } ) ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
async function deleteAppDir ( app , options ) {
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
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 ) ;
2021-09-16 13:59:03 -07:00
if ( ! stat ) return ;
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 ) ;
2021-09-16 13:59:03 -07:00
if ( ! entries ) throw new BoxError ( BoxError . FS _ERROR , ` Error listing ${ resolvedAppDataDir } : ${ safe . error . message } ` ) ;
2018-10-18 19:47:40 -07:00
// 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 ( ) ) {
2019-09-06 11:19:58 -07:00
if ( ! safe . fs . unlinkSync ( appDataDir ) ) {
2021-09-16 13:59:03 -07:00
if ( safe . error . code !== 'ENOENT' ) throw new BoxError ( BoxError . FS _ERROR , ` Error unlinking dir ${ appDataDir } : ${ safe . error . message } ` ) ;
2019-09-06 11:19:58 -07:00
}
2018-10-18 19:47:40 -07:00
} else {
2022-02-16 20:30:33 -08:00
if ( ! safe . fs . rmSync ( appDataDir , { recursive : true } ) ) {
2021-09-16 13:59:03 -07:00
if ( safe . error . code !== 'ENOENT' ) throw new BoxError ( BoxError . FS _ERROR , ` Error removing dir ${ appDataDir } : ${ safe . error . message } ` ) ;
2019-09-06 11:19:58 -07:00
}
2018-10-18 19:47:40 -07:00
}
}
2015-07-20 00:09:47 -07:00
}
2021-08-19 13:24:38 -07:00
async function addCollectdProfile ( app ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2022-06-01 22:44:52 -07:00
const appDataDir = await apps . getStorageDir ( app ) ;
2022-06-02 11:19:33 -07:00
const collectdConf = ejs . render ( COLLECTD _CONFIG _EJS , { appId : app . id , containerId : app . containerId , appDataDir } ) ;
2021-08-19 13:24:38 -07:00
await collectd . addProfile ( app . id , collectdConf ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-19 13:24:38 -07:00
async function removeCollectdProfile ( app ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2021-08-19 13:24:38 -07:00
await collectd . removeProfile ( app . id ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-25 19:41:46 -07:00
async function addLogrotateConfig ( app ) {
2017-08-11 22:05:31 +02:00
assert . strictEqual ( typeof app , 'object' ) ;
2021-08-25 19:41:46 -07:00
const result = await docker . inspect ( app . containerId ) ;
2017-08-11 22:05:31 +02:00
2021-08-25 19:41:46 -07:00
const runVolume = result . Mounts . find ( function ( mount ) { return mount . Destination === '/run' ; } ) ;
if ( ! runVolume ) throw new BoxError ( BoxError . DOCKER _ERROR , 'App does not have /run mounted' ) ;
2017-08-11 22:05:31 +02:00
2021-08-25 19:41:46 -07:00
// logrotate configs can have arbitrary commands, so the config files must be owned by root
const logrotateConf = ejs . render ( LOGROTATE _CONFIG _EJS , { volumePath : runVolume . Source , appId : app . id } ) ;
const tmpFilePath = path . join ( os . tmpdir ( ) , app . id + '.logrotate' ) ;
2019-09-06 11:19:58 -07:00
2021-08-25 19:41:46 -07:00
safe . fs . writeFileSync ( tmpFilePath , logrotateConf ) ;
if ( safe . error ) throw new BoxError ( BoxError . FS _ERROR , ` Error writing logrotate config: ${ safe . error . message } ` ) ;
2019-09-06 11:19:58 -07:00
2021-08-25 19:41:46 -07:00
const [ error ] = await safe ( shell . promises . sudo ( 'addLogrotateConfig' , [ CONFIGURE _LOGROTATE _CMD , 'add' , app . id , tmpFilePath ] , { } ) ) ;
if ( error ) throw new BoxError ( BoxError . LOGROTATE _ERROR , ` Error adding logrotate config: ${ error . message } ` ) ;
2017-08-11 22:05:31 +02:00
}
2021-08-25 19:41:46 -07:00
async function removeLogrotateConfig ( app ) {
2017-08-11 22:05:31 +02:00
assert . strictEqual ( typeof app , 'object' ) ;
2021-08-25 19:41:46 -07:00
const [ error ] = await safe ( shell . promises . sudo ( 'removeLogrotateConfig' , [ CONFIGURE _LOGROTATE _CMD , 'remove' , app . id ] , { } ) ) ;
if ( error ) throw new BoxError ( BoxError . LOGROTATE _ERROR , ` Error removing logrotate config: ${ error . message } ` ) ;
2019-09-06 11:19:58 -07:00
}
2021-09-16 13:59:03 -07:00
async function cleanupLogs ( app ) {
2019-09-06 11:19:58 -07:00
assert . strictEqual ( typeof app , 'object' ) ;
// note that redis container logs are cleaned up by the addon
2021-09-16 13:59:03 -07:00
const [ error ] = await safe ( fs . promises . rm ( path . join ( paths . LOG _DIR , app . id ) , { force : true , recursive : true } ) ) ;
2021-09-25 21:38:09 -07:00
if ( error ) debug ( 'cleanupLogs: cannot cleanup logs:' , error ) ;
2017-08-11 22:05:31 +02:00
}
2021-09-16 13:59:03 -07:00
async function verifyManifest ( manifest ) {
2017-10-12 17:46:15 -07:00
assert . strictEqual ( typeof manifest , 'object' ) ;
2016-01-05 12:14:39 +01:00
2021-09-16 13:59:03 -07:00
let error = manifestFormat . parse ( manifest ) ;
2022-02-07 13:19:59 -08:00
if ( error ) throw new BoxError ( BoxError . BAD _FIELD , ` Manifest error: ${ error . message } ` ) ;
2015-07-20 00:09:47 -07:00
error = apps . checkManifestConstraints ( manifest ) ;
2022-02-07 13:19:59 -08:00
if ( error ) throw new BoxError ( BoxError . CONFLICT , ` Manifest constraint check failed: ${ error . message } ` ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-25 19:41:46 -07:00
async function downloadIcon ( app ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2017-01-19 12:38:41 +01:00
// nothing to download if we dont have an appStoreId
2021-08-25 19:41:46 -07:00
if ( ! app . appStoreId ) return ;
2017-01-19 12:38:41 +01:00
2021-09-25 21:38:09 -07:00
debug ( ` downloadIcon: Downloading icon of ${ app . appStoreId } @ ${ app . manifest . version } ` ) ;
2015-07-20 00:09:47 -07:00
2021-05-13 22:22:55 -07:00
const iconUrl = settings . apiServerOrigin ( ) + '/api/v1/apps/' + app . appStoreId + '/versions/' + app . manifest . version + '/icon' ;
2015-07-20 00:09:47 -07:00
2021-12-07 11:18:26 -08:00
await promiseRetry ( { times : 10 , interval : 5000 , debug } , async function ( ) {
2021-08-25 19:41:46 -07:00
const [ networkError , response ] = await safe ( superagent . get ( iconUrl )
2016-04-20 00:40:20 -07:00
. buffer ( true )
2016-09-12 12:53:51 -07:00
. timeout ( 30 * 1000 )
2021-08-25 19:41:46 -07:00
. ok ( ( ) => true ) ) ;
2015-07-20 00:09:47 -07:00
2021-08-25 19:41:46 -07:00
if ( networkError ) throw new BoxError ( BoxError . NETWORK _ERROR , ` Network error downloading icon : ${ networkError . message } ` ) ;
if ( response . status !== 200 ) return ; // ignore error. this can also happen for apps installed with cloudron-cli
await updateApp ( app , { appStoreIcon : response . body } ) ;
} ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-27 09:52:24 -07:00
async function waitForDnsPropagation ( app ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-07-26 10:10:14 -07:00
if ( ! constants . CLOUDRON ) {
2021-09-25 21:38:09 -07:00
debug ( 'waitForDnsPropagation: Skipping dns propagation check for development' ) ;
2021-08-27 09:52:24 -07:00
return ;
2015-07-20 00:09:47 -07:00
}
2022-01-06 17:02:16 -08:00
const ipv4 = await sysinfo . getServerIPv4 ( ) ;
2022-02-15 12:31:55 -08:00
const ipv6 = await sysinfo . getServerIPv6 ( ) ;
2022-01-06 17:02:16 -08:00
let error ;
2022-01-16 12:32:12 -08:00
[ error ] = await safe ( dns . waitForDnsRecord ( app . subdomain , app . domain , 'A' , ipv4 , { times : 240 } ) ) ;
if ( error ) throw new BoxError ( BoxError . DNS _ERROR , ` DNS A Record is not synced yet: ${ error . message } ` , { ipv4 , subdomain : app . subdomain , domain : app . domain } ) ;
2022-02-15 12:31:55 -08:00
if ( ipv6 ) {
2022-01-16 12:32:12 -08:00
[ error ] = await safe ( dns . waitForDnsRecord ( app . subdomain , app . domain , 'AAAA' , ipv6 , { times : 240 } ) ) ;
if ( error ) throw new BoxError ( BoxError . DNS _ERROR , ` DNS AAAA Record is not synced yet: ${ error . message } ` , { ipv6 , subdomain : app . subdomain , domain : app . domain } ) ;
2022-01-06 17:02:16 -08:00
}
2019-09-06 11:19:58 -07:00
2022-01-14 22:29:47 -08:00
// now wait for redirectDomains and aliasDomains, if any
2022-01-14 22:40:51 -08:00
const allDomains = app . secondaryDomains . concat ( app . redirectDomains ) . concat ( app . aliasDomains ) ;
for ( const domain of allDomains ) {
2022-01-06 17:02:16 -08:00
[ error ] = await safe ( dns . waitForDnsRecord ( domain . subdomain , domain . domain , 'A' , ipv4 , { times : 240 } ) ) ;
if ( error ) throw new BoxError ( BoxError . DNS _ERROR , ` DNS A Record is not synced yet: ${ error . message } ` , { ipv4 , subdomain : domain . subdomain , domain : domain . domain } ) ;
2022-02-15 12:31:55 -08:00
if ( ipv6 ) {
2022-01-06 17:02:16 -08:00
[ error ] = await safe ( dns . waitForDnsRecord ( domain . subdomain , domain . domain , 'AAAA' , ipv6 , { times : 240 } ) ) ;
if ( error ) throw new BoxError ( BoxError . DNS _ERROR , ` DNS AAAA Record is not synced yet: ${ error . message } ` , { ipv6 , subdomain : domain . subdomain , domain : domain . domain } ) ;
}
2021-08-27 09:52:24 -07:00
}
2015-07-20 00:09:47 -07:00
}
2022-06-01 22:44:52 -07:00
async function moveDataDir ( app , targetVolumeId , targetVolumePrefix ) {
2018-12-20 14:33:29 -08:00
assert . strictEqual ( typeof app , 'object' ) ;
2022-06-01 22:44:52 -07:00
assert ( targetVolumeId === null || typeof targetVolumeId === 'string' ) ;
assert ( targetVolumePrefix === null || typeof targetVolumePrefix === 'string' ) ;
2018-12-20 14:33:29 -08:00
2022-06-01 22:44:52 -07:00
const resolvedSourceDir = await apps . getStorageDir ( app ) ;
const resolvedTargetDir = await apps . getStorageDir ( _ . extend ( { } , app , { storageVolumeId : targetVolumeId , storageVolumePrefix : targetVolumePrefix } ) ) ;
2018-12-20 14:33:29 -08:00
2021-09-25 21:38:09 -07:00
debug ( ` moveDataDir: migrating data from ${ resolvedSourceDir } to ${ resolvedTargetDir } ` ) ;
2018-12-20 14:33:29 -08:00
2022-06-08 11:40:40 -07:00
if ( resolvedSourceDir !== resolvedTargetDir ) {
const [ error ] = await safe ( shell . promises . sudo ( 'moveDataDir' , [ MV _VOLUME _CMD , resolvedSourceDir , resolvedTargetDir ] , { } ) ) ;
if ( error ) throw new BoxError ( BoxError . EXTERNAL _ERROR , ` Error migrating data directory: ${ error . message } ` ) ;
}
2019-12-06 08:09:43 -08:00
2022-06-08 11:40:40 -07:00
await updateApp ( app , { storageVolumeId : targetVolumeId , storageVolumePrefix : targetVolumePrefix } ) ;
2018-12-20 14:33:29 -08:00
}
2021-08-25 19:41:46 -07:00
async function downloadImage ( manifest ) {
2019-08-12 20:38:24 -07:00
assert . strictEqual ( typeof manifest , 'object' ) ;
2022-06-06 20:04:22 +02:00
// skip for relay app
2022-06-09 13:57:57 +02:00
if ( manifest . id === constants . PROXY _APP _APPSTORE _ID ) return ;
2022-06-06 20:04:22 +02:00
2021-08-25 19:41:46 -07:00
const info = await docker . info ( ) ;
const [ dfError , diskUsage ] = await safe ( df . file ( info . DockerRootDir ) ) ;
if ( dfError ) throw new BoxError ( BoxError . FS _ERROR , ` Error getting file system info: ${ dfError . message } ` ) ;
2019-08-12 20:38:24 -07:00
2021-08-25 19:41:46 -07:00
if ( diskUsage . available < ( 1024 * 1024 * 1024 ) ) throw new BoxError ( BoxError . DOCKER _ERROR , 'Not enough disk space to pull docker image' , { diskUsage : diskUsage , dockerRootDir : info . DockerRootDir } ) ;
2019-09-06 11:19:58 -07:00
2021-08-25 19:41:46 -07:00
await docker . downloadImage ( manifest ) ;
2019-08-12 20:38:24 -07:00
}
2021-08-25 19:41:46 -07:00
async function startApp ( app ) {
2021-09-25 21:38:09 -07:00
debug ( 'startApp: starting container' ) ;
2020-11-20 11:17:24 -08:00
2021-08-25 19:41:46 -07:00
if ( app . runState === apps . RSTATE _STOPPED ) return ;
2019-09-22 00:20:12 -07:00
2022-06-06 20:04:22 +02:00
// skip for relay app
2022-06-09 13:57:57 +02:00
if ( app . manifest . id === constants . PROXY _APP _APPSTORE _ID ) return ;
2022-06-06 20:04:22 +02:00
2021-08-25 19:41:46 -07:00
await docker . startContainer ( app . id ) ;
2019-09-22 00:20:12 -07:00
}
2021-09-16 13:59:03 -07:00
async function install ( app , args , progressCallback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-16 09:31:34 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-08-26 20:43:15 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
2019-09-30 09:30:49 -07:00
const restoreConfig = args . restoreConfig ; // has to be set when restoring
2019-09-16 09:31:34 -07:00
const overwriteDns = args . overwriteDns ;
2021-02-24 14:47:05 -08:00
const skipDnsSetup = args . skipDnsSetup ;
2019-12-06 09:42:05 -08:00
const oldManifest = args . oldManifest ;
2017-04-11 12:49:21 -07:00
2021-09-16 13:59:03 -07:00
// this protects against the theoretical possibility of an app being marked for install/restore from
// a previous version of box code
await verifyManifest ( app . manifest ) ;
// teardown for re-installs
await progressCallback ( { percent : 10 , message : 'Cleaning up old install' } ) ;
await reverseProxy . unconfigureApp ( app ) ;
await deleteContainers ( app , { managedOnly : true } ) ;
// when restoring, app does not require these addons anymore. remove carefully to preserve the db passwords
let addonsToRemove ;
if ( oldManifest ) {
addonsToRemove = _ . omit ( oldManifest . addons , Object . keys ( app . manifest . addons ) ) ;
} else {
addonsToRemove = app . manifest . addons ;
}
await services . teardownAddons ( app , addonsToRemove ) ;
2018-05-22 12:05:55 -07:00
2022-04-05 09:28:30 -07:00
if ( ! restoreConfig || restoreConfig . remotePath ) { // in-place import should not delete data dir
2021-09-16 13:59:03 -07:00
await deleteAppDir ( app , { removeDirectory : false } ) ; // do not remove any symlinked appdata dir
}
2017-04-11 12:49:21 -07:00
2021-09-16 13:59:03 -07:00
if ( oldManifest && oldManifest . dockerImage === app . manifest . dockerImage ) {
await docker . deleteImage ( oldManifest ) ;
}
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
// allocating container ip here, lets the users "repair" an app if allocation fails at apps.add time
await allocateContainerIp ( app ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 20 , message : 'Downloading icon' } ) ;
await downloadIcon ( app ) ;
2021-02-24 16:57:47 -08:00
2021-09-16 13:59:03 -07:00
if ( ! skipDnsSetup ) {
await progressCallback ( { percent : 30 , message : 'Registering subdomains' } ) ;
2015-07-20 00:09:47 -07:00
2022-01-16 12:32:12 -08:00
await dns . registerLocations ( [ { subdomain : app . subdomain , domain : app . domain } ] . concat ( app . secondaryDomains ) . concat ( app . redirectDomains ) . concat ( app . aliasDomains ) , { overwriteDns } , progressCallback ) ;
2021-09-16 13:59:03 -07:00
}
2020-11-20 14:13:16 -08:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 40 , message : 'Downloading image' } ) ;
await downloadImage ( app . manifest ) ;
await progressCallback ( { percent : 50 , message : 'Creating app data directory' } ) ;
await createAppDir ( app ) ;
2021-09-29 20:16:06 -07:00
if ( ! restoreConfig ) { // install
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 60 , message : 'Setting up addons' } ) ;
await services . setupAddons ( app , app . manifest . addons ) ;
2022-04-05 09:28:30 -07:00
} else if ( app . installationState === apps . ISTATE _PENDING _IMPORT && ! restoreConfig . remotePath ) { // in-place import
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 60 , message : 'Importing addons in-place' } ) ;
await services . setupAddons ( app , app . manifest . addons ) ;
await services . clearAddons ( app , _ . omit ( app . manifest . addons , 'localstorage' ) ) ;
await apps . restoreConfig ( app ) ;
await services . restoreAddons ( app , app . manifest . addons ) ;
2021-09-29 20:16:06 -07:00
} else if ( app . installationState === apps . ISTATE _PENDING _IMPORT ) { // import
await progressCallback ( { percent : 65 , message : 'Downloading backup and restoring addons' } ) ;
await services . setupAddons ( app , app . manifest . addons ) ;
await services . clearAddons ( app , app . manifest . addons ) ;
const backupConfig = restoreConfig . backupConfig ; // can be null
let mountObject = null ;
2022-01-26 12:40:28 -08:00
if ( backupConfig && mounts . isManagedProvider ( backupConfig . provider ) ) {
2021-09-29 20:16:06 -07:00
await progressCallback ( { percent : 70 , message : 'Setting up mount for importing' } ) ;
mountObject = { // keep this in sync with importApp in apps.js
name : ` appimport- ${ app . id } ` ,
hostPath : ` /mnt/appimport- ${ app . id } ` ,
mountType : backupConfig . provider ,
mountOptions : backupConfig . mountOptions
} ;
await mounts . tryAddMount ( mountObject , { timeout : 10 } ) ;
}
await backuptask . downloadApp ( app , restoreConfig , ( progress ) => { progressCallback ( { percent : 75 , message : progress . message } ) ; } ) ;
await apps . restoreConfig ( app ) ;
if ( mountObject ) await mounts . removeMount ( mountObject ) ;
await progressCallback ( { percent : 75 , message : 'Restoring addons' } ) ;
await services . restoreAddons ( app , app . manifest . addons ) ;
} else { // clone and restore
await progressCallback ( { percent : 65 , message : 'Downloading backup and restoring addons' } ) ;
2021-09-16 13:59:03 -07:00
await services . setupAddons ( app , app . manifest . addons ) ;
await services . clearAddons ( app , app . manifest . addons ) ;
await backuptask . downloadApp ( app , restoreConfig , ( progress ) => { progressCallback ( { percent : 65 , message : progress . message } ) ; } ) ;
await progressCallback ( { percent : 70 , message : 'Restoring addons' } ) ;
await services . restoreAddons ( app , app . manifest . addons ) ;
}
await progressCallback ( { percent : 80 , message : 'Creating container' } ) ;
await createContainer ( app ) ;
await startApp ( app ) ;
if ( ! skipDnsSetup ) {
await progressCallback ( { percent : 85 , message : 'Waiting for DNS propagation' } ) ;
await exports . _waitForDnsPropagation ( app ) ;
}
await progressCallback ( { percent : 95 , message : 'Configuring reverse proxy' } ) ;
2021-09-30 09:50:30 -07:00
await reverseProxy . configureApp ( app , AuditSource . APPTASK ) ;
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
async function backup ( app , args , progressCallback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-21 19:45:55 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-08-26 22:11:27 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 10 , message : 'Backing up' } ) ;
2021-09-30 10:45:25 -07:00
const backupId = await backuptask . backupApp ( app , { snapshotOnly : ! ! args . snapshotOnly } , ( progress ) => {
2021-09-16 13:59:03 -07:00
progressCallback ( { percent : 30 , message : progress . message } ) ;
2021-09-30 10:45:25 -07:00
} ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null } ) ;
2021-09-30 10:45:25 -07:00
return backupId ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
async function create ( app , args , progressCallback ) {
2019-09-08 16:57:08 -07:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-21 19:45:55 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-09-08 16:57:08 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 10 , message : 'Cleaning up old install' } ) ;
await deleteContainers ( app , { managedOnly : true } ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
// FIXME: re-setup addons only because sendmail addon to re-inject env vars on mailboxName change
await progressCallback ( { percent : 30 , message : 'Setting up addons' } ) ;
await services . setupAddons ( app , app . manifest . addons ) ;
2019-09-09 15:58:58 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 60 , message : 'Creating container' } ) ;
await createContainer ( app ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await startApp ( app ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
2019-09-08 16:57:08 -07:00
}
2021-09-16 13:59:03 -07:00
async function changeLocation ( app , args , progressCallback ) {
2019-09-08 16:57:08 -07:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-10 15:23:47 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-09-08 16:57:08 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2019-09-10 15:23:47 -07:00
const oldConfig = args . oldConfig ;
2019-09-08 16:57:08 -07:00
const locationChanged = oldConfig . fqdn !== app . fqdn ;
2021-02-24 14:47:05 -08:00
const skipDnsSetup = args . skipDnsSetup ;
2019-09-26 22:30:58 -07:00
const overwriteDns = args . overwriteDns ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 10 , message : 'Cleaning up old install' } ) ;
await reverseProxy . unconfigureApp ( app ) ;
await deleteContainers ( app , { managedOnly : true } ) ;
2021-01-18 17:26:26 -08:00
2021-09-16 13:59:03 -07:00
// unregister old domains
2022-01-14 22:40:51 -08:00
let obsoleteDomains = [ ] ;
if ( oldConfig . secondaryDomains ) {
obsoleteDomains = obsoleteDomains . concat ( oldConfig . secondaryDomains . filter ( function ( o ) {
return ! app . secondaryDomains . some ( function ( n ) { return n . subdomain === o . subdomain && n . domain === o . domain ; } ) ;
} ) ) ;
}
if ( oldConfig . redirectDomains ) {
obsoleteDomains = obsoleteDomains . concat ( oldConfig . redirectDomains . filter ( function ( o ) {
return ! app . redirectDomains . some ( function ( n ) { return n . subdomain === o . subdomain && n . domain === o . domain ; } ) ;
} ) ) ;
}
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
if ( oldConfig . aliasDomains ) {
obsoleteDomains = obsoleteDomains . concat ( oldConfig . aliasDomains . filter ( function ( o ) {
return ! app . aliasDomains . some ( function ( n ) { return n . subdomain === o . subdomain && n . domain === o . domain ; } ) ;
} ) ) ;
}
2019-09-08 16:57:08 -07:00
2022-01-16 12:32:12 -08:00
if ( locationChanged ) obsoleteDomains . push ( { subdomain : oldConfig . subdomain , domain : oldConfig . domain } ) ;
2021-02-24 14:47:05 -08:00
2021-09-16 13:59:03 -07:00
if ( obsoleteDomains . length !== 0 ) await dns . unregisterLocations ( obsoleteDomains , progressCallback ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
// setup dns
if ( ! skipDnsSetup ) {
await progressCallback ( { percent : 30 , message : 'Registering subdomains' } ) ;
2022-01-16 12:32:12 -08:00
await dns . registerLocations ( [ { subdomain : app . subdomain , domain : app . domain } ] . concat ( app . secondaryDomains ) . concat ( app . redirectDomains ) . concat ( app . aliasDomains ) , { overwriteDns } , progressCallback ) ;
2021-09-16 13:59:03 -07:00
}
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
// re-setup addons since they rely on the app's fqdn (e.g oauth)
await progressCallback ( { percent : 50 , message : 'Setting up addons' } ) ;
await services . setupAddons ( app , app . manifest . addons ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 60 , message : 'Creating container' } ) ;
await createContainer ( app ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await startApp ( app ) ;
2021-02-24 16:57:47 -08:00
2021-09-16 13:59:03 -07:00
if ( ! skipDnsSetup ) {
await progressCallback ( { percent : 80 , message : 'Waiting for DNS propagation' } ) ;
await exports . _waitForDnsPropagation ( app ) ;
}
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 90 , message : 'Configuring reverse proxy' } ) ;
2021-09-30 09:50:30 -07:00
await reverseProxy . configureApp ( app , AuditSource . APPTASK ) ;
2020-11-20 14:13:16 -08:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
2019-09-08 16:57:08 -07:00
}
2021-09-16 13:59:03 -07:00
async function migrateDataDir ( app , args , progressCallback ) {
2019-09-08 16:57:08 -07:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-21 19:45:55 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-09-08 16:57:08 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2022-06-01 22:44:52 -07:00
const { newStorageVolumeId , newStorageVolumePrefix } = args ;
assert ( newStorageVolumeId === null || typeof newStorageVolumeId === 'string' ) ;
assert ( newStorageVolumePrefix === null || typeof newStorageVolumePrefix === 'string' ) ;
2019-09-21 19:45:55 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 10 , message : 'Cleaning up old install' } ) ;
await deleteContainers ( app , { managedOnly : true } ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 45 , message : 'Ensuring app data directory' } ) ;
await createAppDir ( app ) ;
2019-09-08 16:57:08 -07:00
2022-06-01 22:44:52 -07:00
// re-setup addons since this creates the localStorage destination
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 50 , message : 'Setting up addons' } ) ;
2022-06-01 22:44:52 -07:00
await services . setupAddons ( _ . extend ( { } , app , { storageVolumeId : newStorageVolumeId , storageVolumePrefix : newStorageVolumePrefix } ) , app . manifest . addons ) ;
2019-09-09 16:37:59 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 60 , message : 'Moving data dir' } ) ;
2022-06-01 22:44:52 -07:00
await moveDataDir ( app , newStorageVolumeId , newStorageVolumePrefix ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 90 , message : 'Creating container' } ) ;
await createContainer ( app ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await startApp ( app ) ;
2019-09-08 16:57:08 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
2022-06-08 11:40:40 -07:00
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
2019-09-08 16:57:08 -07:00
}
2019-12-06 09:23:58 -08:00
// configure is called for an infra update and repair to re-create container, reverseproxy config. it's all "local"
2021-09-16 13:59:03 -07:00
async function configure ( app , args , progressCallback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-21 19:45:55 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-08-26 21:36:18 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 10 , message : 'Cleaning up old install' } ) ;
await reverseProxy . unconfigureApp ( app ) ;
2021-09-25 21:39:45 -07:00
await deleteContainers ( app , { managedOnly : true } ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 20 , message : 'Downloading icon' } ) ;
await downloadIcon ( app ) ;
2017-03-15 20:24:03 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 40 , message : 'Downloading image' } ) ;
await downloadImage ( app . manifest ) ;
2017-02-17 15:00:55 +01:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 45 , message : 'Ensuring app data directory' } ) ;
await createAppDir ( app ) ;
2017-02-17 15:00:55 +01:00
2021-09-16 13:59:03 -07:00
// re-setup addons since they rely on the app's fqdn (e.g oauth)
await progressCallback ( { percent : 50 , message : 'Setting up addons' } ) ;
await services . setupAddons ( app , app . manifest . addons ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 60 , message : 'Creating container' } ) ;
await createContainer ( app ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await startApp ( app ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 90 , message : 'Configuring reverse proxy' } ) ;
2021-09-30 09:50:30 -07:00
await reverseProxy . configureApp ( app , AuditSource . APPTASK ) ;
2020-11-20 14:13:16 -08:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
async function update ( app , args , progressCallback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-21 19:45:55 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-08-26 22:08:40 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
2019-09-21 19:45:55 -07:00
const updateConfig = args . updateConfig ;
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)
2020-11-20 14:13:16 -08:00
const unusedAddons = _ . omit ( app . manifest . addons , Object . keys ( updateConfig . manifest . addons ) ) ;
2021-01-22 11:25:26 +01:00
const httpPortChanged = app . manifest . httpPort !== updateConfig . manifest . httpPort ;
2020-12-17 10:25:23 -08:00
const proxyAuthChanged = ! _ . isEqual ( safe . query ( app . manifest , 'addons.proxyAuth' ) , safe . query ( updateConfig . manifest , 'addons.proxyAuth' ) ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
// this protects against the theoretical possibility of an app being marked for update from
// a previous version of box code
await progressCallback ( { percent : 5 , message : 'Verify manifest' } ) ;
await verifyManifest ( updateConfig . manifest ) ;
if ( ! updateConfig . skipBackup ) {
await progressCallback ( { percent : 15 , message : 'Backing up app' } ) ;
// preserve update backups for 3 weeks
const [ error ] = await safe ( backuptask . backupApp ( app , { preserveSecs : 3 * 7 * 24 * 60 * 60 } , ( progress ) => {
progressCallback ( { percent : 15 , message : ` Backup - ${ progress . message } ` } ) ;
} ) ) ;
if ( error ) {
error . backupError = true ;
throw error ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
}
2021-08-25 19:41:46 -07:00
2021-09-16 13:59:03 -07: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
await progressCallback ( { percent : 25 , message : 'Downloading image' } ) ;
await downloadImage ( 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
await progressCallback ( { percent : 35 , message : 'Cleaning up old install' } ) ;
await deleteContainers ( app , { managedOnly : true } ) ;
if ( app . manifest . dockerImage !== updateConfig . manifest . dockerImage ) await docker . deleteImage ( app . manifest ) ;
// only delete unused addons after backup
await services . teardownAddons ( app , unusedAddons ) ;
// free unused ports
const currentPorts = app . portBindings || { } ;
const newTcpPorts = updateConfig . manifest . tcpPorts || { } ;
const newUdpPorts = updateConfig . manifest . udpPorts || { } ;
for ( const portName of Object . keys ( currentPorts ) ) {
if ( newTcpPorts [ portName ] || newUdpPorts [ portName ] ) continue ; // port still in use
const [ error ] = await safe ( apps . delPortBinding ( currentPorts [ portName ] , apps . PORT _TYPE _TCP ) ) ;
2021-09-25 21:38:09 -07:00
if ( error && error . reason === BoxError . NOT _FOUND ) debug ( 'update: portbinding does not exist in database' , error ) ;
2021-09-16 13:59:03 -07:00
else if ( error ) throw error ;
// also delete from app object for further processing (the db is updated in the next step)
delete app . portBindings [ portName ] ;
}
2022-01-21 19:42:07 +01:00
await updateApp ( app , _ . pick ( updateConfig , 'manifest' , 'appStoreId' , 'memoryLimit' ) ) ; // switch over to the new config
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 45 , message : 'Downloading icon' } ) ;
await downloadIcon ( app ) ;
await progressCallback ( { percent : 60 , message : 'Updating addons' } ) ;
await services . setupAddons ( app , updateConfig . manifest . addons ) ;
await progressCallback ( { percent : 70 , message : 'Creating container' } ) ;
await createContainer ( app ) ;
await startApp ( app ) ;
await progressCallback ( { percent : 90 , message : 'Configuring reverse proxy' } ) ;
2021-11-09 21:50:12 -08:00
if ( proxyAuthChanged || httpPortChanged ) {
2021-09-30 09:50:30 -07:00
await reverseProxy . configureApp ( app , AuditSource . APPTASK ) ;
2021-09-16 13:59:03 -07:00
}
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null , updateTime : new Date ( ) } ) ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
async function start ( app , args , progressCallback ) {
2019-09-22 00:20:12 -07:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof args , 'object' ) ;
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 10 , message : 'Starting app services' } ) ;
await services . startAppServices ( app ) ;
2020-05-18 14:10:00 -07:00
2022-06-09 13:57:57 +02:00
if ( app . manifest . id !== constants . PROXY _APP _APPSTORE _ID ) {
2022-06-08 14:35:50 +02:00
await progressCallback ( { percent : 35 , message : 'Starting container' } ) ;
await docker . startContainer ( app . id ) ;
}
2019-09-22 00:20:12 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 60 , message : 'Adding collectd profile' } ) ;
await addCollectdProfile ( app ) ;
2020-11-09 10:37:22 -08:00
2021-09-16 13:59:03 -07:00
// stopped apps do not renew certs. currently, we don't do DNS to not overwrite existing user settings
await progressCallback ( { percent : 80 , message : 'Configuring reverse proxy' } ) ;
2021-09-30 09:50:30 -07:00
await reverseProxy . configureApp ( app , AuditSource . APPTASK ) ;
2020-01-26 16:05:23 -08:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
2019-09-22 00:20:12 -07:00
}
2021-09-16 13:59:03 -07:00
async function stop ( app , args , progressCallback ) {
2019-09-22 00:20:12 -07:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof args , 'object' ) ;
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 20 , message : 'Stopping container' } ) ;
2022-01-28 09:52:03 -08:00
await reverseProxy . unconfigureApp ( app ) ; // removing nginx configs also means that we can auto-cleanup old certs since they are not referenced
2021-09-16 13:59:03 -07:00
await docker . stopContainers ( app . id ) ;
2019-09-22 00:20:12 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 50 , message : 'Stopping app services' } ) ;
await services . stopAppServices ( app ) ;
2020-05-18 14:10:00 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 80 , message : 'Removing collectd profile' } ) ;
await removeCollectdProfile ( app ) ;
2020-11-09 10:37:22 -08:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
2019-09-22 00:20:12 -07:00
}
2021-09-16 13:59:03 -07:00
async function restart ( app , args , progressCallback ) {
2019-12-20 10:29:29 -08:00
assert . strictEqual ( typeof app , 'object' ) ;
assert . strictEqual ( typeof args , 'object' ) ;
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2022-06-09 13:57:57 +02:00
if ( app . manifest . id !== constants . PROXY _APP _APPSTORE _ID ) {
2022-06-06 20:04:22 +02:00
await progressCallback ( { percent : 20 , message : 'Restarting container' } ) ;
await docker . restartContainer ( app . id ) ;
}
2019-12-20 10:29:29 -08:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 100 , message : 'Done' } ) ;
await updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
2019-12-20 10:29:29 -08:00
}
2021-09-16 13:59:03 -07:00
async function uninstall ( app , args , progressCallback ) {
2016-01-05 12:14:39 +01:00
assert . strictEqual ( typeof app , 'object' ) ;
2019-09-21 19:45:55 -07:00
assert . strictEqual ( typeof args , 'object' ) ;
2019-08-26 21:56:55 -07:00
assert . strictEqual ( typeof progressCallback , 'function' ) ;
2016-01-05 12:14:39 +01:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 20 , message : 'Deleting container' } ) ;
await reverseProxy . unconfigureApp ( app ) ;
await deleteContainers ( app , { } ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 30 , message : 'Teardown addons' } ) ;
await services . teardownAddons ( app , app . manifest . addons ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 40 , message : 'Cleanup file manager' } ) ;
2020-07-29 15:02:30 +02:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 50 , message : 'Deleting app data directory' } ) ;
await deleteAppDir ( app , { removeDirectory : true } ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 60 , message : 'Deleting image' } ) ;
await docker . deleteImage ( app . manifest ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 70 , message : 'Unregistering domains' } ) ;
2022-01-16 12:32:12 -08:00
await dns . unregisterLocations ( [ { subdomain : app . subdomain , domain : app . domain } ] . concat ( app . secondaryDomains ) . concat ( app . redirectDomains ) . concat ( app . aliasDomains ) , progressCallback ) ;
2015-07-20 00:09:47 -07:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 90 , message : 'Cleanup logs' } ) ;
await cleanupLogs ( app ) ;
2018-06-04 11:45:22 +02:00
2021-09-16 13:59:03 -07:00
await progressCallback ( { percent : 95 , message : 'Remove app from database' } ) ;
await apps . del ( app . id ) ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
async function run ( appId , args , progressCallback ) {
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' ) ;
2021-09-16 13:59:03 -07:00
const app = await apps . get ( appId ) ;
2021-09-25 21:38:09 -07:00
debug ( ` run: startTask installationState: ${ app . installationState } runState: ${ app . runState } ` ) ;
2021-09-16 13:59:03 -07:00
let cmd ;
switch ( app . installationState ) {
case apps . ISTATE _PENDING _INSTALL :
case apps . ISTATE _PENDING _CLONE :
case apps . ISTATE _PENDING _RESTORE :
case apps . ISTATE _PENDING _IMPORT :
cmd = install ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _CONFIGURE :
cmd = configure ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _RECREATE _CONTAINER :
case apps . ISTATE _PENDING _RESIZE :
case apps . ISTATE _PENDING _DEBUG :
cmd = create ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _LOCATION _CHANGE :
cmd = changeLocation ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _DATA _DIR _MIGRATION :
cmd = migrateDataDir ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _UNINSTALL :
cmd = uninstall ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _UPDATE :
cmd = update ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _BACKUP :
cmd = backup ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _START :
cmd = start ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _STOP :
cmd = stop ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _PENDING _RESTART :
cmd = restart ( app , args , progressCallback ) ;
break ;
case apps . ISTATE _INSTALLED : // can only happen when we have a bug in our code while testing/development
cmd = updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } ) ;
break ;
default :
2021-09-25 21:38:09 -07:00
debug ( 'run: apptask launched with invalid command' ) ;
2021-09-16 13:59:03 -07:00
throw new BoxError ( BoxError . INTERNAL _ERROR , 'Unknown install command in apptask:' + app . installationState ) ;
}
2021-09-30 10:45:25 -07:00
const [ error , result ] = await safe ( cmd ) ; // only some commands like backup return a result
2021-09-16 13:59:03 -07:00
if ( error ) {
2021-09-25 21:38:09 -07:00
debug ( ` run: app error for state ${ app . installationState } : ` , error ) ;
2021-09-16 13:59:03 -07:00
if ( app . installationState === apps . ISTATE _PENDING _BACKUP ) {
// return to installed state intentionally. the error is stashed only in the task and not the app (the UI shows error state otherwise)
await safe ( updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null } ) , { debug } ) ;
} else if ( app . installationState === apps . ISTATE _PENDING _UPDATE && error . backupError ) {
2021-09-25 21:38:09 -07:00
debug ( 'run: update aborted because backup failed' ) ;
2021-09-16 13:59:03 -07:00
await safe ( updateApp ( app , { installationState : apps . ISTATE _INSTALLED , error : null , health : null } , { debug } ) ) ;
} else {
await safe ( updateApp ( app , { installationState : apps . ISTATE _ERROR , error : makeTaskError ( error , app ) } ) , { debug } ) ;
2015-07-20 00:09:47 -07:00
}
2021-09-16 13:59:03 -07:00
throw error ;
}
2021-09-30 10:45:25 -07:00
return result || null ;
2015-07-20 00:09:47 -07:00
}