diff --git a/box.js b/box.js index 9ee296ddc..d8de086dc 100755 --- a/box.js +++ b/box.js @@ -3,46 +3,57 @@ 'use strict'; let async = require('async'), - constants = require('./src/constants.js'), - debug = require('debug')('box:box'), dockerProxy = require('./src/dockerproxy.js'), + fs = require('fs'), ldap = require('./src/ldap.js'), - server = require('./src/server.js'); + paths = require('./src/paths.js'), + server = require('./src/server.js'), + util = require('util'); -debug(); -debug('=========================================='); -debug(` Cloudron ${constants.VERSION} `); -debug('=========================================='); -debug(); +const NOOP_CALLBACK = function () { }; + +function setupLogging(callback) { + fs.open(paths.BOX_LOG_FILE, 'a', function (error, fd) { + if (error) return callback(error); + + require('debug').log = function (...args) { + fs.appendFileSync(fd, (args.length ? util.format(...args) : '') + '\n'); + }; + + callback(); + }); +} async.series([ + setupLogging, server.start, ldap.start, dockerProxy.start ], function (error) { if (error) { - debug('Error starting server', error); + console.log('Error starting server', error); process.exit(1); } - debug('Cloudron is up and running'); -}); - -var NOOP_CALLBACK = function () { }; - -process.on('SIGINT', function () { - debug('Received SIGINT. Shutting down.'); - - server.stop(NOOP_CALLBACK); - ldap.stop(NOOP_CALLBACK); - dockerProxy.stop(NOOP_CALLBACK); - setTimeout(process.exit.bind(process), 3000); -}); - -process.on('SIGTERM', function () { - debug('Received SIGTERM. Shutting down.'); - - server.stop(NOOP_CALLBACK); - ldap.stop(NOOP_CALLBACK); - dockerProxy.stop(NOOP_CALLBACK); - setTimeout(process.exit.bind(process), 3000); + + const debug = require('debug')('box:box'); // require this here so that logging handler is already setup + + process.on('SIGINT', function () { + debug('Received SIGINT. Shutting down.'); + + server.stop(NOOP_CALLBACK); + ldap.stop(NOOP_CALLBACK); + dockerProxy.stop(NOOP_CALLBACK); + setTimeout(process.exit.bind(process), 3000); + }); + + process.on('SIGTERM', function () { + debug('Received SIGTERM. Shutting down.'); + + server.stop(NOOP_CALLBACK); + ldap.stop(NOOP_CALLBACK); + dockerProxy.stop(NOOP_CALLBACK); + setTimeout(process.exit.bind(process), 3000); + }); + + console.log(`Cloudron is up and running. Logs are at ${paths.BOX_LOG_FILE}`); // this goes to journalctl }); diff --git a/setup/start/systemd/box.service b/setup/start/systemd/box.service index 180051af4..8a1d23265 100644 --- a/setup/start/systemd/box.service +++ b/setup/start/systemd/box.service @@ -14,8 +14,7 @@ WantedBy=multi-user.target Type=idle WorkingDirectory=/home/yellowtent/box Restart=always -; Systemd does not append logs when logging to files, we spawn a shell first and exec to replace it after setting up the pipes -ExecStart=/bin/sh -c 'echo "Logging to /home/yellowtent/platformdata/logs/box.log"; exec /usr/bin/node /home/yellowtent/box/box.js >> /home/yellowtent/platformdata/logs/box.log 2>&1' +ExecStart=/home/yellowtent/box/box.js Environment="HOME=/home/yellowtent" "USER=yellowtent" "DEBUG=box:*,connect-lastmile,-box:ldap" "BOX_ENV=cloudron" "NODE_ENV=production" ; kill apptask processes as well KillMode=control-group diff --git a/src/paths.js b/src/paths.js index 6761f98ad..5d99d0612 100644 --- a/src/paths.js +++ b/src/paths.js @@ -50,6 +50,7 @@ exports = module.exports = { LOG_DIR: path.join(baseDir(), 'platformdata/logs'), TASKS_LOG_DIR: path.join(baseDir(), 'platformdata/logs/tasks'), CRASH_LOG_DIR: path.join(baseDir(), 'platformdata/logs/crash'), + BOX_LOG_FILE: path.join(baseDir(), 'platformdata/logs/box.log'), GHOST_USER_FILE: path.join(baseDir(), 'platformdata/cloudron_ghost.json'), diff --git a/src/server.js b/src/server.js index a0f800e23..49250066a 100644 --- a/src/server.js +++ b/src/server.js @@ -10,6 +10,7 @@ let assert = require('assert'), cloudron = require('./cloudron.js'), constants = require('./constants.js'), database = require('./database.js'), + debug = require('debug')('box:server'), eventlog = require('./eventlog.js'), express = require('express'), http = require('http'), @@ -332,6 +333,12 @@ function start(callback) { assert.strictEqual(typeof callback, 'function'); assert.strictEqual(gHttpServer, null, 'Server is already up and running.'); + debug(); + debug('=========================================='); + debug(` Cloudron ${constants.VERSION} `); + debug('=========================================='); + debug(); + gHttpServer = initializeExpressSync(); async.series([