diff --git a/extensions/linux/index.js b/extensions/linux/index.js index 08791ba4f..ed98e32db 100644 --- a/extensions/linux/index.js +++ b/extensions/linux/index.js @@ -51,6 +51,42 @@ class LinuxExtension extends cli.Extension { // we need to remove it manually here via sudo return this.ui.sudo(`rm -rf ${path.join(instance.dir, 'content')}`); } + + run() { + if (os.platform() !== 'linux') { + // platform isn't linux, we don't need to to anything + return Promise.resolve(); + } + + let ghostuid, ghostgid; + + try { + ghostuid = execa.shellSync('id -u ghost').stdout; + ghostgid = execa.shellSync('id -g ghost').stdout; + } catch (e) { + if (!e.message.match(/no such user/)) { + return Promise.reject(new cli.errors.ProcessError(e)); + } + + // Ghost user doesn't exist, skip + return Promise.resolve(); + } + + let currentuid = process.getuid(); + + if (currentuid === parseInt(ghostuid)) { + // current user is ghost, continue + return Promise.resolve(); + } + + if (currentuid !== 0) { + // we need to be sudo in order to run setuid below + return Promise.reject(new cli.errors.SystemError('Because you have set up a "ghost" system user, you must run `ghost run` via sudo.')); + } + + process.setuid(parseInt(ghostuid)); + process.setgid(parseInt(ghostgid)); + } } module.exports = LinuxExtension; diff --git a/lib/commands/run.js b/lib/commands/run.js index 26dfff5d1..15fe753af 100644 --- a/lib/commands/run.js +++ b/lib/commands/run.js @@ -4,7 +4,7 @@ const spawn = require('child_process').spawn; const Command = require('../command'); class RunCommand extends Command { - run() { + run(argv) { // If the user is running this command directly, output a little note // telling them they're likely looking for `ghost start` if (process.stdin.isTTY) { @@ -14,25 +14,29 @@ class RunCommand extends Command { let instance = this.system.getInstance(); - process.env.paths__contentPath = path.join(process.cwd(), 'content'); - - this.child = spawn(process.execPath, ['current/index.js'], { - cwd: process.cwd(), - stdio: [0, 1, 2, 'ipc'] - }); - - this.child.on('error', (error) => { - this.ui.fail(error); - process.exit(1); - }); - - this.child.on('message', (message) => { - if (message.started) { - instance.process.success(); - return; - } + if (!instance.config.has('paths.contentPath')) { + instance.config.set('paths.contentPath', path.join(instance.dir, 'content')).save(); + } - instance.process.error(message.error); + return this.system.hook('run', argv).then(() => { + this.child = spawn(process.execPath, ['current/index.js'], { + cwd: process.cwd(), + stdio: [0, 1, 2, 'ipc'] + }); + + this.child.on('error', (error) => { + this.ui.fail(error); + process.exit(1); + }); + + this.child.on('message', (message) => { + if (message.started) { + instance.process.success(); + return; + } + + instance.process.error(message.error); + }); }); }