Skip to content

Commit 8496b03

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 860cf18 commit 8496b03

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

extensions/linux/index.js

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

0 commit comments

Comments
 (0)