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(systemd): improve setup checks of extension and process manager #300

Merged
merged 1 commit into from
Jul 6, 2017
Merged
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
31 changes: 31 additions & 0 deletions extensions/systemd/get-uid.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
12 changes: 3 additions & 9 deletions extensions/systemd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down
31 changes: 13 additions & 18 deletions extensions/systemd/systemd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,29 @@
const fs = require('fs');
const execa = require('execa');
const cli = require('../../lib');
const getUid = require('./get-uid');

class SystemdProcessManager extends cli.ProcessManager {
get systemdName() {
return `ghost_${this.instance.name}`;
}

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)));
Expand Down Expand Up @@ -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() {
Expand Down