diff --git a/extensions/systemd/get-uid.js b/extensions/systemd/get-uid.js new file mode 100644 index 000000000..e47cb0cb1 --- /dev/null +++ b/extensions/systemd/get-uid.js @@ -0,0 +1,31 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const execa = require('execa'); + +/** + * Helper function used by both the extension setup method + * and the systemd process manager. This checks if the + * linux ghost user has been set up. If not, the function returns null, + * but if so, it returns the user id of the ghost user + */ +module.exports = function getUid(dir) { + try { + let uid = execa.shellSync('id -u ghost').stdout; + let stat = fs.lstatSync(path.join(dir, 'content')); + + if (stat.uid.toString() !== uid) { + // Ghost user is not the owner of this folder, return null + return null; + } + + return uid; + } catch (e) { + if (!e.message.match(/no such user/)) { + throw e; + } + + return null; + } +}; diff --git a/extensions/systemd/index.js b/extensions/systemd/index.js index 9b004853a..7f91efb71 100644 --- a/extensions/systemd/index.js +++ b/extensions/systemd/index.js @@ -2,10 +2,10 @@ const fs = require('fs-extra'); const path = require('path'); -const execa = require('execa'); const template = require('lodash/template'); const cli = require('../../lib'); +const getUid = require('./get-uid'); class SystemdExtension extends cli.Extension { setup(cmd, argv) { @@ -17,15 +17,9 @@ class SystemdExtension extends cli.Extension { } _setup(argv, ctx, task) { - let uid; - - try { - uid = execa.shellSync('id -u ghost').stdout; - } catch (e) { - if (!e.message.match(/no such user/)) { - return Promise.reject(e); - } + let uid = getUid(ctx.instance.dir); + if (!uid) { this.ui.log('Ghost user has not been set up, please run `ghost setup linux-user` first', 'yellow'); return task.skip(); } diff --git a/extensions/systemd/systemd.js b/extensions/systemd/systemd.js index b35186084..7ca7a64f9 100644 --- a/extensions/systemd/systemd.js +++ b/extensions/systemd/systemd.js @@ -3,6 +3,7 @@ const fs = require('fs'); const execa = require('execa'); const cli = require('../../lib'); +const getUid = require('./get-uid'); class SystemdProcessManager extends cli.ProcessManager { get systemdName() { @@ -10,33 +11,21 @@ class SystemdProcessManager extends cli.ProcessManager { } start() { - if (!this._precheck()) { - return Promise.reject( - new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.') - ); - } + this._precheck(); return this.ui.sudo(`systemctl start ${this.systemdName}`) .catch((error) => Promise.reject(new cli.errors.ProcessError(error))); } stop() { - if (!this._precheck()) { - return Promise.reject( - new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.') - ); - } + this._precheck(); return this.ui.sudo(`systemctl stop ${this.systemdName}`) .catch((error) => Promise.reject(new cli.errors.ProcessError(error))); } restart() { - if (!this._precheck()) { - return Promise.reject( - new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.') - ); - } + this._precheck(); return this.ui.sudo(`systemctl restart ${this.systemdName}`) .catch((error) => Promise.reject(new cli.errors.ProcessError(error))); @@ -82,17 +71,23 @@ class SystemdProcessManager extends cli.ProcessManager { } _precheck() { + let uid = getUid(this.instance.dir); + + if (!uid) { + throw new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup linux-user systemd` and try again.') + } + if (this.instance.cliConfig.get('extension.systemd', false)) { - return true; + return; } // service file exists but for some reason the right property in cliConfig hasn't been set if (fs.existsSync(`/lib/systemd/system/${this.systemdName}.service`)) { this.instance.cliConfig.set('extension.systemd', true); - return true; + return; } - return false; + throw new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.'); } static willRun() {