Skip to content

Commit 0259312

Browse files
committed
fix(systemd): improve setup checks of extension and process manager
closes #291 - add function to double check that linux-user has been run - make help messaging more clear
1 parent e850926 commit 0259312

File tree

3 files changed

+47
-27
lines changed

3 files changed

+47
-27
lines changed

extensions/systemd/get-uid.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const execa = require('execa');
6+
7+
/**
8+
* Helper function used by both the extension setup method
9+
* and the systemd process manager. This checks if the
10+
* linux ghost user has been set up. If not, the function returns null,
11+
* but if so, it returns the user id of the ghost user
12+
*/
13+
module.exports = function getUid(dir) {
14+
try {
15+
let uid = execa.shellSync('id -u ghost').stdout;
16+
let stat = fs.lstatSync(path.join(dir, 'content'));
17+
18+
if (stat.uid.toString() !== uid) {
19+
// Ghost user is not the owner of this folder, return null
20+
return null;
21+
}
22+
23+
return uid;
24+
} catch (e) {
25+
if (!e.message.match(/no such user/)) {
26+
throw e;
27+
}
28+
29+
return null;
30+
}
31+
};

extensions/systemd/index.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
const fs = require('fs-extra');
44
const path = require('path');
5-
const execa = require('execa');
65
const template = require('lodash/template');
76

87
const cli = require('../../lib');
8+
const getUid = require('./get-uid');
99

1010
class SystemdExtension extends cli.Extension {
1111
setup(cmd, argv) {
@@ -17,15 +17,9 @@ class SystemdExtension extends cli.Extension {
1717
}
1818

1919
_setup(argv, ctx, task) {
20-
let uid;
21-
22-
try {
23-
uid = execa.shellSync('id -u ghost').stdout;
24-
} catch (e) {
25-
if (!e.message.match(/no such user/)) {
26-
return Promise.reject(e);
27-
}
20+
let uid = getUid(ctx.instance.dir);
2821

22+
if (!uid) {
2923
this.ui.log('Ghost user has not been set up, please run `ghost setup linux-user` first', 'yellow');
3024
return task.skip();
3125
}

extensions/systemd/systemd.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,29 @@
33
const fs = require('fs');
44
const execa = require('execa');
55
const cli = require('../../lib');
6+
const getUid = require('./get-uid');
67

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

1213
start() {
13-
if (!this._precheck()) {
14-
return Promise.reject(
15-
new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.')
16-
);
17-
}
14+
this._precheck();
1815

1916
return this.ui.sudo(`systemctl start ${this.systemdName}`)
2017
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
2118
}
2219

2320
stop() {
24-
if (!this._precheck()) {
25-
return Promise.reject(
26-
new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.')
27-
);
28-
}
21+
this._precheck();
2922

3023
return this.ui.sudo(`systemctl stop ${this.systemdName}`)
3124
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
3225
}
3326

3427
restart() {
35-
if (!this._precheck()) {
36-
return Promise.reject(
37-
new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.')
38-
);
39-
}
28+
this._precheck();
4029

4130
return this.ui.sudo(`systemctl restart ${this.systemdName}`)
4231
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
@@ -82,17 +71,23 @@ class SystemdProcessManager extends cli.ProcessManager {
8271
}
8372

8473
_precheck() {
74+
let uid = getUid(this.instance.dir);
75+
76+
if (!uid) {
77+
throw new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup linux-user systemd` and try again.')
78+
}
79+
8580
if (this.instance.cliConfig.get('extension.systemd', false)) {
86-
return true;
81+
return;
8782
}
8883

8984
// service file exists but for some reason the right property in cliConfig hasn't been set
9085
if (fs.existsSync(`/lib/systemd/system/${this.systemdName}.service`)) {
9186
this.instance.cliConfig.set('extension.systemd', true);
92-
return true;
87+
return;
9388
}
9489

95-
return false;
90+
throw new cli.errors.SystemError('Systemd process manager has not been set up. Run `ghost setup systemd` and try again.');
9691
}
9792

9893
static willRun() {

0 commit comments

Comments
 (0)