Skip to content

Commit 467efa5

Browse files
committed
feat(linux): add linux user extension
closes TryGhost#189 - add linux extension that creates a `ghost` user on the system, used by systemd (and potentially any other process manager)
1 parent 1958d63 commit 467efa5

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

extensions/linux/index.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
const os = require('os');
4+
const chownr = require('chownr');
5+
const execa = require('execa');
6+
const Promise = require('bluebird');
7+
8+
const cli = require('../../lib');
9+
10+
class LinuxExtension extends cli.Extension {
11+
setup(cmd, argv) {
12+
if (argv.local) {
13+
return;
14+
}
15+
16+
cmd.addStage('linux-user', this.addGhostUser.bind(this), null, 'a ghost system user');
17+
}
18+
19+
addGhostUser(argv, ctx, task) {
20+
if (os.platform() !== 'linux') {
21+
return task.skip('Platform is not linux');
22+
}
23+
24+
try {
25+
execa.shellSync('id ghost');
26+
return task.skip('Ghost user already exists')
27+
} catch (e) {
28+
if (!e.message.match(/no such user/)) {
29+
return Promise.reject(e);
30+
}
31+
}
32+
33+
return this.ui.listr([{
34+
title: 'Creating ghost system user',
35+
task: (ctx) => {
36+
ctx.user = {};
37+
return this.ui.sudo('useradd --system --user-group ghost').then(() => {
38+
return Promise.all([
39+
execa.shell('id -u ghost'),
40+
execa.shell('id -g ghost')
41+
]);
42+
}).then((result) => {
43+
ctx.user.uid = result[0].stdout;
44+
ctx.user.gid = result[1].stdout;
45+
});
46+
}
47+
}, {
48+
title: 'Changing directory permissions',
49+
task: (ctx) => {
50+
return Promise.fromCallback(cb => chownr(ctx.instance.dir, ctx.user.uid, ctx.user.gid, cb));
51+
}
52+
}, {
53+
title: 'Adding current user to ghost group',
54+
task: (ctx) => {
55+
return execa.shell('id -un').then((result) => {
56+
return this.ui.sudo(`usermod --append --groups ghost ${result.stdout}`);
57+
});
58+
}
59+
}], false);
60+
}
61+
}
62+
63+
module.exports = LinuxExtension;

extensions/linux/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "ghost-cli-linux",
33
"description": "general linux user configuration for Ghost",
44
"version": "0.0.0",
5+
"main": "index.js",
56
"keywords":[
67
"ghost-cli-extension"
78
]

extensions/systemd/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require('fs-extra');
44
const path = require('path');
5+
const execa = require('execa');
56
const template = require('lodash/template');
67

78
const cli = require('../../lib');
@@ -11,18 +12,30 @@ class SystemdExtension extends cli.Extension {
1112
let instance = this.system.getInstance();
1213

1314
if (!argv.local && instance.config.get('process') === 'systemd') {
14-
cmd.addStage('systemd', this._setup.bind(this));
15+
cmd.addStage('systemd', this._setup.bind(this), null, 'linux-user');
1516
}
1617
}
1718

18-
_setup(argv, ctx) {
19+
_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+
}
28+
29+
return task.skip('Ghost user has not been set up.');
30+
}
31+
1932
let service = template(fs.readFileSync(path.join(__dirname, 'ghost.service.template'), 'utf8'));
2033
let serviceFilename = `ghost_${ctx.instance.name}.service`;
2134

2235
return ctx.instance.template(service({
2336
name: ctx.instance.name,
2437
dir: process.cwd(),
25-
user: process.getuid(),
38+
user: uid,
2639
environment: this.system.environment,
2740
ghost_exec_path: process.argv.slice(0,2).join(' ')
2841
}), 'systemd service file', serviceFilename, '/lib/systemd/system').then(() => {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"abbrev": "^1.1.0",
4242
"bluebird": "3.5.0",
4343
"chalk": "1.1.3",
44+
"chownr": "1.0.1",
4445
"cli-table2": "0.2.0",
4546
"crontab": "1.1.3",
4647
"debug": "2.6.8",

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ check-error@^1.0.1:
397397
version "1.0.2"
398398
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
399399

400-
chownr@^1.0.1:
400+
chownr@1.0.1, chownr@^1.0.1:
401401
version "1.0.1"
402402
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.0.1.tgz#e2a75042a9551908bebd25b8523d5f9769d79181"
403403

0 commit comments

Comments
 (0)