Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(run): better handling of ghost linux user during ghost run #327

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions extensions/linux/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const fs = require('fs');
const os = require('os');
const execa = require('execa');
const path = require('path');
Expand Down Expand Up @@ -51,6 +52,52 @@ 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(argv, instance) {
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();
}

ghostuid = parseInt(ghostuid);
ghostgid = parseInt(ghostgid);

let stats = fs.lstatSync(path.join(instance.dir, 'content'));

if (stats.uid !== ghostuid && stats.gid !== ghostgid) {
// folder isn't owned by ghost user, skip additional behavior
return Promise.resolve();
}

let currentuid = process.getuid();

if (currentuid === 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 Ghost-CLI has set up a "ghost" system user for this instance, you must run `sudo ghost run` instead.'));
}

process.setgid(ghostgid);
process.setuid(ghostuid);
}
}

module.exports = LinuxExtension;
38 changes: 20 additions & 18 deletions lib/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -16,23 +16,25 @@ class RunCommand extends Command {

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;
}

instance.process.error(message.error);
return this.system.hook('run', argv, instance).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);
});
});
}

Expand Down