Skip to content

Commit

Permalink
fix(systemd): improve setup checks of extension and process manager
Browse files Browse the repository at this point in the history
closes #291
- add function to double check that linux-user has been run
- make help messaging more clear
  • Loading branch information
acburdine committed Jul 6, 2017
1 parent e850926 commit 0259312
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
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

0 comments on commit 0259312

Please sign in to comment.